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