Compare commits

..

No commits in common. "e0455902b0109d99aada495a7ec1381ee5d1444b" and "e7b2d7d833c340bd76a0cfd551658d79d75cf838" have entirely different histories.

12 changed files with 50 additions and 52 deletions

24
main.c
View File

@ -50,23 +50,13 @@ static bool interpret(CState *state, const char *script, const char *mod)
// cosmoV_compileString pushes the result onto the stack (COBJ_ERROR or COBJ_CLOSURE)
if (cosmoV_compileString(state, script, mod)) {
if (!cosmoV_pcall(state, 0, 1)) {
cosmoV_printBacktrace(state, cosmoV_readError(*cosmoV_pop(state)));
// 0 args being passed, 0 results expected
if (!cosmoV_pcall(state, 0, 0)) {
cosmoV_printError(state, cosmoV_readError(*cosmoV_pop(state)));
return false;
}
// if the result is nil, we don't print it
if (IS_NIL(*cosmoV_getTop(state, 0))) {
cosmoV_pop(state);
return true;
}
// otherwise, we print the result
cosmoV_printValue(*cosmoV_getTop(state, 0));
printf("\n");
cosmoV_pop(state);
} else {
cosmoV_printBacktrace(state, cosmoV_readError(*cosmoV_pop(state)));
cosmoV_printError(state, cosmoV_readError(*cosmoV_pop(state)));
return false;
}
@ -168,7 +158,7 @@ void compileScript(CState *state, const char *in, const char *out)
CObjFunction *func = cosmoV_readClosure(*cosmoV_getTop(state, 0))->function;
cosmoD_dump(state, func, fileWriter, (void *)fout);
} else {
cosmoV_printBacktrace(state, cosmoV_readError(*cosmoV_pop(state)));
cosmoV_printError(state, cosmoV_readError(*cosmoV_pop(state)));
}
free(script);
@ -181,13 +171,13 @@ void loadScript(CState *state, const char *in)
{
FILE *file = fopen(in, "rb");
if (!cosmoV_undump(state, fileReader, file)) {
cosmoV_printBacktrace(state, cosmoV_readError(*cosmoV_pop(state)));
cosmoV_printError(state, cosmoV_readError(*cosmoV_pop(state)));
return;
};
printf("[!] loaded %s!\n", in);
if (!cosmoV_pcall(state, 0, 0))
cosmoV_printBacktrace(state, cosmoV_readError(*cosmoV_pop(state)));
cosmoV_printError(state, cosmoV_readError(*cosmoV_pop(state)));
fclose(file);
}

View File

@ -18,7 +18,7 @@ int cosmoB_print(CState *state, int nargs, CValue *args)
CObjString *str = cosmoV_toString(state, args[i]);
printf("%s", cosmoO_readCString(str));
} else { // else, thats pretty expensive for primitives, just print the raw value
cosmoV_printValue(args[i]);
printValue(args[i]);
}
}
printf("\n");

View File

@ -62,7 +62,7 @@ static int constInstruction(const char *name, CChunk *chunk, int offset)
printf("%-16s [%05d] - ", name, index);
CValue val = chunk->constants.values[index];
cosmoV_printValue(val);
printValue(val);
return offset + 1 + (sizeof(uint16_t) / sizeof(INSTRUCTION)); // consume opcode + uint
}
@ -128,7 +128,7 @@ int disasmInstr(CChunk *chunk, int offset, int indent)
CObjFunction *cobjFunc = (CObjFunction *)cosmoV_readRef(val);
offset += 3; // we consumed the opcode + u16
cosmoV_printValue(val);
printValue(val);
printf("\n");
// list the upvalues/locals that are captured

View File

@ -715,13 +715,13 @@ void printObject(CObj *o)
case COBJ_ERROR: {
CObjError *err = (CObjError *)o;
printf("%p -> ", (void *)o);
cosmoV_printValue(err->err);
printValue(err->err);
break;
}
case COBJ_METHOD: {
CObjMethod *method = (CObjMethod *)o;
printf("%p -> ", (void *)method);
cosmoV_printValue(method->func);
printValue(method->func);
break;
}
case COBJ_CLOSURE: {
@ -733,7 +733,7 @@ void printObject(CObj *o)
case COBJ_UPVALUE: {
CObjUpval *upval = (CObjUpval *)o;
printf("%p -> ", (void *)upval->val);
cosmoV_printValue(*upval->val);
printValue(*upval->val);
break;
}
default:

View File

@ -55,7 +55,7 @@ struct CObjError
CCallFrame *frames;
int frameCount;
int line; // reserved for parser errors
bool parserError; // if true, cosmoV_printBacktrace will format the error to the lexer
bool parserError; // if true, cosmoV_printError will format the error to the lexer
};
struct CObjObject

View File

@ -102,6 +102,7 @@ static int expressionPrecedence(CParseState *pstate, int needed, Precedence prec
// returns # of pushed values onto the stack
static int expression(CParseState *pstate, int needed, bool forceNeeded);
static void statement(CParseState *pstate);
static void declaration(CParseState *pstate);
static void parseFunction(CParseState *pstate, FunctionType type);
static void expressionStatement(CParseState *pstate);
static ParseRule *getRule(CTokenType type);
@ -1098,19 +1099,18 @@ static ParseRule *getRule(CTokenType type)
// returns true if it got past the first token (aka prefix wasn't null)
static bool parsePrecedence(CParseState *pstate, Precedence prec)
{
bool canAssign;
ParseFunc prefix, infix;
advance(pstate);
if ((prefix = getRule(pstate->previous.type)->prefix) == NULL)
ParseFunc prefix = getRule(pstate->previous.type)->prefix;
if (prefix == NULL)
return false;
canAssign = prec <= PREC_ASSIGNMENT;
bool canAssign = prec <= PREC_ASSIGNMENT;
prefix(pstate, canAssign, prec);
while (prec <= getRule(pstate->current.type)->level) {
if ((infix = getRule(pstate->current.type)->infix) == NULL)
break;
while (prec <= getRule(pstate->current.type)->level) {
ParseFunc infix = getRule(pstate->current.type)->infix;
advance(pstate);
infix(pstate, canAssign, prec);
}
@ -1264,7 +1264,7 @@ static void endScope(CParseState *pstate)
static void block(CParseState *pstate)
{
while (!check(pstate, TOKEN_END) && !check(pstate, TOKEN_EOF) && !check(pstate, TOKEN_ERROR)) {
statement(pstate);
declaration(pstate);
}
consume(pstate, TOKEN_END, "'end' expected to end block.'");
@ -1321,7 +1321,7 @@ static void ifStatement(CParseState *pstate)
while (!check(pstate, TOKEN_END) && !check(pstate, TOKEN_ELSE) &&
!check(pstate, TOKEN_ELSEIF) && !check(pstate, TOKEN_EOF) &&
!check(pstate, TOKEN_ERROR)) {
statement(pstate);
declaration(pstate);
}
endScope(pstate);
@ -1471,10 +1471,10 @@ static void functionDeclaration(CParseState *pstate)
static void returnStatement(CParseState *pstate)
{
// if (pstate->compiler->type != FTYPE_FUNCTION && pstate->compiler->type != FTYPE_METHOD) {
// error(pstate, "Expected 'return' in function!");
// return;
// }
if (pstate->compiler->type != FTYPE_FUNCTION && pstate->compiler->type != FTYPE_METHOD) {
error(pstate, "Expected 'return' in function!");
return;
}
if (blockFollow(pstate->current)) { // does this return have a value
writeu8(pstate, OP_NIL);
@ -1589,7 +1589,7 @@ static void forLoop(CParseState *pstate)
// parse initializer
if (!match(pstate, TOKEN_EOS)) {
statement(pstate);
expressionStatement(pstate);
consume(pstate, TOKEN_EOS, "Expected ';' after initializer");
}
@ -1705,7 +1705,7 @@ static int expression(CParseState *pstate, int needed, bool forceNeeded)
forceNeeded); // anything above assignments are an expression
}
static void statement(CParseState *pstate)
static void expressionStatement(CParseState *pstate)
{
int savedPushed = pstate->compiler->pushedValues;
@ -1760,6 +1760,16 @@ static void statement(CParseState *pstate)
alignStack(pstate, savedPushed);
}
static void statement(CParseState *pstate)
{
expressionStatement(pstate);
}
static void declaration(CParseState *pstate)
{
statement(pstate);
}
static CObjFunction *endCompiler(CParseState *pstate)
{
popLocals(pstate, pstate->compiler->scopeDepth + 1); // remove the locals from other scopes
@ -1784,7 +1794,7 @@ CObjFunction *cosmoP_compileString(CState *state, const char *source, const char
advance(&parser);
while (!match(&parser, TOKEN_EOF)) {
statement(&parser);
declaration(&parser);
}
consume(&parser, TOKEN_EOF, "End of file expected!");

View File

@ -203,7 +203,7 @@ void cosmoV_printStack(CState *state)
printf("==== [[ stack dump ]] ====\n");
for (CValue *top = state->top - 1; top >= state->stack; top--) {
printf("%d: ", (int)(top - state->stack));
cosmoV_printValue(*top);
printValue(*top);
printf("\n");
}
}

View File

@ -69,8 +69,6 @@ static uint32_t getObjectHash(CObj *obj)
switch (obj->type) {
case COBJ_STRING:
return ((CObjString *)obj)->hash;
case COBJ_CFUNCTION:
return (uint32_t)((CObjCFunction *)obj)->cfunc;
default:
return (uint32_t)obj; // just "hash" the pointer
}
@ -288,9 +286,9 @@ void cosmoT_printTable(CTable *tbl, const char *name)
for (int i = 0; i < cap; i++) {
CTableEntry *entry = &tbl->table[i];
if (!(IS_NIL(entry->key))) {
cosmoV_printValue(entry->key);
printValue(entry->key);
printf(" - ");
cosmoV_printValue(entry->val);
printValue(entry->val);
printf("\n");
}
}

View File

@ -101,7 +101,7 @@ const char *cosmoV_typeStr(CValue val)
}
}
void cosmoV_printValue(CValue val)
void printValue(CValue val)
{
switch (GET_TYPE(val)) {
case COSMO_TNUMBER:

View File

@ -119,7 +119,7 @@ void initValArray(CState *state, CValueArray *val, size_t startCapacity);
void cleanValArray(CState *state, CValueArray *array); // cleans array
void appendValArray(CState *state, CValueArray *array, CValue val);
void cosmoV_printValue(CValue val);
void printValue(CValue val);
COSMO_API bool cosmoV_equal(CState *state, CValue valA, CValue valB);
COSMO_API CObjString *cosmoV_toString(CState *state, CValue val);
COSMO_API cosmo_Number cosmoV_toNumber(CState *state, CValue val);

View File

@ -78,7 +78,7 @@ bool cosmoV_compileString(CState *state, const char *src, const char *name)
return false;
}
void cosmoV_printBacktrace(CState *state, CObjError *err)
void cosmoV_printError(CState *state, CObjError *err)
{
// print stack trace
for (int i = 0; i < err->frameCount; i++) {
@ -129,7 +129,7 @@ void cosmoV_throw(CState *state)
} else {
cosmoV_pushValue(state, val);
fprintf(stderr, "Unhandled panic! ");
cosmoV_printBacktrace(state, error);
cosmoV_printError(state, error);
exit(1);
}
}
@ -340,7 +340,7 @@ void callCValue(CState *state, CValue func, int args, int nresults, int offset)
#ifdef VM_DEBUG
printf("\n");
printIndent(state->frameCount - 1);
cosmoV_printValue(func);
printValue(func);
printf("(%d args)\n", args);
#endif

View File

@ -33,7 +33,7 @@ COSMO_API CObjObject *cosmoV_makeObject(CState *state, int pairs);
COSMO_API void cosmoV_makeTable(CState *state, int pairs);
COSMO_API void cosmoV_concat(CState *state, int vals);
COSMO_API void cosmoV_pushFString(CState *state, const char *format, ...);
COSMO_API void cosmoV_printBacktrace(CState *state, CObjError *err);
COSMO_API void cosmoV_printError(CState *state, CObjError *err);
COSMO_API void cosmoV_throw(CState *state);
COSMO_API void cosmoV_error(CState *state, const char *format, ...);
COSMO_API void cosmoV_insert(CState *state, int indx, CValue val);