Mercurial > repos > rhope
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nworker.rhope Wed Apr 29 02:58:03 2009 -0400 @@ -0,0 +1,302 @@ +Import extendlib.rhope + +Blueprint Range +{ + Start + Stop +} + +Range[start,stop:out] +{ + out <- [[Build["Range"]]Start <<[start]]Stop <<[stop] +} + +First@Range[range:first,none] +{ + If[[[range]Start >>] < [[range]Stop >>]] + { + first <- [range]Start >> + }{ + none <- range + } +} + +Next@Range[range,current:next,none] +{ + pnext <- [current]+[1] + If[[pnext] < [[range]Stop >>]] + { + next <- Val[pnext] + }{ + none <- range + } +} + +Index@Range[range,index:val,none] +{ + val <- index +} + +Set@Range[range,index,val:out] +{ + out <- [[()]Concatenate[range] + ]Set[index,val] +} + +List of Lists[num:out] +{ + out <- Fold[["Append"]Set Input[1, ()],(), Range[0,num]] +} + +Blueprint Worker Ref +{ + Name + Convention + Inputs + Outputs +} + +Worker Ref[name,convention,inputs,outputs:out] +{ + out <- [[[[Build["Worker Ref"]]Name <<[name]]Convention <<[convention]]Inputs <<[inputs]]Outputs <<[outputs] +} + +Blueprint Node Ref +{ + Index + IO Num +} + +Node Ref[index,ionum:out] +{ + out <- [[Build["Node Ref"]]Index <<[index]]IO Num <<[ionum] +} + +=@Node Ref[left,right:out] +{ + ,out <- If[[[left]Index >>] = [[right]Index >>]] + { + out <- [[left]IO Num>>] = [[right]IO Num >>] + } +} + +Blueprint NWorker Node +{ + Type + Data + Inputs + Outputs + Wires From + Wires To +} + +Wire To@NWorker Node[node,from,output,input:out] +{ + out <- [node]Wires To <<[ + [[node]Wires To >>]Set[input, + [[[node]Wires To >>]Index[input] + ]Append[Node Ref[from,output]] + ] + ] +} + +Wire From@NWorker Node[node,to,input,output:out] +{ + out <- [node]Wires From <<[ + [[node]Wires From >>]Set[output, + [[[node]Wires From >>]Index[output] + ]Append[Node Ref[to,input]] + ] + ] +} + +_Dependency[dlist,ref:out] +{ + [dlist]Find[ref] + { + out <- dlist + }{ + out <- [dlist]Append[ref] + } +} + +Dependencies@NWorker Node[node:out] +{ + out <- Fold[["Fold"]Set Input[0, "_Dependency"], (), [node]Wires To >>] +} + + +NWorker Node[type,data,inputs,outputs:out] +{ + out <- [[[[[[Build["NWorker Node"] + ]Type <<[type] + ]Data <<[data] + ]Inputs <<[inputs] + ]Outputs <<[outputs] + ]Wires From <<[List of Lists[outputs]] + ]Wires To <<[List of Lists[inputs]] +} + +Blueprint NWorker +{ + Convention + Nodes + Inputs + Outputs +} + +NWorker[convention:out] +{ + out <- [[[[Build["NWorker"]]Convention <<[convention]]Nodes <<[()]]Inputs <<[()]]Outputs <<[()] +} + +Add Node@NWorker[worker,type,data,inputs,outputs:out,node index] +{ + out <- [worker]Nodes <<[[[worker]Nodes >>]Append[NWorker Node[type,data,inputs,outputs]]] + node index <- [[worker]Nodes >>]Length +} + +Add Worker Call@NWorker[worker,tocall:out,node index] +{ + out, node index <- [worker]Add Node["call",tocall,[tocall]Inputs >>,[tocall]Outputs >>] +} + +Add Constant@NWorker[worker,constant:out,node index] +{ + out, node index <- [worker]Add Node["const",constant,0,1] +} + +Add Input@NWorker[worker,name,number:out,node index] +{ + ,node index <- [worker]Add Node["input",number,0,1] + { out <- [~]Inputs <<[[[~]Inputs >>]Set[number,name]] } +} + +Add Output@NWorker[worker,name,number:out,node index] +{ + ,node index <- [worker]Add Node["output",number,1,0] + { out <- [~]Outputs <<[[[~]Outputs >>]Set[number,name]] } +} + +Add Wire@NWorker[worker,from,output,to,input:out] +{ + fromw <- [[[worker]Nodes >>]Index[from]]Wire From[to,input,output] + tow <- [[[worker]Nodes >>]Index[to]]Wire To[from,output,input] + nodes <- [[[worker]Nodes >>]Set[from, fromw]]Set[to, tow] + out <- [worker]Nodes <<[nodes] +} + +_No Dependencies[list,node,index:out] +{ + [[node]Wires To>>]First + { + out <- Val[list] + }{ + out <- [list]Append[index] + } +} + +No Dependencies@NWorker[worker:out] +{ + out <- Fold["_No Dependencies", (), [worker]Nodes >>] +} + +_Collect Dests[candidates,wire:out] +{ + out <- [candidates]Set[[wire]Index >>, Yes] +} + +Collect Dests@NWorker[worker,candidates,node index:out] +{ + out <- Fold[["Fold"]Set Input[0, "_Collect Dests"], candidates, [[[worker]Nodes >>]Index[node index]]Wires From >>] +} + +Check Dependency@NWorker[worker,nodes,wires,wire index:met?] +{ + ref <- [wires]Index[wire index] + [nodes]Find[[ref]Index >>] + { + [wires]Next[wire index] + { + met? <- [worker]Check Dependency[nodes,wires,~] + }{ + met? <- Yes + } + }{ + Print[[ref]Index >>] + met? <- No + } +} +_Check Dependencies@NWorker[worker,nodes,inputs,input index:met?] +{ + wires <- [inputs]Index[input index] + [wires]First + { + current met? <- [worker]Check Dependency[nodes, wires, ~] + }{ + current met? <- Yes + } + If[current met?] + { + [inputs]Next[input index] + { + met? <- [worker]_Check Dependencies[nodes,inputs,~] + }{ + met? <- Yes + } + }{ + met? <- No + } +} + +Check Dependencies@NWorker[worker,nodes,candidate:met?] +{ + inputs <- [[[worker]Nodes >>]Index[candidate]]Wires To >> + [inputs]First + { + met? <- [worker]_Check Dependencies[nodes, inputs, ~] + }{ + met? <- Yes + } +} + +Dependants@NWorker[worker,direct nodes,nodes:out] +{ + candidates <- Keys[Fold[["Collect Dests"]Set Input[0,worker], (), direct nodes]] + out <- Filter[candidates, [["Check Dependencies"]Set Input[0, worker]]Set Input[1, nodes]] +} + +_Dependency Groups@NWorker[worker,last,all,grouped:out] +{ + current <- [worker]Dependants[last,all] + [current]First + { + out <- [worker]_Dependency Groups[current, [all]Concatenate[current], [grouped]Append[current]] + }{ + out <- grouped + } +} +Dependency Groups@NWorker[worker:out] +{ + no deps <- [worker]No Dependencies + out <- [worker]_Dependency Groups[no deps, no deps, [()]Append[no deps]] +} + +Test[:out] +{ + ref+ <- Worker Ref["+","cdecl",2,1] + ref* <- Worker Ref["*","cdecl",2,1] + ,a <- [NWorker["cdecl"] + ]Add Input["a", 0] { + ,b <- [~]Add Input["b", 1] { + ,c <- [~]Add Input["c", 2] { + ,outref <- [~]Add Output["out", 0] { + ,call+ <- [~]Add Worker Call[ref+] { + ,call* <- [~]Add Worker Call[ref*] { + out <- [[[[[~]Add Wire[a,0,call+,0] + ]Add Wire[b,0,call+,1] + ]Add Wire[call+,0,call*,0] + ]Add Wire[c,0,call*,1] + ]Add Wire[call*,0,outref,0] + }}}}}} +} \ No newline at end of file