Implement "raise" function

This commit is contained in:
Konstantin Nazarov 2024-09-24 20:53:26 +01:00
parent 810707b349
commit efef0c57ae
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
4 changed files with 29 additions and 1 deletions

View file

@ -21,6 +21,7 @@ enum class ErrorCode {
DivisionByZero,
SyntaxError,
RuntimeError,
Raise,
};
class Value;

View file

@ -293,6 +293,28 @@ Result<StackFrame> stdlib_error(const StackFrame& stack) {
return ERROR_OBJ(RuntimeError, val);
}
Result<StackFrame> stdlib_raise(const StackFrame& stack) {
auto stack_size = TRY(stack.size());
if (stack_size > 1) {
auto accumulator = TRY(stack.get(0));
auto res = TRY(stack.set(0, accumulator));
res = TRY(res.ret(0));
return res;
}
auto params = TRY(stack.get(0));
auto size = TRY(params.size());
if (size != 1) return ERROR(ArgumentCountMismatch);
auto val = TRY(params.get(0));
auto cont = Value(TRY(Continuation::create(val, stack)));
return ERROR_OBJ(Raise, cont);
}
#define STDLIB_FUNCTION(name, id) \
[(uint64_t)StdlibFunctionId::id] = {#name, StdlibFunctionId::id, \
stdlib_##name}
@ -312,6 +334,7 @@ static StdlibFunctionEntry function_entries[] = {
STDLIB_FUNCTION(size, Size),
STDLIB_FUNCTION(map, Map),
STDLIB_FUNCTION(error, Error),
STDLIB_FUNCTION(raise, Raise),
[(uint64_t)StdlibFunctionId::Max] = {0, StdlibFunctionId::Max,
stdlib_unknown},
};

View file

@ -19,6 +19,7 @@ enum class StdlibFunctionId : uint64_t {
Size,
Map,
Error,
Raise,
Max,
};

View file

@ -18,10 +18,13 @@ Result<void> print_error(const Result<Value>& res, const String& backtrace) {
auto errobj = TRY(geterrobj().copy());
if (errobj.is<Nil>()) {
debug_print(geterr());
} else {
} else if (errobj.is<Error>()) {
auto message = TRY(errobj.to<Error>()->message());
print_string(*message.to<String>());
std::cout << "\n";
} else {
std::cout << "Uncaught error: ";
debug_print(errobj);
}
if (TRY(backtrace.size()) > 0) {