Skip to content

Commit

Permalink
use special value of -1 for global value if symbol is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
devyn committed Aug 17, 2023
1 parent b69e337 commit 24fcdc4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion stage1/symbol.s
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ symbol_intern:
mv a2, s4
call mem_copy
# create a new object for the symbol
mv a3, zero # LISP_SYMBOL_GLOBAL_VALUE
li a3, -1 # LISP_SYMBOL_GLOBAL_VALUE = undefined
mv a2, s2 # LISP_SYMBOL_LEN
mv a1, s4 # LISP_SYMBOL_BUF
li a0, LISP_OBJECT_TYPE_SYMBOL
Expand Down
17 changes: 14 additions & 3 deletions stage1/words.s
Original file line number Diff line number Diff line change
Expand Up @@ -415,23 +415,31 @@ lookup_var:
mv a0, a1
mv a1, s1
call lookup
bnez a0, .Llookup_var_ret
bnez a0, .Llookup_var_end
# if not found, return global value of the symbol
ld a0, LISP_SYMBOL_GLOBAL_VALUE(s1)
li t0, -1
beq a0, t0, .Llookup_var_undef # undefined
call acquire_object
mv a1, a0
.Llookup_var_ret:
.Llookup_var_end:
# clean up the extra symbol ref
mv t0, a1
mv a0, s1 # symbol
mv s1, t0 # s1 now = found return a1
call release_object
li a0, 1
mv a1, s1
.Llookup_var_ret:
ld ra, 0x00(sp)
ld s1, 0x08(sp)
addi sp, sp, 0x10
ret
.Llookup_var_undef:
mv a0, s1
call release_object
li a0, 0
j .Llookup_var_ret

# Define a new word
#
Expand All @@ -455,9 +463,12 @@ define:
# swap the current global value of the symbol
ld t0, LISP_SYMBOL_GLOBAL_VALUE(a0)
sd a1, LISP_SYMBOL_GLOBAL_VALUE(a0)
# release the old value
# release the old value unless it was undefined
li t1, -1
beq t0, t1, 1f
mv a0, t0
call release_object
1:
# release the symbol
ld a0, 0x08(sp)
call release_object
Expand Down
10 changes: 6 additions & 4 deletions stage2/22-words-util.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
(symbol-addr (ref symbol))
(global-value-addr (peek.d (+ symbol-addr 24)))
)
(seq
(call-native acquire-object$ 0 global-value-addr)
(call-native release-object$ 0 symbol-addr)
(deref global-value-addr)))
(if (number-eq? global-value-addr -1)
(error (quote undefined:) symbol)
(seq
(call-native acquire-object$ 0 global-value-addr)
(call-native release-object$ 0 symbol-addr)
(deref global-value-addr))))
(error (quote not-a-symbol:) symbol))))

0 comments on commit 24fcdc4

Please sign in to comment.