Allowed recursive define expansion.
[scheme.forth.jl.git] / scheme.4th
index f96fec9..3728257 100644 (file)
@@ -239,7 +239,7 @@ objvar symbol-table
 ;
 
 
-: cstr>charlist ( addr n -- symbol-obj )
+: cstr>charlist ( addr n -- charlist )
     dup 0= if
         2drop nil
     else
@@ -256,13 +256,7 @@ objvar symbol-table
     count
 
     cstr>charlist
-    drop symbol-type
-
-    2dup
-
-    symbol-table obj@
-    cons
-    symbol-table obj!
+    charlist>symbol
 
     create swap , ,
     does> dup @ swap 1+ @
@@ -402,6 +396,54 @@ objvar global-env
 nil nil nil extend-env
 global-env obj!
 
+\ }}}
+
+\ ---- Primitives ---- {{{
+
+: make-primitive ( cfa -- )
+    bl word
+    count
+
+    \ 2dup ." Defining primitive " type ." ..." cr
+
+    cstr>charlist
+    charlist>symbol
+  
+    rot primitive-proc-type ( var prim )
+    global-env obj@ define-var
+;
+
+: arg-count-error
+            bold fg red ." Incorrect argument count." reset-term cr
+            abort
+;
+
+: ensure-arg-count ( args n -- )
+    dup 0= if
+        drop nil objeq? false = if
+            arg-count-error
+        then
+    else
+        -rot nil? if
+            arg-count-error
+        then
+        
+        cdr rot 1- recurse
+    then
+;
+
+: arg-type-error
+            bold fg red ." Incorrect argument type." reset-term cr
+            abort
+;
+
+: ensure-arg-type ( arg type -- arg )
+    istype? false = if
+        arg-type-error
+    then
+;
+
+
 \ }}}
 
 \ ---- Macros ---- {{{
@@ -980,19 +1022,32 @@ parse-idx-stack parse-idx-sp !
 : make-lambda ( params body -- lambda-exp )
     lambda-symbol -2rot cons cons ;
 
-: definition-var ( obj -- var )
-    cdr car
-    symbol-type istype? false = if car then
-;
+( Handles recursive expansion of defines in
+  terms of nested lambdas. Most Schemes only
+  handle one level of expansion! )
+: (definition-var-val) ( val var -- val' var' )
+    symbol-type istype? if 2swap car 2swap exit then
 
-: definition-val ( obj -- val )
-    2dup cdr car symbol-type istype? if
-        2drop
-        cdr cdr car
-    else
-        cdr 2swap cdr cdr
-        make-lambda
+    2dup cdr 2swap car
+    symbol-type istype? if
+        2swap ( body procname procargs )
+        2rot ( procname procargs body )
+        make-lambda ( procname lambda-exp )
+        2swap
+        exit
     then
+
+    ( body procargs nextval )
+    -2rot 2swap ( nextval procargs body )
+    make-lambda nil cons ( nextval lambda-exp )
+    2swap ( lambda-exp nextval )
+    recurse
+;
+
+: definition-var-val ( obj -- var val )
+    cdr 2dup cdr 2swap car
+    (definition-var-val)
+    2swap
 ;
 
 : assignment? ( obj -- obj bool )
@@ -1005,12 +1060,9 @@ parse-idx-stack parse-idx-sp !
     cdr cdr car ;
 
 : eval-definition ( obj env -- res )
-    2swap 
-    2over 2over ( env obj env obj )
-    definition-val 2swap ( env obj valexp env )
-    eval  ( env obj val )
-    
-    2swap definition-var 2swap ( env var val )
+    2dup 2rot ( env env obj )
+    definition-var-val ( env env var val )
+    2rot eval  ( env var val )
 
     2rot ( var val env )
     define-var
@@ -1171,7 +1223,15 @@ hide env
 ( Ensure terminating symbol arg name is handled
   specially to allow for variadic procedures. )
 : flatten-proc-args ( argvals argnames -- argvals' argnames' )
-    nil? if exit then
+    nil? if
+        2over nil? false = if
+            bold fg red ." Too many arguments supplied to compound method. Aborting." reset-term cr
+            abort
+        else
+            2drop
+        then
+        exit
+    then
 
     symbol-type istype? if
         nil cons
@@ -1181,7 +1241,16 @@ hide env
         exit
     then
 
-    2over cdr 2over cdr
+    2over
+    nil? if
+        bold fg red ." Too few arguments supplied to compound method. Aborting." reset-term cr
+        abort
+    else
+        cdr
+    then
+
+    2over cdr
+
     recurse ( argvals argnames argvals'' argnames'' )
     2rot car 2swap cons  ( argvals argvals'' argnames' )
     2rot car 2rot cons ( argnames' argvals' )
@@ -1191,7 +1260,7 @@ hide env
 : apply ( proc argvals -- result )
         2swap dup case
             primitive-proc-type of
-                drop execute
+                drop execute     
             endof
 
             compound-proc-type of
@@ -1293,21 +1362,10 @@ hide env
     application? if
 
         2over 2over ( env exp env exp )
-        operator 2dup ( env exp env opname opname )
-
-        lookup-macro nil? if
-            \ Regular function application
+        operator ( env exp env opname )
 
-            2drop ( env exp env opname )
-
-            2swap eval ( env exp proc )
-            -2rot ( proc env exp )
-            operands 2swap ( proc operands env )
-            list-of-vals ( proc argvals )
-
-            apply
-        else
-            \ Macro function evaluation
+        2dup lookup-macro nil? false = if
+             \ Macro function evaluation
 
             ( env exp env opname mproc )
             2swap 2drop -2rot 2drop cdr ( env mproc body )
@@ -1318,8 +1376,20 @@ hide env
 
             2swap
             ['] eval goto-deferred
+        else
+           \ Regular function application
+
+            2drop ( env exp env opname )
+
+            2swap eval ( env exp proc )
+
+            -2rot ( proc env exp )
+            operands 2swap ( proc operands env )
+            list-of-vals ( proc argvals )
+
+            apply
+            exit
         then
-        exit
     then
 
     bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
@@ -1565,59 +1635,12 @@ variable gc-stack-depth
 
 \ }}}
 
-\ ---- Primitives ---- {{{
+\ ---- Standard Library ---- {{{
 
-: make-primitive ( cfa -- )
-    bl word
-    count
+    include scheme-primitives.4th
 
-    \ 2dup ." Defining primitive " type ." ..." cr
-
-    cstr>charlist
-    drop symbol-type
+    s" scheme-library.scm" load 2drop
     
-    2dup
-
-    symbol-table obj@
-    cons
-    symbol-table obj!
-
-    rot primitive-proc-type ( var prim )
-    global-env obj@ define-var
-;
-
-: arg-count-error
-            bold fg red ." Incorrect argument count." reset-term cr
-            abort
-;
-
-: ensure-arg-count ( args n -- )
-    dup 0= if
-        drop nil objeq? false = if
-            arg-count-error
-        then
-    else
-        -rot nil? if
-            arg-count-error
-        then
-        
-        cdr rot 1- recurse
-    then
-;
-
-: arg-type-error
-            bold fg red ." Incorrect argument type." reset-term cr
-            abort
-;
-
-: ensure-arg-type ( arg type -- arg )
-    istype? false = if
-        arg-type-error
-    then
-;
-
-include scheme-primitives.4th
-
 \ }}}
 
 \ ---- REPL ----