Handle empty lines in REPL

This commit is contained in:
Konstantin Nazarov 2024-08-26 13:36:01 +01:00
parent 516407f9c2
commit 56ccf2b054
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
2 changed files with 22 additions and 1 deletions

View file

@ -158,6 +158,25 @@ Result<bool> is_comparison_op(Symbol& sym) {
Result<Value> Compiler::compile(Value& expr) { Result<Value> Compiler::compile(Value& expr) {
auto context = TRY(Context::create()); auto context = TRY(Context::create());
// If expression is empty - just return nil
if (expr.is<Nil>()) {
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<Pair>()) { if (!expr.is<Pair>()) {
return ERROR(CompilationError); return ERROR(CompilationError);
} }

View file

@ -31,7 +31,9 @@ Result<void> run_repl() {
while (true) { while (true) {
auto src = TRY(read_line("vli> ")); auto src = TRY(read_line("vli> "));
auto res = TRY(run_string(src)); auto res = TRY(run_string(src));
debug_print(res); if (!res.is<Nil>()) {
debug_print(res);
}
} }
return Result<void>(); return Result<void>();