Merge branch 'master' of git://factorcode.org/git/jamesnvc

db4
Slava Pestov 2008-05-26 05:35:08 -05:00
commit b6257168b6
2 changed files with 47 additions and 46 deletions

View File

@ -2,7 +2,8 @@
! See http://factorcode.org/license.txt for BSD license.
USING: kernel peg sequences arrays strings combinators.lib
namespaces combinators math bake locals locals.private accessors
vectors syntax lisp.parser assocs parser sequences.lib words quotations ;
vectors syntax lisp.parser assocs parser sequences.lib words quotations
fry ;
IN: lisp
DEFER: convert-form
@ -12,20 +13,20 @@ DEFER: lookup-var
! Functions to convert s-exps to quotations
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: convert-body ( s-exp -- quot )
[ convert-form ] map [ ] [ compose ] reduce ; inline
[ ] [ convert-form compose ] reduce ; inline
: convert-if ( s-exp -- quot )
rest [ convert-form ] map reverse first3 [ % , , if ] bake ;
rest first3 [ convert-form ] tri@ '[ @ , , if ] ;
: convert-begin ( s-exp -- quot )
rest [ convert-form ] map >quotation [ , [ funcall ] each ] bake ;
rest [ convert-form ] [ ] map-as '[ , [ funcall ] each ] ;
: convert-cond ( s-exp -- quot )
rest [ body>> >array [ convert-form ] map first2 swap `{ [ % funcall ] , } bake ]
map >array [ , cond ] bake ;
rest [ body>> first2 [ convert-form ] bi@ [ '[ @ funcall ] ] dip 2array ]
{ } map-as '[ , cond ] ;
: convert-general-form ( s-exp -- quot )
unclip convert-form swap convert-body [ , % funcall ] bake ;
unclip convert-form swap convert-body swap '[ , @ funcall ] ;
! words for convert-lambda
<PRIVATE
@ -42,19 +43,19 @@ DEFER: lookup-var
first3 -rot nip [ body>> ] bi@ [ name>> ] map ; inline
: rest-lambda ( body vars -- quot )
"&rest" swap [ remove ] [ index ] 2bi
[ localize-lambda <lambda> ] dip
[ , cut swap [ % , ] bake , compose ] bake ;
"&rest" swap [ index ] [ remove ] 2bi
localize-lambda <lambda>
'[ , cut '[ @ , ] , compose ] ;
: normal-lambda ( body vars -- quot )
localize-lambda <lambda> [ , compose ] bake ;
localize-lambda <lambda> '[ , compose ] ;
PRIVATE>
: convert-lambda ( s-exp -- quot )
split-lambda dup "&rest" swap member? [ rest-lambda ] [ normal-lambda ] if ;
split-lambda "&rest" over member? [ rest-lambda ] [ normal-lambda ] if ;
: convert-quoted ( s-exp -- quot )
second [ , ] bake ;
second 1quotation ;
: convert-list-form ( s-exp -- quot )
dup first dup lisp-symbol?
@ -70,8 +71,8 @@ PRIVATE>
: convert-form ( lisp-form -- quot )
{ { [ dup s-exp? ] [ body>> convert-list-form ] }
{ [ dup lisp-symbol? ] [ [ , lookup-var ] bake ] }
[ [ , ] bake ]
{ [ dup lisp-symbol? ] [ '[ , lookup-var ] ] }
[ 1quotation ]
} cond ;
: lisp-string>factor ( str -- quot )
@ -98,4 +99,4 @@ ERROR: no-such-var var ;
dup lisp-symbol? [ lookup-var ] when call ; inline
: define-primitve ( name vocab word -- )
swap lookup [ [ , ] compose call ] bake lisp-define ;
swap lookup 1quotation '[ , compose call ] lisp-define ;