]> gitweb.factorcode.org Git - factor.git/blob - extra/lisp/lisp.factor
Updating code for make and fry changes
[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 summary combinators.short-circuit continuations multiline ;
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: define-lisp-macro
16
17 ! Functions to convert s-exps to quotations
18 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
19 : convert-body ( cons -- quot )
20     [ ] [ convert-form compose ] foldl ; inline
21
22 : convert-cond ( cons -- quot )
23     cdr [ 2car [ convert-form ] bi@ 2array ]
24     { } lmap-as '[ _ cond ] ;
25
26 : convert-general-form ( cons -- quot )
27     uncons [ convert-body ] [ convert-form ] bi* '[ _ @ funcall ] ;
28
29 ! words for convert-lambda
30 <PRIVATE
31 : localize-body ( assoc body -- newbody )
32     {
33       { [ dup list? ] [ [ lisp-symbol? ] rot '[ [ name>> _ at ] [ ] bi or ] traverse ] }
34       { [ dup lisp-symbol? ] [ name>> swap at ] }
35      [ nip ]
36     } cond ;
37
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 ;
42
43 : split-lambda ( cons -- body-cons vars-seq )
44     cdr uncons [ name>> ] lmap>array ; inline
45
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 ;
50
51 : normal-lambda ( body vars -- quot )
52     localize-lambda <lambda> lambda-rewrite '[ @ compose call call ] 1quotation ;
53 PRIVATE>
54
55 : convert-lambda ( cons -- quot )
56     split-lambda "&rest" over member? [ rest-lambda ] [ normal-lambda ] if ;
57
58 : convert-quoted ( cons -- quot )
59     cadr 1quotation ;
60
61 : convert-defmacro ( cons -- quot )
62     cdr [ convert-lambda ] [ car name>> ] bi define-lisp-macro [ ] ;
63
64 : macro-expand ( cons -- quot )
65     uncons [ list>seq >quotation ] [ lookup-macro ] bi* call call ;
66
67 <PRIVATE
68 : (expand-macros) ( cons -- cons )
69     [ dup list? [ (expand-macros) dup car lisp-macro? [ macro-expand ] when ] when ] lmap ;
70 PRIVATE>
71
72 : expand-macros ( cons -- cons )
73     dup list? [ (expand-macros) dup car lisp-macro? [ macro-expand ] when ] when ;
74
75 : convert-begin ( cons -- quot )
76     cdr [ convert-form ] [ ] lmap-as [ 1 tail* ] [ but-last ] bi
77     [ '[ { } _ with-datastack drop ] ] map prepend '[ _ [ call ] each ] ;
78
79 : form-dispatch ( cons lisp-symbol -- quot )
80     name>>
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 ]
87     } case ;
88
89 : convert-list-form ( cons -- quot )
90     dup car
91     {
92       { [ dup lisp-symbol? ] [ form-dispatch ] }
93      [ drop convert-general-form ]
94     } cond ;
95
96 : convert-form ( lisp-form -- quot )
97     {
98       { [ dup cons? ] [ convert-list-form ] }
99       { [ dup lisp-var? ] [ lookup-var 1quotation ] }
100       { [ dup lisp-symbol? ] [ '[ _ lookup-var ] ] }
101      [ 1quotation ]
102     } cond ;
103
104 : lisp-string>factor ( str -- quot )
105     lisp-expr expand-macros convert-form ;
106
107 : lisp-eval ( str -- * )
108     lisp-string>factor call ;
109
110 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
111
112 SYMBOL: lisp-env
113 SYMBOL: macro-env
114
115 ERROR: no-such-var variable-name ;
116 M: no-such-var summary drop "No such variable" ;
117
118 : init-env ( -- )
119     H{ } clone lisp-env set
120     H{ } clone macro-env set ;
121
122 : lisp-define ( quot name -- )
123     lisp-env get set-at ;
124     
125 : define-lisp-var ( lisp-symbol body --  )
126     swap name>> lisp-define ;
127
128 : lisp-get ( name -- word )
129     lisp-env get at ;
130
131 : lookup-var ( lisp-symbol -- quot )
132     [ name>> ] [ lisp-var? ] bi [ lisp-get ] [ no-such-var ] if ;
133
134 : lisp-var? ( lisp-symbol -- ? )
135     dup lisp-symbol? [ name>> lisp-env get key? ] [ drop f ] if ;
136
137 : funcall ( quot sym -- * )
138     [ 1array [ call ] with-datastack >quotation ] dip curry call ; inline
139
140 : define-primitive ( name vocab word -- )
141     swap lookup 1quotation '[ _ compose call ] swap lisp-define ;
142
143 : lookup-macro ( lisp-symbol -- lambda )
144     name>> macro-env get at ;
145
146 : define-lisp-macro ( quot name -- )
147     macro-env get set-at ;
148
149 : lisp-macro? ( car -- ? )
150     dup lisp-symbol? [ name>> macro-env get key? ] [ drop f ] if ;
151
152 : define-lisp-builtins ( -- )
153    init-env
154
155    f "#f" lisp-define
156    t "#t" lisp-define
157
158    "+" "math" "+" define-primitive
159    "-" "math" "-" define-primitive
160    "<" "math" "<" define-primitive
161    ">" "math" ">" define-primitive
162
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
169
170    "set" "lisp" "define-lisp-var" define-primitive
171     
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
174     
175    <" (defmacro defun (name vars &rest body)
176         (list (quote setq) name (list (quote lambda) vars body))) "> lisp-eval
177     
178    "(defmacro if (pred tr fl) (list (quote cond) (list pred tr) (list (quote #t) fl)))" lisp-eval
179    ;
180
181 : <LISP 
182     "LISP>" parse-multiline-string define-lisp-builtins
183     lisp-string>factor parsed \ call parsed ; parsing