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:
@@ -16,29 +16,33 @@ namespace Fig
|
||||
enum class AstType : std::uint8_t
|
||||
{
|
||||
AstNode, // 基类
|
||||
Program, // 程序
|
||||
Expr, // 表达式
|
||||
Stmt, // 语句
|
||||
|
||||
/* Expressions */
|
||||
IdentiExpr, // 标识符表达式
|
||||
LiteralExpr, // 字面量表达式
|
||||
PrefixExpr, // 一元 前缀表达式
|
||||
InfixExpr, // 二元 中缀表达式
|
||||
|
||||
IndexExpr, // 后缀表达式,索引
|
||||
CallExpr, // 后缀表达式,函数调用
|
||||
PrefixExpr, // 一元 前缀表达式
|
||||
InfixExpr, // 二元 中缀表达式
|
||||
|
||||
IndexExpr, // 后缀表达式,索引
|
||||
CallExpr, // 后缀表达式,函数调用
|
||||
|
||||
/* Statements */
|
||||
VarDecl,
|
||||
ExprStmt, // 表达式语句,如 println(1)
|
||||
VarDecl, // 变量声明
|
||||
};
|
||||
struct AstNode
|
||||
{
|
||||
AstType type = AstType::AstNode;
|
||||
AstType type = AstType::AstNode;
|
||||
SourceLocation location;
|
||||
|
||||
virtual String toString() const = 0;
|
||||
};
|
||||
|
||||
struct Program;
|
||||
|
||||
struct Expr : public AstNode
|
||||
{
|
||||
Expr()
|
||||
@@ -55,6 +59,31 @@ namespace Fig
|
||||
type = AstType::Stmt;
|
||||
}
|
||||
};
|
||||
|
||||
struct Program final : public AstNode
|
||||
{
|
||||
DynArray<Stmt *> nodes;
|
||||
|
||||
Program()
|
||||
{
|
||||
type = AstType::Program;
|
||||
}
|
||||
|
||||
Program(DynArray<Stmt *> _nodes)
|
||||
{
|
||||
type = AstType::Program;
|
||||
nodes = std::move(_nodes);
|
||||
if (!_nodes.empty())
|
||||
{
|
||||
location = std::move(_nodes.back()->location);
|
||||
}
|
||||
}
|
||||
|
||||
virtual String toString() const override
|
||||
{
|
||||
return "Program";
|
||||
}
|
||||
};
|
||||
}; // namespace Fig
|
||||
|
||||
namespace std
|
||||
@@ -73,4 +102,4 @@ namespace std
|
||||
return std::format_to(ctx.out(), "{}", _node->toString().toStdString());
|
||||
}
|
||||
};
|
||||
};
|
||||
}; // namespace std
|
||||
Reference in New Issue
Block a user