1 ! Copyright (C) 2008 James Cash
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel peg sequences arrays strings
4 namespaces combinators math locals locals.private locals.backend accessors
5 vectors syntax lisp.parser assocs parser 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 ;
67 : expand-macros ( cons -- cons )
68 dup list? [ [ expand-macros ] lmap dup car lisp-macro? [ macro-expand expand-macros ] when ] when ;
70 : convert-begin ( cons -- quot )
71 cdr [ convert-form ] [ ] lmap-as [ 1 tail* ] [ but-last ] bi
72 [ '[ { } _ with-datastack drop ] ] map prepend '[ _ [ call ] each ] ;
74 : form-dispatch ( cons lisp-symbol -- quot )
76 { { "lambda" [ convert-lambda ] }
77 { "defmacro" [ convert-defmacro ] }
78 { "quote" [ convert-quoted ] }
79 { "cond" [ convert-cond ] }
80 { "begin" [ convert-begin ] }
81 [ drop convert-general-form ]
84 : convert-list-form ( cons -- quot )
87 { [ dup lisp-symbol? ] [ form-dispatch ] }
88 [ drop convert-general-form ]
91 : convert-form ( lisp-form -- quot )
93 { [ dup cons? ] [ convert-list-form ] }
94 { [ dup lisp-var? ] [ lookup-var 1quotation ] }
95 { [ dup lisp-symbol? ] [ '[ _ lookup-var ] ] }
99 : lisp-string>factor ( str -- quot )
100 lisp-expr expand-macros convert-form ;
102 : lisp-eval ( str -- * )
103 lisp-string>factor call ;
105 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
110 ERROR: no-such-var variable-name ;
111 M: no-such-var summary drop "No such variable" ;
114 H{ } clone lisp-env set
115 H{ } clone macro-env set ;
117 : lisp-define ( quot name -- )
118 lisp-env get set-at ;
120 : define-lisp-var ( lisp-symbol body -- )
121 swap name>> lisp-define ;
123 : lisp-get ( name -- word )
126 : lookup-var ( lisp-symbol -- quot )
127 [ name>> ] [ lisp-var? ] bi [ lisp-get ] [ no-such-var ] if ;
129 : lisp-var? ( lisp-symbol -- ? )
130 dup lisp-symbol? [ name>> lisp-env get key? ] [ drop f ] if ;
132 : funcall ( quot sym -- * )
133 [ 1array [ call ] with-datastack >quotation ] dip curry call ; inline
135 : define-primitive ( name vocab word -- )
136 swap lookup 1quotation '[ _ compose call ] swap lisp-define ;
138 : lookup-macro ( lisp-symbol -- lambda )
139 name>> macro-env get at ;
141 : define-lisp-macro ( quot name -- )
142 macro-env get set-at ;
144 : lisp-macro? ( car -- ? )
145 dup lisp-symbol? [ name>> macro-env get key? ] [ drop f ] if ;
147 : define-lisp-builtins ( -- )
153 "+" "math" "+" define-primitive
154 "-" "math" "-" define-primitive
155 "<" "math" "<" define-primitive
156 ">" "math" ">" define-primitive
158 "cons" "lists" "cons" define-primitive
159 "car" "lists" "car" define-primitive
160 "cdr" "lists" "cdr" define-primitive
161 "append" "lists" "lappend" define-primitive
162 "nil" "lists" "nil" define-primitive
163 "nil?" "lists" "nil?" define-primitive
165 "set" "lisp" "define-lisp-var" define-primitive
167 "(set 'list (lambda (&rest xs) xs))" lisp-eval
168 "(defmacro setq (var val) (list 'set (list 'quote var) val))" lisp-eval
170 <" (defmacro defun (name vars &rest body)
171 (list 'setq name (cons 'lambda (cons vars body)))) "> lisp-eval
173 "(defmacro if (pred tr fl) (list 'cond (list pred tr) (list (quote #t) fl)))" lisp-eval
177 "LISP>" parse-multiline-string "(begin " prepend ")" append define-lisp-builtins
178 lisp-string>factor parsed \ call parsed ; parsing