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

@@ -3,6 +3,7 @@
#include "cobj.h"
#include "cmem.h"
#include "cvm.h"
#include "clex.h"
#include <string.h>
@@ -75,6 +76,12 @@ void cosmoO_free(CState *state, CObj* obj) {
cosmoM_free(state, CObjMethod, obj); // we don't own the closure or the object so /shrug
break;
}
case COBJ_ERROR: {
CObjError *err = (CObjError*)obj;
cosmoM_freearray(state, CCallFrame, err->frames, err->frameCount);
cosmoM_free(state, CObjError, obj);
break;
}
case COBJ_CLOSURE: {
CObjClosure* closure = (CObjClosure*)obj;
cosmoM_freearray(state, CObjUpval*, closure->upvalues, closure->upvalueCount);
@@ -142,6 +149,21 @@ CObjCFunction *cosmoO_newCFunction(CState *state, CosmoCFunction func) {
return cfunc;
}
CObjError *cosmoO_newError(CState *state, CValue err) {
CObjError *cerror = (CObjError*)cosmoO_allocateBase(state, sizeof(CObjError), COBJ_ERROR);
cerror->err = err;
cerror->frameCount = state->frameCount;
// allocate the callframe
cerror->frames = cosmoM_xmalloc(state, sizeof(CCallFrame) * cerror->frameCount);
// clone the call frame
for (int i = 0; i < state->frameCount; i++)
cerror->frames[i] = state->callFrame[i];
return cerror;
}
CObjMethod *cosmoO_newCMethod(CState *state, CObjCFunction *func, CObjObject *obj) {
CObjMethod *method = (CObjMethod*)cosmoO_allocateBase(state, sizeof(CObjMethod), COBJ_METHOD);
method->func = cosmoV_newObj(func);
@@ -251,6 +273,11 @@ CObjString *cosmoO_pushVFString(CState *state, const char *format, va_list args)
cosmoV_pushString(state, va_arg(args, char *));
break;
}
case 't': { // CToken *
CToken *token = va_arg(args, CToken *);
cosmoV_pushLString(state, token->start, token->length);
break;
}
default: {
char temp[2];
temp[0] = '%';
@@ -420,6 +447,10 @@ CObjString *cosmoO_toString(CState *state, CObj *obj) {
return cosmoO_copyString(state, buf, sz);
}
}
case COBJ_ERROR: {
CObjError *err = (CObjError*)obj;
return cosmoV_toString(state, err->err);
}
case COBJ_DICT: {
char buf[64];
int sz = sprintf(buf, "<dict> %p", (void*)obj) + 1; // +1 for the null character