Sketching out call-cc implementation.
[scheme.forth.jl.git] / src / scheme.4th
index 7b1da05..48c363a 100644 (file)
@@ -39,6 +39,7 @@ make-type pair-type
 make-type symbol-type
 make-type primitive-proc-type
 make-type compound-proc-type
+make-type continuation-type
 make-type port-type
 : istype? ( obj type -- obj bool )
     over = ;
@@ -62,7 +63,7 @@ variable nextexception
 make-exception recoverable-exception
 make-exception unrecoverable-exception
 
-: throw reset-term throw ;
+: throw reset-term cr throw ;
 
 \ }}}
 
@@ -75,6 +76,12 @@ create car-type-cells scheme-memsize allot
 create cdr-cells scheme-memsize allot
 create cdr-type-cells scheme-memsize allot
 
+variable gc-enabled
+false gc-enabled !
+
+: gc-enabled?
+    gc-enabled @ ;
+
 create nextfrees scheme-memsize allot
 :noname
     scheme-memsize 0 do
@@ -90,7 +97,9 @@ variable nextfree
     nextfree !
 
     nextfree @ scheme-memsize >= if
-        collect-garbage
+        gc-enabled? if
+            collect-garbage
+        then
     then
 
     nextfree @ scheme-memsize >= if
@@ -1273,6 +1282,11 @@ parse-idx-stack parse-idx-sp !
         exit
     then
 
+    nextchar [char] ) = if
+        inc-parse-idx
+        except-message: ." unmatched closing parenthesis." recoverable-exception throw
+    then
+
     \ Anything else is parsed as a symbol
     readsymbol charlist>symbol
 
@@ -1286,7 +1300,7 @@ parse-idx-stack parse-idx-sp !
 
 \ }}}
 
-\ ---- Eval ---- {{{
+\ ---- Syntax ---- {{{
 
 : self-evaluating? ( obj -- obj bool )
     boolean-type istype? if true exit then
@@ -1449,7 +1463,7 @@ parse-idx-stack parse-idx-sp !
 
 \ }}}
 
-\ ---- Analyze ----
+\ ---- Analyze ---- {{{
 
 : evaluate-eproc ( eproc env --- res )
 
@@ -1636,15 +1650,7 @@ parse-idx-stack parse-idx-sp !
     then
 ;
 
-: application-executor ( operator-proc arg-procs env -- res )
-    2rot 2over ( aprocs env fproc env )
-    evaluate-eproc ( aprocs env proc )
-
-    -2rot 2swap ( proc env aprocs )
-    evaluate-operand-eprocs ( proc vals )
-
-    2swap ( vals proc )
-    
+: apply ( vals proc )
     dup case
         primitive-proc-type of
             drop execute
@@ -1664,10 +1670,26 @@ parse-idx-stack parse-idx-sp !
                ['] evaluate-eproc goto
         endof
 
+        continuation-type of
+          \ TODO: Apply continuation
+        endof
+
         except-message: ." object '" drop print ." ' not applicable." recoverable-exception throw
     endcase
 ;
 
+: application-executor ( operator-proc arg-procs env -- res )
+    2rot 2over ( aprocs env fproc env )
+    evaluate-eproc ( aprocs env proc )
+
+    -2rot 2swap ( proc env aprocs )
+    evaluate-operand-eprocs ( proc vals )
+
+    2swap ( vals proc )
+
+    ['] apply goto
+;
+
 : analyze-application ( exp -- eproc )
     2dup operator analyze
     2swap operands operand-eproc-list
@@ -1700,6 +1722,7 @@ parse-idx-stack parse-idx-sp !
 
 ; is analyze
 
+\ }}}
 
 \ ---- Macro Expansion ---- {{{
 
@@ -1899,6 +1922,9 @@ parse-idx-stack parse-idx-sp !
 : printcomp ( primobj -- )
     2drop ." <compound procedure>" ;
 
+: printcont ( primobj --)
+    2drop ." <continuation>" ;
+
 : printnone ( noneobj -- )
     2drop ." Unspecified return value" ;
 
@@ -1917,6 +1943,7 @@ parse-idx-stack parse-idx-sp !
     pair-type istype? if ." (" printpair ." )" exit then
     primitive-proc-type istype? if printprim exit then
     compound-proc-type istype? if printcomp exit then
+    continuation-type istype? if printcont exit then
     none-type istype? if printnone exit then
     port-type istype? if printport exit then
 
@@ -1927,9 +1954,6 @@ parse-idx-stack parse-idx-sp !
 
 \ ---- Garbage Collection ---- {{{
 
-variable gc-enabled
-false gc-enabled !
-
 variable gc-stack-depth
 
 : enable-gc
@@ -1939,9 +1963,6 @@ variable gc-stack-depth
 : disable-gc
     false gc-enabled ! ;
 
-: gc-enabled?
-    gc-enabled @ ;
-
 : pairlike? ( obj -- obj bool )
     pair-type istype? if true exit then
     string-type istype? if true exit then
@@ -2041,8 +2062,14 @@ variable gc-stack-depth
     ok-symbol ( port res )
 
     begin
+        \ DEBUG
+        \ bold fg blue ." READ from " 2over drop . ." ==> " reset-term
+
         2over read-port ( port res obj )
 
+        \ DEBUG
+        \ 2dup print cr
+
         2dup EOF character-type objeq? if
             2drop 2swap close-port
             exit
@@ -2060,7 +2087,11 @@ variable gc-stack-depth
 
     include scheme-primitives.4th
 
+    enable-gc
+
     s" scheme-library.scm" load 2drop
+
+    disable-gc
     
 \ }}}
 
@@ -2102,6 +2133,8 @@ variable gc-stack-depth
             throw false
         endcase
     until
+
+    disable-gc
 ;
 
 forth definitions