factor/extra/lisp/lisp.factor

150 lines
4.4 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 locals locals.private locals.backend accessors
2008-06-05 18:15:05 -04:00
vectors syntax lisp.parser assocs parser sequences.lib words
quotations fry lists summary combinators.short-circuit continuations ;
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-05-18 12:53:44 -04:00
DEFER: lookup-var
DEFER: lookup-macro
2008-06-05 20:38:12 -04:00
DEFER: lisp-macro?
DEFER: lisp-var?
2008-06-05 18:15:05 -04:00
DEFER: define-lisp-macro
2008-07-02 23:54:06 -04:00
2008-04-30 16:59:50 -04:00
! Functions to convert s-exps to quotations
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: convert-body ( cons -- quot )
2008-06-05 04:14:26 -04:00
[ ] [ convert-form compose ] foldl ; inline
2008-07-02 23:54:06 -04:00
: convert-cond ( cons -- quot )
2008-06-18 15:09:02 -04:00
cdr [ 2car [ convert-form ] bi@ 2array ]
2008-06-30 11:20:41 -04:00
{ } lmap-as '[ , cond ] ;
2008-07-02 23:54:06 -04:00
: convert-general-form ( cons -- quot )
2008-06-03 23:41:05 -04:00
uncons [ convert-body ] [ convert-form ] bi* '[ , @ funcall ] ;
2008-05-15 22:14:53 -04:00
2008-07-02 23:54:06 -04:00
! words for convert-lambda
<PRIVATE
: localize-body ( assoc body -- newbody )
{
{ [ dup list? ] [ [ lisp-symbol? ] rot '[ [ name>> , at ] [ ] bi or ] traverse ] }
{ [ dup lisp-symbol? ] [ name>> swap at ] }
[ nip ]
} cond ;
2008-06-05 18:15:05 -04:00
: localize-lambda ( body vars -- newvars newbody )
swap [ make-locals dup push-locals ] dip
dupd [ localize-body convert-form ] with lmap>array
>quotation swap pop-locals ;
2008-07-02 23:54:06 -04:00
: split-lambda ( cons -- body-cons vars-seq )
cdr uncons [ name>> ] lmap>array ; inline
2008-07-02 23:54:06 -04:00
2008-06-03 23:41:05 -04:00
: rest-lambda ( body vars -- quot )
"&rest" swap [ remove ] [ index ] 2bi
[ localize-lambda <lambda> lambda-rewrite call ] dip
2008-08-13 23:19:18 -04:00
swap '[ , cut '[ @ , seq>list ] call , call call ] ;
2008-07-02 23:54:06 -04:00
2008-05-11 19:38:38 -04:00
: normal-lambda ( body vars -- quot )
2008-08-23 13:03:09 -04:00
localize-lambda <lambda> lambda-rewrite '[ @ compose call call ] 1quotation ;
2008-05-15 22:14:53 -04:00
PRIVATE>
2008-07-02 23:54:06 -04:00
: convert-lambda ( cons -- quot )
2008-05-26 04:35:18 -04:00
split-lambda "&rest" over member? [ rest-lambda ] [ normal-lambda ] if ;
2008-07-02 23:54:06 -04:00
: convert-quoted ( cons -- quot )
cadr 1quotation ;
2008-07-02 23:54:06 -04:00
2008-06-05 18:15:05 -04:00
: convert-defmacro ( cons -- quot )
cdr [ car ] keep [ convert-lambda ] [ car name>> ] bi define-lisp-macro 1quotation ;
2008-07-02 23:54:06 -04:00
2008-06-18 15:09:02 -04:00
: macro-expand ( cons -- quot )
2008-08-23 13:03:09 -04:00
uncons [ list>seq >quotation ] [ lookup-macro ] bi* call call ;
: (expand-macros) ( cons -- cons )
[ dup list? [ (expand-macros) dup car lisp-macro? [ macro-expand ] when ] when ] lmap ;
: expand-macros ( cons -- cons )
dup list? [ (expand-macros) dup car lisp-macro? [ macro-expand ] when ] when ;
: convert-begin ( cons -- quot )
cdr [ convert-form ] [ ] lmap-as [ 1 tail* ] [ but-last ] bi
2008-08-23 13:03:09 -04:00
[ '[ { } , with-datastack drop ] ] map prepend '[ , [ call ] each ] ;
2008-07-02 23:54:06 -04:00
2008-06-05 18:15:05 -04:00
: form-dispatch ( cons lisp-symbol -- quot )
2008-05-30 01:44:54 -04:00
name>>
{ { "lambda" [ convert-lambda ] }
2008-06-05 18:15:05 -04:00
{ "defmacro" [ convert-defmacro ] }
2008-05-30 01:44:54 -04:00
{ "quote" [ convert-quoted ] }
{ "cond" [ convert-cond ] }
{ "begin" [ convert-begin ] }
2008-05-30 01:44:54 -04:00
[ drop convert-general-form ]
} case ;
2008-07-02 23:54:06 -04:00
: convert-list-form ( cons -- quot )
dup car
2008-08-23 13:03:09 -04:00
{
2008-07-02 23:54:06 -04:00
{ [ dup lisp-symbol? ] [ form-dispatch ] }
2008-05-30 01:44:54 -04:00
[ drop convert-general-form ]
} cond ;
2008-07-02 23:54:06 -04:00
2008-04-21 20:25:55 -04:00
: convert-form ( lisp-form -- quot )
{
{ [ dup cons? ] [ convert-list-form ] }
{ [ dup lisp-var? ] [ lookup-var 1quotation ] }
{ [ dup lisp-symbol? ] [ '[ , lookup-var ] ] }
[ 1quotation ]
2008-05-26 04:35:18 -04:00
} cond ;
: lisp-string>factor ( str -- quot )
2008-08-23 13:03:09 -04:00
lisp-expr expand-macros convert-form ;
: lisp-eval ( str -- * )
lisp-string>factor call ;
2008-07-02 23:54:06 -04:00
2008-04-30 16:59:50 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-05-05 12:49:16 -04:00
SYMBOL: lisp-env
2008-06-03 23:41:05 -04:00
SYMBOL: macro-env
2008-07-02 23:54:06 -04:00
2008-06-18 12:13:28 -04:00
ERROR: no-such-var variable-name ;
M: no-such-var summary drop "No such variable" ;
2008-04-30 16:59:50 -04:00
2008-05-05 12:49:16 -04:00
: init-env ( -- )
2008-06-03 23:41:05 -04:00
H{ } clone lisp-env set
H{ } clone macro-env set ;
2008-04-30 16:59:50 -04:00
2008-06-05 18:15:05 -04:00
: lisp-define ( quot name -- )
lisp-env get set-at ;
2008-07-02 23:54:06 -04:00
: defun ( name quot -- name )
over name>> lisp-define ;
2008-07-02 23:54:06 -04:00
2008-05-05 12:49:16 -04:00
: lisp-get ( name -- word )
2008-06-18 15:09:02 -04:00
lisp-env get at ;
2008-07-02 23:54:06 -04:00
2008-05-18 12:53:44 -04:00
: lookup-var ( lisp-symbol -- quot )
2008-06-18 15:09:02 -04:00
[ name>> ] [ lisp-var? ] bi [ lisp-get ] [ no-such-var ] if ;
2008-07-02 23:54:06 -04:00
: lisp-var? ( lisp-symbol -- ? )
dup lisp-symbol? [ name>> lisp-env get key? ] [ drop f ] if ;
2008-07-02 23:54:06 -04:00
2008-05-05 12:49:16 -04:00
: funcall ( quot sym -- * )
2008-06-18 12:13:28 -04:00
[ 1array [ call ] with-datastack >quotation ] dip
dup lisp-symbol? [ lookup-var ] when curry call ; inline
2008-07-02 23:54:06 -04:00
: define-primitive ( name vocab word -- )
2008-06-19 14:32:46 -04:00
swap lookup 1quotation '[ , compose call ] swap lisp-define ;
2008-07-02 23:54:06 -04:00
2008-06-05 18:15:05 -04:00
: lookup-macro ( lisp-symbol -- lambda )
2008-06-03 23:41:05 -04:00
name>> macro-env get at ;
2008-07-02 23:54:06 -04:00
2008-06-05 18:15:05 -04:00
: define-lisp-macro ( quot name -- )
macro-env get set-at ;
2008-07-02 23:54:06 -04:00
2008-06-03 23:41:05 -04:00
: lisp-macro? ( car -- ? )
2008-06-04 12:47:53 -04:00
dup lisp-symbol? [ name>> macro-env get key? ] [ drop f ] if ;