|
|
|
|
@ -90,16 +90,20 @@ std::shared_ptr<Type> SymbolTable::getTypeFromVarDef(SysYParser::VarDefContext*
|
|
|
|
|
// 假设 ctx 中包含类型节点和数组维度信息。
|
|
|
|
|
// 以下代码为示意,具体实现需根据你的 SysY 语法定义调整。
|
|
|
|
|
|
|
|
|
|
// 1. 判断基本类型(int / float / void 等)
|
|
|
|
|
// 1. 获取基本类型
|
|
|
|
|
std::shared_ptr<Type> 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<Type> SymbolTable::getTypeFromVarDef(SysYParser::VarDefContext*
|
|
|
|
|
std::shared_ptr<Type> SymbolTable::getTypeFromFuncDef(SysYParser::FuncDefContext* ctx) {
|
|
|
|
|
// 提取返回类型
|
|
|
|
|
std::shared_ptr<Type> 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<Type> SymbolTable::getTypeFromFuncDef(SysYParser::FuncDefContext
|
|
|
|
|
std::vector<std::shared_ptr<Type>> param_types;
|
|
|
|
|
if (ctx->paramList()) {
|
|
|
|
|
for (auto param : ctx->paramList()->param()) {
|
|
|
|
|
// 假设 param 包含类型信息
|
|
|
|
|
// 这里简单构建 int 类型,实际需根据参数声明解析
|
|
|
|
|
param_types.push_back(Type::GetInt32Type());
|
|
|
|
|
// 根据参数声明构建类型
|
|
|
|
|
// 这里假设每个参数有类型节点,并且可能包含数组维度
|
|
|
|
|
std::shared_ptr<Type> 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);
|
|
|
|
|
}
|
|
|
|
|
}
|