Repository navigation
Expand file tree
/
Copy pathanalyzer.pxi
More file actions
199 lines (167 loc) · 5.35 KB
/
Copy pathanalyzer.pxi
File metadata and controls
199 lines (167 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
(ns pixie.code-generation.analyzer)
(def *env* nil)
(set-dynamic! (var *env*))
(defmulti analyze-form (fn [x]
(cond
(nil? x) nil
(seq? x) :seq
(vector? x) :vector
(symbol? x) :symbol
(number? x) :number)))
(defmulti analyze-seq (fn [x]
(let [f (first x)]
(if (symbol? f)
f
:invoke))))
;; Special Forms
(defmethod analyze-seq 'do
[x]
{:op :do
:children '[:statements :ret]
:env *env*
:form x
:statements (mapv analyze-form (butlast (next x)))
:ret (analyze-form (last x))})
(defmethod analyze-seq 'if
[[_ test then else :as form]]
{:op :if
:children '[:test :then :else]
:env *env*
:form form
:test (analyze-form test)
:then (analyze-form then)
:else (analyze-form else)})
(defmethod analyze-seq 'fn*
[[_ & body :as form]]
(let [[name body] (if (symbol? (first body))
[(first body)
(next body)]
[(gensym "fn_")
body])
arities (if (vector? (first body))
[body]
body)
analyzed-bodies (reduce
analyze-fn-body
{}
arities)]
{:op :fn
:env *env*
:form form
:name name
:children '[:arities]
:arities (vals analyzed-bodies)}
))
(defn analyze-fn-body [acc [args & body]]
; TODO: Add support for variadic fns
(let [arity (count args)
new-env (reduce
(fn [acc idx]
(let [arg-name (nth args idx)]
(assoc-in acc [:locals arg-name] {:op :binding
:type :arg
:idx idx
:name arg-name
:form arg-name
:env *env*})))
*env*
(range (count args)))]
(assert (not (acc arity)) (str "Duplicate arity for " (cons args body)))
(assoc acc arity {:op :fn-body
:env *env*
:arity arity
:args args
:children '[:body]
:body (binding [*env* new-env]
(analyze-form (cons 'do body)))})))
(defmethod analyze-seq 'let*
[[_ bindings & body :as form]]
(assert (even? (count bindings)) "Let requires an even number of bindings")
(let [parted (partition 2 bindings)
[new-env bindings] (reduce
(fn [[new-env bindings] [name binding-form]]
(let [binding-ast (binding [*env* new-env]
{:op :binding
:type :let
:children [:value]
:form binding-form
:env *env*
:name name
:value (analyze-form binding-form)})]
[(assoc-in new-env [:locals name] binding-ast)
(conj bindings binding-ast)]))
[*env* []]
parted)]
{:op :let
:form form
:children [:bindings :body]
:bindings bindings
:env *env*
:body (binding [*env* new-env]
(analyze-form `(do ~@body)))}))
(defmethod analyze-seq 'def
[[_ nm val :as form]]
{:op :def
:name nm
:form form
:env *env*
:children [:val]
:val (analyze-form val)})
(defmethod analyze-form nil
[_]
{:op :const
:env *env*
:form nil})
(defmethod analyze-seq :default
[[sym & args :as form]]
(let [resolved (resolve-in (the-ns (:ns *env*)) sym)]
(if (and resolved
(macro? @resolved))
(analyze-form (apply @resolved args))
{:op :invoke
:children '[:fn :args]
:form form
:env *env*
:fn (analyze-form sym)
:args (mapv analyze-form args)})))
(defmethod analyze-form :number
[x]
{:op :const
:form x
:env *env*})
(defmethod analyze-form :seq
[x]
(analyze-seq x))
(defmethod analyze-form :symbol
[x]
(if-let [local (get-in *env* [:locals x])]
local
{:op :global
:env *env*
:form x}))
;; ENV Functions
(defn new-env
"Creates a new (empty) environment"
[]
{:ns 'user})
(defn analyze [form]
(binding [*env* (new-env)]
(analyze-form form)))
(defn walk [post pre selector node]
(-> (reduce
(fn [node k]
(let [v (get node k)
result (if (or (vector? v)
(seq? v))
(mapv (partial walk post pre selector) v)
(walk post pre selector v))]
(assoc node k result)))
(pre node)
(selector node))
post))
(defn remove-env [ast]
(walk #(dissoc % :env)
identity
:children
ast))
(remove-env (analyze '(do 1 1 2)))