: type@ ( objvar -- type ) 1+ @ ;
: value! ( newval objvar -- ) ! ;
: type! ( newtype objvar -- ) 1+ ! ;
-: setobj ( newobj objvar -- ) dup rot swap 1+ ! ! ;
-: fetchobj ( objvar -- obj ) dup @ swap 1+ @ ;
+: obj! ( newobj objvar -- ) dup rot swap 1+ ! ! ;
+: obj@ ( objvar -- obj ) dup @ swap 1+ @ ;
: objeq? ( obj obj -- bool )
rot = -rot = and ;
: charlist>symbol ( charlist -- symbol-obj )
- symbol-table fetchobj
+ symbol-table obj@
begin
nil? false =
2drop
drop symbol-type 2dup
- symbol-table fetchobj cons
- symbol-table setobj
+ symbol-table obj@ cons
+ symbol-table obj!
;
2dup
- symbol-table fetchobj
+ symbol-table obj@
cons
- symbol-table setobj
+ symbol-table obj!
create swap , ,
does> dup @ swap 1+ @
create-symbol ok ok-symbol
create-symbol if if-symbol
create-symbol lambda lambda-symbol
+create-symbol λ λ-symbol
\ }}}
objvar vals
: get-vars-vals-frame ( var frame -- bool )
- 2dup frame-vars vars setobj
- frame-vals vals setobj
+ 2dup frame-vars vars obj!
+ frame-vals vals obj!
begin
- vars fetchobj nil objeq? false =
+ vars obj@ nil objeq? false =
while
- 2dup vars fetchobj car objeq? if
+ 2dup vars obj@ car objeq? if
2drop true
exit
then
- vars fetchobj cdr vars setobj
- vals fetchobj cdr vals setobj
+ vars obj@ cdr vars obj!
+ vals obj@ cdr vals obj!
repeat
2drop false
2over 2over first-frame
get-vars-vals-frame if
2drop 2drop
- vars fetchobj vals fetchobj true
+ vars obj@ vals obj@ true
exit
then
objvar env
: define-var ( var val env -- )
- env setobj
+ env obj!
- 2over env fetchobj ( var val var env )
+ 2over env obj@ ( var val var env )
get-vars-vals if
2swap 2drop ( var val vals )
set-car!
2drop
else
- env fetchobj
+ env obj@
first-frame ( var val frame )
add-binding
then
objvar global-env
nil nil nil extend-env
-global-env setobj
+global-env obj!
\ }}}
2dup
- symbol-table fetchobj
+ symbol-table obj@
cons
- symbol-table setobj
+ symbol-table obj!
rot primitive-proc-type ( var prim )
- global-env fetchobj define-var
+ global-env obj@ define-var
;
: arg-count-error
\ Anything else is parsed as a symbol
readsymbol charlist>symbol
+ \ Replace λ with lambda
+ 2dup λ-symbol objeq? if
+ 2drop lambda-symbol
+ then
+
+
; is read
\ }}}
: true? ( boolobj -- bool )
false? invert ;
+: lambda? ( obj -- obj bool )
+ lambda-symbol tagged-list? ;
+
+: lambda-parameters ( obj -- params )
+ cdr car ;
+
+: lambda-body ( obj -- body )
+ cdr cdr ;
+
+: make-procedure ( params body env -- proc )
+ nil
+ cons cons cons
+ drop compound-proc-type
+;
+
: application? ( obj -- obj bool)
pair-type istype? ;
2swap ['] eval goto
then
+ lambda? if
+ 2dup lambda-body
+ 2swap lambda-parameters
+ 2rot make-procedure
+ exit
+ then
+
application? if
2over 2over
operator 2swap eval
: printprim ( primobj -- )
2drop ." <primitive procedure>" ;
+: printcomp ( primobj -- )
+ 2drop ." <compound procedure>" ;
+
:noname ( obj -- )
fixnum-type istype? if printnum exit then
boolean-type istype? if printbool exit then
nil-type istype? if printnil exit then
pair-type istype? if ." (" printpair ." )" exit then
primitive-proc-type istype? if printprim exit then
+ compound-proc-type istype? if printcomp exit then
bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
abort
begin
cr bold fg green ." > " reset-term
read
- global-env fetchobj eval
+ global-env obj@ eval
fg cyan ." ; " print reset-term
again
;