does> dup @ swap 1+ @
;
-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
-create-symbol apply apply-symbol
+create-symbol quote quote-symbol
+create-symbol quasiquote quasiquote-symbol
+create-symbol unquote unquote-symbol
+create-symbol unquote-splicing unquote-splicing-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
\ }}}
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 ---- {{{
quote-symbol recurse nil cons cons exit
then
+ nextchar [char] ` = if
+ inc-parse-idx
+ quasiquote-symbol recurse nil cons cons exit
+ then
+
+ nextchar [char] , = if
+ inc-parse-idx
+ nextchar [char] @ = if
+ inc-parse-idx
+ unquote-splicing-symbol recurse nil cons cons exit
+ else
+ unquote-symbol recurse nil cons cons exit
+ then
+ then
+
eof? if
EOF character-type
inc-parse-idx
: 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 iterative expansion of defines in
+ terms of nested lambdas. Most Schemes only
+ handle one iteration of expansion! )
+: definition-var-val ( obj -- var val )
+
+ cdr 2dup cdr 2swap car ( val var )
+
+ begin
+ symbol-type istype? false =
+ while
+ 2dup cdr 2swap car ( val formals var' )
+ -2rot 2swap ( var' formals val )
+ make-lambda nil cons ( var' val' )
+ 2swap ( val' var' )
+ repeat
+
+ 2swap car
;
-: definition-val ( obj -- val )
- 2dup cdr car symbol-type istype? if
- 2drop
- cdr cdr car
- else
- cdr 2swap cdr cdr
- make-lambda
- then
+: eval-definition ( obj env -- res )
+ 2dup 2rot ( env env obj )
+ definition-var-val ( env env var val )
+ 2rot eval ( env var val )
+
+ 2rot ( var val env )
+ define-var
+
+ ok-symbol
;
: assignment? ( obj -- obj bool )
: assignment-val ( obj -- val )
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 )
-
- 2rot ( var val env )
- define-var
-
- ok-symbol
-;
-
: eval-assignment ( obj env -- res )
2swap
2over 2over ( env obj env obj )
( 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
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' )
2over 2over ( env exp env exp )
operator ( env exp env opname )
- 2dup apply-symbol objeq? if
-
- 2drop 2swap ( env env exp )
- cdr 2dup car 2rot ( env expbody real-opname env )
- 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
-
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
+ else
+ \ Regular function application
- \ Regular function application
+ 2drop ( env exp env opname )
- 2drop ( env exp env opname )
+ 2swap eval ( env exp proc )
- 2swap eval ( env exp proc )
- -2rot ( proc env exp )
- operands 2swap ( proc operands env )
- list-of-vals ( proc argvals )
+ -2rot ( proc env exp )
+ operands 2swap ( proc operands env )
+ list-of-vals ( proc argvals )
- apply
- exit
+ apply
+ exit
+ then
then
bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
\ }}}
-\ ---- 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
-;
-
-include scheme-primitives.4th
-
-\ }}}
-
\ ---- Standard Library ---- {{{
+ include scheme-primitives.4th
+
s" scheme-library.scm" load 2drop
\ }}}