abouttreesummaryrefslogcommitdiff
path: root/src/repr_get.h
blob: 67ff40f3cbd08ff59fe67460cc05e7e4de33803b (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
#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 getExpression(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();
  if (ctx->parameter()->firstParameter() != nullptr) {
    result.parameters.push_back(getVariable(ctx->parameter()->firstParameter()->var()));
    for (auto p : ctx->parameter()->additionalParameter())
      result.parameters.push_back(getVariable(p->var()));
  }
  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 = getExpr(ctx->binaryOperator()->nonOpExpr(0));
  //result.rexpr = 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::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.function = ctx->funcCall()->funcName();
  }
  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 = getExpr(ctx->subscript()->nonSubscriptExpr());
    result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));
  }
  if (ctx->memberAccess() != nullptr) {
    result.type = ExprType::Dot;
    //result._dot.lexpr = ctx->memberAccess()->identifier(0);
    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;
}