From b63b17ed5af683ebd6e5324dbac6d6f4900c281b Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Mon, 9 Sep 2024 22:56:06 +0100 Subject: [PATCH] Compile syntax expressions --- src/compiler.cpp | 19 +++++++++++++++++++ src/compiler.hpp | 2 ++ test/string.cpp | 13 +++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 test/string.cpp diff --git a/src/compiler.cpp b/src/compiler.cpp index efe7583..fe3b367 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -600,6 +600,23 @@ Result Compiler::compile_quote(Context& context, Symbol& op, return std::move(ex); } + +Result 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 Compiler::compile_fn(Context& context, Symbol& op, const Value& expr) { Context ctx = TRY(Context::create(context)); @@ -898,6 +915,8 @@ Result 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); } diff --git a/src/compiler.hpp b/src/compiler.hpp index bdcd97a..87af4ac 100644 --- a/src/compiler.hpp +++ b/src/compiler.hpp @@ -49,6 +49,8 @@ class Compiler { const Value& expr); Result compile_quote(Context& context, Symbol& op, const Value& expr); + Result compile_syntax(Context& context, Symbol& op, + const Value& expr); Result compile_fn(Context& context, Symbol& op, const Value& expr); Result compile_let(Context& context, Symbol& op, diff --git a/test/string.cpp b/test/string.cpp new file mode 100644 index 0000000..2586dda --- /dev/null +++ b/test/string.cpp @@ -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"); +}