X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=src%2Fscheme.4th;h=8d361fc0fa73f36b091dd8cbd8ec9be7d09962de;hb=3f10b9976a9b02716a433666d972e30c6b81fd92;hp=d0bca194f51d9c9a2dd50aa1495679150ac05860;hpb=7575297b49c6d06e524b9dff038083a863720b53;p=scheme.forth.jl.git diff --git a/src/scheme.4th b/src/scheme.4th index d0bca19..8d361fc 100644 --- a/src/scheme.4th +++ b/src/scheme.4th @@ -263,6 +263,7 @@ create-symbol lambda lambda-symbol create-symbol λ λ-symbol create-symbol begin begin-symbol create-symbol eof eof-symbol +create-symbol no-match no-match-symbol \ Symbol to be bound to welcome message procedure by library create-symbol welcome welcome-symbol @@ -1403,34 +1404,21 @@ defer eval-quasiquote-item : definition? ( obj -- obj bool ) define-symbol tagged-list? ; -: make-lambda ( params body -- lambda-exp ) - lambda-symbol -2rot cons cons ; - -( 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 +: definition-var ( obj -- var ) + cdr car ; - 2swap car -; +: definition-val ( obj -- val ) + cdr cdr car ; : eval-definition ( obj env -- res ) - 2dup 2rot ( env env obj ) - definition-var-val ( env env var val ) - 2rot eval ( env var val ) + 2swap + 2over 2over + definition-val 2swap + eval - 2rot ( var val env ) + 2swap definition-var 2swap + + 2rot define-var ok-symbol @@ -1656,20 +1644,6 @@ hide env endcase ; -( Simply evaluates the given procedure with expbody as its argument. ) -: 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 @@ -1749,34 +1723,87 @@ hide env 2over 2over ( env exp env exp ) operator ( env exp env opname ) - 2dup lookup-macro nil? false = if - \ Macro function evaluation + 2swap eval ( env exp proc ) - ( env exp env opname mproc ) - 2swap 2drop -2rot 2drop cdr ( env mproc body ) + -2rot ( proc env exp ) + operands 2swap ( proc operands env ) + list-of-vals ( proc argvals ) - macro-expand + apply + exit + then - 2swap - ['] eval goto-deferred - else - \ Regular function application + except-message: ." tried to evaluate object with unknown type." recoverable-exception throw +; is eval + +\ }}} + +\ ---- Macro Expansion ---- {{{ + +( Simply evaluates the given procedure with expbody as its argument. ) +: macro-eval ( 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 - 2drop ( env exp env opname ) + extend-env eval-sequence eval +; - 2swap eval ( env exp proc ) +defer expand - -2rot ( proc env exp ) - operands 2swap ( proc operands env ) - list-of-vals ( proc argvals ) +: expand-quasiquote ; +: expand-definition ; +: expand-assignment ; +: expand-define-macro ; +: expand-if ; +: expand-lambda ; +: expand-begin ; +: expand-application ; - apply - exit - then +: expand-macro ( exp -- result ) + pair-type istype? invert if exit then + 2dup car symbol-type istype? invert if 2drop exit then + + lookup-macro nil? if + 2drop exit then + + 2over cdr macro-eval + + 2dup no-match-symbol objeq? if + 2drop exit + else + 2swap 2drop then - except-message: ." tried to evaluate object with unknown type." recoverable-exception throw -; is eval + R> drop ['] expand goto-deferred +; + +:noname ( exp -- result ) + + expand-macro + + quasiquote? if expand-quasiquote exit then + + definition? if expand-definition exit then + + assignment? if expand-assignment exit then + + macro-definition? if expand-define-macro exit then + + if? if expand-if exit then + + lambda? if expand-lambda exit then + + begin? if expand-begin exit then + + application? if expand-application exit then + +; is expand \ }}} @@ -2004,6 +2031,8 @@ variable gc-stack-depth 2swap 2drop ( port obj ) + expand + global-env obj@ eval ( port res ) again ; @@ -2014,7 +2043,9 @@ variable gc-stack-depth include scheme-primitives.4th - s" scheme-library.scm" load 2drop + s" scheme-derived-forms.scm" load 2drop + +\ s" scheme-library.scm" load 2drop \ }}} @@ -2032,6 +2063,8 @@ variable gc-stack-depth true exit then + expand + global-env obj@ eval fg cyan ." ; " print reset-term @@ -2045,7 +2078,7 @@ variable gc-stack-depth enable-gc \ Display welcome message - welcome-symbol nil cons global-env obj@ eval 2drop + \ welcome-symbol nil cons global-env obj@ eval 2drop begin ['] repl-body catch