comparison nworker.rhope @ 44:a7c79ac22efc

Beginning of basic type inference
author Mike Pavone <pavone@retrodev.com>
date Sat, 31 Oct 2009 21:28:28 -0400
parents 709df3e82bb4
children 6202b866d72c
comparison
equal deleted inserted replaced
43:709df3e82bb4 44:a7c79ac22efc
165 { 165 {
166 Name 166 Name
167 Convention 167 Convention
168 Inputs 168 Inputs
169 Outputs 169 Outputs
170 } 170 Is Method?
171 171 }
172 Worker Ref[name,convention,inputs,outputs:out] 172
173 { 173 Worker Ref[name,convention,inputs,outputs,ismethod?:out]
174 out <- [[[[Build["Worker Ref"]]Name <<[name]]Convention <<[convention]]Inputs <<[inputs]]Outputs <<[outputs] 174 {
175 out <- [[[[[Build["Worker Ref"]]Name <<[name]]Convention <<[convention]]Inputs <<[inputs]]Outputs <<[outputs]]Is Method ? <<[ismethod?]
175 } 176 }
176 177
177 Blueprint Node Ref 178 Blueprint Node Ref
178 { 179 {
179 Index 180 Index
196 Blueprint NWorker Node 197 Blueprint NWorker Node
197 { 198 {
198 Type 199 Type
199 Data 200 Data
200 Inputs 201 Inputs
202 Input Types
201 Outputs 203 Outputs
204 Output Types
202 Wires From 205 Wires From
203 Wires To 206 Wires To
204 Conditions 207 Conditions
205 } 208 }
206 209
223 ]Append[Node Ref[to,input]] 226 ]Append[Node Ref[to,input]]
224 ] 227 ]
225 ] 228 ]
226 } 229 }
227 230
231 _Has Input Types@NWorker Node[node,input num:does,does not]
232 {
233 does <- If[[input num] > [[node]Inputs >>]] {}
234 {
235 ,does not <- [[node]Input Types >>]Index[input num]
236 {
237 count <- [~]Index[1]
238 ,does not <- If[[count] = [[[[node]Wires To >>]Index[input num]]Length]]
239 {
240 does,does not <- [node]_Has Input Types[[input num]+[1]]
241 }
242 }
243 }
244 }
245
246 Has Input Types?@NWorker Node[node:does,does not]
247 {
248 If[[[[node]Inputs >>]Length] > [0]]
249 {
250 does,does not <- _Has Input Types[node,0]
251 }{
252 does <- Yes
253 }
254 }
255
228 _Dependency[dlist,ref:out] 256 _Dependency[dlist,ref:out]
229 { 257 {
230 [dlist]Find[ref] 258 [dlist]Find[ref]
231 { 259 {
232 out <- dlist 260 out <- dlist
241 } 269 }
242 270
243 271
244 NWorker Node[type,data,inputs,outputs:out] 272 NWorker Node[type,data,inputs,outputs:out]
245 { 273 {
246 out <- [[[[[[[Build["NWorker Node"] 274 out <- [[[[[[[[[Build["NWorker Node"]
247 ]Type <<[type] 275 ]Type <<[type]
248 ]Data <<[data] 276 ]Data <<[data]
249 ]Inputs <<[inputs] 277 ]Inputs <<[inputs]
250 ]Outputs <<[outputs] 278 ]Outputs <<[outputs]
251 ]Wires From <<[List of Lists[outputs]] 279 ]Wires From <<[List of Lists[outputs]]
252 ]Wires To <<[List of Lists[[inputs]+[1]]] 280 ]Wires To <<[List of Lists[[inputs]+[1]]]
253 ]Conditions <<[AndSet[]] 281 ]Conditions <<[AndSet[]]
282 ]Input Types <<[()]
283 ]Output Types <<[()]
254 } 284 }
255 285
256 Blueprint NWorker 286 Blueprint NWorker
257 { 287 {
258 Convention 288 Convention
262 Outputs 292 Outputs
263 Output Types 293 Output Types
264 Uses 294 Uses
265 NodeResults 295 NodeResults
266 Free Temps 296 Free Temps
297 Name
298 Builtin?
267 } 299 }
268 300
269 NWorker[convention:out] 301 NWorker[convention:out]
270 { 302 {
271 out <- [[[[[[Build["NWorker"]]Convention <<[convention]]Nodes <<[()]]Inputs <<[()]]Outputs <<[()]]Input Types <<[()]]Output Types <<[()] 303 out <- [[[[[[[[Build["NWorker"]]Convention <<[convention]]Nodes <<[()]]Inputs <<[()]]Outputs <<[()]]Input Types <<[()]]Output Types <<[()]]Name <<["Anonymous"]]Builtin <<[No]
272 } 304 }
273 305
274 Add Node@NWorker[worker,type,data,inputs,outputs:out,node index] 306 Add Node@NWorker[worker,type,data,inputs,outputs:out,node index]
275 { 307 {
276 out <- [worker]Nodes <<[[[worker]Nodes >>]Append[NWorker Node[type,data,inputs,outputs]]] 308 out <- [worker]Nodes <<[[[worker]Nodes >>]Append[NWorker Node[type,data,inputs,outputs]]]
277 node index <- [[worker]Nodes >>]Length 309 node index <- [[worker]Nodes >>]Length
310 }
311
312 Propagate Type[nodelist,dest,prog,worker,type:out]
313 {
314 node <- [nodelist]Index[[dest]Index >>]
315
316 [[node]Input Types >>]Index[[dest]IO Num >>]
317 {
318 existing type <- [~]Index[0]
319 new count <- [[~]Index[1]]+[1]
320 If[[[existing type]Name >>] = [[type]Name >>]]
321 {
322 If[[[existing type]Variant >>] = [[type]Variant >>]]
323 {
324 If[[[existing type]Params >>] = [[type]Params >>]]
325 {
326 new type <- Val[existing type]
327 }{
328 new variant <- [existing type]Variant >>
329 new params <- ()
330 }
331 }{
332 new variant <- "Boxed"
333 If[[[existing type]Params >>] = [[type]Params >>]]
334 {
335 new params <- [existing type]Params >>
336 }{
337 new params <- ()
338 }
339 }
340 new type <- [[existing type]Set Variant[new variant]]Params <<[new params]
341 }{
342 new type <- Type Instance["Any Type"]
343 }
344 }{
345 new type <- Val[type]
346 new count <- 1
347 }
348 new node <- [node]Input Types <<[ [[node]Input Types >>]Set[[dest]IO Num >>, new type] ]
349 out <- Infer Types Node[[nodelist]Set[[dest]Index >>, new node], new node, [dest]Index >>, prog, worker]
350 }
351
352 Propagate Types[nodelist,dests,output num,prog,worker,source node:out]
353 {
354 out <- Fold[[[["Propagate Type"]Set Input[2, prog]]Set Input[3, worker]]Set Input[4, [[source node]Output Types >>]Index[output num]], nodelist, dests]
355 }
356
357 Infer Types Node[nodelist,node,index,prog,worker:out]
358 {
359 If[[[node]Type >>] = ["const"]]
360 {
361 //Temporary hack
362 If[[Type Of[[node]Data >>]] = ["Whole Number"]]
363 {
364 outtype <- Type Instance["Whole Number"]
365 }{
366 outtype <- Type Instance[Type Of[[node]Data >>]]
367 }
368 nextnode <- [node]Output Types <<[ [()]Append[outtype] ]
369
370 }{
371 If[[[node]Type >>] = ["input"]]
372 {
373 nextnode <- [node]Output Types <<[ [()]Append[ [[worker]Input Types >>]Index[[node]Data >>] ] ]
374 }{
375 If[[[node]Type >>] = ["output"]]
376 {
377 out <- nodelist
378 }{
379 [node]Has Input Types?
380 {
381 If[[[node]Type >>] = ["setfield"]]
382 {
383 nextnode <- [node]Output Types <<[ [()]Append[ [[[node]Input Types >>]Index[0]]Index[0] ] ]
384 }{
385 If[[[node]Type >>] = ["getfield"]]
386 {
387 type <- [[[node]Input Types >>]Index[0]]Index[0]
388 If[[[type]Name >>] = ["Any Type"]]
389 {
390 outtype <- Val[type]
391 }{
392 outtype <- [prog]Find Field[[node]Data >>, type] {}
393 {
394 //TODO: Return errors rather than printing them
395 Print[
396 [[[[["Type "
397 ]Append[[type]Name >>]
398 ]Append[" does not have a field named "]
399 ]Append[[node]Data >>]
400 ]Append[" in worker "]
401 ]Append[worker name]]
402 }
403 }
404 nextnode <- [node]Output Types <<[ [()]Append[outtype] ]
405 }{
406 worker name <- [[node]Data >>]Name >>
407 [prog]Is Method?[worker name]
408 {
409 first arg type <- [[[node]Input Types >>]Index[0]]Index[0]
410 If[[[first arg type]Name >>] = ["Any Type"]]
411 {
412 outtypes <- Fold[["Append"]Set Input[1, Type Instance["Any Type"]], (), Range[0, [node]Inputs >>]]
413 }{
414 worker def <- [prog]Find Method[worker name, first arg type] {}
415 {
416 //TODO: Return errors instead of printing them
417 Print[
418 [[[[["Type "
419 ]Append[[first arg type]Name >>]
420 ]Append[" does not support method "]
421 ]Append[worker name]
422 ]Append[" in worker "]
423 ]Append[ [worker]Name >> ]]
424 }
425 }
426 }{
427 worker def <- [prog]Find Worker Def[worker name]
428 }
429 outtypes <- [worker def]Output Types >>
430 nextnode <- [node]Output Types <<[ outtypes ]
431 }
432 }
433 }{
434 out <- nodelist
435 }
436 }
437 }
438 }
439
440 Val[nextnode]
441 {
442 nextlist <- [nodelist]Set[index, nextnode]
443 out <- Fold[[[["Propagate Types"]Set Input[3, prog]]Set Input[4, worker]]Set Input[5, nextnode], nodelist, [nextnode]Wires From >>]
444 }
445 }
446
447 Infer Types@NWorker[worker,prog:out]
448 {
449 out <- [worker]Nodes <<[Fold[[["Infer Types Node"]Set Input[3, prog]]Set Input[4, worker], [worker]Nodes >>, [worker]Nodes >>]]
278 } 450 }
279 451
280 Add Worker Call@NWorker[worker,tocall:out,node index] 452 Add Worker Call@NWorker[worker,tocall:out,node index]
281 { 453 {
282 out, node index <- [worker]Add Node["call",tocall,[tocall]Inputs >>,[tocall]Outputs >>] 454 out, node index <- [worker]Add Node["call",tocall,[tocall]Inputs >>,[tocall]Outputs >>]
686 out <- Fold["Node Result Vars", (), [worker]Nodes >>] 858 out <- Fold["Node Result Vars", (), [worker]Nodes >>]
687 } 859 }
688 860
689 Compile Worker@NWorker[worker,program,name:out] 861 Compile Worker@NWorker[worker,program,name:out]
690 { 862 {
691 Print[["Compiling: "]Append[name]] 863 If[[worker]Builtin? >>]
692 { 864 {
693 ifunc <- Fold["Set Output Type", Fold["Set Input Type", [program]Create Function[name,[worker]Inputs >>, [worker]Outputs >>, [worker]Convention >>], [worker]Input Types >>], [worker]Output Types >>] 865 out <- program
694 866 }{
695 res vars <- [worker]Result Vars 867 Print[["Compiling: "]Append[name]]
696 func <- Fold["Set Null", Fold["Set Null", Fold[["Allocate Var"]Set Input[2, "Any Type"], ifunc, res vars], res vars], [worker]Outputs >>] 868 {
697 869 ifunc <- Fold["Set Output Type", Fold["Set Input Type", [program]Create Function[name,[worker]Inputs >>, [worker]Outputs >>, [worker]Convention >>], [worker]Input Types >>], [worker]Output Types >>]
698 groups <- [worker]Dependency Groups 870
699 [groups]First 871 res vars <- [worker]Result Vars
700 { 872 func <- Fold["Set Null", Fold["Set Null", Fold[["Allocate Var"]Set Input[2, "Any Type"], ifunc, res vars], res vars], [worker]Outputs >>]
701 final func <- [worker]Compile Group[program,func,groups, ~] 873
702 }{ 874 groups <- [worker]Dependency Groups
703 final func <- Val[func] 875 [groups]First
704 } 876 {
705 out <- [program]Store Function[Fold["Release", Fold[["Release Var"]Set Input[0, worker], final func, res vars], [worker]Inputs >>]] 877 final func <- [worker]Compile Group[program,func,groups, ~]
878 }{
879 final func <- Val[func]
880 }
881 out <- [program]Store Function[Fold["Release", Fold[["Release Var"]Set Input[0, worker], final func, res vars], [worker]Inputs >>]]
882 }
706 } 883 }
707 } 884 }
708 885
709 Test[:out] 886 Test[:out]
710 { 887 {
923 out <- [[[Build["NProgram"]]Blueprints <<[New@Dictionary[]]]Workers <<[New@Dictionary[]]]Worker Refs <<[New@Dictionary[]] 1100 out <- [[[Build["NProgram"]]Blueprints <<[New@Dictionary[]]]Workers <<[New@Dictionary[]]]Worker Refs <<[New@Dictionary[]]
924 } 1101 }
925 1102
926 Bind Worker@NProgram[prog,name,worker:out] 1103 Bind Worker@NProgram[prog,name,worker:out]
927 { 1104 {
928 after bind <- [prog]Workers << [ [[prog]Workers >>]Set[name, worker] ] 1105 after bind <- [prog]Workers << [ [[prog]Workers >>]Set[name, [worker]Name <<[name]] ]
929 parts <- [name]Split["@"] 1106 parts <- [name]Split["@"]
930 [parts]Index[1] 1107 [parts]Index[1]
931 { 1108 {
932 out <- [after bind]Blueprints <<[ [[after bind]Blueprints >>]Set[~, [[[after bind]Blueprints >>]Index[~]]Add Method[[parts]Index[0]] ] ] 1109 out <- [after bind]Blueprints <<[ [[after bind]Blueprints >>]Set[~, [[[after bind]Blueprints >>]Index[~]]Add Method[[parts]Index[0]] ] ]
933 }{ 1110 }{
959 Compile Program@NProgram[prog, backend:out] 1136 Compile Program@NProgram[prog, backend:out]
960 { 1137 {
961 out <- Fold["_Compile Program", Fold["_Compile Program BP Special", Fold["_Compile Program BP", backend, [prog]Blueprints >>], [prog]Blueprints >>], [prog]Workers >>] 1138 out <- Fold["_Compile Program", Fold["_Compile Program BP Special", Fold["_Compile Program BP", backend, [prog]Blueprints >>], [prog]Blueprints >>], [prog]Workers >>]
962 } 1139 }
963 1140
1141 Register Method@NProgram[prog, name, convention, inputs, outputs: out]
1142 {
1143 out <- [prog]Worker Refs <<[ [[prog]Worker Refs >>]Set[name, Worker Ref[name, convention, inputs, outputs, Yes]]]
1144 }
1145
964 Register Worker@NProgram[prog, name, convention, inputs, outputs: out] 1146 Register Worker@NProgram[prog, name, convention, inputs, outputs: out]
965 { 1147 {
966 Print[["Register Worker "]Append[name]] 1148 Print[["Register Worker "]Append[name]]
967 after reg <- [prog]Worker Refs <<[ [[prog]Worker Refs >>]Set[name, Worker Ref[name, convention, inputs, outputs]]] 1149 after reg <- [prog]Worker Refs <<[ [[prog]Worker Refs >>]Set[name, Worker Ref[name, convention, inputs, outputs, No]]]
968 parts <- [name]Split["@"] 1150 parts <- [name]Split["@"]
969 [parts]Index[1] 1151 [parts]Index[1]
970 { 1152 {
971 out <- [prog]Register Worker[[parts]Index[0], convention, inputs, outputs] 1153 out <- [after reg]Register Method[[parts]Index[0], convention, inputs, outputs]
972 }{ 1154 }{
973 out <- Val[after reg] 1155 out <- Val[after reg]
974 } 1156 }
975 } 1157 }
976 1158
977 Register Builtins@NProgram[prog:out] 1159 Register Builtins@NProgram[prog:out]
978 { 1160 {
979 out <- [[[[[[[[[[[prog]Register Worker["+@Int32", "rhope", 2, 1] 1161 registered <- [[[[[[[[[[[prog]Register Worker["+@Int32", "rhope", 2, 1]
980 ]Register Worker["-@Int32", "rhope", 2, 1] 1162 ]Register Worker["-@Int32", "rhope", 2, 1]
981 ]Register Worker["*@Int32", "rhope", 2, 1] 1163 ]Register Worker["*@Int32", "rhope", 2, 1]
982 ]Register Worker["/@Int32", "rhope", 2, 1] 1164 ]Register Worker["/@Int32", "rhope", 2, 1]
983 ]Register Worker["LShift@Int32", "rhope", 2, 1] 1165 ]Register Worker["LShift@Int32", "rhope", 2, 1]
984 ]Register Worker["RShift@Int32", "rhope", 2, 1] 1166 ]Register Worker["RShift@Int32", "rhope", 2, 1]
985 ]Register Worker["Print", "rhope", 1, 1] 1167 ]Register Worker["Print", "rhope", 1, 1]
986 ]Register Worker["If@Yes No", "rhope", 1, 2] 1168 ]Register Worker["If@Boolean", "rhope", 1, 2]
987 ]Register Worker["<@Int32", "rhope", 2, 1] 1169 ]Register Worker["<@Int32", "rhope", 2, 1]
988 ]Register Worker[">@Int32", "rhope", 2, 1] 1170 ]Register Worker[">@Int32", "rhope", 2, 1]
989 ]Register Worker["Build", "rhope", 1, 1] 1171 ]Register Worker["Build", "rhope", 1, 1]
1172
1173 out <- [[[[[[[[[[[[[registered]Bind Blueprint["Int32", NBlueprint[]]
1174 ]Bind Worker["+@Int32",
1175 [[[[[NWorker["rhope"]
1176 ]Inputs <<[("left","right")]
1177 ]Input Types <<[ [[()]Append[Type Instance["Int32"]]]Type Instance["Int32"] ]
1178 ]Outputs <<[("out")]
1179 ]Output types <<[ [()]Append[Type Instance["Int32"]] ]
1180 ]Builin? <<[Yes]]
1181 ]Bind Worker["-@Int32",
1182 [[[[[NWorker["rhope"]
1183 ]Inputs <<[("left","right")]
1184 ]Input Types <<[ [[()]Append[Type Instance["Int32"]]]Type Instance["Int32"] ]
1185 ]Outputs <<[("out")]
1186 ]Output types <<[ [()]Append[Type Instance["Int32"]] ]
1187 ]Builin? <<[Yes]]
1188 ]Bind Worker["*@Int32",
1189 [[[[[NWorker["rhope"]
1190 ]Inputs <<[("left","right")]
1191 ]Input Types <<[ [[()]Append[Type Instance["Int32"]]]Type Instance["Int32"] ]
1192 ]Outputs <<[("out")]
1193 ]Output types <<[ [()]Append[Type Instance["Int32"]] ]
1194 ]Builin? <<[Yes]]
1195 ]Bind Worker["/@Int32",
1196 [[[[[NWorker["rhope"]
1197 ]Inputs <<[("left","right")]
1198 ]Input Types <<[ [[()]Append[Type Instance["Int32"]]]Type Instance["Int32"] ]
1199 ]Outputs <<[("out")]
1200 ]Output types <<[ [()]Append[Type Instance["Int32"]] ]
1201 ]Builin? <<[Yes]]
1202 ]Bind Worker["LShift@Int32",
1203 [[[[[NWorker["rhope"]
1204 ]Inputs <<[("left","right")]
1205 ]Input Types <<[ [[()]Append[Type Instance["Int32"]]]Type Instance["Int32"] ]
1206 ]Outputs <<[("out")]
1207 ]Output types <<[ [()]Append[Type Instance["Int32"]] ]
1208 ]Builin? <<[Yes]]
1209 ]Bind Worker["RShift@Int32",
1210 [[[[[NWorker["rhope"]
1211 ]Inputs <<[("left","right")]
1212 ]Input Types <<[ [[()]Append[Type Instance["Int32"]]]Type Instance["Int32"] ]
1213 ]Outputs <<[("out")]
1214 ]Output types <<[ [()]Append[Type Instance["Int32"]] ]
1215 ]Builin? <<[Yes]]
1216 ]Bind Worker["<@Int32",
1217 [[[[[NWorker["rhope"]
1218 ]Inputs <<[("left","right")]
1219 ]Input Types <<[ [[()]Append[Type Instance["Int32"]]]Type Instance["Int32"] ]
1220 ]Outputs <<[("out")]
1221 ]Output types <<[ [()]Append[Type Instance["Int32"]] ]
1222 ]Builin? <<[Yes]]
1223 ]Bind Worker[">@Int32",
1224 [[[[[NWorker["rhope"]
1225 ]Inputs <<[("left","right")]
1226 ]Input Types <<[ [[()]Append[Type Instance["Int32"]]]Type Instance["Int32"] ]
1227 ]Outputs <<[("out")]
1228 ]Output types <<[ [()]Append[Type Instance["Int32"]] ]
1229 ]Builin? <<[Yes]]
1230 ]Bind Blueprint["Boolean", NBlueprint[]]
1231 ]Bind Worker["If@Boolean",
1232 [[[[[NWorker["rhope"]
1233 ]Inputs <<[("condition")]
1234 ]Input Types <<[ [()]Append[Type Instance["Boolean"]] ]
1235 ]Outputs <<[("isyes","isno")]
1236 ]Output types <<[ [[()]Append[Type Instance["Boolean"]]]Append[Type Instance["Boolean"]] ]
1237 ]Builin? <<[Yes]]
1238 ]Bind Worker["Print",
1239 [[[[[NWorker["rhope"]
1240 ]Inputs <<[("value")]
1241 ]Input Types <<[ [()]Append[Type Instance["Any Type"]] ]
1242 ]Outputs <<[("out")]
1243 ]Output types <<[ [()]Append[Type Instance["Int32"]] ]
1244 ]Builin? <<[Yes]]
1245 ]Bind Worker["Build",
1246 [[[[[NWorker["rhope"]
1247 ]Inputs <<[("type")]
1248 ]Input Types <<[ [()]Append[Type Instance["Blueprint"]] ]
1249 ]Outputs <<[("out")]
1250 ]Output types <<[ [()]Append[Type Instance["Any Type"]] ]
1251 ]Builin? <<[Yes]]
990 } 1252 }
991 1253
992 Find Worker@NProgram[prog, name:out,notfound] 1254 Find Worker@NProgram[prog, name:out,notfound]
993 { 1255 {
994 Print[ ["Find Worker@NProgram: "]Append[name] ] 1256 Print[ ["Find Worker@NProgram: "]Append[name] ]
995 out,notfound <- [[prog]Worker Refs >>]Index[name] 1257 out,notfound <- [[prog]Worker Refs >>]Index[name]
996 } 1258 }
997 1259
1260 Find Worker Def@NProgram[prog,name:out,notfound]
1261 {
1262 out,notfound <- [[prog]Workers >>]Index[name]
1263 }
1264
998 Find Method@NProgram[prog, name, type:out,notfound] 1265 Find Method@NProgram[prog, name, type:out,notfound]
999 { 1266 {
1000 bp,notfound <- [[prog]Blueprints >>]Index[[type]Name >>] 1267 bp,notfound <- [[prog]Blueprints >>]Index[[type]Name >>]
1001 ,notfound <- If[[bp]Understands Method[name]] 1268 ,notfound <- If[[bp]Understands Method[name]]
1002 { 1269 {
1007 Find Field@NProgram[prog, name, type:fieldtype,notfound] 1274 Find Field@NProgram[prog, name, type:fieldtype,notfound]
1008 { 1275 {
1009 bp,notfound <- [[prog]Blueprints >>]Index[[type]Name >>] 1276 bp,notfound <- [[prog]Blueprints >>]Index[[type]Name >>]
1010 fieldtype,notfound <- [bp]Get Field Type[name] 1277 fieldtype,notfound <- [bp]Get Field Type[name]
1011 } 1278 }
1279
1280 Implicit Conversion@NProgram[prog, fromtype, totype:func,notfound]
1281 {
1282 notfound <- No
1283 }
1284
1285 Is Method?@NProgram[prog,name:is,is not]
1286 {
1287 ,is not <- [[prog]Worker Refs>>]Index[name]
1288 {
1289 is,is not <- If[[~]Is Method? >>]
1290 }
1291 }