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

@@ -140,29 +140,41 @@ static void freeParseState(CParseState *pstate) {
cosmoL_freeLexState(pstate->state, pstate->lex);
}
static void errorAt(CParseState *pstate, CToken *token, const char * msg) {
static void errorAt(CParseState *pstate, CToken *token, const char *format, va_list args) {
if (pstate->hadError)
return;
fprintf(stderr, "[line %d] Objection", token->line);
if (token->type == TOKEN_EOF) {
fprintf(stderr, " at end");
cosmoV_pushString(pstate->state, "At end: ");
} else if (!(token->type == TOKEN_ERROR)) {
fprintf(stderr, " at '%.*s'", token->length, token->start);
cosmoV_pushFString(pstate->state, "At '%t'", token); // this is why the '%t' exist in cosmoO_pushFString lol
}
printf(": \n\t%s\n", msg);
cosmoO_pushVFString(pstate->state, format, args);
cosmoV_concat(pstate->state, 2); // concats the two strings together
CObjError *err = cosmoV_throw(pstate->state);
err->line = token->line;
err->parserError = true;
pstate->hadError = true;
pstate->panic = true;
}
static void errorAtCurrent(CParseState *pstate, const char *msg) {
errorAt(pstate, &pstate->current, msg);
static void errorAtCurrent(CParseState *pstate, const char *format, ...) {
va_list args;
va_start(args, format);
errorAt(pstate, &pstate->current, format, args);
va_end(args);
}
static void error(CParseState *pstate, const char *msg) {
errorAt(pstate, &pstate->previous, msg);
static void error(CParseState *pstate, const char *format, ...) {
va_list args;
va_start(args, format);
errorAt(pstate, &pstate->previous, format, args);
va_end(args);
}
static void advance(CParseState *pstate) {