make-type character-type
make-type string-type
make-type nil-type
+make-type none-type
make-type pair-type
make-type symbol-type
make-type primitive-proc-type
: istype? ( obj type -- obj bool )
over = ;
-\ ------ List-structured memory ------ {{{
+\ ---- List-structured memory ---- {{{
10000 constant scheme-memsize
: nil 0 nil-type ;
: nil? nil-type istype? ;
+: none 0 none-type ;
+: none? none-type istype? ;
+
: objvar create nil swap , , ;
: value@ ( objvar -- val ) @ ;
objvar symbol-table
: duplicate-charlist ( charlist -- copy )
- 2dup nil objeq? false = if
+ nil? false = if
2dup car 2swap cdr recurse cons
then ;
;
-: cstr>charlist ( addr n -- symbol-obj )
+: cstr>charlist ( addr n -- charlist )
dup 0= if
2drop nil
else
count
cstr>charlist
- drop symbol-type
-
- 2dup
-
- symbol-table obj@
- cons
- symbol-table obj!
+ charlist>symbol
create swap , ,
does> dup @ swap 1+ @
;
-create-symbol quote quote-symbol
-create-symbol define define-symbol
-create-symbol set! set!-symbol
-create-symbol ok ok-symbol
-create-symbol if if-symbol
-create-symbol lambda lambda-symbol
-create-symbol λ λ-symbol
+create-symbol quote quote-symbol
+create-symbol define define-symbol
+create-symbol define-macro define-macro-symbol
+create-symbol set! set!-symbol
+create-symbol ok ok-symbol
+create-symbol if if-symbol
+create-symbol lambda lambda-symbol
+create-symbol λ λ-symbol
+create-symbol begin begin-symbol
\ }}}
: get-vars-vals ( var env -- vars? vals? bool )
begin
- 2dup nil objeq? false =
+ nil? false =
while
2over 2over first-frame
get-vars-vals-frame if
hide env
+: make-procedure ( params body env -- proc )
+ nil
+ cons cons cons
+ drop compound-proc-type
+;
+
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 ---- {{{
+
+objvar macro-table
+
+( Look up macro in macro table. Returns nil if
+ no macro is found. )
+: lookup-macro ( name_symbol -- proc )
+ macro-table obj@
+
+ begin
+ nil? false =
+ while
+ 2over 2over
+ car car objeq? if
+ 2swap 2drop
+ car cdr
+ exit
+ then
+
+ cdr
+ repeat
+
+ 2swap 2drop
+;
+
+: make-macro ( name_symbol params body env -- )
+ make-procedure
+
+ 2swap ( proc name_symbol )
+
+ macro-table obj@
+
+ begin
+ nil? false =
+ while
+ 2over 2over ( proc name table name table )
+ car car objeq? if
+ 2swap 2drop ( proc table )
+ car ( proc entry )
+ set-cdr!
+ exit
+ then
+
+ cdr
+ repeat
+
+ 2drop
+
+ 2swap cons
+ macro-table obj@ cons
+ macro-table obj!
+;
+
+\ }}}
+
\ ---- Read ---- {{{
variable parse-idx
character-type istype? if true exit then
string-type istype? if true exit then
nil-type istype? if true exit then
+ none-type istype? if true exit then
false
;
ok-symbol
;
+: macro-definition? ( obj -- obj bool )
+ define-macro-symbol tagged-list? ;
+
+: macro-definition-name ( exp -- mname )
+ cdr car car ;
+
+: macro-definition-params ( exp -- params )
+ cdr car cdr ;
+
+: macro-definition-body ( exp -- body )
+ cdr cdr ;
+
+objvar env
+: eval-define-macro ( obj env -- res )
+ env obj!
+
+ 2dup macro-definition-name 2swap ( name obj )
+ 2dup macro-definition-params 2swap ( name params obj )
+ macro-definition-body ( name params body )
+
+ env obj@ ( name params body env )
+
+ make-macro
+
+ ok-symbol
+;
+hide env
+
: if? ( obj -- obj bool )
if-symbol tagged-list? ;
: if-alternative ( ifobj -- alt|false )
cdr cdr cdr
- 2dup nil objeq? if
+ nil? if
2drop false
else
car
: lambda-body ( obj -- body )
cdr cdr ;
-: make-procedure ( params body env -- proc )
- nil
- cons cons cons
- drop compound-proc-type
+: begin? ( obj -- obj bool )
+ begin-symbol tagged-list? ;
+
+: begin-actions ( obj -- actions )
+ cdr ;
+
+: eval-sequence ( explist env -- finalexp env )
+ ( Evaluates all bar the final expressions in
+ an an expression list. The final expression
+ is returned to allow for tail optimization. )
+
+ 2swap ( env explist )
+
+ \ Abort on empty list
+ nil? if
+ 2drop none
+ 2swap exit
+ then
+
+ begin
+ 2dup cdr ( env explist nextexplist )
+ nil? false =
+ while
+ -2rot car 2over ( nextexplist env exp env )
+ eval
+ 2drop \ discard result
+ 2swap ( env nextexplist )
+ repeat
+
+ 2drop car 2swap ( finalexp env )
;
-: application? ( obj -- obj bool)
+: application? ( obj -- obj bool )
pair-type istype? ;
: operator ( obj -- operator )
: procedure-env ( proc -- body )
drop pair-type cdr cdr car ;
-: apply ( proc args )
+( Ensure terminating symbol arg name is handled
+ specially to allow for variadic procedures. )
+: flatten-proc-args ( argvals argnames -- argvals' argnames' )
+ nil? if exit then
+
+ symbol-type istype? if
+ nil cons
+ 2swap
+ nil cons
+ 2swap
+ exit
+ then
+
+ 2over cdr 2over cdr
+ recurse ( argvals argnames argvals'' argnames'' )
+ 2rot car 2swap cons ( argvals argvals'' argnames' )
+ 2rot car 2rot cons ( argnames' argvals' )
+ 2swap
+;
+
+:noname
+ \ Dummy apply procedure
+ \ Should never actually run!
+ ." Error: Dummy apply procedure executed!" cr
+; dup make-primitive apply
+objvar dummy-apply-proc
+primitive-proc-type dummy-apply-proc obj!
+
+: apply ( proc argvals -- result )
2swap dup case
primitive-proc-type of
- drop execute
+ drop execute
endof
compound-proc-type of
- 2dup procedure-body ( args proc body )
- -2rot 2dup procedure-params ( body args proc params )
- -2rot procedure-env ( body params args procenv )
-
- extend-env ( body env )
+ 2dup procedure-body ( argvals proc body )
+ -2rot 2dup procedure-params ( body argvals proc argnames )
+ -2rot procedure-env ( body argnames argvals procenv )
- 2swap ( env body )
+ -2rot 2swap
+ flatten-proc-args
+ 2swap 2rot
- begin
- 2dup cdr 2dup nil objeq? false =
- while
- -2rot car 2over ( nextbody env exp env )
- eval
- 2drop \ discard result
- 2swap ( env nextbody )
- repeat
+ extend-env ( body env )
- 2drop ( env body )
- car 2swap ( exp env )
+ eval-sequence
R> drop ['] eval goto-deferred \ Tail call optimization
endof
- bold fg red ." Object not applicable. Aboring." reset-term cr
+ bold fg red ." Object not applicable. Aborting." reset-term cr
abort
endcase
;
+: macro-expand ( proc expbody -- result )
+ 2swap
+ 2dup procedure-body ( expbody proc procbody )
+ -2rot 2dup procedure-params ( procbody expbody proc argnames )
+ -2rot procedure-env ( procbody argnames expbody procenv )
+
+ -2rot 2swap
+ flatten-proc-args
+ 2swap 2rot
+
+ extend-env eval-sequence eval
+;
+
:noname ( obj env -- result )
2swap
exit
then
+ macro-definition? if
+ 2swap eval-define-macro
+ exit
+ then
+
if? if
2over 2over
if-predicate
exit
then
+ begin? if
+ begin-actions 2swap
+ eval-sequence
+ ['] eval goto-deferred
+ then
+
application? if
- 2over 2over
- operator 2swap eval
- -2rot
- operands 2swap list-of-vals
+
+ 2over 2over ( env exp env exp )
+ operator ( env exp env opname )
+
+ 2dup lookup-macro nil? false = if
+ \ Macro function evaluation
+
+ ( env exp env opname mproc )
+ 2swap 2drop -2rot 2drop cdr ( env mproc body )
+
+ 2dup print cr
+ macro-expand
+ 2dup print cr
+
+ 2swap
+ ['] eval goto-deferred
+ then
+
+ \ Regular function application
+
+ 2drop ( env exp env opname )
+
+ 2swap eval ( env exp proc )
+
+ 2dup dummy-apply-proc obj@ objeq? if
+ 2drop ( env exp )
+ cdr 2over 2over car ( env expbody env real-opname )
+ 2swap eval ( env expbody proc )
+
+ 2swap cdr
+ nil? false = if car then ( env proc real-operand )
+
+ 2rot eval ( proc argvals )
+
+ pair-type istype? false = if
+ bold fg red ." Error: apply requires a list of operand arguments." cr
+ reset-term abort
+ then
+
+ apply
+ exit
+ then
+
+ -2rot ( proc env exp )
+ operands 2swap ( proc operands env )
+ list-of-vals ( proc argvals )
apply
exit
: printcomp ( primobj -- )
2drop ." <compound procedure>" ;
+: printnone ( noneobj -- )
+ 2drop ." Unspecified return value" ;
+
:noname ( obj -- )
fixnum-type istype? if printfixnum exit then
realnum-type istype? if printrealnum 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
+ none-type istype? if printnone exit then
bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
abort
gc-unmark
symbol-table obj@ gc-mark-obj
+ macro-table obj@ gc-mark-obj
global-env obj@ gc-mark-obj
depth gc-stack-depth @ do
\ }}}
-\ ---- Primitives ---- {{{
-
-: make-primitive ( cfa -- )
- bl word
- count
-
- \ 2dup ." Defining primitive " type ." ..." cr
-
- cstr>charlist
- drop symbol-type
-
- 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 2dup nil objeq? 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
-
-\ }}}
-
\ ---- Loading files ---- {{{
: charlist>cstr ( charlist addr -- n )
dup 2swap ( origaddr addr charlist )
begin
- 2dup nil objeq? false =
+ nil? false =
while
2dup cdr 2swap car
drop ( origaddr addr charlist char )
swap -
;
-: load ( addr n -- )
+: load ( addr n -- finalResult )
open-input-file
empty-parse-str
+ ok-symbol ( port res )
+
begin
- 2dup read-port
+ 2over read-port ( port res obj )
2dup EOF character-type objeq? if
- 2drop close-port
+ 2drop 2swap close-port
exit
then
- global-env obj@ eval
- 2drop
+ 2swap 2drop ( port obj )
+
+ global-env obj@ eval ( port res )
again
;
\ }}}
+\ ---- Standard Library ---- {{{
+
+ include scheme-primitives.4th
+
+ s" scheme-library.scm" load 2drop
+
+\ }}}
+
\ ---- REPL ----
: repl
read-console
2dup EOF character-type objeq? if
+ 2drop
bold fg blue ." Moriturus te saluto." reset-term cr
exit
then
again
;
-: test s" fact.scm" ;
-test load
-
forth definitions
\ vim:fdm=marker