From 6445dae04c41b22f80de544e9cf7252cfa403f28 Mon Sep 17 00:00:00 2001 From: CPunch Date: Wed, 9 Dec 2020 12:23:16 -0600 Subject: [PATCH] added module names for functions --- src/cmem.c | 1 + src/cobj.c | 1 + src/cobj.h | 1 + src/cparse.c | 13 +++++++++---- src/cparse.h | 2 +- src/cvm.c | 2 +- src/main.c | 12 ++++++------ 7 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/cmem.c b/src/cmem.c index 3711274..ef9c65a 100644 --- a/src/cmem.c +++ b/src/cmem.c @@ -104,6 +104,7 @@ void blackenObject(CState *state, CObj *obj) { case COBJ_FUNCTION: { CObjFunction *func = (CObjFunction*)obj; markObject(state, (CObj*)func->name); + markObject(state, (CObj*)func->module); markArray(state, &func->chunk.constants); break; diff --git a/src/cobj.c b/src/cobj.c index c0650e6..7419b30 100644 --- a/src/cobj.c +++ b/src/cobj.c @@ -112,6 +112,7 @@ CObjFunction *cosmoO_newFunction(CState *state) { func->args = 0; func->upvals = 0; func->name = NULL; + func->module = NULL; initChunk(state, &func->chunk, ARRAY_START); return func; diff --git a/src/cobj.h b/src/cobj.h index 1550526..e2911df 100644 --- a/src/cobj.h +++ b/src/cobj.h @@ -55,6 +55,7 @@ typedef struct CObjFunction { int args; int upvals; CObjString *name; + CObjString *module; // name of the "module" } CObjFunction; typedef struct CObjCFunction { diff --git a/src/cparse.c b/src/cparse.c index fea7105..15a0370 100644 --- a/src/cparse.c +++ b/src/cparse.c @@ -41,7 +41,8 @@ typedef struct CCompilerState { typedef struct { CLexState *lex; - CCompilerState* compiler; + CCompilerState *compiler; + CObjString *module; // name of the module (can be NULL) CState *state; CToken current; CToken previous; // token right after the current token @@ -95,9 +96,12 @@ static void initCompilerState(CParseState* pstate, CCompilerState *ccstate, Func ccstate->savedPushed = 0; ccstate->type = type; ccstate->function = cosmoO_newFunction(pstate->state); + ccstate->function->module = pstate->module; if (type != FTYPE_SCRIPT) ccstate->function->name = cosmoO_copyString(pstate->state, pstate->previous.start, pstate->previous.length); + else + ccstate->function->name = cosmoO_copyString(pstate->state, UNNAMEDCHUNK, strlen(UNNAMEDCHUNK)); // mark first local slot as used (this'll hold the CObjFunction of the current function, or if it's a method it'll hold the currently bounded object) Local *local = &ccstate->locals[ccstate->localCount++]; @@ -107,13 +111,14 @@ static void initCompilerState(CParseState* pstate, CCompilerState *ccstate, Func local->name.length = 0; } -static void initParseState(CParseState *pstate, CCompilerState *ccstate, CState *s, const char *source) { +static void initParseState(CParseState *pstate, CCompilerState *ccstate, CState *s, const char *source, const char *module) { pstate->lex = cosmoL_newLexState(s, source); pstate->state = s; pstate->hadError = false; pstate->panic = false; pstate->compiler = ccstate; + pstate->module = cosmoO_copyString(s, module, strlen(module)); initCompilerState(pstate, ccstate, FTYPE_SCRIPT, NULL); // enclosing starts as NULL } @@ -1194,11 +1199,11 @@ static CObjFunction *endCompiler(CParseState *pstate) { // ================================================================ [API] ================================================================ -CObjFunction* cosmoP_compileString(CState *state, const char *source) { +CObjFunction* cosmoP_compileString(CState *state, const char *source, const char *module) { CParseState parser; CCompilerState compiler; cosmoM_freezeGC(state); // ignore all GC events while compiling - initParseState(&parser, &compiler, state, source); + initParseState(&parser, &compiler, state, source, module); advance(&parser); diff --git a/src/cparse.h b/src/cparse.h index 0dd6b2e..1765cde 100644 --- a/src/cparse.h +++ b/src/cparse.h @@ -5,6 +5,6 @@ #include "clex.h" // compiles source into CChunk, if NULL is returned, a syntaxical error has occured and pushed onto the stack -CObjFunction* cosmoP_compileString(CState *state, const char *source); +CObjFunction* cosmoP_compileString(CState *state, const char *source, const char *module); #endif \ No newline at end of file diff --git a/src/cvm.c b/src/cvm.c index 633384d..a5b4aba 100644 --- a/src/cvm.c +++ b/src/cvm.c @@ -19,7 +19,7 @@ void cosmoV_error(CState *state, const char *format, ...) { int line = chunk->lineInfo[frame->pc - chunk->buf - 1]; if (i == state->frameCount - 1) { // it's the last call frame, prepare for the objection to be printed - fprintf(stderr, "Objection on [line %d] in ", line); + fprintf(stderr, "Objection in %.*s on [line %d] in ", function->module->length, function->module->str, line); if (function->name == NULL) { // unnamed chunk fprintf(stderr, "%s\n\t", UNNAMEDCHUNK); } else { diff --git a/src/main.c b/src/main.c index 0eb160e..aed1198 100644 --- a/src/main.c +++ b/src/main.c @@ -29,11 +29,11 @@ CValue cosmoB_input(CState *state, int nargs, CValue *args) { return cosmoV_newObj(cosmoO_copyString(state, line, strlen(line)-1)); // -1 for the \n } -static void interpret(CState *state, const char* script) { - // cosmoP_compileString pushes the result onto the stack (NIL or COBJ_FUNCTION) - CObjFunction* func = cosmoP_compileString(state, script); +static void interpret(CState *state, const char *script, const char *mod) { + // cosmoP_compileString pushes the result onto the stack (NIL or COBJ_CLOSURE) + CObjFunction* func = cosmoP_compileString(state, script, mod); if (func != NULL) { - disasmChunk(&func->chunk, "_main", 0); + disasmChunk(&func->chunk, func->name != NULL ? func->name->str : "_main", 0); COSMOVMRESULT res = cosmoV_call(state, 0); // 0 args being passed @@ -62,7 +62,7 @@ static void repl() { break; } - interpret(state, line); + interpret(state, line, "REPL"); } cosmoV_freeState(state); @@ -107,7 +107,7 @@ static void runFile(const char* fileName) { cosmoV_register(state, "input", cosmoV_newObj(cosmoO_newCFunction(state, cosmoB_input))); - interpret(state, script); + interpret(state, script, fileName); cosmoV_freeState(state); free(script);