Skip to content

Commit 1402795

Browse files
committed
improve syntax error line numbers. closes #6179
1 parent f5b5b63 commit 1402795

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/ast.c

+14-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ static uint8_t flisp_system_image[] = {
3030
extern fltype_t *iostreamtype;
3131
static fltype_t *jvtype=NULL;
3232

33+
static value_t true_sym;
34+
static value_t false_sym;
35+
static value_t fl_error_sym;
36+
static value_t fl_null_sym;
37+
3338
static jl_value_t *scm_to_julia(value_t e, int expronly);
3439
static value_t julia_to_scm(jl_value_t *v);
3540

@@ -75,7 +80,7 @@ value_t fl_invoke_julia_macro(value_t *args, uint32_t nargs)
7580
JL_GC_POP();
7681
value_t opaque = cvalue(jvtype, sizeof(void*));
7782
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = jl_exception_in_transit;
78-
return fl_list2(symbol("error"), opaque);
83+
return fl_list2(fl_error_sym, opaque);
7984
}
8085
// protect result from GC, otherwise it could be freed during future
8186
// macro expansions, since it will be referenced only from scheme and
@@ -109,9 +114,6 @@ static builtinspec_t julia_flisp_ast_ext[] = {
109114
{ NULL, NULL }
110115
};
111116

112-
static value_t true_sym;
113-
static value_t false_sym;
114-
115117
DLLEXPORT void jl_init_frontend(void)
116118
{
117119
fl_init(2*512*1024);
@@ -132,6 +134,8 @@ DLLEXPORT void jl_init_frontend(void)
132134
assign_global_builtins(julia_flisp_ast_ext);
133135
true_sym = symbol("true");
134136
false_sym = symbol("false");
137+
fl_error_sym = symbol("error");
138+
fl_null_sym = symbol("null");
135139
}
136140

137141
DLLEXPORT void jl_lisp_prompt(void)
@@ -374,7 +378,7 @@ static value_t julia_to_scm(jl_value_t *v)
374378
temp = julia_to_scm_(v);
375379
}
376380
FL_CATCH_EXTERN {
377-
temp = fl_list2(symbol("error"), cvalue_static_cstring("expression too large"));
381+
temp = fl_list2(fl_error_sym, cvalue_static_cstring("expression too large"));
378382
}
379383
return temp;
380384
}
@@ -414,7 +418,7 @@ static value_t julia_to_scm_(jl_value_t *v)
414418
return FL_F;
415419
}
416420
if (v == jl_nothing) {
417-
return fl_cons(symbol("null"), FL_NIL);
421+
return fl_cons(fl_null_sym, FL_NIL);
418422
}
419423
if (jl_is_expr(v)) {
420424
jl_expr_t *ex = (jl_expr_t*)v;
@@ -514,9 +518,12 @@ jl_value_t *jl_parse_next(void)
514518
if (isfixnum(a)) {
515519
jl_lineno = numval(a);
516520
//jl_printf(JL_STDERR, " on line %d\n", jl_lineno);
517-
return scm_to_julia(cdr_(c),0);
521+
c = cdr_(c);
518522
}
519523
}
524+
// for error, get most recent line number
525+
if (iscons(c) && car_(c) == fl_error_sym)
526+
jl_lineno = numval(fl_applyn(0, symbol_value(symbol("jl-parser-current-lineno"))));
520527
return scm_to_julia(c,0);
521528
}
522529

src/jlfrontend.scm

+3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@
198198
e
199199
(expand-toplevel-expr e))))))))))
200200

201+
(define (jl-parser-current-lineno)
202+
(input-port-line (ts:port current-token-stream)))
203+
201204
; expand a piece of raw surface syntax to an executable thunk
202205
(define (jl-expand-to-thunk expr)
203206
(parser-wrap (lambda ()

0 commit comments

Comments
 (0)