]> gitweb.factorcode.org Git - factor.git/blob - extra/lisp/lisp.factor
109083de378d0e6e24c9336840214193e4b4c1bf
[factor.git] / extra / lisp / lisp.factor
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 ;
7 IN: lisp
8
9 DEFER: convert-form
10 DEFER: funcall
11 DEFER: lookup-var
12 DEFER: lookup-macro
13 DEFER: lisp-macro?
14 DEFER: lisp-var?
15 DEFER: macro-expand
16 DEFER: define-lisp-macro
17     
18 ERROR: no-such-var variable-name ;
19 M: no-such-var summary drop "No such variable" ;
20     
21 ! Functions to convert s-exps to quotations
22 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23 : convert-body ( cons -- quot )
24     [ ] [ convert-form compose ] foldl ; inline
25     
26 : convert-begin ( cons -- quot )  
27     cdr [ convert-form ] [ ] lmap-as '[ , [ call ] each ] ;
28     
29 : convert-cond ( cons -- quot )  
30     cdr [ 2car [ convert-form ] bi@ [ '[ @ call ] ] dip 2array ]
31     { } lmap-as '[ , cond ]  ;
32     
33 : convert-general-form ( cons -- quot )
34     uncons [ convert-body ] [ convert-form ] bi* '[ , @ funcall ] ;
35
36 ! words for convert-lambda  
37 <PRIVATE  
38 : localize-body ( assoc body -- assoc newbody )  
39     {
40       { [ dup list? ] [ [ lisp-symbol? ] pick '[ [ name>> , at ] [ ] bi or ] traverse ] }
41       { [ dup lisp-symbol? ] [ name>> over at ] }
42      [ ]
43     } cond ;
44
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 ;
48                    
49 : split-lambda ( cons -- body-cons vars-seq )
50     cdr uncons [ car ] [ [ name>> ] lmap>array ] bi* ; inline
51     
52 : rest-lambda ( body vars -- quot )
53     "&rest" swap [ index ] [ remove ] 2bi
54     swapd localize-lambda <lambda>
55     '[ , cut '[ @ , seq>list ] call , call ] ;
56     
57 : normal-lambda ( body vars -- quot )
58     localize-lambda <lambda> lambda-rewrite [ compose call ] compose 1quotation ;
59 PRIVATE>
60     
61 : convert-lambda ( cons -- quot )  
62     split-lambda "&rest" over member? [ rest-lambda ] [ normal-lambda ] if ;
63     
64 : convert-quoted ( cons -- quot )  
65     cadr 1quotation ;
66     
67 : convert-defmacro ( cons -- quot )
68     cdr [ car ] keep [ convert-lambda ] [ car name>> ] bi define-lisp-macro 1quotation ;
69     
70 : form-dispatch ( cons lisp-symbol -- quot )
71     name>>
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 ]
78     } case ;
79     
80 : convert-list-form ( cons -- quot )  
81     dup car
82     { { [ dup lisp-macro?  ] [ drop macro-expand ] }
83       { [ dup lisp-symbol? ] [ form-dispatch ] } 
84      [ drop convert-general-form ]
85     } cond ;
86     
87 : convert-form ( lisp-form -- quot )
88     {
89       { [ dup cons? ] [ convert-list-form ] }
90       { [ dup lisp-var? ] [ lookup-var 1quotation ] }
91       { [ dup lisp-symbol? ] [ '[ , lookup-var ] ] }
92      [ 1quotation ]
93     } cond ;
94     
95 : compile-form ( lisp-ast -- quot )
96     convert-form lambda-rewrite call ; inline
97     
98 : macro-expand ( cons -- quot )
99     uncons [ list>seq [ ] like ] [ lookup-macro lambda-rewrite call ] bi* call compile-form call ;
100     
101 : lisp-string>factor ( str -- quot )
102     lisp-expr parse-result-ast compile-form ;
103     
104 : lisp-eval ( str -- * )    
105   lisp-string>factor call ;
106     
107 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
108
109 SYMBOL: lisp-env
110 SYMBOL: macro-env
111
112 : init-env ( -- )
113     H{ } clone lisp-env set
114     H{ } clone macro-env set ;
115
116 : lisp-define ( quot name -- )
117     lisp-env get set-at ;
118     
119 : defun ( name quot -- name )    
120     over name>> lisp-define ;
121     
122 : lisp-get ( name -- word )
123     dup lisp-env get at [ ] [ no-such-var ] ?if ;
124     
125 : lookup-var ( lisp-symbol -- quot )
126     name>> lisp-get ;
127     
128 : lisp-var? ( lisp-symbol -- ? )
129     dup lisp-symbol? [ name>> lisp-env get key? ] [ drop f ] if ;
130     
131 : funcall-arg-list ( args -- newargs )    
132     [ ] [ dup \ funcall = [ drop 2 cut* [ funcall ] compose call ] when suffix ] reduce ;
133     
134 : funcall ( quot sym -- * )
135     [ funcall-arg-list ] dip
136     dup lisp-symbol? [ lookup-var ] when curry call ; inline
137     
138 : define-primitive ( name vocab word -- )  
139     swap lookup 1quotation '[ , compose call ] swap lisp-define ; ! '[ , compose call ] swap lisp-define ;
140     
141 : lookup-macro ( lisp-symbol -- lambda )
142     name>> macro-env get at ;
143     
144 : define-lisp-macro ( quot name -- )
145     macro-env get set-at ;
146     
147 : lisp-macro? ( car -- ? )
148     dup lisp-symbol? [ name>> macro-env get key? ] [ drop f ] if ;