Adding environments.
[scheme.forth.jl.git] / scheme.4th
index 4df51d0..825ba58 100644 (file)
@@ -16,7 +16,7 @@ include defer-is.4th
 : istype? ( obj type -- obj bool )
     over = ;
 
-\ ------ Memory ------
+\ ------ Cons cell memory ------
 
 1000 constant N
 create car-cells N allot
@@ -50,6 +50,18 @@ variable nextfree
     cdr-type-cells + @
 ;
 
+: set-car! ( obj pair-obj -- )
+    drop dup
+    rot swap  car-type-cells + !
+    car-cells + !
+;
+
+: set-cdr! ( obj pair-obj -- )
+    drop dup
+    rot swap  cdr-type-cells + !
+    cdr-cells + !
+;
+
 : caar car car ;
 : cadr cdr car ;
 : cdar car cdr ;
@@ -58,7 +70,7 @@ variable nextfree
 : nil 0 nil-type ;
 : nil? nil-type istype? ;
 
-: objvar create 0 , 0 , ;
+: objvar create nil swap , , ;
 
 : value@ ( objvar -- val ) @ ;
 : type@ ( objvar -- type ) 1+ @ ;
@@ -67,14 +79,13 @@ variable nextfree
 : setobj ( newobj objvar -- ) dup rot swap 1+ ! ! ; 
 : fetchobj ( objvar -- obj ) dup @ swap 1+ @ ; 
 
-objvar symbol-table
-nil symbol-table setobj
-
 : objeq? ( obj obj -- bool )
     rot = -rot = and ;
 
 \ ---- Pre-defined symbols ----
 
+objvar symbol-table
+
 : (create-symbol) ( addr n -- symbol-obj )
     dup 0= if
         2drop nil
@@ -104,7 +115,32 @@ nil symbol-table setobj
     does> dup @ swap 1+ @
 ;
 
-create-symbol quote quote
+create-symbol quote quote-symbol
+create-symbol define define-symbol
+create-symbol set! set!-symbol
+
+\ ---- Environments ----
+
+objvar global-environment
+
+: enclosing-env ( env -- env )
+    cdr ;
+
+: first-frame ( env -- frame )
+    car ;
+
+: make-frame ( vars vals -- frame )
+    cons ;
+
+: frame-vars ( frame -- vars )
+    car ;
+
+: frame-vals ( frame -- vals )
+    cdr ;
+
+: add-binding ( var val frame -- )
+;
+    
 
 \ ---- Read ----
 
@@ -275,30 +311,6 @@ parse-idx-stack parse-idx-sp !
 : string? ( -- bool )
     nextchar [char] " = ;
 
-: initial? ( -- bool )
-    nextchar [char] A >= nextchar [char] Z <= and if true exit then
-    nextchar [char] a >= nextchar [char] z <= and if true exit then
-    nextchar [char] * = if true exit then
-    nextchar [char] / = if true exit then
-    nextchar [char] > = if true exit then
-    nextchar [char] < = if true exit then
-    nextchar [char] = = if true exit then
-    nextchar [char] ? = if true exit then
-    nextchar [char] ! = if true exit then
-    false
-;
-
-: symbol? ( -- bool )
-    initial? if true exit then
-    nextchar [char] + =
-    nextchar [char] - = or if
-        inc-parse-idx
-        delim? if dec-parse-idx true exit then
-        dec-parse-idx
-    then
-    false
-;
-
 : readnum ( -- num-atom )
     minus? dup if
         inc-parse-idx
@@ -517,11 +529,6 @@ defer read
         exit
     then
 
-    symbol? if
-        readsymbol charlist>symbol
-        exit
-    then
-
     pair? if
         inc-parse-idx
 
@@ -543,7 +550,7 @@ defer read
 
     nextchar [char] ' = if
         inc-parse-idx
-        quote recurse nil cons cons exit
+        quote-symbol recurse nil cons cons exit
     then
 
     eof? if
@@ -551,10 +558,8 @@ defer read
         quit
     then
 
-    bold fg red ." Error parsing string starting at character '"
-    nextchar emit
-    ." '. Aborting." reset-term cr
-    abort
+    \ Anything else is assumed to be a symbol
+    readsymbol charlist>symbol
 
 ; is read
 
@@ -579,7 +584,7 @@ defer read
     then ;
 
 : quote? ( obj -- obj bool )
-    quote tagged-list?  ;
+    quote-symbol tagged-list?  ;
 
 : quote-body ( quote-obj -- quote-body-obj )
     cadr ;