Mercurial > repos > rhope
comparison nworker.rhope @ 2:73e978d590c7
Adding WIP compiler code
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 29 Apr 2009 02:58:03 -0400 |
parents | |
children | 90c20dc3f4e5 |
comparison
equal
deleted
inserted
replaced
1:b3f71490858c | 2:73e978d590c7 |
---|---|
1 Import extendlib.rhope | |
2 | |
3 Blueprint Range | |
4 { | |
5 Start | |
6 Stop | |
7 } | |
8 | |
9 Range[start,stop:out] | |
10 { | |
11 out <- [[Build["Range"]]Start <<[start]]Stop <<[stop] | |
12 } | |
13 | |
14 First@Range[range:first,none] | |
15 { | |
16 If[[[range]Start >>] < [[range]Stop >>]] | |
17 { | |
18 first <- [range]Start >> | |
19 }{ | |
20 none <- range | |
21 } | |
22 } | |
23 | |
24 Next@Range[range,current:next,none] | |
25 { | |
26 pnext <- [current]+[1] | |
27 If[[pnext] < [[range]Stop >>]] | |
28 { | |
29 next <- Val[pnext] | |
30 }{ | |
31 none <- range | |
32 } | |
33 } | |
34 | |
35 Index@Range[range,index:val,none] | |
36 { | |
37 val <- index | |
38 } | |
39 | |
40 Set@Range[range,index,val:out] | |
41 { | |
42 out <- [[()]Concatenate[range] | |
43 ]Set[index,val] | |
44 } | |
45 | |
46 List of Lists[num:out] | |
47 { | |
48 out <- Fold[["Append"]Set Input[1, ()],(), Range[0,num]] | |
49 } | |
50 | |
51 Blueprint Worker Ref | |
52 { | |
53 Name | |
54 Convention | |
55 Inputs | |
56 Outputs | |
57 } | |
58 | |
59 Worker Ref[name,convention,inputs,outputs:out] | |
60 { | |
61 out <- [[[[Build["Worker Ref"]]Name <<[name]]Convention <<[convention]]Inputs <<[inputs]]Outputs <<[outputs] | |
62 } | |
63 | |
64 Blueprint Node Ref | |
65 { | |
66 Index | |
67 IO Num | |
68 } | |
69 | |
70 Node Ref[index,ionum:out] | |
71 { | |
72 out <- [[Build["Node Ref"]]Index <<[index]]IO Num <<[ionum] | |
73 } | |
74 | |
75 =@Node Ref[left,right:out] | |
76 { | |
77 ,out <- If[[[left]Index >>] = [[right]Index >>]] | |
78 { | |
79 out <- [[left]IO Num>>] = [[right]IO Num >>] | |
80 } | |
81 } | |
82 | |
83 Blueprint NWorker Node | |
84 { | |
85 Type | |
86 Data | |
87 Inputs | |
88 Outputs | |
89 Wires From | |
90 Wires To | |
91 } | |
92 | |
93 Wire To@NWorker Node[node,from,output,input:out] | |
94 { | |
95 out <- [node]Wires To <<[ | |
96 [[node]Wires To >>]Set[input, | |
97 [[[node]Wires To >>]Index[input] | |
98 ]Append[Node Ref[from,output]] | |
99 ] | |
100 ] | |
101 } | |
102 | |
103 Wire From@NWorker Node[node,to,input,output:out] | |
104 { | |
105 out <- [node]Wires From <<[ | |
106 [[node]Wires From >>]Set[output, | |
107 [[[node]Wires From >>]Index[output] | |
108 ]Append[Node Ref[to,input]] | |
109 ] | |
110 ] | |
111 } | |
112 | |
113 _Dependency[dlist,ref:out] | |
114 { | |
115 [dlist]Find[ref] | |
116 { | |
117 out <- dlist | |
118 }{ | |
119 out <- [dlist]Append[ref] | |
120 } | |
121 } | |
122 | |
123 Dependencies@NWorker Node[node:out] | |
124 { | |
125 out <- Fold[["Fold"]Set Input[0, "_Dependency"], (), [node]Wires To >>] | |
126 } | |
127 | |
128 | |
129 NWorker Node[type,data,inputs,outputs:out] | |
130 { | |
131 out <- [[[[[[Build["NWorker Node"] | |
132 ]Type <<[type] | |
133 ]Data <<[data] | |
134 ]Inputs <<[inputs] | |
135 ]Outputs <<[outputs] | |
136 ]Wires From <<[List of Lists[outputs]] | |
137 ]Wires To <<[List of Lists[inputs]] | |
138 } | |
139 | |
140 Blueprint NWorker | |
141 { | |
142 Convention | |
143 Nodes | |
144 Inputs | |
145 Outputs | |
146 } | |
147 | |
148 NWorker[convention:out] | |
149 { | |
150 out <- [[[[Build["NWorker"]]Convention <<[convention]]Nodes <<[()]]Inputs <<[()]]Outputs <<[()] | |
151 } | |
152 | |
153 Add Node@NWorker[worker,type,data,inputs,outputs:out,node index] | |
154 { | |
155 out <- [worker]Nodes <<[[[worker]Nodes >>]Append[NWorker Node[type,data,inputs,outputs]]] | |
156 node index <- [[worker]Nodes >>]Length | |
157 } | |
158 | |
159 Add Worker Call@NWorker[worker,tocall:out,node index] | |
160 { | |
161 out, node index <- [worker]Add Node["call",tocall,[tocall]Inputs >>,[tocall]Outputs >>] | |
162 } | |
163 | |
164 Add Constant@NWorker[worker,constant:out,node index] | |
165 { | |
166 out, node index <- [worker]Add Node["const",constant,0,1] | |
167 } | |
168 | |
169 Add Input@NWorker[worker,name,number:out,node index] | |
170 { | |
171 ,node index <- [worker]Add Node["input",number,0,1] | |
172 { out <- [~]Inputs <<[[[~]Inputs >>]Set[number,name]] } | |
173 } | |
174 | |
175 Add Output@NWorker[worker,name,number:out,node index] | |
176 { | |
177 ,node index <- [worker]Add Node["output",number,1,0] | |
178 { out <- [~]Outputs <<[[[~]Outputs >>]Set[number,name]] } | |
179 } | |
180 | |
181 Add Wire@NWorker[worker,from,output,to,input:out] | |
182 { | |
183 fromw <- [[[worker]Nodes >>]Index[from]]Wire From[to,input,output] | |
184 tow <- [[[worker]Nodes >>]Index[to]]Wire To[from,output,input] | |
185 nodes <- [[[worker]Nodes >>]Set[from, fromw]]Set[to, tow] | |
186 out <- [worker]Nodes <<[nodes] | |
187 } | |
188 | |
189 _No Dependencies[list,node,index:out] | |
190 { | |
191 [[node]Wires To>>]First | |
192 { | |
193 out <- Val[list] | |
194 }{ | |
195 out <- [list]Append[index] | |
196 } | |
197 } | |
198 | |
199 No Dependencies@NWorker[worker:out] | |
200 { | |
201 out <- Fold["_No Dependencies", (), [worker]Nodes >>] | |
202 } | |
203 | |
204 _Collect Dests[candidates,wire:out] | |
205 { | |
206 out <- [candidates]Set[[wire]Index >>, Yes] | |
207 } | |
208 | |
209 Collect Dests@NWorker[worker,candidates,node index:out] | |
210 { | |
211 out <- Fold[["Fold"]Set Input[0, "_Collect Dests"], candidates, [[[worker]Nodes >>]Index[node index]]Wires From >>] | |
212 } | |
213 | |
214 Check Dependency@NWorker[worker,nodes,wires,wire index:met?] | |
215 { | |
216 ref <- [wires]Index[wire index] | |
217 [nodes]Find[[ref]Index >>] | |
218 { | |
219 [wires]Next[wire index] | |
220 { | |
221 met? <- [worker]Check Dependency[nodes,wires,~] | |
222 }{ | |
223 met? <- Yes | |
224 } | |
225 }{ | |
226 Print[[ref]Index >>] | |
227 met? <- No | |
228 } | |
229 } | |
230 _Check Dependencies@NWorker[worker,nodes,inputs,input index:met?] | |
231 { | |
232 wires <- [inputs]Index[input index] | |
233 [wires]First | |
234 { | |
235 current met? <- [worker]Check Dependency[nodes, wires, ~] | |
236 }{ | |
237 current met? <- Yes | |
238 } | |
239 If[current met?] | |
240 { | |
241 [inputs]Next[input index] | |
242 { | |
243 met? <- [worker]_Check Dependencies[nodes,inputs,~] | |
244 }{ | |
245 met? <- Yes | |
246 } | |
247 }{ | |
248 met? <- No | |
249 } | |
250 } | |
251 | |
252 Check Dependencies@NWorker[worker,nodes,candidate:met?] | |
253 { | |
254 inputs <- [[[worker]Nodes >>]Index[candidate]]Wires To >> | |
255 [inputs]First | |
256 { | |
257 met? <- [worker]_Check Dependencies[nodes, inputs, ~] | |
258 }{ | |
259 met? <- Yes | |
260 } | |
261 } | |
262 | |
263 Dependants@NWorker[worker,direct nodes,nodes:out] | |
264 { | |
265 candidates <- Keys[Fold[["Collect Dests"]Set Input[0,worker], (), direct nodes]] | |
266 out <- Filter[candidates, [["Check Dependencies"]Set Input[0, worker]]Set Input[1, nodes]] | |
267 } | |
268 | |
269 _Dependency Groups@NWorker[worker,last,all,grouped:out] | |
270 { | |
271 current <- [worker]Dependants[last,all] | |
272 [current]First | |
273 { | |
274 out <- [worker]_Dependency Groups[current, [all]Concatenate[current], [grouped]Append[current]] | |
275 }{ | |
276 out <- grouped | |
277 } | |
278 } | |
279 Dependency Groups@NWorker[worker:out] | |
280 { | |
281 no deps <- [worker]No Dependencies | |
282 out <- [worker]_Dependency Groups[no deps, no deps, [()]Append[no deps]] | |
283 } | |
284 | |
285 Test[:out] | |
286 { | |
287 ref+ <- Worker Ref["+","cdecl",2,1] | |
288 ref* <- Worker Ref["*","cdecl",2,1] | |
289 ,a <- [NWorker["cdecl"] | |
290 ]Add Input["a", 0] { | |
291 ,b <- [~]Add Input["b", 1] { | |
292 ,c <- [~]Add Input["c", 2] { | |
293 ,outref <- [~]Add Output["out", 0] { | |
294 ,call+ <- [~]Add Worker Call[ref+] { | |
295 ,call* <- [~]Add Worker Call[ref*] { | |
296 out <- [[[[[~]Add Wire[a,0,call+,0] | |
297 ]Add Wire[b,0,call+,1] | |
298 ]Add Wire[call+,0,call*,0] | |
299 ]Add Wire[c,0,call*,1] | |
300 ]Add Wire[call*,0,outref,0] | |
301 }}}}}} | |
302 } |