Files
Fig/src/Sema/Type.hpp
PuqiAR 51a939ac45 对编译器和虚拟机进行重构,以支持闭包和垃圾回收功能
- 去除了不再使用的结构,并更新了编译器以处理新的闭包语义。
- 改进了 Compiler,使其能够生成带有源位置跟踪的指令。
- 在 FunctionObject 和 VM 中引入了作用域变量管理,以支持动态闭包。
- 实现了使用标记-扫描(Mark-And-Sweep) (Tri-Color tracing) 算法的垃圾回收机制,包括对作用域变量的处理。
- 在 VM 中增加了函数加载和作用域变量检索的支持。
- 更新了对象模型,包括引入 InstanceObject 并改进内存管理。
- 添加了用于调试的全局变量打印功能。
2026-03-11 16:53:10 +08:00

116 lines
2.7 KiB
C++

/*!
@file src/Sema/Type.hpp
@brief 类型系统定义:对齐 NaN-boxing 物理布局
*/
#pragma once
#include <Deps/Deps.hpp>
#include <Error/Error.hpp>
namespace Fig
{
enum class TypeTag : std::uint8_t
{
Int,
Double,
String,
Bool,
Null,
Any,
Function,
Struct,
Interface
};
class BaseType;
struct Type
{
BaseType *base = nullptr;
bool isNullable = false;
bool operator==(const Type &other) const
{
return base == other.base && isNullable == other.isNullable;
}
bool operator!=(const Type &other) const
{
return !(*this == other);
}
bool is(TypeTag tag) const;
String toString() const;
bool isAssignableTo(const Type &target) const;
};
class BaseType
{
public:
TypeTag tag;
String name;
BaseType(TypeTag t, String n) : tag(t), name(std::move(n)) {}
virtual ~BaseType() = default;
};
class FuncType : public BaseType
{
public:
DynArray<Type> paramTypes;
Type retType;
FuncType(DynArray<Type> params, Type ret) :
BaseType(TypeTag::Function, "Function"), paramTypes(std::move(params)), retType(ret)
{
}
};
class StructType : public BaseType
{
public:
struct Field
{
String name;
Type type;
bool isPublic;
int index;
};
DynArray<Field> fields;
HashMap<String, size_t> fieldMap;
HashMap<String, struct FnDefStmt *> methods;
StructType(String n) : BaseType(TypeTag::Struct, std::move(n)) {}
void AddField(String name, Type type, bool isPublic)
{
size_t idx = fields.size();
fields.push_back({name, type, isPublic, (int) idx});
fieldMap[name] = idx;
}
};
class InterfaceType : public BaseType
{
public:
struct MethodSig
{
String name;
DynArray<Type> params;
Type retType;
};
HashMap<String, MethodSig> methods;
InterfaceType(String n) : BaseType(TypeTag::Interface, std::move(n)) {}
};
class TypeContext
{
public:
DynArray<BaseType *> allTypes;
BaseType *intType, *doubleType, *stringType, *boolType, *anyType, *nullType;
TypeContext();
~TypeContext();
Type GetBasic(TypeTag tag, bool nullable = false);
Type CreateFuncType(DynArray<Type> params, Type ret);
};
} // namespace Fig