重构类型系统并改进诊断功能
- 更新了类型系统,新增了类型并优化了结构。 - 引入了基类型和派生类,用于函数、结构体和接口类型。 - 实现了类型上下文,用于管理内置类型和类型解析。 - 添加了诊断类,用于收集和报告警告和错误。 - 通过改进错误处理增强了虚拟机执行,以应对递归限制问题。 - 实现了反汇编器,将字节码转换为代码,以改善调试和分析。 - 添加了新的抽象语法树节点,用于成员表达式、对象初始化、接口和结构体定义。 - 引入了语义错误测试,包括重定义、未声明的变量和无效的结构字段。
This commit is contained in:
@@ -1,36 +1,125 @@
|
||||
/*!
|
||||
@file src/Compiler/Compiler.cpp
|
||||
@brief 编译器实现
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-18
|
||||
@brief 编译器主逻辑实现:物理 Bootstrapper 与双步扫描
|
||||
*/
|
||||
|
||||
#include <Compiler/Compiler.hpp>
|
||||
#include <Ast/Stmt/FnDefStmt.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
Result<CompiledModule *, Error> Compiler::Compile(Program *program)
|
||||
{
|
||||
current->freeReg = 0;
|
||||
module = new CompiledModule();
|
||||
|
||||
for (Stmt *stmt : program->nodes)
|
||||
// 1. 预留 Protos[0] 给 Bootstrapper
|
||||
Proto *bootProto = new Proto();
|
||||
bootProto->name = "[bootstrapper]";
|
||||
module->protos.push_back(bootProto);
|
||||
|
||||
int initIdx = -1;
|
||||
int mainIdx = -1;
|
||||
|
||||
// 2. 第一步:预扫描顶层函数,锁定物理索引
|
||||
for (auto *stmt : program->nodes)
|
||||
{
|
||||
auto result = compileStmt(static_cast<Stmt *>(stmt));
|
||||
if (!result)
|
||||
if (stmt->type == AstType::FnDefStmt)
|
||||
{
|
||||
return std::unexpected(result.error());
|
||||
auto *f = static_cast<FnDefStmt *>(stmt);
|
||||
int idx = (int) module->protos.size();
|
||||
Proto *p = new Proto();
|
||||
p->name = f->name;
|
||||
p->numParams = (uint8_t) f->params.size();
|
||||
p->maxRegisters = p->numParams;
|
||||
|
||||
module->protos.push_back(p);
|
||||
|
||||
// 连接物理符号到索引
|
||||
if (f->resolvedSymbol)
|
||||
{
|
||||
f->resolvedSymbol->index = idx;
|
||||
}
|
||||
|
||||
if (f->name == "init")
|
||||
initIdx = idx;
|
||||
if (f->name == "main")
|
||||
mainIdx = idx;
|
||||
}
|
||||
}
|
||||
|
||||
if (mainFuncIndex != -1)
|
||||
// 3. 第二步:在 Bootstrapper 环境中编译所有语句
|
||||
FuncState bootState(bootProto, nullptr);
|
||||
current = &bootState;
|
||||
|
||||
for (auto *stmt : program->nodes)
|
||||
{
|
||||
std::uint8_t baseReg = AllocReg();
|
||||
Emit(Op::iABC(OpCode::FastCall, mainFuncIndex, baseReg, 0));
|
||||
auto res = compileStmt(stmt);
|
||||
if (!res)
|
||||
{
|
||||
return std::unexpected(res.error());
|
||||
}
|
||||
}
|
||||
|
||||
Emit(Op::iABC(OpCode::Exit, 0, 0, 0)); // 一定要退出,这是虚拟机退出信号,否则ub
|
||||
|
||||
CompiledModule *compiledModule = new CompiledModule(fileName, allProtos);
|
||||
return compiledModule;
|
||||
// 4. 发射 Bootstrapper 引导指令
|
||||
if (initIdx != -1)
|
||||
{
|
||||
emit(Op::iABC(OpCode::FastCall, (uint8_t) initIdx, 0, 0));
|
||||
}
|
||||
|
||||
if (mainIdx != -1)
|
||||
{
|
||||
emit(Op::iABC(OpCode::FastCall, (uint8_t) mainIdx, 0, 0));
|
||||
}
|
||||
|
||||
emit(Op::iAsBx(OpCode::Exit, 0, 0));
|
||||
|
||||
return module;
|
||||
}
|
||||
}; // namespace Fig
|
||||
|
||||
int Compiler::getGlobalID(const String &name)
|
||||
{
|
||||
if (globalIDMap.contains(name))
|
||||
return globalIDMap[name];
|
||||
int id = (int) globalIDMap.size();
|
||||
globalIDMap[name] = id;
|
||||
return id;
|
||||
}
|
||||
|
||||
Result<Register, Error> Compiler::allocateReg(const SourceLocation &loc)
|
||||
{
|
||||
if (current->freereg >= MAX_REGISTERS)
|
||||
{
|
||||
return std::unexpected(Error(ErrorType::RegisterOverflow, "too many registers", "", loc));
|
||||
}
|
||||
|
||||
Register reg = current->freereg++;
|
||||
if (reg >= current->proto->maxRegisters)
|
||||
{
|
||||
current->proto->maxRegisters = reg + 1;
|
||||
}
|
||||
return reg;
|
||||
}
|
||||
|
||||
void Compiler::freeReg(Register count)
|
||||
{
|
||||
if (current->freereg >= count)
|
||||
{
|
||||
current->freereg -= count;
|
||||
}
|
||||
}
|
||||
|
||||
int Compiler::addConstant(Value val)
|
||||
{
|
||||
if (current->constantMap.contains(val))
|
||||
return current->constantMap[val];
|
||||
int idx = (int) current->proto->constants.size();
|
||||
current->proto->constants.push_back(val);
|
||||
current->constantMap[val] = idx;
|
||||
return idx;
|
||||
}
|
||||
|
||||
void Compiler::emit(Instruction instr)
|
||||
{
|
||||
current->proto->code.push_back(instr);
|
||||
}
|
||||
} // namespace Fig
|
||||
|
||||
Reference in New Issue
Block a user