From c194a34efb8fd72b0177792b2c7c7eef45590bf1 Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Wed, 4 Sep 2024 23:24:12 +0100 Subject: [PATCH] Add self-evaluating symbols that start with ":" --- src/compiler.cpp | 17 +++++++++++++++++ test/collections.vli | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/src/compiler.cpp b/src/compiler.cpp index 57b0ac1..836e69b 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -897,6 +897,23 @@ Result Compiler::compile_symbol(Context& context, const Value& value) { Expression ex = TRY(Expression::create()); + // Symbol may be self-evaluating (e.g. :foo), in which case we just use it as + // its own value + + if (value.is()) { + Symbol& sym = *value.to(); + + if (sym.size() > 0 && TRY(sym[0]) == ':') { + int64_t c = TRY(context.add_const(value)); + + uint64_t reg = context.alloc_reg(); + TRY(ex.add_opcode(Oc::Mov, {0, (int64_t)reg}, {1, (int64_t)c})); + + ex.reg = reg; + return std::move(ex); + } + } + auto maybe_reg = context.get_var(TRY(value.copy())); if (!maybe_reg.has_error()) { diff --git a/test/collections.vli b/test/collections.vli index c4c290b..0f85575 100644 --- a/test/collections.vli +++ b/test/collections.vli @@ -9,3 +9,7 @@ (assert (= (get {1 2 3 4} 3) 4)) + +;; Accessing collections by symbol +(assert (= (get {:foo 1 :bar 2} :bar) + 2))