diff --git a/src/common.cpp b/src/common.cpp index 5e37c14..da5312f 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -898,3 +898,14 @@ Result build_string(uint64_t value) { auto val = Value(TRY(Int64::create((int64_t)value))); return write_one(val); } + +Result print_string(const String& s) { + auto ba = TRY(ByteArray::create(s)); + auto size = TRY(ba.size()); + + for (uint64_t i = 0; i < size; i++) { + std::cout << TRY(ba[i]); + } + + return Result(); +} diff --git a/src/common.hpp b/src/common.hpp index d6e54ff..998a751 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -1336,3 +1336,5 @@ Result build_string(const T& value, Args&&... args) { first = TRY(first.concat(rest)); return first; } + +Result print_string(const String& s); diff --git a/src/compiler.cpp b/src/compiler.cpp index 8e8aa18..10b3643 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -622,29 +622,25 @@ Result Compiler::compile_fn(Context& context, Symbol& op, Context ctx = TRY(Context::create(context)); ctx.maxreg = 1; // Reserve the slot for function itself - auto first = TRY(expr.rest()); - - if (!TRY(syntax_is_list(first))) { - return ERROR(CompilationError); - } + auto rest = TRY(expr.rest()); Value name = TRY(Nil::create()); - auto maybe_name = TRY(first.first()); + auto maybe_name = TRY(expr.second()); auto maybe_name_unwrapped = TRY(syntax_unwrap(maybe_name)); if (maybe_name_unwrapped.is()) { name = TRY(maybe_name_unwrapped.copy()); ctx.fname = TRY(name.copy()); - first = TRY(first.rest()); + rest = TRY(rest.rest()); - if (!TRY(syntax_is_list(first))) { + if (!TRY(syntax_is_list(rest))) { return ERROR(CompilationError); } } - auto param = TRY(first.first()); + auto param = TRY(rest.first()); uint64_t arity = 0; while (!TRY(syntax_is_nil(param))) { @@ -655,7 +651,7 @@ Result Compiler::compile_fn(Context& context, Symbol& op, auto param_name_unwrapped = TRY(syntax_unwrap(param_name)); if (!param_name_unwrapped.is()) { - return ERROR(CompilationError); + return syntax_error(param_name, "Parameter name must be a symbol"); } int64_t reg = TRY(ctx.add_var(param_name_unwrapped)); @@ -664,13 +660,13 @@ Result Compiler::compile_fn(Context& context, Symbol& op, arity++; } - Value second = TRY(first.rest()); + Value body = TRY(rest.rest()); - if (!TRY(syntax_is_list(second))) { + if (!TRY(syntax_is_list(body))) { return ERROR(CompilationError); } - auto ex = TRY(compile_body(ctx, second)); + auto ex = TRY(compile_body(ctx, body)); TRY(ex.add_opcode(Oc::Mov, {0, (int64_t)0}, {0, (int64_t)ex.reg})); TRY(ex.add_opcode(Oc::Ret, {0, (int64_t)0})); @@ -821,9 +817,6 @@ Result Compiler::compile_body(Context& context, const Value& expr) { int64_t maxreg = context.maxreg; while (!cur.is()) { - if (!TRY(syntax_is_list(cur))) { - return ERROR(CompilationError); - } auto expr_val = TRY(cur.first()); // debug_print(expr_val); diff --git a/src/stdlib.cpp b/src/stdlib.cpp index e21e11e..43ac346 100644 --- a/src/stdlib.cpp +++ b/src/stdlib.cpp @@ -17,17 +17,6 @@ Result stdlib_unknown(const Array& params) { return ERROR(NotImplemented); } -Result print_string(const String& s) { - auto ba = TRY(ByteArray::create(s)); - auto size = TRY(ba.size()); - - for (uint64_t i = 0; i < size; i++) { - std::cout << TRY(ba[i]); - } - - return Result(); -} - Result stdlib_print(const Array& params) { auto size = TRY(params.size()); for (uint64_t i = 0; i < size; i++) { diff --git a/src/valeri.cpp b/src/valeri.cpp index 1124612..df54453 100644 --- a/src/valeri.cpp +++ b/src/valeri.cpp @@ -34,7 +34,9 @@ Result print_error(const Result& res) { if (errobj.is()) { debug_print(geterr()); } else { - debug_print(errobj); + auto message = TRY(errobj.to()->message()); + print_string(*message.to()); + std::cout << "\n"; } } @@ -54,7 +56,13 @@ Result run_repl() { } auto parsed = maybe_parsed.release_value(); - auto compiled = TRY(compile(fname, parsed)); + auto maybe_compiled = compile(fname, parsed); + if (maybe_compiled.has_error()) { + print_error(maybe_compiled); + continue; + } + auto compiled = maybe_compiled.release_value(); + Module& mod = *compiled.to(); auto vm = TRY(VM::create()); auto maybe_res = vm.run(mod, globals); @@ -85,11 +93,20 @@ Result run(int argc, const char* argv[]) { } src = TRY(read_stdin()); auto fname = TRY(String::create("")); - TRY(run_string(fname, src)); + auto res = run_string(fname, src); + if (res.has_error()) { + print_error(res); + exit(1); + } + } else { src = TRY(read_file(argv[1])); auto fname = TRY(String::create(argv[1])); - TRY(run_string(fname, src)); + auto res = run_string(fname, src); + if (res.has_error()) { + print_error(res); + exit(1); + } } return Result();