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