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

@@ -8,6 +8,7 @@
#include "ctable.h"
typedef struct CState CState;
typedef struct CCallFrame CCallFrame;
typedef uint32_t cosmo_Flag;
typedef enum {
@@ -16,6 +17,7 @@ typedef enum {
COBJ_DICT, // dictionary
COBJ_FUNCTION,
COBJ_CFUNCTION,
COBJ_ERROR,
// internal use
COBJ_METHOD,
COBJ_CLOSURE,
@@ -43,6 +45,15 @@ typedef struct CObjString {
uint32_t hash; // for hashtable lookup
} CObjString;
typedef struct CObjError {
CommonHeader; // "is a" CObj
bool parserError; // if true, cosmoV_printError will format the error to the lexer
int frameCount;
int line; // reserved for parser errors
CValue err; // error string
CCallFrame *frames;
} CObjError;
typedef struct CObjObject {
CommonHeader; // "is a" CObj
cosmo_Flag istringFlags; // enables us to have a much faster lookup for reserved IStrings (like __init, __index, etc.)
@@ -129,6 +140,7 @@ CObjObject *cosmoO_newObject(CState *state);
CObjDict *cosmoO_newDictionary(CState *state);
CObjFunction *cosmoO_newFunction(CState *state);
CObjCFunction *cosmoO_newCFunction(CState *state, CosmoCFunction func);
CObjError *cosmoO_newError(CState *state, CValue err);
CObjMethod *cosmoO_newMethod(CState *state, CObjClosure *func, CObjObject *obj);
CObjMethod *cosmoO_newCMethod(CState *state, CObjCFunction *func, CObjObject *obj);
CObjClosure *cosmoO_newClosure(CState *state, CObjFunction *func);
@@ -161,6 +173,7 @@ CObjString *cosmoO_allocateString(CState *state, const char *str, size_t sz, uin
'%d' - decimal numbers [int]
'%f' - floating point [double]
'%s' - strings [const char*]
'%t' - cosmo tokens [CToken *]
*/
CObjString *cosmoO_pushVFString(CState *state, const char *format, va_list args);