feat: Implement compiler and virtual machine for Fig language

- Added Compiler class with methods for compiling programs, statements, and expressions.
- Introduced Proto structure to hold compiled bytecode and constants.
- Implemented expression compilation including literals, identifiers, and infix expressions.
- Developed statement compilation for variable declarations and expression statements.
- Created a VM class to execute compiled bytecode with support for arithmetic and comparison operations.
- Added Object and Value classes for handling different data types and memory management.
- Implemented String and Struct objects for enhanced data representation.
- Established a parser for parsing variable declarations and statements.
- Included tests for the VM and object representations.
This commit is contained in:
2026-02-20 14:05:56 +08:00
parent f2e899c7a7
commit 2631f76da1
31 changed files with 1722 additions and 94 deletions

28
src/Compiler/Compiler.cpp Normal file
View File

@@ -0,0 +1,28 @@
/*!
@file src/Compiler/Compiler.cpp
@brief 编译器实现
@author PuqiAR (im@puqiar.top)
@date 2026-02-18
*/
#include <Compiler/Compiler.hpp>
namespace Fig
{
Result<Proto *, Error> Compiler::Compile(Program *program)
{
current->proto = new Proto();
current->freeReg = 0;
for (Stmt *stmt : program->nodes)
{
const auto &result = CompileStmt(static_cast<Stmt *>(stmt));
if (!result)
{
return std::unexpected(result.error());
}
}
Emit(Op::iABC(OpCode::Exit, 0, 0, 0)); // 一定要退出,这是虚拟机退出信号,否则ub
return current->proto;
}
}; // namespace Fig