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:
75
src/VM/VM.hpp
Normal file
75
src/VM/VM.hpp
Normal file
@@ -0,0 +1,75 @@
|
||||
/*!
|
||||
@file src/VM/VM.hpp
|
||||
@brief 虚拟机核心执行引擎
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-19
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Compiler/Compiler.hpp>
|
||||
#include <Object/Object.hpp>
|
||||
#include <cassert>
|
||||
|
||||
#include <iostream> // debug
|
||||
#include <print>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
class VM
|
||||
{
|
||||
private:
|
||||
static constexpr unsigned int MAX_REGISTERS = 1024;
|
||||
|
||||
// 一次性分配
|
||||
Value registers[MAX_REGISTERS];
|
||||
|
||||
public:
|
||||
VM()
|
||||
{
|
||||
for (unsigned int i = 0; i < MAX_REGISTERS; ++i)
|
||||
{
|
||||
registers[i] = Value::GetNullInstance();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
inline OpCode decodeOpCode(Instruction inst)
|
||||
{
|
||||
return static_cast<OpCode>(inst & 0xFF);
|
||||
}
|
||||
inline std::uint8_t decodeA(Instruction inst)
|
||||
{
|
||||
return (inst >> 8) & 0xFF;
|
||||
}
|
||||
inline std::uint16_t decodeBx(Instruction inst)
|
||||
{
|
||||
return (inst >> 16) & 0xFFFF;
|
||||
}
|
||||
inline std::uint8_t decodeB(Instruction inst)
|
||||
{
|
||||
return (inst >> 16) & 0xFF;
|
||||
}
|
||||
inline std::uint8_t decodeC(Instruction inst)
|
||||
{
|
||||
return (inst >> 24) & 0xFF;
|
||||
}
|
||||
|
||||
public:
|
||||
// 执行入口:接收 Proto
|
||||
Result<Value, Error> Execute(Proto *proto);
|
||||
|
||||
inline void PrintRegisters()
|
||||
{
|
||||
std::cout << "=== Registers ===" << '\n';
|
||||
for (unsigned int i = 0; i < MAX_REGISTERS; ++i)
|
||||
{
|
||||
Value &v = registers[i];
|
||||
if (!v.IsNull())
|
||||
{
|
||||
std::println("[{}] {}", i, v.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace Fig
|
||||
Reference in New Issue
Block a user