Add compiler context

This commit is contained in:
Konstantin Nazarov 2024-08-04 20:38:56 +01:00
parent 0924692d4b
commit 92f546107e
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 48 additions and 11 deletions

View file

@ -1,17 +1,41 @@
#include "compiler.hpp" #include "compiler.hpp"
Result<Value> Compiler::compile(Value& expr) { struct Context {
auto env = TRY(Array::create(_arena)); Context() {}
auto constants = TRY(Array::create(_arena)); 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));
} }
Result<Value> Compiler::compile_expr(Value& expr) {
return Value(TRY(Nil::create(_arena)));
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(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()) { switch (expr.tag()) {
case Tag::Pair: case Tag::Pair:
return compile_list(*expr.to<Pair>()); return compile_list(context, *expr.to<Pair>());
case Tag::Nil: case Tag::Nil:
case Tag::Bool: case Tag::Bool:
case Tag::Int64: case Tag::Int64:
@ -27,7 +51,11 @@ Result<Value> Compiler::compile_expr(Value& expr) {
return ErrorCode::TypeMismatch; 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)); auto first = TRY(expr.first(_arena));
if (first.is<Symbol>()) {
Symbol& sym = *first.to<Symbol>();
}
return ErrorCode::TypeMismatch; return ErrorCode::TypeMismatch;
} }

View file

@ -2,13 +2,22 @@
#include "common.hpp" #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 { class Compiler {
public: public:
Compiler(Arena& arena) : _arena(arena) {} Compiler(Arena& arena) : _arena(arena) {}
Result<Value> compile(Value& expr); Result<Value> compile(Value& expr);
Result<Value> compile_expr(Value& expr); Result<Expression> compile_expr(Context& context, Value& expr);
Result<Value> compile_list(Pair& expr); Result<Expression> compile_list(Context& context, Pair& expr);
private: private:
Arena& _arena; Arena& _arena;

View file

@ -22,7 +22,7 @@ int main() {
DIEX(debug_print(arena, s2)); DIEX(debug_print(arena, s2));
Compiler c = Compiler(arena); Compiler c = Compiler(arena);
auto compiled = DIEX(c.compile_expr(r)); auto compiled = DIEX(c.compile(r));
return 0; return 0;
} }