Skip to content

Commit

Permalink
Merge pull request #267 from Morpho-lang/moduleinfo
Browse files Browse the repository at this point in the history
Module info
  • Loading branch information
softmattertheory authored Jun 1, 2024
2 parents cde7a16 + 0179673 commit 6434493
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/classes/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ bool json_lexstring(lexer *l, token *tok, error *err) {
}

if (lex_isatend(l)) {
morpho_writeerrorwithid(err, LEXER_UNTERMINATEDSTRING, tok->line, tok->posn);
morpho_writeerrorwithid(err, LEXER_UNTERMINATEDSTRING, NULL, tok->line, tok->posn);
return false;
}

Expand Down Expand Up @@ -144,7 +144,7 @@ bool json_lexnumber(lexer *l, token *tok, error *err) {
return true;

json_lexnumberinvld:
morpho_writeerrorwithid(err, JSON_NMBRFRMT, tok->line, tok->posn);
morpho_writeerrorwithid(err, JSON_NMBRFRMT, NULL, tok->line, tok->posn);
return false;
}

Expand Down
7 changes: 4 additions & 3 deletions src/core/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ static void compiler_error(compiler *c, syntaxtreenode *node, errorid id, ... )
int line = (node ? node->line : ERROR_POSNUNIDENTIFIABLE);
int posn = (node ? node->posn : ERROR_POSNUNIDENTIFIABLE);

char *file = (MORPHO_ISSTRING(c->currentmodule) ? MORPHO_GETCSTRING(c->currentmodule) : NULL);

va_start(args, id);
morpho_writeerrorwithidvalist(&c->err, id, line, posn, args);

morpho_writeerrorwithidvalist(&c->err, id, NULL, line, posn, args);
va_end(args);
}

Expand Down Expand Up @@ -3527,7 +3528,7 @@ static codeinfo compiler_import(compiler *c, syntaxtreenode *node, registerindx
}

} else {
c->err.module = cc.err.module;
c->err.file = (cc.err.file ? cc.err.file : MORPHO_GETCSTRING(modname));
}

debugannotation_setmodule(&c->out->annotations, compiler_getmodule(c));
Expand Down
11 changes: 6 additions & 5 deletions src/core/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,11 @@ static void vm_bindobjectwithoutcollect(vm *v, value obj) {
void vm_runtimeerror(vm *v, ptrdiff_t iindx, errorid id, ...) {
va_list args;
int line=ERROR_POSNUNIDENTIFIABLE, posn=ERROR_POSNUNIDENTIFIABLE;
debug_infofromindx(v->current, iindx, NULL, &line, &posn, NULL, NULL);

value module=MORPHO_NIL;
debug_infofromindx(v->current, iindx, &module, &line, &posn, NULL, NULL);

va_start(args, id);
morpho_writeerrorwithidvalist(&v->err, id, line, posn, args);
morpho_writeerrorwithidvalist(&v->err, id, NULL, line, posn, args);
va_end(args);
}

Expand Down Expand Up @@ -1548,7 +1549,7 @@ void morpho_runtimeerror(vm *v, errorid id, ...) {
error_init(&err);

va_start(args, id);
morpho_writeerrorwithidvalist(&err, id, ERROR_POSNUNIDENTIFIABLE, ERROR_POSNUNIDENTIFIABLE, args);
morpho_writeerrorwithidvalist(&err, id, NULL, ERROR_POSNUNIDENTIFIABLE, ERROR_POSNUNIDENTIFIABLE, args);
va_end(args);

morpho_error(v, &err);
Expand All @@ -1562,7 +1563,7 @@ void morpho_runtimewarning(vm *v, errorid id, ...) {
error_init(&err);

va_start(args, id);
morpho_writeerrorwithidvalist(&err, id, ERROR_POSNUNIDENTIFIABLE, ERROR_POSNUNIDENTIFIABLE, args);
morpho_writeerrorwithidvalist(&err, id, NULL, ERROR_POSNUNIDENTIFIABLE, ERROR_POSNUNIDENTIFIABLE, args);
va_end(args);

morpho_warning(v, &err);
Expand Down
14 changes: 8 additions & 6 deletions src/datastructures/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ DEFINE_VARRAY(errordefinition, errordefinition)
/** Prints to an error block
* @param err Error struct to fill out
* @param cat The category of error */
static void error_printf(error *err, errorcategory cat, int line, int posn, char *message, va_list args) {
static void error_printf(error *err, errorcategory cat, char *file, int line, int posn, char *message, va_list args) {

err->cat=cat;
err->file=file;
err->line=line;
err->posn=posn;

Expand All @@ -47,7 +48,7 @@ static void error_printf(error *err, errorcategory cat, int line, int posn, char
void error_clear(error *err) {
err->cat=ERROR_NONE;
err->id=NULL;
err->module=NULL;
err->file=NULL;
err->line=ERROR_POSNUNIDENTIFIABLE; err->posn=ERROR_POSNUNIDENTIFIABLE;
}

Expand Down Expand Up @@ -84,14 +85,14 @@ bool morpho_getdefinitionfromid(errorid id, errordefinition **def) {
* @param line The line at which the error occurred, if identifiable.
* @param posn The position in the line at which the error occurred, if identifiable.
* @param args Additional parameters (the data for the printf commands in the message) */
void morpho_writeerrorwithidvalist(error *err, errorid id, int line, int posn, va_list args) {
void morpho_writeerrorwithidvalist(error *err, errorid id, char *file, int line, int posn, va_list args) {
error_init(err);
errordefinition *def;

err->id=id;
if (morpho_getdefinitionfromid(id, &def)) {
/* Print the message with requested args */
error_printf(err, def->cat, line, posn, def->msg, args);
error_printf(err, def->cat, file, line, posn, def->msg, args);
} else {
UNREACHABLE("Undefined error generated.");
}
Expand All @@ -100,13 +101,14 @@ void morpho_writeerrorwithidvalist(error *err, errorid id, int line, int posn, v
/** @brief Writes an error message to an error structure
* @param err The error structure
* @param id The error id.
* @param file The file in which the error ocdured, if relevant.
* @param line The line at which the error occurred, if identifiable.
* @param posn The position in the line at which the error occurred, if identifiable.
* @param ... Additional parameters (the data for the printf commands in the message) */
void morpho_writeerrorwithid(error *err, errorid id, int line, int posn, ...) {
void morpho_writeerrorwithid(error *err, errorid id, char *file, int line, int posn, ...) {
va_list args;
va_start(args, posn);
morpho_writeerrorwithidvalist(err, id, line, posn, args);
morpho_writeerrorwithidvalist(err, id, file, line, posn, args);
va_end(args);
}

Expand Down
6 changes: 3 additions & 3 deletions src/datastructures/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ typedef errorcategory morphoerror;
typedef struct {
errorcategory cat;
errorid id;
char *module;
char *file;
int line, posn;
char msg[MORPHO_ERRORSTRINGSIZE];
} error;
Expand Down Expand Up @@ -195,8 +195,8 @@ void morpho_unreachable(const char *explanation);
void error_init(error *err);
void error_clear(error *err);

void morpho_writeerrorwithid(error *err, errorid id, int line, int posn, ...);
void morpho_writeerrorwithidvalist(error *err, errorid id, int line, int posn, va_list args);
void morpho_writeerrorwithid(error *err, errorid id, char *file, int line, int posn, ...);
void morpho_writeerrorwithidvalist(error *err, errorid id, char *file, int line, int posn, va_list args);
void morpho_writeusererror(error *err, errorid id, char *message);
void morpho_defineerror(errorid id, errorcategory cat, char *message);

Expand Down
16 changes: 12 additions & 4 deletions src/debug/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,16 @@ void morpho_stacktrace(vm *v) {
else morpho_printf(v, "global");

int line=0;
if (debug_infofromindx(v->current, indx, NULL, &line, NULL, NULL, NULL)) {

value module = MORPHO_NIL;
if (debug_infofromindx(v->current, indx, &module, &line, NULL, NULL, NULL)) {
morpho_printf(v, " at line %u", line);

if (!MORPHO_ISNIL(module)) {
morpho_printf(v, " in module '");
morpho_printvalue(v, module);
morpho_printf(v, "'");
}
}

morpho_printf(v, "\n");
Expand Down Expand Up @@ -271,7 +279,7 @@ void debugger_error(debugger *debug, errorid id, ... ) {
if (!debug->err || debug->err->id!=ERROR_NONE) return; // Ensure errors are not overwritten.
va_list args;
va_start(args, id);
morpho_writeerrorwithidvalist(debug->err, id, ERROR_POSNUNIDENTIFIABLE, ERROR_POSNUNIDENTIFIABLE, args);
morpho_writeerrorwithidvalist(debug->err, id, NULL, ERROR_POSNUNIDENTIFIABLE, ERROR_POSNUNIDENTIFIABLE, args);
va_end(args);
}

Expand Down Expand Up @@ -618,9 +626,9 @@ void debugger_showlocation(debugger *debug, instructionindx indx) {
else morpho_printf(v, "anonymous fn");

if (!MORPHO_ISNIL(module)) {
morpho_printf(v, " in \"");
morpho_printf(v, " in '");
morpho_printvalue(v, module);
morpho_printf(v, "\"");
morpho_printf(v, "'");
}
morpho_printf(v, " at line %i [instruction %ti]", line, indx);
}
Expand Down
2 changes: 1 addition & 1 deletion src/morpho.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ extern value cloneselector;
void morpho_version(version *v);

/* Error handling */
void morpho_writeerrorwithid(error *err, errorid id, int line, int position, ...);
void morpho_writeerrorwithid(error *err, errorid id, char *file, int line, int posn, ...);
void morpho_defineerror(errorid id, errorcategory cat, char *message);
errorid morpho_geterrorid(error *err);

Expand Down
6 changes: 3 additions & 3 deletions src/support/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ bool lex_skipmultilinecomment(lexer *l, token *tok, error *err) {
switch (c) {
case '\0':
/* If we come to the end of the file, the token is marked as incomplete. */
morpho_writeerrorwithid(err, LEXER_UNTERMINATEDCOMMENT, startline, startpsn);
morpho_writeerrorwithid(err, LEXER_UNTERMINATEDCOMMENT, NULL, startline, startpsn);
lex_recordtoken(l, TOKEN_INCOMPLETE, tok);
return false;
case '\n':
Expand Down Expand Up @@ -400,7 +400,7 @@ bool lex_string(lexer *l, token *tok, error *err) {

if (lex_isatend(l)) {
/* Unterminated string */
morpho_writeerrorwithid(err, LEXER_UNTERMINATEDSTRING, startline, startpsn);
morpho_writeerrorwithid(err, LEXER_UNTERMINATEDSTRING, NULL, startline, startpsn);
lex_recordtoken(l, TOKEN_INCOMPLETE, tok);
return false;
}
Expand Down Expand Up @@ -672,7 +672,7 @@ bool lex(lexer *l, token *tok, error *err) {
if (lex_identifytoken(l, &defn)) {
lex_recordtoken(l, defn->type, tok);
} else {
morpho_writeerrorwithid(err, LEXER_UNRECOGNIZEDTOKEN, l->line, l->posn);
morpho_writeerrorwithid(err, LEXER_UNRECOGNIZEDTOKEN, NULL, l->line, l->posn);
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/support/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void parse_error(parser *p, bool use_prev, errorid id, ... ) {
if (ERROR_FAILED(*p->err)) return;

va_start(args, id);
morpho_writeerrorwithid(p->err, id, tok->line, tok->posn, args);
morpho_writeerrorwithid(p->err, id, NULL, tok->line, tok->posn, args);
va_end(args);
}

Expand Down Expand Up @@ -1683,7 +1683,7 @@ bool parse_stringtovaluearray(char *string, unsigned int nmax, value *v, unsigne
case TOKEN_EOF:
break;
default:
morpho_writeerrorwithid(err, PARSE_UNRECGNZEDTOK, ERROR_POSNUNIDENTIFIABLE, ERROR_POSNUNIDENTIFIABLE);
morpho_writeerrorwithid(err, PARSE_UNRECGNZEDTOK, NULL, ERROR_POSNUNIDENTIFIABLE, ERROR_POSNUNIDENTIFIABLE);
return false;
break;
}
Expand Down

0 comments on commit 6434493

Please sign in to comment.