diff --git a/include/sem/SymbolTable.h b/include/sem/SymbolTable.h index d52fc9b..b200642 100644 --- a/include/sem/SymbolTable.h +++ b/include/sem/SymbolTable.h @@ -29,8 +29,12 @@ struct Symbol { // 对于函数,额外存储参数列表(类型已包含在函数类型中,这里仅用于快速访问) std::vector> param_types; - // 对于常量,存储常量值(此处仅支持 int32) - int const_value = 0; + // 对于常量,存储常量值(这里支持 int32 和 float) + union ConstantValue { + int i32; + float f32; + } const_value; + bool is_int_const = true; // 标记常量类型,用于区分 int 和 float // 关联的语法树节点(用于报错位置或进一步分析) SysYParser::VarDefContext* var_def_ctx = nullptr; diff --git a/src/sem/SymbolTable.cpp b/src/sem/SymbolTable.cpp index 69fc91f..18a28ad 100644 --- a/src/sem/SymbolTable.cpp +++ b/src/sem/SymbolTable.cpp @@ -90,16 +90,20 @@ std::shared_ptr SymbolTable::getTypeFromVarDef(SysYParser::VarDefContext* // 假设 ctx 中包含类型节点和数组维度信息。 // 以下代码为示意,具体实现需根据你的 SysY 语法定义调整。 - // 1. 判断基本类型(int / float / void 等) + // 1. 获取基本类型 std::shared_ptr base_type; - if (ctx->type() && ctx->type()->INT()) { - base_type = Type::GetInt32Type(); - } else if (ctx->type() && ctx->type()->FLOAT()) { - // 若你扩展了 float 类型,可类似处理 - // base_type = Type::GetFloatType(); + // 假设 ctx 中有 type() 方法返回类型节点,且类型节点有 INT()、FLOAT() 等方法 + if (ctx->type()) { + if (ctx->type()->INT()) { + base_type = Type::GetInt32Type(); + } else if (ctx->type()->FLOAT()) { + base_type = Type::GetFloatType(); + } else { + // 默认为 int + base_type = Type::GetInt32Type(); + } } else { - // 默认为 int - base_type = Type::GetInt32Type(); + base_type = Type::GetInt32Type(); // 默认 } // 2. 处理数组维度 @@ -124,10 +128,16 @@ std::shared_ptr SymbolTable::getTypeFromVarDef(SysYParser::VarDefContext* std::shared_ptr SymbolTable::getTypeFromFuncDef(SysYParser::FuncDefContext* ctx) { // 提取返回类型 std::shared_ptr ret_type; - if (ctx->type() && ctx->type()->VOID()) { - ret_type = Type::GetVoidType(); - } else if (ctx->type() && ctx->type()->INT()) { - ret_type = Type::GetInt32Type(); + if (ctx->type()) { + if (ctx->type()->VOID()) { + ret_type = Type::GetVoidType(); + } else if (ctx->type()->INT()) { + ret_type = Type::GetInt32Type(); + } else if (ctx->type()->FLOAT()) { + ret_type = Type::GetFloatType(); + } else { + ret_type = Type::GetInt32Type(); // 默认 + } } else { ret_type = Type::GetInt32Type(); // 默认 } @@ -136,12 +146,28 @@ std::shared_ptr SymbolTable::getTypeFromFuncDef(SysYParser::FuncDefContext std::vector> param_types; if (ctx->paramList()) { for (auto param : ctx->paramList()->param()) { - // 假设 param 包含类型信息 - // 这里简单构建 int 类型,实际需根据参数声明解析 - param_types.push_back(Type::GetInt32Type()); + // 根据参数声明构建类型 + // 这里假设每个参数有类型节点,并且可能包含数组维度 + std::shared_ptr param_type; + if (param->type()) { + if (param->type()->INT()) { + param_type = Type::GetInt32Type(); + } else if (param->type()->FLOAT()) { + param_type = Type::GetFloatType(); + } else { + param_type = Type::GetInt32Type(); + } + } else { + param_type = Type::GetInt32Type(); + } + + // 处理数组参数(如果存在维度) + // if (param->arraySpecifier()) { ... } + + param_types.push_back(param_type); } } // 创建函数类型 return Type::GetFunctionType(ret_type, param_types); -} +} \ No newline at end of file