]> gitweb.ps.run Git - toc/blob - src/toc.h
add namespace, private struct member grammar, change bracket style
[toc] / src / toc.h
1 #pragma once\r
2 \r
3 #include <iostream>\r
4 #include <sstream>\r
5 \r
6 #include "repr.h"\r
7 \r
8 template<typename T>\r
9 std::ostream & operator<< (std::ostream & out, const std::vector<T> & v)\r
10 {\r
11   bool comma = false;\r
12   for (auto t : v)\r
13   {\r
14     if (comma) out << ", ";\r
15     else comma = true;\r
16     out << t;\r
17   }\r
18   return out;\r
19 }\r
20 \r
21 std::ostream & operator<< (std::ostream & out, const Type & t);\r
22 std::ostream & operator<< (std::ostream & out, const Variable & v);\r
23 std::ostream & operator<< (std::ostream & out, const Body & b);\r
24 std::ostream & operator<< (std::ostream & out, const UnaryOperatorExpr & o);\r
25 std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o);\r
26 std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o);\r
27 std::ostream & operator<< (std::ostream & out, const Expr & e);\r
28 std::ostream & operator<< (std::ostream & out, const Stmt & s);\r
29 \r
30 void tocFunction (std::ostream & out, const Function & f, bool stub);\r
31 void tocStruct   (std::ostream & out, const Struct & s, bool stub);\r
32 void tocProgram  (std::ostream & out, const Program & p);\r
33 \r
34 static const int TAB_WIDTH = 2;\r
35 static int indentation = 0;\r
36 static void indent(std::ostream & out, int change = 0)\r
37 {\r
38   indentation += change;\r
39   out << std::string(indentation, ' ');\r
40 }\r
41 \r
42 std::ostream & operator<< (std::ostream & out, const Type & t)\r
43 {\r
44   out << t.name;\r
45 \r
46   return out;\r
47 }\r
48 std::ostream & operator<< (std::ostream & out, const Variable & v)\r
49 {\r
50   out << v.type << " ";\r
51 \r
52   std::stringstream sstr;\r
53   std::string s = v.name;\r
54 \r
55   for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++)\r
56   {\r
57     if (m->type == TypeModifierType::Pointer)\r
58     {\r
59       sstr.str(std::string());\r
60       sstr << "*(" << s << ")";\r
61       s = sstr.str();\r
62     }\r
63     else\r
64     {\r
65       sstr.str(std::string());\r
66       sstr << "(" << s << ")[";\r
67       if (m->_staticArray)\r
68         sstr << m->_arraySize;\r
69       sstr << "]";\r
70       s = sstr.str();\r
71     }\r
72   }\r
73   out << s;\r
74 \r
75   return out;\r
76 }\r
77 std::ostream & operator<< (std::ostream & out, const Body & b)\r
78 {\r
79   indent(out);\r
80   out << "{\n";\r
81   indentation += 2;\r
82 \r
83   for (auto v : b.variables)\r
84   {\r
85     indent(out);\r
86     out << v << ";\n";\r
87   }\r
88 \r
89   out << "\n";\r
90   \r
91   for (auto s : b.statements)\r
92   {\r
93     indent(out);\r
94     out << s << "\n";\r
95   }\r
96 \r
97   indent(out, -2);\r
98   out << "}\n";\r
99 \r
100   return out;\r
101 }\r
102 std::ostream & operator<< (std::ostream & out, const UnaryOperatorExpr & o)\r
103 {\r
104   if (o.type == UnaryOperatorType::IncrementPost || o.type == UnaryOperatorType::DecrementPost)\r
105   {\r
106     out << UnaryOperatorTypeStrings[(int)o.type] << *o.expr;\r
107   }\r
108   else\r
109   {\r
110     out << *o.expr << UnaryOperatorTypeStrings[(int)o.type];\r
111   }\r
112 \r
113   return out;\r
114 }\r
115 std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o)\r
116 {\r
117   out << *o.lexpr << " " << BinaryOperatorTypeStrings[(int)o.type] << " " << *o.rexpr;\r
118 \r
119   return out;\r
120 }\r
121 std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o)\r
122 {\r
123   out << *o.lexpr << " ? " << *o.rexprTrue << " : " << *o.rexprFalse;\r
124 \r
125   return out;\r
126 }\r
127 std::ostream & operator<< (std::ostream & out, const Expr & e)\r
128 {\r
129   if (e.parenthesized)\r
130     out << "(";\r
131 \r
132   switch (e.type)\r
133   {\r
134   case ExprType::Func:\r
135     out << e._func.functionName << "(" << e._func.arguments << ")"; break;\r
136   case ExprType::Lit:\r
137     /**/ if (e._lit.type == LitType::Int) out << e._lit._int;\r
138     else if (e._lit.type == LitType::Decimal) out << e._lit._decimal;\r
139     else if (e._lit.type == LitType::String) out << e._lit._string;\r
140     else if (e._lit.type == LitType::Bool) out << e._lit._bool;\r
141     break;\r
142   case ExprType::Identifier:\r
143     out << e._identifier.name; break;\r
144   case ExprType::Brackets:\r
145     out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;\r
146   case ExprType::Dot:\r
147     out << *e._dot.expr << "." << e._dot.ident.name; break;\r
148   case ExprType::UnaryOperator:\r
149     out << e._unaryOperator; break;\r
150   case ExprType::BinaryOperator:\r
151     out << e._binaryOperator; break;\r
152   case ExprType::TernaryOperator:\r
153     out << e._ternaryOperator; break;\r
154   }\r
155   \r
156   if (e.parenthesized)\r
157     out << ")";\r
158 \r
159   return out;\r
160 }\r
161 std::ostream & operator<< (std::ostream & out, const Stmt & s)\r
162 {\r
163   switch (s.type)\r
164   {\r
165   case StmtType::If:\r
166     out << "if (" << s._if.condition << ")\n" << s._if.body; break;\r
167   case StmtType::Switch:\r
168     out << "switch (" << s._switch.ident.name << ")\n{\n";\r
169     for (auto c : s._switch.cases)\r
170     {\r
171       indent(out, 2);\r
172       out << "case " << *c.expr << ": " << c.body << "break;";\r
173     }\r
174     indent(out, -2);\r
175     out << "}\n";\r
176     break;\r
177   case StmtType::For:\r
178     out << "for (" <<\r
179       s._for.varName << " = " << *s._for.initValue << "; " <<\r
180       *s._for.condition << "; " <<\r
181       *s._for.action <<\r
182       ")\n" << s._for.body; break;\r
183   case StmtType::While:\r
184     out << "while (" << s._while.condition << ")\n" << s._while.body; break;\r
185   case StmtType::Assign:\r
186     out << s._assign.name << " = " << s._assign.expr << ";"; break;\r
187   case StmtType::Return:\r
188     out << "return " << s._return.expr << ";"; break;\r
189   case StmtType::Expr:\r
190     out << s._expr << ";"; break;\r
191   }\r
192 \r
193   return out;\r
194 }\r
195 \r
196 \r
197 void tocFunction (std::ostream & out, const Function & f, bool stub)\r
198 {\r
199   out << f.returnType << " " << f.name << " (" << f.parameters << ")";\r
200 \r
201   if (stub)\r
202   {\r
203     out << ";\n";\r
204   }\r
205   else\r
206   {\r
207     out << "\n" << f.body;\r
208   }\r
209 }\r
210 void tocStruct (std::ostream & out, const Struct & s, bool stub)\r
211 {\r
212   out << "struct " << s.name;\r
213   if (stub)\r
214   {\r
215     out << ";\n";\r
216     for (auto m : s.methods)\r
217     {\r
218       m.parameters.insert(m.parameters.begin(),\r
219       {"this",\r
220       {s.name,\r
221       {{TypeModifierType::Pointer, false, -1}}}});\r
222       out << m.returnType << " " <<\r
223         s.name << "_" << m.name <<\r
224         " (" << m.parameters << ");\n";\r
225     }\r
226     return;\r
227   }\r
228   out << "\n{\n";\r
229   indentation += 2;\r
230 \r
231   for (auto m : s.members)\r
232   {\r
233     indent(out);\r
234     out << m << ";\n";\r
235   }\r
236 \r
237   indent(out, -2);\r
238   out << "};\n";\r
239   \r
240   for (auto m : s.methods)\r
241   {\r
242     m.parameters.insert(m.parameters.begin(),\r
243     {"this",\r
244     {s.name,\r
245     {{TypeModifierType::Pointer, false, -1}}}});  \r
246     out << m.returnType << " " << s.name << "_" << m.name << " (" << m.parameters << ")\n" << m.body;\r
247   }\r
248 }\r
249 void tocProgram (std::ostream & out, const Program & p)\r
250 {\r
251   for (auto s : p.structs)\r
252   {\r
253     tocStruct(out, s, true);\r
254   }\r
255   for (auto f : p.functions)\r
256   {\r
257     tocFunction(out, f, true);\r
258   }\r
259 \r
260   for (auto v : p.variables)\r
261   {\r
262     out << v << ";\n";\r
263   }\r
264   for (auto s : p.structs)\r
265   {\r
266     tocStruct(out, s, false);\r
267   }\r
268   for (auto f : p.functions)\r
269   {\r
270     tocFunction(out, f, false);\r
271   }\r
272 }\r