factor/extra/lisp/lisp.factor

116 lines
3.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 accessors
2008-05-26 04:35:18 -04:00
vectors syntax lisp.parser assocs parser sequences.lib words quotations
fry lisp.conses ;
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
2008-05-30 01:44:54 -04:00
DEFER: lisp-macro?
DEFER: lookup-macro
2008-04-21 20:25:55 -04:00
2008-04-30 16:59:50 -04:00
! Functions to convert s-exps to quotations
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: convert-body ( cons -- quot )
2008-05-26 04:35:18 -04:00
[ ] [ convert-form compose ] reduce ; inline
2008-04-21 20:25:55 -04:00
: convert-if ( cons -- quot )
2008-05-26 04:35:18 -04:00
rest first3 [ convert-form ] tri@ '[ @ , , if ] ;
: convert-begin ( cons -- quot )
2008-05-26 04:35:18 -04:00
rest [ convert-form ] [ ] map-as '[ , [ funcall ] each ] ;
: convert-cond ( cons -- quot )
2008-05-26 04:35:18 -04:00
rest [ body>> first2 [ convert-form ] bi@ [ '[ @ funcall ] ] dip 2array ]
{ } map-as '[ , cond ] ;
: convert-general-form ( cons -- quot )
uncons convert-form swap convert-body swap '[ , @ funcall ] ;
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 )
2008-05-26 04:35:18 -04:00
[ dup lisp-symbol? [ over dupd [ name>> ] dip at swap or ]
[ dup cons? [ body>> localize-body <s-exp> ] when ] if
] map ;
2008-05-26 04:35:18 -04:00
2008-05-05 12:49:16 -04:00
: localize-lambda ( body vars -- newbody newvars )
2008-05-26 04:35:18 -04:00
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 ( cons -- body vars )
2008-05-26 04:35:18 -04:00
first3 -rot nip [ body>> ] bi@ [ name>> ] map ; inline
2008-05-11 19:38:38 -04:00
: rest-lambda ( body vars -- quot )
2008-05-26 04:35:18 -04:00
"&rest" swap [ index ] [ remove ] 2bi
localize-lambda <lambda>
'[ , cut '[ @ , ] , compose ] ;
2008-05-11 19:38:38 -04:00
: normal-lambda ( body vars -- quot )
2008-05-26 04:35:18 -04:00
localize-lambda <lambda> '[ , compose ] ;
2008-05-15 22:14:53 -04:00
PRIVATE>
2008-05-26 04:35:18 -04:00
: convert-lambda ( cons -- quot )
2008-05-26 04:35:18 -04:00
split-lambda "&rest" over member? [ rest-lambda ] [ normal-lambda ] if ;
: convert-quoted ( cons -- quot )
cdr>> 1quotation ;
2008-05-26 04:35:18 -04:00
2008-05-30 01:44:54 -04:00
: form-dispatch ( lisp-symbol -- quot )
name>>
{ { "lambda" [ convert-lambda ] }
{ "quote" [ convert-quoted ] }
{ "if" [ convert-if ] }
{ "begin" [ convert-begin ] }
{ "cond" [ convert-cond ] }
[ drop convert-general-form ]
} case ;
: macro-expand ( cons -- quot )
uncons lookup-macro macro-call convert-form ;
2008-05-30 01:44:54 -04:00
: convert-list-form ( cons -- quot )
dup car>>
2008-05-30 01:44:54 -04:00
{ { [ dup lisp-macro? ] [ macro-expand ] }
{ [ dup lisp-symbol? ] [ form-dispatch ] }
[ drop convert-general-form ]
} cond ;
2008-05-26 04:35:18 -04:00
2008-04-21 20:25:55 -04:00
: convert-form ( lisp-form -- quot )
{
{ [ dup cons? ] [ convert-list-form ] }
{ [ dup lisp-symbol? ] [ '[ , lookup-var ] ] }
[ 1quotation ]
2008-05-26 04:35:18 -04:00
} cond ;
2008-04-30 16:59:50 -04:00
: lisp-string>factor ( str -- quot )
2008-05-26 04:35:18 -04:00
lisp-expr parse-result-ast convert-form lambda-rewrite call ;
2008-05-26 15:48:22 -04:00
: lisp-eval ( str -- * )
lisp-string>factor call ;
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 ( -- )
2008-05-26 04:35:18 -04:00
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 -- )
2008-05-26 04:35:18 -04:00
swap lisp-env get set-at ;
2008-05-05 12:49:16 -04:00
: lisp-get ( name -- word )
2008-05-26 04:35:18 -04:00
dup lisp-env get at [ ] [ no-such-var throw ] ?if ;
2008-05-18 12:53:44 -04:00
: lookup-var ( lisp-symbol -- quot )
2008-05-26 04:35:18 -04:00
name>> lisp-get ;
2008-05-05 12:49:16 -04:00
: funcall ( quot sym -- * )
2008-05-26 04:35:18 -04:00
dup lisp-symbol? [ lookup-var ] when call ; inline
2008-05-26 15:48:22 -04:00
: define-primitive ( name vocab word -- )
2008-05-26 04:35:18 -04:00
swap lookup 1quotation '[ , compose call ] lisp-define ;