feat: 实现控制流语句并优化类型解析

- 新增了 ReturnStmt、BreakStmt 和 ContinueStmt 结构,以支持 AST 中的控制流。
- 引入了 TypeInfo 和 TypeContext 以实现更好的类型管理和解析。
- 对分析器进行了增强,能够处理函数定义和返回语句,包括类型检查和错误处理。
- 更新了编译器和解析器以适应新的控制流语句和类型解析逻辑。
- 重构了现有代码以提高清晰度和可维护性,包括对表达式和语句中的类型处理的更改。
- 删除了过时的 String 和 Struct 定义,代之以 StringObject 和 StructObject 以实现更好的对象表示。
This commit is contained in:
2026-02-28 20:42:15 +08:00
parent bb23ddf9fa
commit 1fe9ccf7ea
18 changed files with 642 additions and 137 deletions

View File

@@ -111,11 +111,6 @@ namespace Fig
return IsDouble() || IsInt();
}
[[nodiscard]] constexpr bool IsObject() const
{
return (v_ & (SIGN_BIT | QNAN_MASK)) == (SIGN_BIT | QNAN_MASK);
}
[[nodiscard]] constexpr bool IsNull() const
{
return v_ == (QNAN_MASK | TAG_NULL);
@@ -126,6 +121,11 @@ namespace Fig
return (v_ | 1) == (QNAN_MASK | TAG_TRUE);
}
[[nodiscard]] constexpr bool IsObject() const
{
return (v_ & (SIGN_BIT | QNAN_MASK)) == (SIGN_BIT | QNAN_MASK);
}
// 提取数据 (Unbox / As)
[[nodiscard]] constexpr double AsDouble() const
{
@@ -182,33 +182,7 @@ namespace Fig
// 类函数
[[nodiscard]]
constexpr String ToString() const
{
if (IsNull())
{
return "null";
}
else if (IsInt())
{
return std::to_string(AsInt());
}
else if (IsDouble())
{
return std::format("{}", AsDouble());
}
else if (IsBool())
{
return (AsBool() ? "true" : "false");
}
else if (IsObject())
{
return "Object"; // TODO: 分派
}
else
{
return "Unknow";
}
}
constexpr String ToString() const;
};
/*
@@ -223,15 +197,35 @@ namespace Fig
Instance,
};
struct Struct /* : public Object */; // 结构体基类的定义,前向声明
struct StructObject /* : public Object */; // 结构体基类的定义,前向声明
// Total 24 bytes size
struct Object
{
Object *next; // 8 bytes: gc链表
Struct *klass; // 8 bytes: 一切皆对象,父类指针
StructObject *klass; // 8 bytes: 一切皆对象,父类指针
ObjectType type; // 1 byte : 类型
bool isMarked = false; // 1 byte : gc标记
// + 6 bytes padding
constexpr bool isString() const
{
return type == ObjectType::String;
}
constexpr bool isFunction() const
{
return type == ObjectType::Function;
}
constexpr bool isStruct() const
{
return type == ObjectType::Struct;
}
constexpr bool isInstance() const
{
return type == ObjectType::Instance;
}
};
} // namespace Fig