feat: 实现控制流语句并优化类型解析
- 新增了 ReturnStmt、BreakStmt 和 ContinueStmt 结构,以支持 AST 中的控制流。 - 引入了 TypeInfo 和 TypeContext 以实现更好的类型管理和解析。 - 对分析器进行了增强,能够处理函数定义和返回语句,包括类型检查和错误处理。 - 更新了编译器和解析器以适应新的控制流语句和类型解析逻辑。 - 重构了现有代码以提高清晰度和可维护性,包括对表达式和语句中的类型处理的更改。 - 删除了过时的 String 和 Struct 定义,代之以 StringObject 和 StructObject 以实现更好的对象表示。
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
/*!
|
||||
@file src/Sema/Type.hpp
|
||||
@brief 前端类型检查的类型定义
|
||||
@brief 前端类型检查的类型定义和类型驻留池
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-23
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Deps/Deps.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
namespace Fig
|
||||
@@ -23,6 +24,113 @@ namespace Fig
|
||||
Struct,
|
||||
};
|
||||
|
||||
// TODO: 复杂类型的推导(泛型,结构体)
|
||||
// 添加 TypeInfo 结构体,目前先用 TypeTag
|
||||
};
|
||||
struct TypeInfo
|
||||
{
|
||||
TypeTag tag;
|
||||
String name; // 完整路径序列化, 如 Int, std.file.File
|
||||
|
||||
bool isAny() const
|
||||
{
|
||||
return tag == TypeTag::Any;
|
||||
}
|
||||
};
|
||||
|
||||
// 全局唯一类型驻留池
|
||||
class TypeContext
|
||||
{
|
||||
private:
|
||||
DynArray<TypeInfo *> allTypes;
|
||||
|
||||
// 缓存
|
||||
TypeInfo *typeAny;
|
||||
TypeInfo *typeNull;
|
||||
TypeInfo *typeInt;
|
||||
TypeInfo *typeDouble;
|
||||
TypeInfo *typeBool;
|
||||
TypeInfo *typeString;
|
||||
TypeInfo *typeFunction;
|
||||
TypeInfo *typeStruct;
|
||||
|
||||
public:
|
||||
TypeInfo *GetAny()
|
||||
{
|
||||
return typeAny;
|
||||
}
|
||||
TypeInfo *GetNull()
|
||||
{
|
||||
return typeNull;
|
||||
}
|
||||
TypeInfo *GetInt()
|
||||
{
|
||||
return typeInt;
|
||||
}
|
||||
TypeInfo *GetDouble()
|
||||
{
|
||||
return typeDouble;
|
||||
}
|
||||
TypeInfo *GetBool()
|
||||
{
|
||||
return typeBool;
|
||||
}
|
||||
TypeInfo *GetString()
|
||||
{
|
||||
return typeString;
|
||||
}
|
||||
TypeInfo *GetFunction()
|
||||
{
|
||||
return typeFunction;
|
||||
}
|
||||
TypeInfo *GetStruct()
|
||||
{
|
||||
return typeStruct;
|
||||
}
|
||||
|
||||
TypeInfo *ResolveTypePath(const String &fullName)
|
||||
{
|
||||
for (auto *t : allTypes)
|
||||
{
|
||||
if (t->name == fullName)
|
||||
return t;
|
||||
}
|
||||
return nullptr; // 没找到该类型
|
||||
}
|
||||
|
||||
TypeInfo *ResolveTypePath(const DynArray<String> &path)
|
||||
{
|
||||
// TODO: 支持Module 系统, 查 Module 的导出表
|
||||
String fullName = path.empty() ? "" : path[0];
|
||||
for (size_t i = 1; i < path.size(); ++i)
|
||||
{
|
||||
fullName += "." + path[i];
|
||||
}
|
||||
|
||||
return ResolveTypePath(fullName);
|
||||
}
|
||||
|
||||
~TypeContext()
|
||||
{
|
||||
for (TypeInfo *t : allTypes)
|
||||
delete t;
|
||||
}
|
||||
|
||||
TypeContext()
|
||||
{
|
||||
typeAny = createBuiltin(TypeTag::Any, "Any");
|
||||
typeNull = createBuiltin(TypeTag::Null, "Null");
|
||||
typeInt = createBuiltin(TypeTag::Int, "Int");
|
||||
typeDouble = createBuiltin(TypeTag::Double, "Double");
|
||||
typeBool = createBuiltin(TypeTag::Bool, "Bool");
|
||||
typeString = createBuiltin(TypeTag::String, "String");
|
||||
typeFunction = createBuiltin(TypeTag::Function, "Function");
|
||||
typeStruct = createBuiltin(TypeTag::Struct, "Struct");
|
||||
}
|
||||
|
||||
private:
|
||||
TypeInfo *createBuiltin(TypeTag tag, String name)
|
||||
{
|
||||
TypeInfo *t = new TypeInfo{tag, std::move(name)};
|
||||
allTypes.push_back(t);
|
||||
return t;
|
||||
}
|
||||
};
|
||||
}; // namespace Fig
|
||||
Reference in New Issue
Block a user