重构类型系统并改进诊断功能
- 更新了类型系统,新增了类型并优化了结构。 - 引入了基类型和派生类,用于函数、结构体和接口类型。 - 实现了类型上下文,用于管理内置类型和类型解析。 - 添加了诊断类,用于收集和报告警告和错误。 - 通过改进错误处理增强了虚拟机执行,以应对递归限制问题。 - 实现了反汇编器,将字节码转换为代码,以改善调试和分析。 - 添加了新的抽象语法树节点,用于成员表达式、对象初始化、接口和结构体定义。 - 引入了语义错误测试,包括重定义、未声明的变量和无效的结构字段。
This commit is contained in:
93
src/Sema/Type.cpp
Normal file
93
src/Sema/Type.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
/*!
|
||||
@file src/Sema/Type.cpp
|
||||
@brief 类型系统实现
|
||||
*/
|
||||
|
||||
#include <Sema/Type.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
bool Type::is(TypeTag t) const
|
||||
{
|
||||
return base && base->tag == t;
|
||||
}
|
||||
|
||||
String Type::toString() const
|
||||
{
|
||||
if (!base)
|
||||
return "Unknown";
|
||||
if (base->tag == TypeTag::Function)
|
||||
{
|
||||
auto *ft = static_cast<FuncType *>(base);
|
||||
String sig = "func(";
|
||||
for (size_t i = 0; i < ft->paramTypes.size(); ++i)
|
||||
{
|
||||
sig += ft->paramTypes[i].toString();
|
||||
if (i < ft->paramTypes.size() - 1)
|
||||
sig += ", ";
|
||||
}
|
||||
sig += ") -> " + ft->retType.toString();
|
||||
return sig;
|
||||
}
|
||||
|
||||
String res = base->name;
|
||||
if (isNullable && base->tag != TypeTag::Null)
|
||||
res += "?";
|
||||
return res;
|
||||
}
|
||||
|
||||
bool Type::isAssignableTo(const Type &target) const
|
||||
{
|
||||
if (target.is(TypeTag::Any) || this->is(TypeTag::Any))
|
||||
return true; // Any 逃逸通道
|
||||
if (this->is(TypeTag::Null) && target.isNullable)
|
||||
return true; // Null 安全赋值
|
||||
return this->base == target.base && (!this->isNullable || target.isNullable); // 严格匹配
|
||||
}
|
||||
|
||||
TypeContext::TypeContext()
|
||||
{
|
||||
intType = new BaseType(TypeTag::Int, "Int");
|
||||
doubleType = new BaseType(TypeTag::Double, "Double");
|
||||
stringType = new BaseType(TypeTag::String, "String");
|
||||
boolType = new BaseType(TypeTag::Bool, "Bool");
|
||||
anyType = new BaseType(TypeTag::Any, "Any");
|
||||
nullType = new BaseType(TypeTag::Null, "Null");
|
||||
|
||||
allTypes.push_back(intType);
|
||||
allTypes.push_back(doubleType);
|
||||
allTypes.push_back(stringType);
|
||||
allTypes.push_back(boolType);
|
||||
allTypes.push_back(anyType);
|
||||
allTypes.push_back(nullType);
|
||||
}
|
||||
|
||||
TypeContext::~TypeContext()
|
||||
{
|
||||
for (auto t : allTypes)
|
||||
delete t;
|
||||
}
|
||||
|
||||
Type TypeContext::GetBasic(TypeTag tag, bool nullable)
|
||||
{
|
||||
BaseType *b = nullptr;
|
||||
switch (tag)
|
||||
{
|
||||
case TypeTag::Int: b = intType; break;
|
||||
case TypeTag::Double: b = doubleType; break;
|
||||
case TypeTag::String: b = stringType; break;
|
||||
case TypeTag::Bool: b = boolType; break;
|
||||
case TypeTag::Any: b = anyType; break;
|
||||
case TypeTag::Null: b = nullType; break;
|
||||
default: break;
|
||||
}
|
||||
return {b, nullable};
|
||||
}
|
||||
|
||||
Type TypeContext::CreateFuncType(DynArray<Type> params, Type ret)
|
||||
{
|
||||
auto *ft = new FuncType(std::move(params), ret);
|
||||
allTypes.push_back(ft);
|
||||
return Type{ft, false};
|
||||
}
|
||||
} // namespace Fig
|
||||
Reference in New Issue
Block a user