1 module dpp2.translation.node;
2 
3 
4 import dpp.from;
5 
6 
7 string[] translate(in from!"dpp2.sea.node".Node[] nodes) {
8     import std.algorithm: map;
9     import std.array: join;
10     return nodes.map!translate.join;
11 }
12 
13 
14 string[] translate(in from!"dpp2.sea.node".Node node)
15     @safe pure
16 {
17     import dpp2.sea.node;
18     import sumtype: match;
19 
20     return node.match!(
21         translateStruct,
22         translateField,
23         translateTypedef,
24     );
25 }
26 
27 
28 string[] translateStruct(in from!"dpp2.sea.node".Struct struct_)
29     @safe pure
30 {
31     import dpp2.sea.node: Node;
32     import std.algorithm: map;
33     import std.array: array, join;
34 
35     string[] lines;
36 
37     const spelling = struct_.spelling == ""
38         ? struct_.typeSpelling
39         : struct_.spelling;
40 
41     lines ~= "struct " ~ spelling;
42     lines ~= "{";
43 
44     lines ~= struct_
45         .nodes
46         .map!translate
47         .join
48         ;
49 
50     lines ~= "}";
51 
52     return lines;
53 }
54 
55 
56 string[] translateField(in from!"dpp2.sea.node".Field field)
57     @safe pure
58 {
59     import dpp2.translation.type: translate;
60     return ["    " ~ translate(field.type) ~ " " ~ field.spelling ~ ";"];
61 }
62 
63 
64 string[] translateTypedef(in from!"dpp2.sea.node".Typedef typedef_)
65     @safe pure
66 {
67     import dpp2.translation.type: translate;
68     const underlyingTranslation = translate(typedef_.underlying);
69     return typedef_.spelling == underlyingTranslation
70         ? []
71         : ["alias " ~ typedef_.spelling ~ " = " ~  underlyingTranslation ~ ";"];
72 }