Start adding code generation

This commit is contained in:
Anton Reinhard
2023-08-31 18:24:48 +02:00
parent 32fcd069d7
commit f1edce258a
6 changed files with 251 additions and 10 deletions

32
src/code_gen/main.jl Normal file
View File

@@ -0,0 +1,32 @@
using DataStructures
function gen_code(graph::DAG)
code = Vector{Expr}()
sizehint!(code, length(graph.nodes))
nodeQueue = PriorityQueue{Node, Int}()
inputSyms = Vector{Symbol}()
for node in get_entry_nodes(graph)
enqueue!(nodeQueue, node => 1)
push!(inputSyms, Symbol("data_$(replace(string(node.id), "-"=>"_"))_in"))
end
node = nothing
while !isempty(nodeQueue)
prio = peek(nodeQueue)[2]
node = dequeue!(nodeQueue)
push!(code, get_expression(node))
for parent in node.parents
if (!haskey(nodeQueue, parent))
enqueue!(nodeQueue, parent => prio + length(parent.children))
end
end
end
# node is now the last node we looked at -> the output node
outSym = Symbol("data_$(replace(string(node.id), "-"=>"_"))")
return (code = Expr(:block, code...), inputSymbols = inputSyms, outputSymbol = outSym)
end