factor/extra/lisp/lisp.factor

72 lines
2.2 KiB
Factor
Raw Normal View History

2008-04-17 02:37:03 -04:00
! Copyright (C) 2008 James Cash
! See http://factorcode.org/license.txt for BSD license.
2008-04-26 17:20:12 -04:00
USING: kernel peg sequences arrays strings combinators.lib
namespaces combinators math bake locals.private accessors vectors syntax lisp.parser ;
2008-04-17 12:37:31 -04:00
IN: lisp
2008-04-17 02:37:03 -04:00
2008-04-21 20:25:55 -04:00
DEFER: convert-form
2008-04-30 16:59:50 -04:00
DEFER: funcall
2008-04-21 20:25:55 -04:00
2008-04-30 16:59:50 -04:00
! Functions to convert s-exps to quotations
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-04-26 17:20:12 -04:00
: convert-body ( s-exp -- quot )
[ convert-form ] map reverse [ ] [ compose ] reduce ; inline
2008-04-21 20:25:55 -04:00
2008-04-26 17:20:12 -04:00
: convert-if ( s-exp -- quot )
2008-04-30 16:59:50 -04:00
rest [ convert-form ] map reverse first3 [ % , , if ] bake ;
2008-04-21 20:25:55 -04:00
2008-04-26 17:20:12 -04:00
: convert-begin ( s-exp -- quot )
2008-04-30 16:59:50 -04:00
rest convert-body ;
2008-04-24 16:35:42 -04:00
2008-04-26 17:20:12 -04:00
: convert-cond ( s-exp -- quot )
2008-04-30 16:59:50 -04:00
rest [ [ convert-body map ] map ] [ % cond ] bake ;
2008-04-24 16:35:42 -04:00
: convert-general-form ( s-exp -- quot )
2008-04-30 16:59:50 -04:00
unclip swap convert-body [ % , funcall ] bake ;
2008-04-24 16:35:42 -04:00
<PRIVATE
: localize-body ( vars body -- newbody )
[ dup lisp-symbol? [ tuck name>> swap member? [ name>> make-local ] [ ] if ]
[ dup s-exp? [ body>> localize-body <s-exp> ] [ nip ] if ] if ] with map ;
2008-04-24 16:35:42 -04:00
PRIVATE>
2008-04-26 17:20:12 -04:00
: convert-lambda ( s-exp -- quot )
first3 -rot nip [ body>> ] bi@ reverse [ name>> ] map dup make-locals dup push-locals
2008-04-24 16:35:42 -04:00
[ swap localize-body convert-body ] dipd pop-locals swap <lambda> ;
2008-04-30 16:59:50 -04:00
: convert-quoted ( s-exp -- quot )
second [ , ] bake ;
2008-04-26 17:20:12 -04:00
: convert-list-form ( s-exp -- quot )
2008-04-30 16:59:50 -04:00
dup first dup lisp-symbol?
[ name>>
2008-04-26 17:20:12 -04:00
{ { "lambda" [ convert-lambda ] }
2008-04-30 16:59:50 -04:00
{ "quote" [ convert-quoted ] }
2008-04-26 17:20:12 -04:00
{ "if" [ convert-if ] }
{ "begin" [ convert-begin ] }
{ "cond" [ convert-cond ] }
[ drop convert-general-form ]
2008-04-30 16:59:50 -04:00
} case ]
[ drop convert-general-form ] if ;
2008-04-21 20:25:55 -04:00
: convert-form ( lisp-form -- quot )
{ { [ dup s-exp? ] [ body>> convert-list-form ] }
2008-04-21 20:25:55 -04:00
[ [ , ] [ ] make ]
2008-04-26 17:20:12 -04:00
} cond ;
2008-04-30 16:59:50 -04:00
: lisp-string>factor ( str -- quot )
lisp-expr parse-result-ast convert-form ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SYMBOL: lisp-env
H{ } clone lisp-env set
: define-lisp-word ( name body -- )
lisp-env get set-at ;
: get-lisp-word ( name -- word )
lisp-env get at ;
: funcall ( quot sym -- * )
name>> get-lisp-word call ;