1 ! Copyright (C) 2008 James Cash
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel peg sequences arrays strings combinators.lib
4 namespaces combinators math locals locals.private locals.backend accessors
5 vectors syntax lisp.parser assocs parser sequences.lib words
6 quotations fry lists inspector combinators.short-circuit ;
16 DEFER: define-lisp-macro
18 ERROR: no-such-var variable-name ;
19 M: no-such-var summary drop "No such variable" ;
21 ! Functions to convert s-exps to quotations
22 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23 : convert-body ( cons -- quot )
24 [ ] [ convert-form compose ] foldl ; inline
26 : convert-begin ( cons -- quot )
27 cdr [ convert-form ] [ ] lmap-as '[ , [ call ] each ] ;
29 : convert-cond ( cons -- quot )
30 cdr [ 2car [ convert-form ] bi@ [ '[ @ call ] ] dip 2array ]
31 { } lmap-as '[ , cond ] ;
33 : convert-general-form ( cons -- quot )
34 uncons [ convert-body ] [ convert-form ] bi* '[ , @ funcall ] ;
36 ! words for convert-lambda
38 : localize-body ( assoc body -- assoc newbody )
40 { [ dup list? ] [ [ lisp-symbol? ] pick '[ [ name>> , at ] [ ] bi or ] traverse ] }
41 { [ dup lisp-symbol? ] [ name>> over at ] }
45 : localize-lambda ( body vars -- newvars newbody )
46 make-locals dup push-locals swap
47 [ swap localize-body convert-form swap pop-locals ] dip swap ;
49 : split-lambda ( cons -- body-cons vars-seq )
50 cdr uncons [ car ] [ [ name>> ] lmap>array ] bi* ; inline
52 : rest-lambda ( body vars -- quot )
53 "&rest" swap [ index ] [ remove ] 2bi
54 swapd localize-lambda <lambda>
55 '[ , cut '[ @ , seq>list ] call , call ] ;
57 : normal-lambda ( body vars -- quot )
58 localize-lambda <lambda> lambda-rewrite [ compose call ] compose 1quotation ;
61 : convert-lambda ( cons -- quot )
62 split-lambda "&rest" over member? [ rest-lambda ] [ normal-lambda ] if ;
64 : convert-quoted ( cons -- quot )
67 : convert-defmacro ( cons -- quot )
68 cdr [ car ] keep [ convert-lambda ] [ car name>> ] bi define-lisp-macro 1quotation ;
70 : form-dispatch ( cons lisp-symbol -- quot )
72 { { "lambda" [ convert-lambda ] }
73 { "defmacro" [ convert-defmacro ] }
74 { "quote" [ convert-quoted ] }
75 { "begin" [ convert-begin ] }
76 { "cond" [ convert-cond ] }
77 [ drop convert-general-form ]
80 : convert-list-form ( cons -- quot )
82 { { [ dup lisp-macro? ] [ drop macro-expand ] }
83 { [ dup lisp-symbol? ] [ form-dispatch ] }
84 [ drop convert-general-form ]
87 : convert-form ( lisp-form -- quot )
89 { [ dup cons? ] [ convert-list-form ] }
90 { [ dup lisp-var? ] [ lookup-var 1quotation ] }
91 { [ dup lisp-symbol? ] [ '[ , lookup-var ] ] }
95 : compile-form ( lisp-ast -- quot )
96 convert-form lambda-rewrite call ; inline
98 : macro-expand ( cons -- quot )
99 uncons [ list>seq [ ] like ] [ lookup-macro lambda-rewrite call ] bi* call compile-form call ;
101 : lisp-string>factor ( str -- quot )
102 lisp-expr parse-result-ast compile-form ;
104 : lisp-eval ( str -- * )
105 lisp-string>factor call ;
107 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
113 H{ } clone lisp-env set
114 H{ } clone macro-env set ;
116 : lisp-define ( quot name -- )
117 lisp-env get set-at ;
119 : defun ( name quot -- name )
120 over name>> lisp-define ;
122 : lisp-get ( name -- word )
123 dup lisp-env get at [ ] [ no-such-var ] ?if ;
125 : lookup-var ( lisp-symbol -- quot )
128 : lisp-var? ( lisp-symbol -- ? )
129 dup lisp-symbol? [ name>> lisp-env get key? ] [ drop f ] if ;
131 : funcall-arg-list ( args -- newargs )
132 [ ] [ dup \ funcall = [ drop 2 cut* [ funcall ] compose call ] when suffix ] reduce ;
134 : funcall ( quot sym -- * )
135 [ funcall-arg-list ] dip
136 dup lisp-symbol? [ lookup-var ] when curry call ; inline
138 : define-primitive ( name vocab word -- )
139 swap lookup 1quotation '[ , compose call ] swap lisp-define ; ! '[ , compose call ] swap lisp-define ;
141 : lookup-macro ( lisp-symbol -- lambda )
142 name>> macro-env get at ;
144 : define-lisp-macro ( quot name -- )
145 macro-env get set-at ;
147 : lisp-macro? ( car -- ? )
148 dup lisp-symbol? [ name>> macro-env get key? ] [ drop f ] if ;