mirror of
https://github.com/CPunch/Cosmo.git
synced 2024-11-05 08:10:05 +00:00
added module names for functions
This commit is contained in:
parent
9aa7fa1381
commit
6445dae04c
@ -104,6 +104,7 @@ void blackenObject(CState *state, CObj *obj) {
|
|||||||
case COBJ_FUNCTION: {
|
case COBJ_FUNCTION: {
|
||||||
CObjFunction *func = (CObjFunction*)obj;
|
CObjFunction *func = (CObjFunction*)obj;
|
||||||
markObject(state, (CObj*)func->name);
|
markObject(state, (CObj*)func->name);
|
||||||
|
markObject(state, (CObj*)func->module);
|
||||||
markArray(state, &func->chunk.constants);
|
markArray(state, &func->chunk.constants);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -112,6 +112,7 @@ CObjFunction *cosmoO_newFunction(CState *state) {
|
|||||||
func->args = 0;
|
func->args = 0;
|
||||||
func->upvals = 0;
|
func->upvals = 0;
|
||||||
func->name = NULL;
|
func->name = NULL;
|
||||||
|
func->module = NULL;
|
||||||
|
|
||||||
initChunk(state, &func->chunk, ARRAY_START);
|
initChunk(state, &func->chunk, ARRAY_START);
|
||||||
return func;
|
return func;
|
||||||
|
@ -55,6 +55,7 @@ typedef struct CObjFunction {
|
|||||||
int args;
|
int args;
|
||||||
int upvals;
|
int upvals;
|
||||||
CObjString *name;
|
CObjString *name;
|
||||||
|
CObjString *module; // name of the "module"
|
||||||
} CObjFunction;
|
} CObjFunction;
|
||||||
|
|
||||||
typedef struct CObjCFunction {
|
typedef struct CObjCFunction {
|
||||||
|
13
src/cparse.c
13
src/cparse.c
@ -41,7 +41,8 @@ typedef struct CCompilerState {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
CLexState *lex;
|
CLexState *lex;
|
||||||
CCompilerState* compiler;
|
CCompilerState *compiler;
|
||||||
|
CObjString *module; // name of the module (can be NULL)
|
||||||
CState *state;
|
CState *state;
|
||||||
CToken current;
|
CToken current;
|
||||||
CToken previous; // token right after the current token
|
CToken previous; // token right after the current token
|
||||||
@ -95,9 +96,12 @@ static void initCompilerState(CParseState* pstate, CCompilerState *ccstate, Func
|
|||||||
ccstate->savedPushed = 0;
|
ccstate->savedPushed = 0;
|
||||||
ccstate->type = type;
|
ccstate->type = type;
|
||||||
ccstate->function = cosmoO_newFunction(pstate->state);
|
ccstate->function = cosmoO_newFunction(pstate->state);
|
||||||
|
ccstate->function->module = pstate->module;
|
||||||
|
|
||||||
if (type != FTYPE_SCRIPT)
|
if (type != FTYPE_SCRIPT)
|
||||||
ccstate->function->name = cosmoO_copyString(pstate->state, pstate->previous.start, pstate->previous.length);
|
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)
|
// 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++];
|
Local *local = &ccstate->locals[ccstate->localCount++];
|
||||||
@ -107,13 +111,14 @@ static void initCompilerState(CParseState* pstate, CCompilerState *ccstate, Func
|
|||||||
local->name.length = 0;
|
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->lex = cosmoL_newLexState(s, source);
|
||||||
|
|
||||||
pstate->state = s;
|
pstate->state = s;
|
||||||
pstate->hadError = false;
|
pstate->hadError = false;
|
||||||
pstate->panic = false;
|
pstate->panic = false;
|
||||||
pstate->compiler = ccstate;
|
pstate->compiler = ccstate;
|
||||||
|
pstate->module = cosmoO_copyString(s, module, strlen(module));
|
||||||
|
|
||||||
initCompilerState(pstate, ccstate, FTYPE_SCRIPT, NULL); // enclosing starts as NULL
|
initCompilerState(pstate, ccstate, FTYPE_SCRIPT, NULL); // enclosing starts as NULL
|
||||||
}
|
}
|
||||||
@ -1194,11 +1199,11 @@ static CObjFunction *endCompiler(CParseState *pstate) {
|
|||||||
|
|
||||||
// ================================================================ [API] ================================================================
|
// ================================================================ [API] ================================================================
|
||||||
|
|
||||||
CObjFunction* cosmoP_compileString(CState *state, const char *source) {
|
CObjFunction* cosmoP_compileString(CState *state, const char *source, const char *module) {
|
||||||
CParseState parser;
|
CParseState parser;
|
||||||
CCompilerState compiler;
|
CCompilerState compiler;
|
||||||
cosmoM_freezeGC(state); // ignore all GC events while compiling
|
cosmoM_freezeGC(state); // ignore all GC events while compiling
|
||||||
initParseState(&parser, &compiler, state, source);
|
initParseState(&parser, &compiler, state, source, module);
|
||||||
|
|
||||||
advance(&parser);
|
advance(&parser);
|
||||||
|
|
||||||
|
@ -5,6 +5,6 @@
|
|||||||
#include "clex.h"
|
#include "clex.h"
|
||||||
|
|
||||||
// compiles source into CChunk, if NULL is returned, a syntaxical error has occured and pushed onto the stack
|
// 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
|
#endif
|
@ -19,7 +19,7 @@ void cosmoV_error(CState *state, const char *format, ...) {
|
|||||||
int line = chunk->lineInfo[frame->pc - chunk->buf - 1];
|
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
|
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
|
if (function->name == NULL) { // unnamed chunk
|
||||||
fprintf(stderr, "%s\n\t", UNNAMEDCHUNK);
|
fprintf(stderr, "%s\n\t", UNNAMEDCHUNK);
|
||||||
} else {
|
} else {
|
||||||
|
12
src/main.c
12
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
|
return cosmoV_newObj(cosmoO_copyString(state, line, strlen(line)-1)); // -1 for the \n
|
||||||
}
|
}
|
||||||
|
|
||||||
static void interpret(CState *state, const char* script) {
|
static void interpret(CState *state, const char *script, const char *mod) {
|
||||||
// cosmoP_compileString pushes the result onto the stack (NIL or COBJ_FUNCTION)
|
// cosmoP_compileString pushes the result onto the stack (NIL or COBJ_CLOSURE)
|
||||||
CObjFunction* func = cosmoP_compileString(state, script);
|
CObjFunction* func = cosmoP_compileString(state, script, mod);
|
||||||
if (func != NULL) {
|
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
|
COSMOVMRESULT res = cosmoV_call(state, 0); // 0 args being passed
|
||||||
|
|
||||||
@ -62,7 +62,7 @@ static void repl() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
interpret(state, line);
|
interpret(state, line, "REPL");
|
||||||
}
|
}
|
||||||
|
|
||||||
cosmoV_freeState(state);
|
cosmoV_freeState(state);
|
||||||
@ -107,7 +107,7 @@ static void runFile(const char* fileName) {
|
|||||||
|
|
||||||
cosmoV_register(state, "input", cosmoV_newObj(cosmoO_newCFunction(state, cosmoB_input)));
|
cosmoV_register(state, "input", cosmoV_newObj(cosmoO_newCFunction(state, cosmoB_input)));
|
||||||
|
|
||||||
interpret(state, script);
|
interpret(state, script, fileName);
|
||||||
|
|
||||||
cosmoV_freeState(state);
|
cosmoV_freeState(state);
|
||||||
free(script);
|
free(script);
|
||||||
|
Loading…
Reference in New Issue
Block a user