Mercurial > repos > rhope
comparison parse.rhope @ 2:73e978d590c7
Adding WIP compiler code
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 29 Apr 2009 02:58:03 -0400 |
parents | |
children | 0c5f24b4f69d |
comparison
equal
deleted
inserted
replaced
1:b3f71490858c | 2:73e978d590c7 |
---|---|
1 | |
2 Blueprint Error | |
3 { | |
4 Message | |
5 Line | |
6 Column | |
7 } | |
8 | |
9 Error[msg,text:out] | |
10 { | |
11 out <- [[[Build["Error"]]Message <<[msg]]Line << [ [[text]Line >>]+[1] ]]Column << [ [[text]Column >>]+[1] ] | |
12 } | |
13 | |
14 To String@Error[error:out] | |
15 { | |
16 out <- [[[[[error]Message >>]Append[" on line "]]Append[[error]Line >>]]Append[" at column "]]Append[[error]Column >>] | |
17 } | |
18 | |
19 Blueprint PImport Node | |
20 { | |
21 File | |
22 } | |
23 | |
24 PImport Node[file:out] | |
25 { | |
26 out <- [Build["PImport Node"]]File <<[file] | |
27 } | |
28 | |
29 Blueprint Worker Node | |
30 { | |
31 Name | |
32 Blueprint | |
33 Inputs | |
34 Outputs | |
35 Tree | |
36 } | |
37 | |
38 Add Node Input@Worker Node[node,input:out] | |
39 { | |
40 Print["Add Node Input"] | |
41 Print[["Input: "]Append[To String[input]]] | |
42 out <- [node]Inputs <<[[[node]Inputs >>]Append[input]] | |
43 } | |
44 | |
45 Add Node Output@Worker Node[node,output:out] | |
46 { | |
47 out <- [node]Outputs <<[[[node]Outputs >>]Append[output]] | |
48 } | |
49 | |
50 Body[node,tokens,current,depth:out,out index, done] | |
51 { | |
52 Print[["Body: Depth="]Append[depth]] | |
53 If[[depth] > [0]] | |
54 { | |
55 token <- [tokens]Index[current] | |
56 [token]Type Match["Block Begin"] | |
57 { | |
58 ,end stream <- [tokens]Next[current] | |
59 { | |
60 out,out index, done <- Body[node,tokens,~,[depth]+[1]] | |
61 } | |
62 }{ | |
63 [token]Type Match["Block End"] | |
64 { | |
65 [tokens]Next[current] | |
66 { | |
67 out,out index, done <- Body[node,tokens,~,[depth]-[1]] | |
68 }{ | |
69 ,end stream <- If[[depth] = [1]] | |
70 { | |
71 Print["done"] | |
72 out <- Val[node] | |
73 done <- Yes | |
74 } | |
75 } | |
76 }{ | |
77 ,end stream <- [tokens]Next[current] | |
78 { | |
79 out,out index, done <- Body[node,tokens,~,depth] | |
80 } | |
81 } | |
82 } | |
83 }{ | |
84 out <- node | |
85 out index <- current | |
86 } | |
87 Val[end stream] | |
88 { | |
89 Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
90 } | |
91 } | |
92 | |
93 Before Body[node,tokens,current:out,out index, done] | |
94 { | |
95 Print["Before body"] | |
96 token <- [tokens]Index[current] | |
97 next,end stream <- [tokens]Next[current] | |
98 { | |
99 [token]Type Match["Block Begin"] | |
100 { | |
101 out,out index, done <- Body[node,tokens,next,1] | |
102 }{ | |
103 out,out index, done <- Before Body[node,tokens,next] | |
104 } | |
105 } | |
106 Val[end stream] | |
107 { | |
108 Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
109 } | |
110 } | |
111 | |
112 Outputs[node,tokens,current:out,out index, done] | |
113 { | |
114 Print["outputs"] | |
115 token <- [tokens]Index[current] | |
116 next,end stream <- [tokens]Next[current] | |
117 { | |
118 [token]Type Match["Symbol"] | |
119 { | |
120 out,out index, done <- Outputs[[node]Add Node Output[[token]Text >>], tokens, next] | |
121 }{ | |
122 [token]Type Match["Args End"] | |
123 { | |
124 out,out index, done <- Before Body[node, tokens, next] | |
125 }{ | |
126 [token]Type Match[("List Separator","Block Comment","Line Comment","Newline")] | |
127 { | |
128 out,out index, done <- Outputs[node, tokens, next] | |
129 }{ | |
130 Print[To String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
131 } | |
132 } | |
133 } | |
134 } | |
135 Val[end stream] | |
136 { | |
137 Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
138 } | |
139 } | |
140 | |
141 Inputs[node,tokens,current:out,out index, done] | |
142 { | |
143 Print["Inputs"] | |
144 token <- [tokens]Index[current] | |
145 next,end stream <- [tokens]Next[current] | |
146 { | |
147 [token]Type Match["Symbol"] | |
148 { | |
149 | |
150 out,out index, done <- Inputs[[node]Add Node Input[[token]Text >>], tokens, next] | |
151 }{ | |
152 [token]Type Match["Name Separator"] | |
153 { | |
154 Print["in out sep"] | |
155 out,out index, done <- Outputs[node, tokens, next] | |
156 }{ | |
157 [token]Type Match["Args End"] | |
158 { | |
159 out,out index, done <- Before Body[node, tokens, next] | |
160 }{ | |
161 [token]Type Match[("List Separator","Block Comment","Line Comment","Newline")] | |
162 { | |
163 out,out index,done <- Inputs[node, tokens, next] | |
164 }{ | |
165 Print[To String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
166 } | |
167 } | |
168 } | |
169 } | |
170 } | |
171 Val[end stream] | |
172 { | |
173 Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
174 } | |
175 } | |
176 | |
177 Method[node,tokens,current:out,out index,done] | |
178 { | |
179 token <- [tokens]Index[current] | |
180 next <- [tokens]Next[current] | |
181 { | |
182 [token]Type Match["Symbol"] | |
183 { | |
184 out,out index,done <- Before Inputs[[node]Blueprint <<[[token]Text >>], tokens, next] | |
185 }{ | |
186 [token]Type Match[("List Separator","Block Comment","Line Comment","Newline")] | |
187 { | |
188 out,out index,done <- Method[node, tokens, next] | |
189 }{ | |
190 Print[To String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
191 } | |
192 } | |
193 }{ | |
194 Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
195 } | |
196 } | |
197 | |
198 //TODO: support method declarations | |
199 Before Inputs[node, tokens, current:out, out index, done] | |
200 { | |
201 Print["Before Inputs"] | |
202 token <- [tokens]Index[current] | |
203 [token]Type Match["Args Begin"] | |
204 { | |
205 [tokens]Next[current] | |
206 { | |
207 out,out index, done <- Inputs[node, tokens, ~] | |
208 }{ | |
209 Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
210 } | |
211 }{ | |
212 [token]Type Match["Method Separator"] | |
213 { | |
214 [tokens]Next[current] | |
215 { | |
216 out,out index,done <- Method[node, tokens, ~] | |
217 }{ | |
218 Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
219 } | |
220 }{ | |
221 [token]Type Match[("Line Comment","Block Comment","Newline")] | |
222 { | |
223 continue <- Yes | |
224 }{ | |
225 Print[To String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
226 } | |
227 Val[continue] | |
228 { | |
229 [tokens]Next[current] | |
230 { | |
231 out,out index, done <- Before Inputs[node, tokens, ~] | |
232 }{ | |
233 Print[To String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
234 } | |
235 } | |
236 } | |
237 } | |
238 } | |
239 | |
240 Worker Node[name, tokens, current:out,out index, done] | |
241 { | |
242 Print[["Worker: "]Append[To String[name]]] | |
243 out,out index, done <- Before Inputs[[[[[Build["Worker Node"]]Name <<[name]]Inputs <<[()]]Outputs <<[()]]Tree <<[()], tokens, current] | |
244 } | |
245 | |
246 Blueprint PBlueprint Node | |
247 { | |
248 Name | |
249 Fields | |
250 } | |
251 | |
252 PBlueprint Node[name, tokens, current:out,out index,done] | |
253 { | |
254 out <- [[Build["PBlueprint Node"]]Name <<[name]]Fields <<[()] | |
255 out index <- current | |
256 //TODO: Parse rest of blueprint definition | |
257 } | |
258 | |
259 | |
260 Top Level[tokens, current, nodes:out] | |
261 { | |
262 token <- [tokens]Index[current] | |
263 Print[[token]Type >>] | |
264 [token]Type Match["Symbol"] | |
265 { | |
266 [[token]Text >>]After["Import "] | |
267 { | |
268 next nodes <- [nodes]Append[PImport Node[~]] | |
269 next index <- Val[current] | |
270 }{ | |
271 blueprint name,worker name <- [~]After["Blueprint "] | |
272 { | |
273 [tokens]Next[current] | |
274 { | |
275 , next index <- PBlueprint Node[blueprint name, tokens, ~] | |
276 { | |
277 next nodes <- [nodes]Append[~] | |
278 } {} { | |
279 Print["done!"] | |
280 out <- Val[next nodes] | |
281 } | |
282 }{ | |
283 Print[To String[Error[["Unexpected end of stream after symbol "]Append[[token]Text >>], [token]Raw Text >>]]] | |
284 } | |
285 }{ | |
286 [tokens]Next[current] | |
287 { | |
288 , next index <- Worker Node[worker name, tokens, ~] | |
289 { | |
290 next nodes <- [nodes]Append[~] | |
291 } {} { | |
292 Print["done!"] | |
293 out <- Val[next nodes] | |
294 } | |
295 }{ | |
296 Print[To String[Error[["Unexpected end of stream after symbol "]Append[[token]Text >>], [token]Raw Text >>]]] | |
297 } | |
298 } | |
299 } | |
300 | |
301 }{ | |
302 [token]Type Match[("Newline","Block Comment","Line Comment")] | |
303 { | |
304 next nodes <- Val[nodes] | |
305 next index <- Val[current] | |
306 }{ | |
307 Print[To String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]] | |
308 } | |
309 } | |
310 [tokens]Next[next index] | |
311 { | |
312 out <- Top Level[tokens, ~, next nodes] | |
313 }{ | |
314 out <- Val[next nodes] | |
315 } | |
316 } | |
317 | |
318 Parse[tokens:out] | |
319 { | |
320 [tokens]First | |
321 { | |
322 out <- Top Level[tokens, ~, ()] | |
323 }{ | |
324 out <- () | |
325 } | |
326 } |