2024-07-30 22:52:23 +00:00
|
|
|
#include "compiler.hpp"
|
|
|
|
|
2024-08-04 19:38:56 +00:00
|
|
|
struct Context {
|
|
|
|
Context() {}
|
|
|
|
Context(Value&& env, Array&& constants)
|
|
|
|
: env(std::move(env)),
|
|
|
|
constants(std::move(constants)),
|
|
|
|
maxreg(0),
|
|
|
|
maxconst(0) {}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
2024-07-30 22:52:23 +00:00
|
|
|
|
2024-08-04 19:38:56 +00:00
|
|
|
bool is_primitive_op(Symbol& sym) {
|
|
|
|
// return (sym.cmp("+") == 0);
|
|
|
|
return false;
|
2024-07-30 22:52:23 +00:00
|
|
|
}
|
|
|
|
|
2024-08-04 19:38:56 +00:00
|
|
|
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) {
|
2024-07-30 22:52:23 +00:00
|
|
|
switch (expr.tag()) {
|
|
|
|
case Tag::Pair:
|
2024-08-04 19:38:56 +00:00
|
|
|
return compile_list(context, *expr.to<Pair>());
|
2024-07-30 22:52:23 +00:00
|
|
|
case Tag::Nil:
|
|
|
|
case Tag::Bool:
|
|
|
|
case Tag::Int64:
|
|
|
|
case Tag::Float:
|
|
|
|
case Tag::String:
|
|
|
|
case Tag::Symbol:
|
|
|
|
case Tag::Syntax:
|
|
|
|
case Tag::Array:
|
|
|
|
case Tag::ByteArray:
|
2024-08-01 17:56:38 +00:00
|
|
|
case Tag::Dict:
|
2024-07-30 22:52:23 +00:00
|
|
|
return ErrorCode::TypeMismatch;
|
|
|
|
}
|
|
|
|
return ErrorCode::TypeMismatch;
|
|
|
|
}
|
|
|
|
|
2024-08-04 19:38:56 +00:00
|
|
|
Result<Expression> Compiler::compile_list(Context& context, Pair& expr) {
|
2024-08-01 17:56:38 +00:00
|
|
|
auto first = TRY(expr.first(_arena));
|
2024-08-04 19:38:56 +00:00
|
|
|
|
|
|
|
if (first.is<Symbol>()) {
|
|
|
|
Symbol& sym = *first.to<Symbol>();
|
|
|
|
}
|
2024-07-30 22:52:23 +00:00
|
|
|
return ErrorCode::TypeMismatch;
|
|
|
|
}
|