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

127
src/VM/VM.cpp Normal file
View File

@@ -0,0 +1,127 @@
/*!
@file src/VM/VM.hpp
@brief 虚拟机核心执行引擎实现
@author PuqiAR (im@puqiar.top)
@date 2026-02-19
*/
#include <VM/VM.hpp>
#define BINARY_ARITHMETIC_OP(opCode, op) \
case OpCode::opCode: { \
std::uint8_t b = decodeB(inst); \
std::uint8_t c = decodeC(inst); \
Value lhs = registers[b]; \
Value rhs = registers[c]; \
if (lhs.IsInt() && rhs.IsInt()) [[likely]] \
{ \
registers[a] = Value::FromInt(lhs.AsInt() op rhs.AsInt()); \
} \
else if (lhs.IsDouble() && rhs.IsDouble()) [[likely]] \
{ \
registers[a] = Value::FromDouble(lhs.AsDouble() op rhs.AsDouble()); \
} \
/* 隐式类型提升Int 与 Double 混合运算 */ \
else if (lhs.IsInt() && rhs.IsDouble()) [[likely]] \
{ \
registers[a] = Value::FromDouble(lhs.AsInt() op rhs.AsDouble()); \
} \
else if (lhs.IsDouble() && rhs.IsInt()) [[likely]] \
{ \
registers[a] = Value::FromDouble(lhs.AsDouble() op rhs.AsInt()); \
} \
else \
{ \
assert(false && "VM Runtime Error: Unsupported types for arithmetic operation"); \
} \
break; \
}
#define BINARY_COMPARE_OP(opCode, op) \
case OpCode::opCode: { \
std::uint8_t b = decodeB(inst); \
std::uint8_t c = decodeC(inst); \
Value lhs = registers[b]; \
Value rhs = registers[c]; \
if (lhs.IsInt() && rhs.IsInt()) [[likely]] \
{ \
registers[a] = (lhs.AsInt() op rhs.AsInt()) ? Value::GetTrueInstance() : Value::GetFalseInstance(); \
} \
else if (lhs.IsDouble() && rhs.IsDouble()) [[likely]] \
{ \
registers[a] = (lhs.AsDouble() op rhs.AsDouble()) ? Value::GetTrueInstance() : Value::GetFalseInstance(); \
} \
else if (lhs.IsInt() && rhs.IsDouble()) [[likely]] \
{ \
registers[a] = (lhs.AsInt() op rhs.AsDouble()) ? Value::GetTrueInstance() : Value::GetFalseInstance(); \
} \
else if (lhs.IsDouble() && rhs.IsInt()) [[likely]] \
{ \
registers[a] = (lhs.AsDouble() op rhs.AsInt()) ? Value::GetTrueInstance() : Value::GetFalseInstance(); \
} \
else \
{ \
/* TODO: 非数字比较 */ \
assert(false && "VM Runtime Error: Unsupported types for comparison"); \
} \
break; \
}
namespace Fig
{
Result<Value, Error> VM::Execute(Proto *proto)
{
// 指令指针 (Instruction Pointer / PC) 和 常量池指针
const Instruction *ip = proto->code.data();
const Value *k = proto->constants.data();
// 核心解释器循环 (The Dispatch Loop)
while (true)
{
// 取指并递增指针
Instruction inst = *ip++;
// 解码 OpCode 和 A 操作数
OpCode op = decodeOpCode(inst);
std::uint8_t a = decodeA(inst);
switch (op)
{
case OpCode::Exit: {
return Value::GetNullInstance();
}
case OpCode::LoadK: {
std::uint16_t bx = decodeBx(inst);
registers[a] = k[bx]; // constants
break;
}
case OpCode::Mov: {
std::uint16_t bx = decodeBx(inst);
registers[a] = registers[bx];
break;
}
BINARY_ARITHMETIC_OP(Add, +);
BINARY_ARITHMETIC_OP(Sub, -);
BINARY_ARITHMETIC_OP(Mul, *);
BINARY_ARITHMETIC_OP(Div, /);
BINARY_COMPARE_OP(Equal, ==);
BINARY_COMPARE_OP(NotEqual, !=);
BINARY_COMPARE_OP(Greater, >);
BINARY_COMPARE_OP(Less, <);
BINARY_COMPARE_OP(GreaterEqual, >=);
BINARY_COMPARE_OP(LessEqual, <=);
case OpCode::Return: {
return registers[a];
}
default: {
assert(false && "VM: Unknown OpCode encountered!");
}
}
}
return Value::GetNullInstance();
}
}; // namespace Fig