fix(sem)修正符号表的语法错误

mxr 3 weeks ago
parent a0d5288351
commit c4479eaa1e

@ -21,13 +21,13 @@ enum class SymbolKind {
struct Symbol {
std::string name;
SymbolKind kind;
std::shared_ptr<Type> type; // 指向 Type 对象的智能指针
std::shared_ptr<ir::Type> type; // 指向 Type 对象的智能指针
int scope_level = 0; // 定义时的作用域深度
int stack_offset = -1; // 局部变量/参数栈偏移(全局变量为 -1
bool is_initialized = false; // 是否已初始化
// 对于函数,额外存储参数列表(类型已包含在函数类型中,这里仅用于快速访问)
std::vector<std::shared_ptr<Type>> param_types;
std::vector<std::shared_ptr<ir::Type>> param_types;
// 对于常量,存储常量值(这里支持 int32 和 float
union ConstantValue {
@ -62,8 +62,8 @@ class SymbolTable {
SysYParser::VarDefContext* Lookup(const std::string& name) const;
// ----- 辅助函数:从语法树节点构造 Type -----
static std::shared_ptr<Type> getTypeFromVarDef(SysYParser::VarDefContext* ctx);
static std::shared_ptr<Type> getTypeFromFuncDef(SysYParser::FuncDefContext* ctx);
static std::shared_ptr<ir::Type> getTypeFromVarDef(SysYParser::VarDefContext* ctx);
static std::shared_ptr<ir::Type> getTypeFromFuncDef(SysYParser::FuncDefContext* ctx);
private:
// 作用域栈:每个元素是一个从名字到符号的映射

@ -85,25 +85,25 @@ SysYParser::VarDefContext* SymbolTable::Lookup(const std::string& name) const {
}
// ---------- 辅助函数:从语法树节点构造 Type ----------
std::shared_ptr<Type> SymbolTable::getTypeFromVarDef(SysYParser::VarDefContext* ctx) {
std::shared_ptr<ir::Type> SymbolTable::getTypeFromVarDef(SysYParser::VarDefContext* ctx) {
// 这里需要根据实际的语法树结构来提取类型。
// 假设 ctx 中包含类型节点和数组维度信息。
// 以下代码为示意,具体实现需根据你的 SysY 语法定义调整。
// 1. 获取基本类型
std::shared_ptr<Type> base_type;
std::shared_ptr<ir::Type> base_type;
// 假设 ctx 中有 type() 方法返回类型节点,且类型节点有 INT()、FLOAT() 等方法
if (ctx->type()) {
if (ctx->type()->INT()) {
base_type = Type::GetInt32Type();
base_type = ir::Type::GetInt32Type();
} else if (ctx->type()->FLOAT()) {
base_type = Type::GetFloatType();
base_type = ir::Type::GetFloatType();
} else {
// 默认为 int
base_type = Type::GetInt32Type();
base_type = ir::Type::GetInt32Type();
}
} else {
base_type = Type::GetInt32Type(); // 默认
base_type = ir::Type::GetInt32Type(); // 默认
}
// 2. 处理数组维度
@ -121,44 +121,44 @@ std::shared_ptr<Type> SymbolTable::getTypeFromVarDef(SysYParser::VarDefContext*
} else {
// 数组类型:递归构建数组类型
// 注意Type::GetArrayType 需要元素类型和维度列表
return Type::GetArrayType(base_type, dims);
return ir::Type::GetArrayType(base_type, dims);
}
}
std::shared_ptr<Type> SymbolTable::getTypeFromFuncDef(SysYParser::FuncDefContext* ctx) {
std::shared_ptr<ir::Type> SymbolTable::getTypeFromFuncDef(SysYParser::FuncDefContext* ctx) {
// 提取返回类型
std::shared_ptr<Type> ret_type;
std::shared_ptr<ir::Type> ret_type;
if (ctx->type()) {
if (ctx->type()->VOID()) {
ret_type = Type::GetVoidType();
ret_type = ir::Type::GetVoidType();
} else if (ctx->type()->INT()) {
ret_type = Type::GetInt32Type();
ret_type = ir::Type::GetInt32Type();
} else if (ctx->type()->FLOAT()) {
ret_type = Type::GetFloatType();
ret_type = ir::Type::GetFloatType();
} else {
ret_type = Type::GetInt32Type(); // 默认
ret_type = ir::Type::GetInt32Type(); // 默认
}
} else {
ret_type = Type::GetInt32Type(); // 默认
ret_type = ir::Type::GetInt32Type(); // 默认
}
// 提取参数类型列表
std::vector<std::shared_ptr<Type>> param_types;
std::vector<std::shared_ptr<ir::Type>> param_types;
if (ctx->paramList()) {
for (auto param : ctx->paramList()->param()) {
// 根据参数声明构建类型
// 这里假设每个参数有类型节点,并且可能包含数组维度
std::shared_ptr<Type> param_type;
std::shared_ptr<ir::Type> param_type;
if (param->type()) {
if (param->type()->INT()) {
param_type = Type::GetInt32Type();
param_type = ir::Type::GetInt32Type();
} else if (param->type()->FLOAT()) {
param_type = Type::GetFloatType();
param_type = ir::Type::GetFloatType();
} else {
param_type = Type::GetInt32Type();
param_type = ir::Type::GetInt32Type();
}
} else {
param_type = Type::GetInt32Type();
param_type = ir::Type::GetInt32Type();
}
// 处理数组参数(如果存在维度)
@ -169,5 +169,5 @@ std::shared_ptr<Type> SymbolTable::getTypeFromFuncDef(SysYParser::FuncDefContext
}
// 创建函数类型
return Type::GetFunctionType(ret_type, param_types);
return ir::Type::GetFunctionType(ret_type, param_types);
}
Loading…
Cancel
Save