-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpressionPrinter.cs
169 lines (140 loc) · 4.86 KB
/
ExpressionPrinter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Compiler
{
public class Printer
{
public string Print(GrammarNode gnode)
{
switch (gnode)
{
case ArrayNode anode: return Print(anode);
case ArrayAssign assign: return Print(assign);
case ArrayTypeNode av: return Print(av);
case BinaryOperationNode bnode: return Print(bnode);
case BoolNode bnode: return Print(bnode);
case ByteNode bnode: return Print(bnode);
case CastNode cnode: return Print(cnode);
case DeclarationNode dnode: return Print(dnode);
case FixedTypeNode fv: return Print(fv);
case FuncNode fnode: return Print(fnode);
case Initialization init: return Print(init);
case IntegerNode inode: return Print(inode);
case LoopNode lnode: return Print(lnode);
case MethodNode mnode: return Print(mnode);
case ReturnExpression rexpr: return Print(rexpr);
case PrimitiveTypeNode p: return Print(p);
case SimpleAssignement sa: return Print(sa);
case VariableNode vnode: return Print(vnode);
case VoidNode vnode: return Print(vnode);
default: throw new Exception("Printer not defined for " + gnode);
}
}
public string Print(IntegerNode inode)
{
return inode.t + "";
}
public string Print(BoolNode bnode)
{
return "" + bnode.t;
}
public string Print(ByteNode bnode)
{
return "" + bnode.t;
}
public string Print(VariableNode vnode)
{
return "" + vnode.t;
}
public string Print(CastNode cnode)
{
return "(" + Print(cnode.primitive) + ")" + " " + Print(cnode.tnode);
}
public string Print(BinaryOperationNode bnode)
{
return "(" + Print(bnode.left) + ")"
+ bnode.operation + "(" + Print(bnode.right) + ")";
}
public string Print(ArrayNode anode)
{
return anode.identifier + "[" + Print(anode.index) + "]";
}
public String Print(FuncNode fnode)
{
if (fnode.arguments.Count == 0)
return fnode.identifier + "(" + ")";
string str = Print(fnode.arguments[0]);
for (int i = 1; i < fnode.arguments.Count; i++)
{
str += "," + Print(fnode.arguments[i]);
}
return fnode.identifier + "(" + str + ")";
}
public String Print(MethodNode mnode)
{
string declarations = (mnode.inputs.Count) == 0 ? ""
: Print(mnode.inputs[0]);
string bodyString = "";
mnode.body.ForEach(x => bodyString += "\t" + Print(x) + "\n");
for (int i = 1; i < mnode.inputs.Count; i++)
{
declarations += "," + Print(mnode.inputs[i]);
}
return "Secure " + Print(mnode.t) + " " + mnode.name
+ "(" + declarations + ")"
+ "\n { \n" + bodyString + "\n } \n";
}
public string Print(PrimitiveTypeNode p)
{
return p.type;
}
public string Print(VoidNode v)
{
return "void";
}
public string Print(ArrayTypeNode av)
{
return Print(av.type) + "[]";
}
public string Print(FixedTypeNode ftn)
{
return "fixed " + Print(ftn.simpleTypeNode);
}
public String Print(DeclarationNode dnode)
{
return Print(dnode.t) + " " + dnode.name;
}
public String Print(LoopNode lnode)
{
string str = "loop(int " + lnode.name + "="
+ lnode.init + "," + lnode.times
+ ")" + "\n" + "{";
foreach (Expression exp in lnode.lexpr)
{
str += "\n \t" + Print(exp);
}
return str + "\n" + "}" + "\n";
}
public string Print(ReturnExpression rexpr)
{
return " return " + Print(rexpr.node) + " ; ";
}
public String Print(SimpleAssignement sassign)
{
return sassign.name + "=" + Print(sassign.tree);
}
public String Print(Initialization init)
{
return Print(init.type) + " " + init.name
+ "=" + Print(init.tree);
}
public string Print(ArrayAssign aa)
{
return aa.name + "[" + Print(aa.index)
+ "]" + " = " + Print(aa.tree);
}
}
}