Added CObjError, cosmoV_throw(), pcall(), and cosmoV_printError()

Errors are now handled very differently, parser errors and VM errors are now treated the same.
When cosmoV_error is called, cosmoV_throw is also called, which formats the error object and sets the panic state.
state->error now points to the latest CObjError when state->panic is true. To get a nice formatted Objection message, use
cosmoV_printError() and pass the state->error. pcall() was added to the standard base library. When called, the first argument
passed is called with the subsequent arguments given. If the call completed successfully, `true`,`nil` is returned. However
when an error occurs during the call, `false`,`<error>` is returned. Simply print the `<error>` to retrieve the error string.
This commit is contained in:
2021-01-05 22:27:59 -06:00
parent 417a1f15f1
commit eb2f50e456
13 changed files with 195 additions and 65 deletions

View File

@@ -21,7 +21,7 @@ int cosmoB_assert(CState *state, int nargs, CValue *args) {
}
if (!IS_BOOLEAN(args[0])) {
cosmoV_error(state, "assert() expected (<boolean>), got (%s!)", cosmoV_typeStr(args[0]));
cosmoV_typeError(state, "assert()", "<boolean>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
@@ -43,13 +43,28 @@ int cosmoB_type(CState *state, int nargs, CValue *args) {
return 1; // 1 return value, the type string :D
}
int cosmoB_pcall(CState *state, int nargs, CValue *args) {
if (nargs < 1) {
cosmoV_error(state, "pcall() expected at least 1 argument!");
return 0;
}
// call the passed callable
COSMOVMRESULT res = cosmoV_pcall(state, nargs-1, 1);
// insert false before the result
cosmo_insert(state, 0, cosmoV_newBoolean(res == COSMOVM_OK));
cosmoV_printStack(state);
return 2;
}
// ================================================================ [STRING.*] ================================================================
// string.sub
int cosmoB_sSub(CState *state, int nargs, CValue *args) {
if (nargs == 2) {
if (!IS_STRING(args[0]) || !IS_NUMBER(args[1])) {
cosmoV_error(state, "string.sub() expected (<string>, <number>), got (%s, %s)!", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
cosmoV_typeError(state, "string.sub()", "<string>, <number>", "%s, %s", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
return 0;
}
@@ -58,14 +73,14 @@ int cosmoB_sSub(CState *state, int nargs, CValue *args) {
// make sure we stay within memory
if (indx < 0 || indx >= str->length) {
cosmoV_error(state, "string.sub() Expected index to be 0-%d, got %d!", str->length, indx);
cosmoV_error(state, "string.sub() expected index to be 0-%d, got %d!", str->length, indx);
return 0;
}
cosmoV_pushLString(state, str->str + ((int)indx), str->length - ((int)indx));
} else if (nargs == 3) {
if (!IS_STRING(args[0]) || !IS_NUMBER(args[1]) || !IS_NUMBER(args[2])) {
cosmoV_error(state, "string.sub() expected (<string>, <number>, <number>), got (%s, %s, %s)!", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]), cosmoV_typeStr(args[2]));
cosmoV_typeError(state, "string.sub()", "<string>, <number>, <number>", "%s, %s, %s", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]), cosmoV_typeStr(args[2]));
return 0;
}
@@ -75,7 +90,7 @@ int cosmoB_sSub(CState *state, int nargs, CValue *args) {
// make sure we stay within memory
if (indx + length < 0 || indx + length >= str->length || indx < 0 || indx >= str->length) {
cosmoV_error(state, "string.sub() Expected subbed string goes out of bounds, max length is %d!", str->length);
cosmoV_error(state, "string.sub() expected subbed string goes out of bounds, max length is %d!", str->length);
return 0;
}
@@ -101,6 +116,10 @@ void cosmoB_loadLibrary(CState *state) {
cosmoV_pushString(state, "type");
cosmoV_pushCFunction(state, cosmoB_type);
// pcall
cosmoV_pushString(state, "pcall");
cosmoV_pushCFunction(state, cosmoB_pcall);
// string.
cosmoV_pushString(state, "string");
@@ -112,7 +131,7 @@ void cosmoB_loadLibrary(CState *state) {
// string.
// register these all to the global table
cosmoV_register(state, 4);
cosmoV_register(state, 5);
}
// ================================================================ [DEBUG] ================================================================