added module names for functions

This commit is contained in:
CPunch 2020-12-09 12:23:16 -06:00
parent 9aa7fa1381
commit 6445dae04c
7 changed files with 20 additions and 12 deletions

View File

@ -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;

View File

@ -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;

View File

@ -55,6 +55,7 @@ typedef struct CObjFunction {
int args;
int upvals;
CObjString *name;
CObjString *module; // name of the "module"
} CObjFunction;
typedef struct CObjCFunction {

View File

@ -42,6 +42,7 @@ typedef struct CCompilerState {
typedef struct {
CLexState *lex;
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);

View File

@ -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

View File

@ -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 {

View File

@ -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);