Starting work on converting lisp.factor to use cons cells

db4
James Cash 2008-06-01 23:59:38 -04:00
parent 27586218e8
commit f0fdac5b7d
1 changed files with 23 additions and 19 deletions

View File

@ -6,42 +6,45 @@ vectors syntax lisp.parser assocs parser sequences.lib words quotations
fry ; fry ;
IN: lisp IN: lisp
: uncons ( cons -- cdr car )
[ cdr>> ] [ car>> ] bi ;
DEFER: convert-form DEFER: convert-form
DEFER: funcall DEFER: funcall
DEFER: lookup-var DEFER: lookup-var
DEFER: lisp-macro? DEFER: lisp-macro?
DEFER: looku-macro DEFER: lookup-macro
! Functions to convert s-exps to quotations ! Functions to convert s-exps to quotations
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: convert-body ( s-exp -- quot ) : convert-body ( cons -- quot )
[ ] [ convert-form compose ] reduce ; inline [ ] [ convert-form compose ] reduce ; inline
: convert-if ( s-exp -- quot ) : convert-if ( cons -- quot )
rest first3 [ convert-form ] tri@ '[ @ , , if ] ; rest first3 [ convert-form ] tri@ '[ @ , , if ] ;
: convert-begin ( s-exp -- quot ) : convert-begin ( cons -- quot )
rest [ convert-form ] [ ] map-as '[ , [ funcall ] each ] ; rest [ convert-form ] [ ] map-as '[ , [ funcall ] each ] ;
: convert-cond ( s-exp -- quot ) : convert-cond ( cons -- quot )
rest [ body>> first2 [ convert-form ] bi@ [ '[ @ funcall ] ] dip 2array ] rest [ body>> first2 [ convert-form ] bi@ [ '[ @ funcall ] ] dip 2array ]
{ } map-as '[ , cond ] ; { } map-as '[ , cond ] ;
: convert-general-form ( s-exp -- quot ) : convert-general-form ( cons -- quot )
unclip convert-form swap convert-body swap '[ , @ funcall ] ; uncons convert-form swap convert-body swap '[ , @ funcall ] ;
! words for convert-lambda ! words for convert-lambda
<PRIVATE <PRIVATE
: localize-body ( assoc body -- assoc newbody ) : localize-body ( assoc body -- assoc newbody )
[ dup lisp-symbol? [ over dupd [ name>> ] dip at swap or ] [ dup lisp-symbol? [ over dupd [ name>> ] dip at swap or ]
[ dup s-exp? [ body>> localize-body <s-exp> ] when ] if [ dup cons? [ body>> localize-body <s-exp> ] when ] if
] map ; ] map ;
: localize-lambda ( body vars -- newbody newvars ) : localize-lambda ( body vars -- newbody newvars )
make-locals dup push-locals swap make-locals dup push-locals swap
[ swap localize-body <s-exp> convert-form swap pop-locals ] dip swap ; [ swap localize-body <s-exp> convert-form swap pop-locals ] dip swap ;
: split-lambda ( s-exp -- body vars ) : split-lambda ( cons -- body vars )
first3 -rot nip [ body>> ] bi@ [ name>> ] map ; inline first3 -rot nip [ body>> ] bi@ [ name>> ] map ; inline
: rest-lambda ( body vars -- quot ) : rest-lambda ( body vars -- quot )
@ -53,11 +56,11 @@ DEFER: looku-macro
localize-lambda <lambda> '[ , compose ] ; localize-lambda <lambda> '[ , compose ] ;
PRIVATE> PRIVATE>
: convert-lambda ( s-exp -- quot ) : convert-lambda ( cons -- quot )
split-lambda "&rest" over member? [ rest-lambda ] [ normal-lambda ] if ; split-lambda "&rest" over member? [ rest-lambda ] [ normal-lambda ] if ;
: convert-quoted ( s-exp -- quot ) : convert-quoted ( cons -- quot )
second 1quotation ; cdr>> 1quotation ;
: form-dispatch ( lisp-symbol -- quot ) : form-dispatch ( lisp-symbol -- quot )
name>> name>>
@ -69,20 +72,21 @@ PRIVATE>
[ drop convert-general-form ] [ drop convert-general-form ]
} case ; } case ;
: macro-expand ( s-exp -- quot ) : macro-expand ( cons -- quot )
unclip-slice lookup-macro macro-call convert-form ; uncons lookup-macro macro-call convert-form ;
: convert-list-form ( s-exp -- quot ) : convert-list-form ( cons -- quot )
dup first dup car>>
{ { [ dup lisp-macro? ] [ macro-expand ] } { { [ dup lisp-macro? ] [ macro-expand ] }
{ [ dup lisp-symbol? ] [ form-dispatch ] } { [ dup lisp-symbol? ] [ form-dispatch ] }
[ drop convert-general-form ] [ drop convert-general-form ]
} cond ; } cond ;
: convert-form ( lisp-form -- quot ) : convert-form ( lisp-form -- quot )
{ { [ dup s-exp? ] [ body>> convert-list-form ] } {
{ [ dup lisp-symbol? ] [ '[ , lookup-var ] ] } { [ dup cons? ] [ convert-list-form ] }
[ 1quotation ] { [ dup lisp-symbol? ] [ '[ , lookup-var ] ] }
[ 1quotation ]
} cond ; } cond ;
: lisp-string>factor ( str -- quot ) : lisp-string>factor ( str -- quot )