Add compiler context
This commit is contained in:
parent
0924692d4b
commit
92f546107e
3 changed files with 48 additions and 11 deletions
|
@ -1,17 +1,41 @@
|
|||
#include "compiler.hpp"
|
||||
|
||||
Result<Value> Compiler::compile(Value& expr) {
|
||||
auto env = TRY(Array::create(_arena));
|
||||
auto constants = TRY(Array::create(_arena));
|
||||
struct Context {
|
||||
Context() {}
|
||||
Context(Value&& env, Array&& constants)
|
||||
: env(std::move(env)),
|
||||
constants(std::move(constants)),
|
||||
maxreg(0),
|
||||
maxconst(0) {}
|
||||
|
||||
return compile_expr(expr);
|
||||
static Result<Context> create(Arena& arena) {
|
||||
auto env = TRY(Nil::create(arena));
|
||||
auto constants = TRY(Array::create(arena));
|
||||
|
||||
return Context(std::move(env), std::move(constants));
|
||||
}
|
||||
|
||||
Value env;
|
||||
Array constants;
|
||||
uint64_t maxreg;
|
||||
uint64_t maxconst;
|
||||
};
|
||||
|
||||
bool is_primitive_op(Symbol& sym) {
|
||||
// return (sym.cmp("+") == 0);
|
||||
return false;
|
||||
}
|
||||
Result<Value> Compiler::compile_expr(Value& expr) {
|
||||
return Value(TRY(Nil::create(_arena)));
|
||||
|
||||
Result<Value> Compiler::compile(Value& expr) {
|
||||
auto context = TRY(Context::create(_arena));
|
||||
|
||||
TRY(compile_expr(context, expr));
|
||||
return ErrorCode::NotImplemented;
|
||||
}
|
||||
Result<Expression> Compiler::compile_expr(Context& context, Value& expr) {
|
||||
switch (expr.tag()) {
|
||||
case Tag::Pair:
|
||||
return compile_list(*expr.to<Pair>());
|
||||
return compile_list(context, *expr.to<Pair>());
|
||||
case Tag::Nil:
|
||||
case Tag::Bool:
|
||||
case Tag::Int64:
|
||||
|
@ -27,7 +51,11 @@ Result<Value> Compiler::compile_expr(Value& expr) {
|
|||
return ErrorCode::TypeMismatch;
|
||||
}
|
||||
|
||||
Result<Value> Compiler::compile_list(Pair& expr) {
|
||||
Result<Expression> Compiler::compile_list(Context& context, Pair& expr) {
|
||||
auto first = TRY(expr.first(_arena));
|
||||
|
||||
if (first.is<Symbol>()) {
|
||||
Symbol& sym = *first.to<Symbol>();
|
||||
}
|
||||
return ErrorCode::TypeMismatch;
|
||||
}
|
||||
|
|
|
@ -2,13 +2,22 @@
|
|||
|
||||
#include "common.hpp"
|
||||
|
||||
struct Context;
|
||||
|
||||
struct Expression {
|
||||
Expression() {}
|
||||
Expression(uint64_t reg, Array&& code) : reg(reg), code(std::move(code)) {}
|
||||
uint64_t reg;
|
||||
Array code;
|
||||
};
|
||||
|
||||
class Compiler {
|
||||
public:
|
||||
Compiler(Arena& arena) : _arena(arena) {}
|
||||
|
||||
Result<Value> compile(Value& expr);
|
||||
Result<Value> compile_expr(Value& expr);
|
||||
Result<Value> compile_list(Pair& expr);
|
||||
Result<Expression> compile_expr(Context& context, Value& expr);
|
||||
Result<Expression> compile_list(Context& context, Pair& expr);
|
||||
|
||||
private:
|
||||
Arena& _arena;
|
||||
|
|
|
@ -22,7 +22,7 @@ int main() {
|
|||
DIEX(debug_print(arena, s2));
|
||||
|
||||
Compiler c = Compiler(arena);
|
||||
auto compiled = DIEX(c.compile_expr(r));
|
||||
auto compiled = DIEX(c.compile(r));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue