From 51dfe6ac5a67695eedaa4984f2bfdaf3a35bc80f Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Sat, 31 Aug 2024 17:30:10 +0100 Subject: [PATCH] A few simplifications of object conversion --- src/common.hpp | 7 +++++++ src/compiler.cpp | 6 +++--- src/vli.cpp | 2 +- src/vm.cpp | 2 +- src/writer.hpp | 4 ++-- test/function.vli | 5 +++++ 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/common.hpp b/src/common.hpp index b956ab6..d0a832b 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -1163,6 +1163,13 @@ Result reverse(Value& val); Result debug_print(const String& val); Result debug_print(const Value& val); +template +Result debug_print(const T& val) + requires std::derived_from +{ + return debug_print(TRY(val.copy_value())); +} + template Result Dict::insert(const K& key, const V& value) { Value k = TRY(Value::create(key)); diff --git a/src/compiler.cpp b/src/compiler.cpp index 4f04abd..a64fe62 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -610,9 +610,9 @@ Result Compiler::compile_fn(Context& context, Symbol& op, TRY(Array::create()))); // std::cout << "--------------- LAMBDA " << arity << "\n"; - // TRY(debug_print(TRY(expr.copy_value()))); - // TRY(debug_print(TRY(ctx.constants.copy()))); - // TRY(debug_print(TRY(ex.code.copy()))); + // TRY(debug_print(expr)); + // TRY(debug_print(ctx.constants)); + // TRY(debug_print(ex.code)); Expression ex_res = TRY(Expression::create()); diff --git a/src/vli.cpp b/src/vli.cpp index 13cfb09..b79b2c7 100644 --- a/src/vli.cpp +++ b/src/vli.cpp @@ -40,7 +40,7 @@ Result run_repl() { } Result run(int argc, const char* argv[]) { - String src = DIEX(String::create("")); + String src = TRY(String::create("")); if (argc == 1) { if (stdin_isatty()) { return run_repl(); diff --git a/src/vm.cpp b/src/vm.cpp index cbb6e5a..c2f849e 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -231,7 +231,7 @@ Result VM::step() { if (!opcode.is()) return ERROR(TypeMismatch); Opcode& oc = *opcode.to(); - // TRY(debug_print(TRY(oc.copy_value()))); + // TRY(debug_print(oc)); switch (oc.opcode()) { case Oc::Mov: diff --git a/src/writer.hpp b/src/writer.hpp index c2a6f7f..9092bec 100644 --- a/src/writer.hpp +++ b/src/writer.hpp @@ -36,7 +36,7 @@ Result write_one(const T& value) requires std::derived_from { auto w = Writer(); - auto v = DIEX(value.copy_value()); + auto v = TRY(value.copy_value()); return w.write_one(v); } @@ -47,6 +47,6 @@ Result write_multiple(const T& value) requires std::derived_from { auto w = Writer(); - auto v = DIEX(value.copy_value()); + auto v = TRY(value.copy_value()); return w.write_multiple(v); } diff --git a/test/function.vli b/test/function.vli index bc0971a..684667d 100644 --- a/test/function.vli +++ b/test/function.vli @@ -11,3 +11,8 @@ (let ((square (fn (x) (* x x)))) (assert (= (square 4) 16)) ) + +(let ((x 42)) + (assert (= ((fn (y) (+ x y) 1) + 43))) + )