Implement "raise" function
This commit is contained in:
parent
810707b349
commit
efef0c57ae
4 changed files with 29 additions and 1 deletions
|
@ -21,6 +21,7 @@ enum class ErrorCode {
|
|||
DivisionByZero,
|
||||
SyntaxError,
|
||||
RuntimeError,
|
||||
Raise,
|
||||
};
|
||||
|
||||
class Value;
|
||||
|
|
|
@ -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},
|
||||
};
|
||||
|
|
|
@ -19,6 +19,7 @@ enum class StdlibFunctionId : uint64_t {
|
|||
Size,
|
||||
Map,
|
||||
Error,
|
||||
Raise,
|
||||
Max,
|
||||
};
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue