Compile syntax expressions

This commit is contained in:
Konstantin Nazarov 2024-09-09 22:56:06 +01:00
parent 1958cfb285
commit b63b17ed5a
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 34 additions and 0 deletions

View file

@ -600,6 +600,23 @@ Result<Expression> Compiler::compile_quote(Context& context, Symbol& op,
return std::move(ex);
}
Result<Expression> Compiler::compile_syntax(Context& context, Symbol& op,
const Value& expr) {
auto quoted = TRY(expr.second());
uint64_t maxreg = context.maxreg;
Expression ex = TRY(Expression::create());
int64_t quoted_const = TRY(context.add_const(quoted));
TRY(ex.add_opcode(Oc::Mov, {0, (int64_t)maxreg}, {1, (int64_t)quoted_const}));
ex.reg = maxreg;
context.maxreg = maxreg + 1;
return std::move(ex);
}
Result<Expression> Compiler::compile_fn(Context& context, Symbol& op,
const Value& expr) {
Context ctx = TRY(Context::create(context));
@ -898,6 +915,8 @@ Result<Expression> Compiler::compile_list(Context& context, const Value& expr) {
return compile_let(context, sym, expr);
} else if (TRY(sym.cmp("quote")) == 0) {
return compile_quote(context, sym, expr);
} else if (TRY(sym.cmp("syntax")) == 0) {
return compile_syntax(context, sym, expr);
} else {
return compile_function_call(context, expr);
}

View file

@ -49,6 +49,8 @@ class Compiler {
const Value& expr);
Result<Expression> compile_quote(Context& context, Symbol& op,
const Value& expr);
Result<Expression> compile_syntax(Context& context, Symbol& op,
const Value& expr);
Result<Expression> compile_fn(Context& context, Symbol& op,
const Value& expr);
Result<Expression> compile_let(Context& context, Symbol& op,

13
test/string.cpp Normal file
View file

@ -0,0 +1,13 @@
#include "common.hpp"
#include "die.hpp"
#include "reader.hpp"
#include "test.hpp"
#include "writer.hpp"
StaticArena<64 * 1024 * 1024> arena;
TEST_CASE(dict_insert) {
auto t = DIEX(build_string("foo", "bar", Value(DIEX(Nil::create()))));
ASSERT_EQUALS(t, "foobarnil");
}