From 56ccf2b054816870311a75d19e32db8446f60f6d Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Mon, 26 Aug 2024 13:36:01 +0100 Subject: [PATCH] Handle empty lines in REPL --- src/compiler.cpp | 19 +++++++++++++++++++ src/vli.cpp | 4 +++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/compiler.cpp b/src/compiler.cpp index adff360..f222e1f 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -158,6 +158,25 @@ Result is_comparison_op(Symbol& sym) { Result Compiler::compile(Value& expr) { auto context = TRY(Context::create()); + // If expression is empty - just return nil + if (expr.is()) { + auto ex = TRY(Expression::create()); + uint64_t reg = context.alloc_reg(); + int64_t c = TRY(context.add_const(TRY(Nil::create()))); + TRY(ex.add_opcode(Oc::Mov, {0, (int64_t)reg}, {1, c})); + TRY(ex.add_opcode(Oc::Ret, {0, (int64_t)reg})); + ex.reg = reg; + + Value name = TRY(Nil::create()); + auto fun = TRY(Function::create(name, 0, context.constants, ex.code, + TRY(Array::create()))); + + auto mod = TRY(Module::create(name, fun, context.globals_dict)); + + return Value(std::move(mod)); + } + + // Otherwise perform actual compilation of the expression if (!expr.is()) { return ERROR(CompilationError); } diff --git a/src/vli.cpp b/src/vli.cpp index 4f35e87..13cfb09 100644 --- a/src/vli.cpp +++ b/src/vli.cpp @@ -31,7 +31,9 @@ Result run_repl() { while (true) { auto src = TRY(read_line("vli> ")); auto res = TRY(run_string(src)); - debug_print(res); + if (!res.is()) { + debug_print(res); + } } return Result();