Skip to content

Commit 0912f10

Browse files
committed
Fix line number information in error messages
1 parent 096bf57 commit 0912f10

File tree

6 files changed

+38
-21
lines changed

6 files changed

+38
-21
lines changed

Diff for: run.c

+23-12
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,29 @@ void free_var(Variant var)
3131
}
3232
}
3333

34+
static lineno_t get_current_line(Runtime* R)
35+
{
36+
if (R->function == NULL) {
37+
return 0;
38+
}
3439

35-
_Noreturn void runtimeerror(char* fmt)
40+
return R->function->lineinfo[R->ip - R->function->code];
41+
}
42+
43+
void runtimeerror(Runtime* R, char* fmt)
3644
{
37-
printf("Runtime Error: ");
38-
puts(fmt);
39-
abort();
45+
printf("Runtime Error: %s:%d\n", fmt, get_current_line(R));
4046
}
4147

42-
_Noreturn void raise_fatal(Runtime* R, char* fmt, ...)
48+
void raise_fatal(Runtime* R, char* fmt, ...)
4349
{
4450
printf("Fatal Error: ");
4551
va_list ap;
4652
va_start(ap, fmt);
4753
vprintf(fmt, ap);
4854
va_end(ap);
49-
printf(" in %s:%zu\n", R->file, 0L);
50-
abort();
55+
printf(" in %s:%u\n", R->file, get_current_line(R));
56+
R->hasError = true;
5157
}
5258

5359

@@ -59,8 +65,10 @@ static Runtime* create_runtime(State* S)
5965
ret->stacksize = 0;
6066
ret->stack = calloc(ret->stackcapacity, sizeof(*ret->stack));
6167
ret->scope = create_scope();
68+
ret->hasError = false;
6269
ret->state = S;
6370
ret->file = NULL;
71+
ret->function = NULL;
6472

6573
return ret;
6674
}
@@ -297,8 +305,9 @@ static void run_assignmentexpr(Runtime* R, Function* fn)
297305

298306
void run_function(Runtime* R, Function* fn)
299307
{
308+
R->function = fn;
300309
R->ip = fn->code;
301-
while ((size_t)(R->ip - fn->code) < fn->codesize) {
310+
while ((size_t)(R->ip - fn->code) < fn->codesize && !R->hasError) {
302311
Operator op = (Operator) *R->ip++;
303312
int64_t lint;
304313
Variant var;
@@ -413,12 +422,14 @@ void run_function(Runtime* R, Function* fn)
413422
free_var(var);
414423
break;
415424
case OP_INVALID:
416-
runtimeerror("OP_INVALID");
425+
runtimeerror(R, "OP_INVALID");
417426
break;
418427
default:
419-
runtimeerror("Unexpected OP");
428+
runtimeerror(R, "Unexpected OP");
420429
}
421430
}
431+
432+
R->function = NULL;
422433
}
423434

424435
static void init_builtin_functions(State* S)
@@ -432,7 +443,7 @@ static void init_builtin_functions(State* S)
432443
void run_file(const char* filepath) {
433444
FILE* handle = fopen(filepath, "r");
434445
AST* ast = parse(handle);
435-
print_ast(ast,0);
446+
// print_ast(ast,0);
436447

437448
Function* fn = create_function();
438449
State* S = create_state();
@@ -442,7 +453,7 @@ void run_file(const char* filepath) {
442453

443454
Runtime* R = create_runtime(S);
444455
R->file = strdup(filepath);
445-
print_code(fn, "<pseudomain>");
456+
// print_code(fn, "<pseudomain>");
446457
run_function(R, fn);
447458
destroy_state(S);
448459
destroy_runtime(R);

Diff for: run.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include "stack.h"
88

99

10-
_Noreturn void runtimeerror(char* fmt);
11-
_Noreturn void raise_fatal(Runtime* R, char*, ...);
10+
void runtimeerror(Runtime* R, char* fmt);
11+
void raise_fatal(Runtime* R, char*, ...);
1212
void run_file(const char*);
1313
void run_function(Runtime*, Function*);
1414
Variant cpy_var(Variant var);

Diff for: scope.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Variant lookup(Runtime *R, char *str)
4242
static void try_vars_resize(Scope* scope)
4343
{
4444
try_resize(&scope->capacity, scope->size, (void*)&scope->vars,
45-
sizeof(*scope->vars), runtimeerror);
45+
sizeof(*scope->vars), die);
4646
}
4747

4848
Variable* set_var(Runtime* R, char* str, Variant var)

Diff for: stack.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ char* vartostring(Variant var)
109109
break;
110110
}
111111

112-
runtimeerror("Assertion failed: May not reach end of tostring function.");
112+
die("Assertion failed: May not reach end of tostring function.");
113113
return NULL;
114114
}
115115

@@ -142,7 +142,7 @@ int64_t vartolong(Variant var)
142142
break;
143143
}
144144

145-
runtimeerror("tolong for undefined value.");
145+
die("tolong for undefined value.");
146146
return 0;
147147
}
148148

@@ -212,7 +212,7 @@ Variant vartotype(Variant var, VARIANTTYPE type)
212212
break;
213213
case TYPE_FUNCTION:
214214
case TYPE_CFUNCTION:
215-
runtimeerror("Cannot convert function");
215+
die("Cannot convert function");
216216
break;
217217
case TYPE_MAX_VALUE:
218218
assert(false && "Undefined Type given");

Diff for: stack.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <inttypes.h>
55
#include <stddef.h>
6+
#include <stdio.h>
67
#include <assert.h>
78
#include "crossplatform/stdnoreturn.h"
89
#include "enum-util.h"
@@ -48,20 +49,25 @@ typedef struct Variable {
4849
typedef struct Runtime {
4950
size_t stacksize;
5051
size_t stackcapacity;
52+
Function* function;
5153
codepoint_t* ip;
5254
Variant* stack;
5355
Scope* scope;
5456
State* state; // non-owning ptr
57+
bool hasError;
5558

5659
char* file;
5760
} Runtime;
5861

5962

60-
_Noreturn void runtimeerror(char* fmt);
63+
static _Noreturn void die(char* msg) {
64+
puts(msg);
65+
abort();
66+
}
6167
static inline void try_stack_resize(Runtime* R)
6268
{
6369
try_resize(&R->stackcapacity, R->stacksize, (void*)&R->stack,
64-
sizeof(*R->stack), runtimeerror);
70+
sizeof(*R->stack), die);
6571
}
6672

6773
Variant* stackidx(Runtime* R, long idx);

Diff for: tests/funcundefined.expect

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Fatal Error: Call to undefined function this_is_undefined() in ./tests/funcundefined.php:0
1+
Fatal Error: Call to undefined function this_is_undefined() in ./tests/funcundefined.php:4

0 commit comments

Comments
 (0)