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 summary combinators.short-circuit continuations multiline ;
15 DEFER: define-lisp-macro
17 ! Functions to convert s-exps to quotations
18 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
19 : convert-body ( cons -- quot )
20 [ ] [ convert-form compose ] foldl ; inline
22 : convert-cond ( cons -- quot )
23 cdr [ 2car [ convert-form ] bi@ 2array ]
24 { } lmap-as '[ _ cond ] ;
26 : convert-general-form ( cons -- quot )
27 uncons [ convert-body ] [ convert-form ] bi* '[ _ @ funcall ] ;
29 ! words for convert-lambda
31 : localize-body ( assoc body -- newbody )
33 { [ dup list? ] [ [ lisp-symbol? ] rot '[ [ name>> _ at ] [ ] bi or ] traverse ] }
34 { [ dup lisp-symbol? ] [ name>> swap at ] }
38 : localize-lambda ( body vars -- newvars newbody )
39 swap [ make-locals dup push-locals ] dip
40 dupd [ localize-body convert-form ] with lmap>array
41 >quotation swap pop-locals ;
43 : split-lambda ( cons -- body-cons vars-seq )
44 cdr uncons [ name>> ] lmap>array ; inline
46 : rest-lambda ( body vars -- quot )
47 "&rest" swap [ remove ] [ index ] 2bi
48 [ localize-lambda <lambda> lambda-rewrite call ] dip
49 swap '[ _ cut '[ @ _ seq>list ] call _ call call ] 1quotation ;
51 : normal-lambda ( body vars -- quot )
52 localize-lambda <lambda> lambda-rewrite '[ @ compose call call ] 1quotation ;
55 : convert-lambda ( cons -- quot )
56 split-lambda "&rest" over member? [ rest-lambda ] [ normal-lambda ] if ;
58 : convert-quoted ( cons -- quot )
61 : convert-defmacro ( cons -- quot )
62 cdr [ convert-lambda ] [ car name>> ] bi define-lisp-macro [ ] ;
64 : macro-expand ( cons -- quot )
65 uncons [ list>seq >quotation ] [ lookup-macro ] bi* call call ;
68 : (expand-macros) ( cons -- cons )
69 [ dup list? [ (expand-macros) dup car lisp-macro? [ macro-expand ] when ] when ] lmap ;
72 : expand-macros ( cons -- cons )
73 dup list? [ (expand-macros) dup car lisp-macro? [ macro-expand ] when ] when ;
75 : convert-begin ( cons -- quot )
76 cdr [ convert-form ] [ ] lmap-as [ 1 tail* ] [ but-last ] bi
77 [ '[ { } _ with-datastack drop ] ] map prepend '[ _ [ call ] each ] ;
79 : form-dispatch ( cons lisp-symbol -- quot )
81 { { "lambda" [ convert-lambda ] }
82 { "defmacro" [ convert-defmacro ] }
83 { "quote" [ convert-quoted ] }
84 { "cond" [ convert-cond ] }
85 { "begin" [ convert-begin ] }
86 [ drop convert-general-form ]
89 : convert-list-form ( cons -- quot )
92 { [ dup lisp-symbol? ] [ form-dispatch ] }
93 [ drop convert-general-form ]
96 : convert-form ( lisp-form -- quot )
98 { [ dup cons? ] [ convert-list-form ] }
99 { [ dup lisp-var? ] [ lookup-var 1quotation ] }
100 { [ dup lisp-symbol? ] [ '[ _ lookup-var ] ] }
104 : lisp-string>factor ( str -- quot )
105 lisp-expr expand-macros convert-form ;
107 : lisp-eval ( str -- * )
108 lisp-string>factor call ;
110 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
115 ERROR: no-such-var variable-name ;
116 M: no-such-var summary drop "No such variable" ;
119 H{ } clone lisp-env set
120 H{ } clone macro-env set ;
122 : lisp-define ( quot name -- )
123 lisp-env get set-at ;
125 : define-lisp-var ( lisp-symbol body -- )
126 swap name>> lisp-define ;
128 : lisp-get ( name -- word )
131 : lookup-var ( lisp-symbol -- quot )
132 [ name>> ] [ lisp-var? ] bi [ lisp-get ] [ no-such-var ] if ;
134 : lisp-var? ( lisp-symbol -- ? )
135 dup lisp-symbol? [ name>> lisp-env get key? ] [ drop f ] if ;
137 : funcall ( quot sym -- * )
138 [ 1array [ call ] with-datastack >quotation ] dip curry call ; inline
140 : define-primitive ( name vocab word -- )
141 swap lookup 1quotation '[ _ compose call ] swap lisp-define ;
143 : lookup-macro ( lisp-symbol -- lambda )
144 name>> macro-env get at ;
146 : define-lisp-macro ( quot name -- )
147 macro-env get set-at ;
149 : lisp-macro? ( car -- ? )
150 dup lisp-symbol? [ name>> macro-env get key? ] [ drop f ] if ;
152 : define-lisp-builtins ( -- )
158 "+" "math" "+" define-primitive
159 "-" "math" "-" define-primitive
160 "<" "math" "<" define-primitive
161 ">" "math" ">" define-primitive
163 "cons" "lists" "cons" define-primitive
164 "car" "lists" "car" define-primitive
165 "cdr" "lists" "cdr" define-primitive
166 "append" "lists" "lappend" define-primitive
167 "nil" "lists" "nil" define-primitive
168 "nil?" "lists" "nil?" define-primitive
170 "set" "lisp" "define-lisp-var" define-primitive
172 "(lambda (&rest xs) xs)" lisp-string>factor first "list" lisp-define
173 "(defmacro setq (var val) (list (quote set) (list (quote quote) var) val))" lisp-eval
175 <" (defmacro defun (name vars &rest body)
176 (list (quote setq) name (list (quote lambda) vars body))) "> lisp-eval
178 "(defmacro if (pred tr fl) (list (quote cond) (list pred tr) (list (quote #t) fl)))" lisp-eval
182 "LISP>" parse-multiline-string define-lisp-builtins
183 lisp-string>factor parsed \ call parsed ; parsing