Handle empty lines in REPL
This commit is contained in:
parent
516407f9c2
commit
56ccf2b054
2 changed files with 22 additions and 1 deletions
|
@ -158,6 +158,25 @@ Result<bool> is_comparison_op(Symbol& sym) {
|
|||
Result<Value> Compiler::compile(Value& expr) {
|
||||
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>()) {
|
||||
return ERROR(CompilationError);
|
||||
}
|
||||
|
|
|
@ -31,8 +31,10 @@ Result<void> run_repl() {
|
|||
while (true) {
|
||||
auto src = TRY(read_line("vli> "));
|
||||
auto res = TRY(run_string(src));
|
||||
if (!res.is<Nil>()) {
|
||||
debug_print(res);
|
||||
}
|
||||
}
|
||||
|
||||
return Result<void>();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue