factor/extra/lisp/lisp.factor

138 lines
4.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
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
2008-06-03 23:41:05 -04:00
fry lists inspector ;
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
DEFER: macro-call
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-06-05 04:14:26 -04:00
[ ] [ convert-form compose ] foldl ; inline
2008-04-21 20:25:55 -04:00
: convert-if ( cons -- quot )
2008-06-03 23:41:05 -04:00
cdr 3car [ convert-form ] tri@ '[ @ , , if ] ;
2008-05-26 04:35:18 -04:00
: convert-begin ( cons -- quot )
2008-06-03 23:41:05 -04:00
cdr [ convert-form ] [ ] lmap-as '[ , [ funcall ] each ] ;
2008-05-26 04:35:18 -04:00
: convert-cond ( cons -- quot )
2008-06-03 23:41:05 -04:00
cdr [ 2car [ convert-form ] bi@ [ '[ @ funcall ] ] dip 2array ]
{ } lmap-as '[ , cond ] ;
2008-05-26 04:35:18 -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
! words for convert-lambda
2008-04-24 16:35:42 -04:00
<PRIVATE
: localize-body ( assoc body -- assoc newbody )
2008-06-03 23:41:05 -04:00
dupd [ dup lisp-symbol? [ tuck name>> swap at swap or ]
[ dup cons? [ localize-body ] when nip ] if
2008-06-04 00:58:02 -04:00
] with lmap>array ;
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
2008-06-03 23:41:05 -04:00
[ swap localize-body seq>cons convert-form swap pop-locals ] dip swap ;
2008-05-05 12:49:16 -04:00
2008-06-03 23:41:05 -04:00
: split-lambda ( cons -- body-cons vars-seq )
2008-06-04 00:58:02 -04:00
3car -rot nip [ name>> ] lmap>array ; inline
2008-05-26 04:35:18 -04:00
2008-06-03 23:41:05 -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-06-04 12:47:53 -04:00
: convert-unquoted ( cons -- quot )
"unquote not valid outside of quasiquote!" throw ;
: convert-quasiquoted ( cons -- newcons )
2008-06-05 04:14:26 -04:00
[ { [ dup list? ] [ car dup lisp-symbol? ] [ name>> "unquote" equal? dup ] } && nip ]
[ cadr ] traverse ;
2008-06-04 12:47:53 -04:00
2008-05-30 01:44:54 -04:00
: form-dispatch ( lisp-symbol -- quot )
name>>
{ { "lambda" [ convert-lambda ] }
{ "quote" [ convert-quoted ] }
2008-06-04 12:47:53 -04:00
{ "unquote" [ convert-unquoted ] }
{ "quasiquote" [ convert-quasiquoted ] }
2008-05-30 01:44:54 -04:00
{ "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-06-03 23:41:05 -04:00
SYMBOL: macro-env
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-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-06-03 23:41:05 -04:00
dup lisp-env get at [ ] [ no-such-var ] ?if ;
2008-05-26 04:35:18 -04:00
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-06-03 23:41:05 -04:00
swap lookup 1quotation '[ , compose call ] lisp-define ;
: lookup-macro ( lisp-symbol -- macro )
name>> macro-env get at ;
: lisp-macro? ( car -- ? )
2008-06-04 12:47:53 -04:00
dup lisp-symbol? [ name>> macro-env get key? ] [ drop f ] if ;