1 module tfd.gen_ops;
2 
3 import pegged.grammar;
4 import tfd.pbtxt;
5 
6 
7 string codegen(ParseTree tree)
8 {
9   assert(tree.name == "PbTxt.Dict");
10   assert(tree.matches[0] == "op");
11 
12   string opname;
13   // TODO
14   int nin;
15   foreach (child; tree.children)
16   {
17     switch (child.matches[0])
18     {
19       case "name":
20         assert(child.name == "PbTxt.Pair");
21         opname = child.matches[1];
22         break;
23       default:
24         break;
25     }
26   }
27 
28   string option = `struct ` ~ opname ~ `Option {}`;
29   
30   return option ~ "\n" ~ "TF_Operation* " ~ opname ~
31       `(TF_Graph* graph, TF_Status* status, string name, TF_Operation*[` ~ nin ~ `] inops) 
32 {
33   import std.string : fromStringz;
34 
35   auto desc = TF_NewOperation(graph, "` ~ opname ~ `", name);
36   TF_Output[` ~ nin ~ `] inputs;
37   static foreach (i; 0 .. ` ~ nin ~ `)
38   {
39     inputs[i] = TF_Output(inops[i], 0);
40   }
41   TF_AddInputList(desc, inputs.ptr, inputs.length);
42   TF_Operation* op = TF_FinishOperation(desc, status);
43   assert(TF_GetCode(status) == TF_OK, TF_Message(status).fromStringz);
44   return desc;
45 }`;
46 }
47 
48 unittest
49 {
50   import std;
51 
52   auto tree = PbTxt(exampleProto);
53   auto root = tree.children[0];
54 
55   auto addN = root.children[0];
56   writeln(codegen(addN));
57   
58   auto identity = root.children[1];
59 }