]> gitweb.ps.run Git - toc/blob - src/repr_get.h
complete grammar
[toc] / src / repr_get.h
1 #pragma once\r
2 \r
3 #include "repr.h"\r
4 \r
5 Type         getType(TocParser::TypeContext * ctx);\r
6 Variable     getVariable(TocParser::VarContext * ctx);\r
7 Body         getBody(TocParser::BodyContext * ctx);\r
8 Function     getFunction(TocParser::FuncContext * ctx);\r
9 Struct       getStruct(TocParser::StructDeclContext * ctx);\r
10 Program      getProgram(TocParser::ProgContext * ctx);\r
11 //Expr         getExpr(TocParser::NonOpExprContext * ctx);\r
12 //Expr         getExpr(TocParser::NonAccessExprContext * ctx);\r
13 Expr         getExpr(TocParser::ExprContext * ctx);\r
14 Stmt         getStmt(TocParser::StmtContext * ctx);\r
15 \r
16 Type getType(TocParser::TypeContext * ctx) {\r
17   Type result;\r
18   result.name = ctx->typeName()->NAME()->toString();\r
19   return result;\r
20 }\r
21 Variable getVariable(TocParser::VarContext * ctx) {\r
22   Variable result;\r
23   result.name = ctx->varName()->NAME()->toString();\r
24   result.type = getType(ctx->type());\r
25   return result;\r
26 }\r
27 Body getBody(TocParser::BodyContext * ctx) {\r
28   Body result;\r
29   for (auto s : ctx->stmt()) {\r
30     if (s->varDecl() != nullptr) {\r
31       result.variables.push_back(getVariable(s->varDecl()->var()));\r
32     }\r
33     else {\r
34       result.statements.push_back(getStmt(s));\r
35     }\r
36   }\r
37   return result;\r
38 }\r
39 Function getFunction(TocParser::FuncContext * ctx) {\r
40   Function result;\r
41   result.name = ctx->funcName()->NAME()->toString();\r
42   result.returnType = getType(ctx->type());\r
43   if (!ctx->parameter()->var().empty()) {\r
44     for (auto p : ctx->parameter()->var())\r
45       result.parameters.push_back(getVariable(p));\r
46   }\r
47   result.body = getBody(ctx->body());\r
48   return result;\r
49 }\r
50 Struct getStruct(TocParser::StructDeclContext * ctx) {\r
51   Struct result;\r
52   result.name = ctx->structName()->NAME()->toString();\r
53   for (auto m : ctx->structMember()) {\r
54     if (m->structVar() != nullptr) {\r
55       result.members.push_back(getVariable(m->structVar()->var()));\r
56     }\r
57     if (m->structMethod() != nullptr) {\r
58       result.methods.push_back(getFunction(m->structMethod()->func()));\r
59     }\r
60   }\r
61   return result;\r
62 }\r
63 Program getProgram(TocParser::ProgContext * ctx) {\r
64   Program result;\r
65   for (auto d : ctx->decl()) {\r
66     if (d->varDecl() != nullptr) {\r
67       result.variables.push_back(getVariable(d->varDecl()->var()));\r
68     }\r
69     if (d->funcDecl() != nullptr) {\r
70       result.functions.push_back(getFunction(d->funcDecl()->func()));\r
71     }\r
72     if (d->structDecl() != nullptr) {\r
73       result.structs.push_back(getStruct(d->structDecl()));\r
74     }\r
75   }\r
76   return result;\r
77 }\r
78 OperatorExpr getOperatorExpr(TocParser::OpExprContext * ctx) {\r
79   OperatorExpr result;\r
80   result.lexpr = std::make_unique<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(0)));\r
81   result.rexpr = std::make_unique<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(1)));\r
82   \r
83   std::string op = ctx->binaryOp()->BINARY_OP(0)->toString();\r
84   for (auto o : ops) {\r
85 \r
86   }\r
87   if (op == "+")  result.type = OperatorType::Plus;\r
88   if (op == "-")  result.type = OperatorType::Minus;\r
89   if (op == "*")  result.type = OperatorType::Multiply;\r
90   if (op == "/")  result.type = OperatorType::Divide;\r
91   if (op == "==") result.type = OperatorType::Equals;\r
92   if (op == "!=") result.type = OperatorType::NotEquals;\r
93   if (op == "<")  result.type = OperatorType::LessThan;\r
94   if (op == ">")  result.type = OperatorType::GreaterThan;\r
95   return result;\r
96 }\r
97 Expr getExpr(TocParser::NonOpExprContext * ctx) {\r
98   Expr result;\r
99   if (ctx->funcCall() != nullptr) {\r
100     result.type = ExprType::Call;\r
101     for (auto e : ctx->funcCall()->expr())\r
102       result._call.arguments.push_back(getExpr(e));\r
103       result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();\r
104   }\r
105   if (ctx->literal() != nullptr) {\r
106     result.type = ExprType::Literal;\r
107     result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());\r
108   }\r
109   if (ctx->identifier() != nullptr) {\r
110     result.type = ExprType::Variable;\r
111     result._variable.name = ctx->identifier()->varName()->NAME()->toString();\r
112   }\r
113   if (ctx->subscript() != nullptr) {\r
114     result.type = ExprType::Brackets;\r
115     result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->nonSubscriptExpr()));\r
116     result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));\r
117   }\r
118   if (ctx->memberAccess() != nullptr) {\r
119     result.type = ExprType::Dot;\r
120     Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();\r
121     result._dot.lexpr = std::make_unique<Expr>(e);\r
122     result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();\r
123   }\r
124   return result;\r
125 }\r
126 Expr getExpr(TocParser::NonAccessExprContext * ctx) {\r
127   Expr result;\r
128   if (ctx->funcCall() != nullptr) {\r
129     result.type = ExprType::Call;\r
130     for (auto e : ctx->funcCall()->expr())\r
131       result._call.arguments.push_back(getExpr(e));\r
132       result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();\r
133   }\r
134   if (ctx->literal() != nullptr) {\r
135     result.type = ExprType::Literal;\r
136     result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());\r
137   }\r
138   if (ctx->identifier() != nullptr) {\r
139     result.type = ExprType::Variable;\r
140     result._variable.name = ctx->identifier()->varName()->NAME()->toString();\r
141   }\r
142   if (ctx->memberAccess() != nullptr) {\r
143     result.type = ExprType::Dot;\r
144     Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();\r
145     result._dot.lexpr = std::make_unique<Expr>(e);\r
146     result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();\r
147   }\r
148   return result;\r
149 }\r
150 Expr getExpr(TocParser::ExprContext * ctx) {\r
151   Expr result;\r
152   if (ctx->funcCall() != nullptr) {\r
153     result.type = ExprType::Call;\r
154     for (auto e : ctx->funcCall()->expr())\r
155       result._call.arguments.push_back(getExpr(e));\r
156       result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();\r
157   }\r
158   if (ctx->literal() != nullptr) {\r
159     result.type = ExprType::Literal;\r
160     result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());\r
161   }\r
162   if (ctx->identifier() != nullptr) {\r
163     result.type = ExprType::Variable;\r
164     result._variable.name = ctx->identifier()->varName()->NAME()->toString();\r
165   }\r
166   if (ctx->subscript() != nullptr) {\r
167     result.type = ExprType::Brackets;\r
168     result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->nonSubscriptExpr()));\r
169     result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));\r
170   }\r
171   if (ctx->memberAccess() != nullptr) {\r
172     result.type = ExprType::Dot;\r
173     Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();\r
174     result._dot.lexpr = std::make_unique<Expr>(e);\r
175     result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();\r
176   }\r
177   if (ctx->operatorExpr() != nullptr) {\r
178     result.type = ExprType::Operator;\r
179     result._operator = getOperatorExpr(ctx->operatorExpr());\r
180   }\r
181   return result;\r
182 }\r
183 Stmt getStmt(TocParser::StmtContext * ctx) {\r
184   Stmt result;\r
185   if (ctx->conditional() != nullptr) {\r
186     result.type = StmtType::If;\r
187     result._if.condition = getExpr(ctx->conditional()->ifCond()->expr());\r
188     result._if.body = getBody(ctx->conditional()->ifCond()->body());\r
189   }\r
190   if (ctx->loop() != nullptr) {\r
191     result.type = StmtType::While;\r
192     result._while.condition = getExpr(ctx->loop()->whileLoop()->expr());\r
193     result._while.body = getBody(ctx->loop()->whileLoop()->body());\r
194   }\r
195   if (ctx->assignment() != nullptr) {\r
196     result.type = StmtType::Assign;\r
197     //result._assign.lexpr = getExpr(ctx->assignment()->);\r
198     result._assign.rexpr = getExpr(ctx->assignment()->expr());\r
199   }\r
200   if (ctx->returnStmt() != nullptr) {\r
201     result.type = StmtType::Return;\r
202     result._return.expr = getExpr(ctx->returnStmt()->expr());\r
203   }\r
204   if (ctx->expr() != nullptr) {\r
205     result.type = StmtType::Expr;\r
206     result._expr = getExpr(ctx->expr());\r
207   }\r
208   if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr) {\r
209     result.type = StmtType::Assign;\r
210     result._assign.rexpr = getExpr(ctx->varDecl()->var()->expr());\r
211   }\r
212   return result;\r
213 }