|
|
|
|
@ -3,8 +3,8 @@
|
|
|
|
|
|
|
|
|
|
// ---------- 构造函数 ----------
|
|
|
|
|
SymbolTable::SymbolTable() {
|
|
|
|
|
// 初始化全局作用域
|
|
|
|
|
scopes_.emplace_back();
|
|
|
|
|
scopes_.emplace_back(); // 初始化全局作用域
|
|
|
|
|
registerBuiltinFunctions(); // 注册内置库函数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------- 作用域管理 ----------
|
|
|
|
|
@ -199,4 +199,98 @@ std::shared_ptr<ir::Type> SymbolTable::getTypeFromFuncDef(SysYParser::FuncDefCon
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ir::Type::GetFunctionType(ret_type, param_types);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----- 注册内置库函数-----
|
|
|
|
|
void SymbolTable::registerBuiltinFunctions() {
|
|
|
|
|
// 确保当前处于全局作用域(scopes_ 只有一层)
|
|
|
|
|
// 1. getint: int getint()
|
|
|
|
|
Symbol getint;
|
|
|
|
|
getint.name = "getint";
|
|
|
|
|
getint.kind = SymbolKind::Function;
|
|
|
|
|
getint.type = Type::GetFunctionType(Type::GetInt32Type(), {}); // 无参数
|
|
|
|
|
getint.param_types = {};
|
|
|
|
|
getint.scope_level = 0;
|
|
|
|
|
getint.is_builtin = true;
|
|
|
|
|
addSymbol(getint);
|
|
|
|
|
|
|
|
|
|
// 2. getfloat: float getfloat()
|
|
|
|
|
Symbol getfloat;
|
|
|
|
|
getfloat.name = "getfloat";
|
|
|
|
|
getfloat.kind = SymbolKind::Function;
|
|
|
|
|
getfloat.type = Type::GetFunctionType(Type::GetFloatType(), {});
|
|
|
|
|
getfloat.param_types = {};
|
|
|
|
|
getfloat.scope_level = 0;
|
|
|
|
|
getfloat.is_builtin = true;
|
|
|
|
|
addSymbol(getfloat);
|
|
|
|
|
|
|
|
|
|
// 3. getch: int getch()
|
|
|
|
|
Symbol getch;
|
|
|
|
|
getch.name = "getch";
|
|
|
|
|
getch.kind = SymbolKind::Function;
|
|
|
|
|
getch.type = Type::GetFunctionType(Type::GetInt32Type(), {});
|
|
|
|
|
getch.param_types = {};
|
|
|
|
|
getch.scope_level = 0;
|
|
|
|
|
getch.is_builtin = true;
|
|
|
|
|
addSymbol(getch);
|
|
|
|
|
|
|
|
|
|
// 4. putint: void putint(int)
|
|
|
|
|
std::vector<std::shared_ptr<Type>> putint_params = { Type::GetInt32Type() };
|
|
|
|
|
Symbol putint;
|
|
|
|
|
putint.name = "putint";
|
|
|
|
|
putint.kind = SymbolKind::Function;
|
|
|
|
|
putint.type = Type::GetFunctionType(Type::GetVoidType(), putint_params);
|
|
|
|
|
putint.param_types = putint_params;
|
|
|
|
|
putint.scope_level = 0;
|
|
|
|
|
putint.is_builtin = true;
|
|
|
|
|
addSymbol(putint);
|
|
|
|
|
|
|
|
|
|
// 5. putfloat: void putfloat(float)
|
|
|
|
|
std::vector<std::shared_ptr<Type>> putfloat_params = { Type::GetFloatType() };
|
|
|
|
|
Symbol putfloat;
|
|
|
|
|
putfloat.name = "putfloat";
|
|
|
|
|
putfloat.kind = SymbolKind::Function;
|
|
|
|
|
putfloat.type = Type::GetFunctionType(Type::GetVoidType(), putfloat_params);
|
|
|
|
|
putfloat.param_types = putfloat_params;
|
|
|
|
|
putfloat.scope_level = 0;
|
|
|
|
|
putfloat.is_builtin = true;
|
|
|
|
|
addSymbol(putfloat);
|
|
|
|
|
|
|
|
|
|
// 6. putch: void putch(int)
|
|
|
|
|
std::vector<std::shared_ptr<Type>> putch_params = { Type::GetInt32Type() };
|
|
|
|
|
Symbol putch;
|
|
|
|
|
putch.name = "putch";
|
|
|
|
|
putch.kind = SymbolKind::Function;
|
|
|
|
|
putch.type = Type::GetFunctionType(Type::GetVoidType(), putch_params);
|
|
|
|
|
putch.param_types = putch_params;
|
|
|
|
|
putch.scope_level = 0;
|
|
|
|
|
putch.is_builtin = true;
|
|
|
|
|
addSymbol(putch);
|
|
|
|
|
|
|
|
|
|
// 7. getarray: int getarray(int a[])
|
|
|
|
|
// 参数类型: int a[] 退化为 int* 即 PtrInt32
|
|
|
|
|
std::vector<std::shared_ptr<Type>> getarray_params = { Type::GetPtrInt32Type() };
|
|
|
|
|
Symbol getarray;
|
|
|
|
|
getarray.name = "getarray";
|
|
|
|
|
getarray.kind = SymbolKind::Function;
|
|
|
|
|
getarray.type = Type::GetFunctionType(Type::GetInt32Type(), getarray_params);
|
|
|
|
|
getarray.param_types = getarray_params;
|
|
|
|
|
getarray.scope_level = 0;
|
|
|
|
|
getarray.is_builtin = true;
|
|
|
|
|
addSymbol(getarray);
|
|
|
|
|
|
|
|
|
|
// 8. putarray: void putarray(int n, int a[])
|
|
|
|
|
// 参数: int n, int a[] -> 实际类型: int, int*
|
|
|
|
|
std::vector<std::shared_ptr<Type>> putarray_params = { Type::GetInt32Type(), Type::GetPtrInt32Type() };
|
|
|
|
|
Symbol putarray;
|
|
|
|
|
putarray.name = "putarray";
|
|
|
|
|
putarray.kind = SymbolKind::Function;
|
|
|
|
|
putarray.type = Type::GetFunctionType(Type::GetVoidType(), putarray_params);
|
|
|
|
|
putarray.param_types = putarray_params;
|
|
|
|
|
putarray.scope_level = 0;
|
|
|
|
|
putarray.is_builtin = true;
|
|
|
|
|
addSymbol(putarray);
|
|
|
|
|
|
|
|
|
|
// 9. putf: void putf(char fmt[], ...) —— 可选,但为了完整性
|
|
|
|
|
// 参数: char fmt[] 退化为 char*,但 SysY 中没有 char 类型,可能使用 int 数组或特殊处理,此处略过
|
|
|
|
|
}
|