abouttreesummaryrefslogcommitdiff
path: root/src/repr_get.h
blob: 7dbeea36bb5af1f195e252b7144edd63ac9f1261 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#pragma once

#include "repr.h"

Type         getType(TocParser::TypeContext * ctx);
Variable     getVariable(TocParser::VarContext * ctx);
Body         getBody(TocParser::BodyContext * ctx);
Function     getFunction(TocParser::FuncContext * ctx);
Struct       getStruct(TocParser::StructDeclContext * ctx);
Program      getProgram(TocParser::ProgContext * ctx);
OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx);
Expr         getExpr(TocParser::NonOpExprContext * ctx);
Expr         getExpr(TocParser::NonSubscriptExprContext * ctx);
Expr         getExpr(TocParser::ExprContext * ctx);
Stmt         getStmt(TocParser::StmtContext * ctx);

Type getType(TocParser::TypeContext * ctx) {
  Type result;
  result.name = ctx->typeName()->NAME()->toString();
  return result;
}
Variable getVariable(TocParser::VarContext * ctx) {
  Variable result;
  result.name = ctx->varName()->NAME()->toString();
  result.type = getType(ctx->type());
  return result;
}
Body getBody(TocParser::BodyContext * ctx) {
  Body result;
  for (auto s : ctx->stmt()) {
    if (s->varDecl() != nullptr) {
      result.variables.push_back(getVariable(s->varDecl()->var()));
    }
    else {
      result.statements.push_back(getStmt(s));
    }
  }
  return result;
}
Function getFunction(TocParser::FuncContext * ctx) {
  Function result;
  result.name = ctx->funcName()->NAME()->toString();
  result.returnType = getType(ctx->type());
  if (!ctx->parameter()->var().empty()) {
    for (auto p : ctx->parameter()->var())
      result.parameters.push_back(getVariable(p));
  }
  result.body = getBody(ctx->body());
  return result;
}
Struct getStruct(TocParser::StructDeclContext * ctx) {
  Struct result;
  result.name = ctx->structName()->NAME()->toString();
  for (auto m : ctx->structMember()) {
    if (m->structVar() != nullptr) {
      result.members.push_back(getVariable(m->structVar()->var()));
    }
    if (m->structMethod() != nullptr) {
      result.methods.push_back(getFunction(m->structMethod()->func()));
    }
  }
  return result;
}
Program getProgram(TocParser::ProgContext * ctx) {
  Program result;
  for (auto d : ctx->decl()) {
    if (d->varDecl() != nullptr) {
      result.variables.push_back(getVariable(d->varDecl()->var()));
    }
    if (d->funcDecl() != nullptr) {
      result.functions.push_back(getFunction(d->funcDecl()->func()));
    }
    if (d->structDecl() != nullptr) {
      result.structs.push_back(getStruct(d->structDecl()));
    }
  }
  return result;
}
OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx) {
  OperatorExpr result;
  result.lexpr = std::make_unique<Expr>(getExpr(ctx->binaryOperator()->nonOpExpr(0)));
  result.rexpr = std::make_unique<Expr>(getExpr(ctx->binaryOperator()->nonOpExpr(1)));
  
  std::string op = ctx->binaryOperator()->BINARY_OPERATOR(0)->toString();
  if (op == "+")  result.type = OperatorType::Plus;
  if (op == "-")  result.type = OperatorType::Minus;
  if (op == "*")  result.type = OperatorType::Multiply;
  if (op == "/")  result.type = OperatorType::Divide;
  if (op == "==") result.type = OperatorType::Equals;
  if (op == "!=") result.type = OperatorType::NotEquals;
  if (op == "<")  result.type = OperatorType::LessThan;
  if (op == ">")  result.type = OperatorType::GreaterThan;
  return result;
}
Expr getExpr(TocParser::NonOpExprContext * ctx) {
  Expr result;
  if (ctx->funcCall() != nullptr) {
    result.type = ExprType::Call;
    for (auto e : ctx->funcCall()->expr())
      result._call.arguments.push_back(getExpr(e));
      result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();
  }
  if (ctx->literal() != nullptr) {
    result.type = ExprType::Literal;
    result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());
  }
  if (ctx->identifier() != nullptr) {
    result.type = ExprType::Variable;
    result._variable.name = ctx->identifier()->varName()->NAME()->toString();
  }
  if (ctx->subscript() != nullptr) {
    result.type = ExprType::Brackets;
    result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->nonSubscriptExpr()));
    result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));
  }
  if (ctx->memberAccess() != nullptr) {
    result.type = ExprType::Dot;
    Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();
    result._dot.lexpr = std::make_unique<Expr>(e);
    result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();
  }
  return result;
}
Expr getExpr(TocParser::NonSubscriptExprContext * ctx) {
  Expr result;
  if (ctx->funcCall() != nullptr) {
    result.type = ExprType::Call;
    for (auto e : ctx->funcCall()->expr())
      result._call.arguments.push_back(getExpr(e));
      result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();
  }
  if (ctx->literal() != nullptr) {
    result.type = ExprType::Literal;
    result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());
  }
  if (ctx->identifier() != nullptr) {
    result.type = ExprType::Variable;
    result._variable.name = ctx->identifier()->varName()->NAME()->toString();
  }
  if (ctx->memberAccess() != nullptr) {
    result.type = ExprType::Dot;
    Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();
    result._dot.lexpr = std::make_unique<Expr>(e);
    result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();
  }
  return result;
}
Expr getExpr(TocParser::ExprContext * ctx) {
  Expr result;
  if (ctx->funcCall() != nullptr) {
    result.type = ExprType::Call;
    for (auto e : ctx->funcCall()->expr())
      result._call.arguments.push_back(getExpr(e));
      result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();
  }
  if (ctx->literal() != nullptr) {
    result.type = ExprType::Literal;
    result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());
  }
  if (ctx->identifier() != nullptr) {
    result.type = ExprType::Variable;
    result._variable.name = ctx->identifier()->varName()->NAME()->toString();
  }
  if (ctx->subscript() != nullptr) {
    result.type = ExprType::Brackets;
    result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->nonSubscriptExpr()));
    result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));
  }
  if (ctx->memberAccess() != nullptr) {
    result.type = ExprType::Dot;
    Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();
    result._dot.lexpr = std::make_unique<Expr>(e);
    result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();
  }
  if (ctx->operatorExpr() != nullptr) {
    result.type = ExprType::Operator;
    result._operator = getOperatorExpr(ctx->operatorExpr());
  }
  return result;
}
Stmt getStmt(TocParser::StmtContext * ctx) {
  Stmt result;
  if (ctx->conditional() != nullptr) {
    result.type = StmtType::If;
    result._if.condition = getExpr(ctx->conditional()->ifCond()->expr());
    result._if.body = getBody(ctx->conditional()->ifCond()->body());
  }
  if (ctx->loop() != nullptr) {
    result.type = StmtType::While;
    result._while.condition = getExpr(ctx->loop()->whileLoop()->expr());
    result._while.body = getBody(ctx->loop()->whileLoop()->body());
  }
  if (ctx->assignment() != nullptr) {
    result.type = StmtType::Assign;
    //result._assign.lexpr = getExpr(ctx->assignment()->);
    result._assign.rexpr = getExpr(ctx->assignment()->expr());
  }
  if (ctx->returnStmt() != nullptr) {
    result.type = StmtType::Return;
    result._return.expr = getExpr(ctx->returnStmt()->expr());
  }
  if (ctx->expr() != nullptr) {
    result.type = StmtType::Expr;
    result._expr = getExpr(ctx->expr());
  }
  if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr) {
    result.type = StmtType::Assign;
    result._assign.rexpr = getExpr(ctx->varDecl()->var()->expr());
  }
  return result;
}