factor/extra/lisp/lisp.factor

98 lines
3.0 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
2008-05-05 12:49:16 -04:00
namespaces combinators math bake locals locals.private accessors
2008-05-15 22:14:53 -04:00
vectors syntax lisp.parser assocs parser sequences.lib words ;
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:00:03 -04:00
DEFER: lookup-vars
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 )
2008-05-05 12:49:16 -04:00
[ convert-form ] map [ ] [ 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-05-05 12:49:16 -04:00
rest convert-form ;
2008-04-24 16:35:42 -04:00
2008-04-26 17:20:12 -04:00
: convert-cond ( s-exp -- quot )
2008-05-05 12:49:16 -04:00
rest [ [ convert-form map ] map ] [ % cond ] bake ;
2008-04-24 16:35:42 -04:00
: convert-general-form ( s-exp -- quot )
2008-05-18 12:00:03 -04:00
unclip convert-form swap convert-body [ , lookup-vars % funcall ] bake ;
2008-05-15 22:14:53 -04:00
! words for convert-lambda
2008-04-24 16:35:42 -04:00
<PRIVATE
: localize-body ( assoc body -- assoc newbody )
[ dup lisp-symbol? [ over dupd [ name>> ] dip at swap or ]
[ dup s-exp? [ body>> localize-body <s-exp> ] when ] if
] map ;
2008-05-05 12:49:16 -04:00
: localize-lambda ( body vars -- newbody newvars )
make-locals dup push-locals swap
[ swap localize-body <s-exp> convert-form swap pop-locals ] dip swap ;
2008-05-05 12:49:16 -04:00
: split-lambda ( s-exp -- body vars )
2008-05-11 19:38:38 -04:00
first3 -rot nip [ body>> ] bi@ [ name>> ] map ; inline
2008-05-05 12:49:16 -04:00
2008-05-11 19:38:38 -04:00
: rest-lambda ( body vars -- quot )
"&rest" swap [ remove ] [ index ] 2bi
[ localize-lambda <lambda> ] dip
2008-05-18 12:00:03 -04:00
[ , lookup-vars cut swap [ % , ] bake , compose ] bake ;
2008-05-11 19:38:38 -04:00
: normal-lambda ( body vars -- quot )
2008-05-18 12:00:03 -04:00
localize-lambda <lambda> [ lookup-vars , compose ] bake ;
2008-05-15 22:14:53 -04:00
PRIVATE>
2008-04-24 16:35:42 -04:00
2008-04-26 17:20:12 -04:00
: convert-lambda ( s-exp -- quot )
2008-05-11 19:38:38 -04:00
split-lambda dup "&rest" swap member? [ rest-lambda ] [ normal-lambda ] if ;
2008-04-24 16:35:42 -04:00
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 )
2008-05-11 19:38:38 -04:00
dup s-exp? [ body>> convert-list-form ]
2008-05-15 22:14:53 -04:00
[ [ , ] bake ] if ;
2008-05-11 19:38:38 -04:00
2008-04-30 16:59:50 -04:00
: lisp-string>factor ( str -- quot )
2008-05-12 00:34:10 -04:00
lisp-expr parse-result-ast convert-form lambda-rewrite call ;
2008-05-05 12:49:16 -04:00
2008-04-30 16:59:50 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-05-05 12:49:16 -04:00
SYMBOL: lisp-env
ERROR: no-such-var var ;
2008-04-30 16:59:50 -04:00
2008-05-05 12:49:16 -04:00
: init-env ( -- )
H{ } clone lisp-env set ;
2008-04-30 16:59:50 -04:00
2008-05-05 12:49:16 -04:00
: lisp-define ( name quot -- )
swap lisp-env get set-at ;
2008-04-30 16:59:50 -04:00
2008-05-05 12:49:16 -04:00
: lisp-get ( name -- word )
2008-05-15 22:14:53 -04:00
dup lisp-env get at [ ] [ no-such-var throw ] ?if ;
2008-04-30 16:59:50 -04:00
2008-05-05 12:49:16 -04:00
: funcall ( quot sym -- * )
2008-05-15 22:14:53 -04:00
dup lisp-symbol? [ name>> lisp-get ] when call ; inline
2008-05-18 12:00:03 -04:00
: lookup-vars ( q -- p )
[ dup lisp-symbol? [ name>> lisp-get ] when ] map ;
2008-05-15 22:14:53 -04:00
: define-primitve ( name vocab word -- )
swap lookup [ [ , ] compose call ] bake lisp-define ;