set! on an undefined variable now triggers an error

This commit is contained in:
Alex Shinn 2012-02-29 21:33:30 +09:00
parent 51df221034
commit d6aaaa99d1
2 changed files with 10 additions and 2 deletions

5
eval.c
View file

@ -103,8 +103,11 @@ static sexp sexp_env_cell_define (sexp ctx, sexp env, sexp key,
}
#endif
for (ls=sexp_env_bindings(env); sexp_pairp(ls); ls=sexp_env_next_cell(ls))
if (sexp_car(ls) == key)
if (sexp_car(ls) == key) {
if (sexp_cdr(ls) == SEXP_UNDEF)
sexp_cdr(ls) = value;
return ls;
}
sexp_gc_preserve2(ctx, cell, ls);
sexp_env_push(ctx, env, cell, key, value);
sexp_gc_release2(ctx);

7
vm.c
View file

@ -50,7 +50,7 @@ void sexp_stack_trace (sexp ctx, sexp out) {
sexp sexp_stack_trace_op (sexp ctx, sexp self, sexp_sint_t n, sexp out) {
sexp_stack_trace(ctx, out);
return SEXP_UNDEF;
return SEXP_VOID;
}
/************************* code generation ****************************/
@ -197,6 +197,11 @@ static void generate_set (sexp ctx, sexp set) {
}
if (! sexp_lambdap(sexp_ref_loc(ref))) {
/* global vars are set directly */
if (sexp_cdr(sexp_ref_cell(ref)) == SEXP_UNDEF) {
/* force an undefined variable error if still undef at runtime */
generate_ref(ctx, ref, 1);
emit(ctx, SEXP_OP_DROP);
}
emit_push(ctx, sexp_ref_cell(ref));
emit(ctx, SEXP_OP_SET_CDR);
} else {