: istype? ( obj type -- obj bool )
over = ;
-\ ------ Cons cell memory ------
+\ ------ Cons cell memory ------ {{{
1000 constant N
create car-cells N allot
: objeq? ( obj obj -- bool )
rot = -rot = and ;
-\ ---- Pre-defined symbols ----
+\ }}}
+
+\ ---- Pre-defined symbols ---- {{{
objvar symbol-table
create-symbol define define-symbol
create-symbol set! set!-symbol
-\ ---- Environments ----
+\ }}}
+
+\ ---- Environments ---- {{{
objvar global-env
objvar vars
objvar vals
-: lookup-var-frame ( var frame -- val? bool )
+: get-vars-vals-frame ( var frame -- bool )
2dup frame-vars vars setobj
frame-vals vals setobj
vars fetchobj nil objeq? false =
while
2dup vars fetchobj car objeq? if
- 2drop
- vals fetchobj car true
+ 2drop true
exit
then
2drop false
;
-: lookup-var ( var env -- val )
+: get-vars-vals ( var env -- vars? vals? bool )
+
begin
2dup nil objeq? false =
while
2over 2over first-frame
lookup-var-frame if
- -rot 2drop -rot 2drop
+ 2drop 2drop
+ vars fetchobj vals fetchobj true
exit
then
enclosing-env
repeat
- bold fg red ." Unbound variable. Aborting." reset-term cr
- abort
+ 2drop 2drop
+ false
;
-objvar val
-
-: set-var-frame ( var frame -- )
- 2dup frame-vars vars setobj
- frame-vals vals setobj
-
- begin
- vars fetchobj nil objeq? false =
- while
- 2dup vars fetchobj car objeq? if
- 2drop
- \ *** TODO ***
- then
+hide vars
+hide vals
- vars fetchobj cdr vars setobj
- vals fetchobj cdr vals setobj
- repeat
+: lookup-var ( var env -- val )
+ get-vars-vals if
+ 2swap 2drop car
+ else
+ bold fg red ." Tried to read unbound variable." reset-term abort
+ then
;
-
: set-var ( var val env -- )
+ >R >R 2swap R> R> ( val var env )
+ get-vars-vals if
+ 2swap 2drop ( val vals )
+ set-car!
+ else
+ bold fg red ." Tried to set unbound variable." reset-term abort
+ then
+;
- 2swap val setobj
-
- begin
- 2dup nil objeq? false =
- while
- 2over 2over first-frame
- set-var-frame if
- exit
- then
+objvar env
- enclosing-env
- repeat
+: define-var ( var val env -- )
+ env objset
- bold fg red ." Unbound variable. Aborting." reset-term cr
- abort
+ 2over env objfetch ( var val var env )
+ get-vars-vals if
+ 2swap 2drop ( var val vals )
+ set-car!
+ 2drop
+ else
+ env objfetch
+ first-frame ( var val frame )
+ add-binding
+ then
;
-hide vars
-hide vals
-hide val
+hide env
+
+\ }}}
-\ ---- Read ----
+\ ---- Read ---- {{{
variable parse-idx
variable stored-parse-idx
string? if
inc-parse-idx
+
readstring
drop string-type
then
inc-parse-idx
+
exit
then
; is read
-\ ---- Eval ----
+\ }}}
+
+\ ---- Eval ---- {{{
: self-evaluating? ( obj -- obj bool )
boolean-type istype? if true exit then
: quote-body ( quote-obj -- quote-body-obj )
cadr ;
+
+: variable? ( obj -- obj bool )
+ symbol-type istype? ;
+
+: definition? ( obj -- obj bool )
+ define-symbol tagged-list? ;
+
+: assignment? ( obj -- obj bool )
+ set-symbol tagged-list? ;
: eval ( obj env -- result )
2swap
exit
then
+ variable? if
+ 2swap lookup-var
+ exit
+ then
+
bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
abort
;
-\ ---- Print ----
+\ }}}
+
+\ ---- Print ---- {{{
: printnum ( numobj -- ) drop 0 .R ;
abort
; is print
+\ }}}
+
\ ---- REPL ----
: repl