]> gitweb.ps.run Git - toc/blobdiff - src/repr_get.h
comments
[toc] / src / repr_get.h
index 7dbeea36bb5af1f195e252b7144edd63ac9f1261..373b366ae3849da99bb4c7a1441986bf65c34efd 100644 (file)
 \r
 #include "repr.h"\r
 \r
-Type         getType(TocParser::TypeContext * ctx);\r
-Variable     getVariable(TocParser::VarContext * ctx);\r
-Body         getBody(TocParser::BodyContext * ctx);\r
-Function     getFunction(TocParser::FuncContext * ctx);\r
-Struct       getStruct(TocParser::StructDeclContext * ctx);\r
-Program      getProgram(TocParser::ProgContext * ctx);\r
-OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx);\r
-Expr         getExpr(TocParser::NonOpExprContext * ctx);\r
-Expr         getExpr(TocParser::NonSubscriptExprContext * ctx);\r
-Expr         getExpr(TocParser::ExprContext * ctx);\r
-Stmt         getStmt(TocParser::StmtContext * ctx);\r
-\r
-Type getType(TocParser::TypeContext * ctx) {\r
+// Transform ANTLR-generated types to corresponding IR types recursively\r
+\r
+Type                getType(TocParser::TypeContext * ctx);\r
+Variable            getVariable(TocParser::VarContext * ctx);\r
+Body                getBody(TocParser::BodyContext * ctx, std::shared_ptr<Context> parent);\r
+Function            getFunction(TocParser::FuncContext * ctx, std::shared_ptr<Context> parent);\r
+Struct              getStruct(TocParser::StructDeclContext * ctx, std::shared_ptr<Context> parent);\r
+Namespace           getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr<Context> parent);\r
+Program             getProgram(TocParser::ProgContext * ctx, std::shared_ptr<Context> parent);\r
+\r
+\r
+Expr                getExpr(TocParser::FuncExprContext * ctx);\r
+Expr                getExpr(TocParser::MethodExprContext * ctx);\r
+Expr                getExpr(TocParser::LitExprContext * ctx);\r
+Expr                getExpr(TocParser::ParenExprContext * ctx);\r
+Expr                getExpr(TocParser::DotExprContext * ctx);\r
+Expr                getExpr(TocParser::PrefixOpExprContext * ctx);\r
+Expr                getExpr(TocParser::PostfixOpExprContext * ctx);\r
+Expr                getExpr(TocParser::BinaryOpExprContext * ctx);\r
+Expr                getExpr(TocParser::TernaryOpExprContext * ctx);\r
+Expr                getExpr(TocParser::BracketExprContext * ctx);\r
+Expr                getExpr(TocParser::IdentifierExprContext * ctx);\r
+Expr                getExpr(TocParser::ExprContext * ctx);\r
+\r
+Stmt                getStmt(TocParser::StmtContext * ctx, std::shared_ptr<Context> parent);\r
+\r
+// all of these functions get the relevant information\r
+// from the parse tree and call each other for sub expressions\r
+// the getVariable is called for variable declarations and parameter definitions\r
+// for example, because they have the same rule in the grammar file\r
+\r
+Type getType(TocParser::TypeContext * ctx)\r
+{\r
   Type result;\r
+  for (auto n : ctx->namespaceSpecifier())\r
+    result.namespacePrefixes.push_back(n->typeName()->getText());\r
   result.name = ctx->typeName()->NAME()->toString();\r
+  for (auto m : ctx->typeModifier())\r
+  {\r
+    bool isPointer = m->getText() == "*";\r
+    bool isStaticArray = m->INT_LIT() != nullptr;\r
+\r
+    result.modifiers.emplace_back(\r
+      isPointer ? TypeModifierType::Pointer : TypeModifierType::Array,\r
+      isStaticArray,\r
+      isStaticArray ? atoi(m->INT_LIT()->toString().c_str()) : -1\r
+    );\r
+  }\r
+  if (ctx->genericInstantiation() != nullptr)\r
+  {\r
+    for (auto g : ctx->genericInstantiation()->type())\r
+    {\r
+      result.genericInstantiation.push_back(getType(g));\r
+    }\r
+  }\r
   return result;\r
 }\r
-Variable getVariable(TocParser::VarContext * ctx) {\r
+Variable getVariable(TocParser::VarContext * ctx)\r
+{\r
   Variable result;\r
   result.name = ctx->varName()->NAME()->toString();\r
   result.type = getType(ctx->type());\r
   return result;\r
 }\r
-Body getBody(TocParser::BodyContext * ctx) {\r
+Body getBody(TocParser::BodyContext * ctx, std::shared_ptr<Context> parent)\r
+{\r
   Body result;\r
-  for (auto s : ctx->stmt()) {\r
-    if (s->varDecl() != nullptr) {\r
-      result.variables.push_back(getVariable(s->varDecl()->var()));\r
+  result.ctx = std::make_unique<Context>();\r
+  result.ctx->parent = parent;\r
+  for (auto s : ctx->stmt())\r
+  {\r
+    if (s->varDecl() != nullptr)\r
+    {\r
+      result.ctx->variables.push_back(getVariable(s->varDecl()->var()));\r
+      if (s->varDecl()->var()->expr() != nullptr)\r
+        result.statements.push_back(getStmt(s, result.ctx));\r
     }\r
-    else {\r
-      result.statements.push_back(getStmt(s));\r
+    else\r
+    {\r
+      result.statements.push_back(getStmt(s, result.ctx));\r
     }\r
   }\r
   return result;\r
 }\r
-Function getFunction(TocParser::FuncContext * ctx) {\r
+Function getFunction(TocParser::FuncContext * ctx, std::shared_ptr<Context> parent)\r
+{\r
   Function result;\r
   result.name = ctx->funcName()->NAME()->toString();\r
   result.returnType = getType(ctx->type());\r
-  if (!ctx->parameter()->var().empty()) {\r
+  if (ctx->genericDecl() != nullptr)\r
+  {\r
+    for (auto t : ctx->genericDecl()->typeName())\r
+    {\r
+      result.genericTypeNames.push_back(t->getText());\r
+    }\r
+  }\r
+\r
+  if (!ctx->parameter()->var().empty())\r
+  {\r
     for (auto p : ctx->parameter()->var())\r
       result.parameters.push_back(getVariable(p));\r
   }\r
-  result.body = getBody(ctx->body());\r
+\r
+  if (ctx->body() != nullptr)\r
+  {\r
+    result.body = getBody(ctx->body(), parent);\r
+    result.defined = true;\r
+  }\r
+  else\r
+  {\r
+    result.defined = false;\r
+  }\r
   return result;\r
 }\r
-Struct getStruct(TocParser::StructDeclContext * ctx) {\r
+Struct getStruct(TocParser::StructDeclContext * ctx, std::shared_ptr<Context> parent)\r
+{\r
   Struct result;\r
   result.name = ctx->structName()->NAME()->toString();\r
-  for (auto m : ctx->structMember()) {\r
-    if (m->structVar() != nullptr) {\r
-      result.members.push_back(getVariable(m->structVar()->var()));\r
+  if (ctx->genericDecl() != nullptr)\r
+  {\r
+    for (auto t : ctx->genericDecl()->typeName())\r
+    {\r
+      result.genericTypeNames.push_back(t->getText());\r
     }\r
-    if (m->structMethod() != nullptr) {\r
-      result.methods.push_back(getFunction(m->structMethod()->func()));\r
+  }\r
+\r
+  for (auto m : ctx->structMember())\r
+  {\r
+    if (m->structVar() != nullptr)\r
+    {\r
+      result.members.push_back({\r
+        getVariable(m->structVar()->var()),\r
+        m->privateDecl() != nullptr\r
+      });\r
+    }\r
+    if (m->structMethod() != nullptr)\r
+    {\r
+      result.methods.push_back({\r
+        getFunction(m->structMethod()->func(), parent),\r
+        m->privateDecl() != nullptr\r
+      });\r
     }\r
   }\r
   return result;\r
 }\r
-Program getProgram(TocParser::ProgContext * ctx) {\r
-  Program result;\r
-  for (auto d : ctx->decl()) {\r
-    if (d->varDecl() != nullptr) {\r
-      result.variables.push_back(getVariable(d->varDecl()->var()));\r
+Namespace getNamespace(TocParser::NamespaceDeclContext * ctx, std::shared_ptr<Context> parent)\r
+{\r
+  Namespace result;\r
+  result.ctx = std::make_unique<Context>();\r
+  result.name = ctx->typeName()->getText();\r
+  result.ctx->name = result.name;\r
+  for (auto d : ctx->decl())\r
+  {\r
+    if (d->varDecl() != nullptr)\r
+    {\r
+      result.ctx->variables.push_back(getVariable(d->varDecl()->var()));\r
     }\r
-    if (d->funcDecl() != nullptr) {\r
-      result.functions.push_back(getFunction(d->funcDecl()->func()));\r
+    if (d->funcDecl() != nullptr)\r
+    {\r
+      result.ctx->functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
     }\r
-    if (d->structDecl() != nullptr) {\r
-      result.structs.push_back(getStruct(d->structDecl()));\r
+    if (d->structDecl() != nullptr)\r
+    {\r
+      result.ctx->structs.push_back(getStruct(d->structDecl(), result.ctx));\r
+    }\r
+    if (d->namespaceDecl() != nullptr)\r
+    {\r
+      result.ctx->namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
     }\r
   }\r
   return result;\r
 }\r
-OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx) {\r
-  OperatorExpr result;\r
-  result.lexpr = std::make_unique<Expr>(getExpr(ctx->binaryOperator()->nonOpExpr(0)));\r
-  result.rexpr = std::make_unique<Expr>(getExpr(ctx->binaryOperator()->nonOpExpr(1)));\r
-  \r
-  std::string op = ctx->binaryOperator()->BINARY_OPERATOR(0)->toString();\r
-  if (op == "+")  result.type = OperatorType::Plus;\r
-  if (op == "-")  result.type = OperatorType::Minus;\r
-  if (op == "*")  result.type = OperatorType::Multiply;\r
-  if (op == "/")  result.type = OperatorType::Divide;\r
-  if (op == "==") result.type = OperatorType::Equals;\r
-  if (op == "!=") result.type = OperatorType::NotEquals;\r
-  if (op == "<")  result.type = OperatorType::LessThan;\r
-  if (op == ">")  result.type = OperatorType::GreaterThan;\r
+Program getProgram(TocParser::ProgContext * ctx, std::shared_ptr<Context> parent)\r
+{\r
+  Program result;\r
+  result.ctx = std::make_unique<Context>();\r
+  for (auto d : ctx->decl())\r
+  {\r
+    if (d->varDecl() != nullptr)\r
+    {\r
+      result.ctx->variables.push_back(getVariable(d->varDecl()->var()));\r
+    }\r
+    if (d->funcDecl() != nullptr)\r
+    {\r
+      result.ctx->functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
+    }\r
+    if (d->structDecl() != nullptr)\r
+    {\r
+      result.ctx->structs.push_back(getStruct(d->structDecl(), result.ctx));\r
+    }\r
+    if (d->namespaceDecl() != nullptr)\r
+    {\r
+      result.ctx->namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
+    }\r
+  }\r
   return result;\r
 }\r
-Expr getExpr(TocParser::NonOpExprContext * ctx) {\r
-  Expr result;\r
-  if (ctx->funcCall() != nullptr) {\r
-    result.type = ExprType::Call;\r
-    for (auto e : ctx->funcCall()->expr())\r
-      result._call.arguments.push_back(getExpr(e));\r
-      result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();\r
-  }\r
-  if (ctx->literal() != nullptr) {\r
-    result.type = ExprType::Literal;\r
-    result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());\r
-  }\r
-  if (ctx->identifier() != nullptr) {\r
-    result.type = ExprType::Variable;\r
-    result._variable.name = ctx->identifier()->varName()->NAME()->toString();\r
-  }\r
-  if (ctx->subscript() != nullptr) {\r
-    result.type = ExprType::Brackets;\r
-    result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->nonSubscriptExpr()));\r
-    result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));\r
+template<typename OpType>\r
+OpType getOperatorType(const std::string & s, std::string typeStrings[])\r
+{\r
+  for (int i = 0; i < (int)OpType::COUNT; i++)\r
+  {\r
+    if (typeStrings[i] == s)\r
+    {\r
+      return (OpType)i;\r
+    }\r
   }\r
-  if (ctx->memberAccess() != nullptr) {\r
-    result.type = ExprType::Dot;\r
-    Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();\r
-    result._dot.lexpr = std::make_unique<Expr>(e);\r
-    result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();\r
+  return OpType::COUNT;\r
+}\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+// Expressions are somewhat of an exception, because some of their\r
+// grammar rules are recursive, so they have to be defined\r
+// in a single rule using Labels (https://github.com/antlr/antlr4/blob/master/doc/parser-rules.md#alternative-labels)\r
+// Because this results in a polymorphic type, getExpr for the base expression type\r
+// is always called and from there the polymorphic type is determined at runtime\r
+Expr getExpr(TocParser::FuncExprContext * ctx)\r
+{\r
+  Expr result;\r
+  result.type = ExprType::Func;\r
+  for (auto n : ctx->namespaceSpecifier())\r
+    result._func.namespacePrefixes.push_back(n->typeName()->getText());\r
+  result._func.functionName = ctx->funcName()->NAME()->toString();\r
+  if (ctx->genericInstantiation() != nullptr)\r
+  {\r
+    for (auto g : ctx->genericInstantiation()->type())\r
+    {\r
+      result._func.genericInstantiation.push_back(getType(g));\r
+    }\r
   }\r
+  for (auto e : ctx->expr())\r
+    result._func.arguments.push_back(getExpr(e));\r
   return result;\r
 }\r
-Expr getExpr(TocParser::NonSubscriptExprContext * ctx) {\r
+Expr getExpr(TocParser::MethodExprContext * ctx)\r
+{\r
   Expr result;\r
-  if (ctx->funcCall() != nullptr) {\r
-    result.type = ExprType::Call;\r
-    for (auto e : ctx->funcCall()->expr())\r
-      result._call.arguments.push_back(getExpr(e));\r
-      result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();\r
-  }\r
-  if (ctx->literal() != nullptr) {\r
-    result.type = ExprType::Literal;\r
-    result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());\r
-  }\r
-  if (ctx->identifier() != nullptr) {\r
-    result.type = ExprType::Variable;\r
-    result._variable.name = ctx->identifier()->varName()->NAME()->toString();\r
-  }\r
-  if (ctx->memberAccess() != nullptr) {\r
-    result.type = ExprType::Dot;\r
-    Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();\r
-    result._dot.lexpr = std::make_unique<Expr>(e);\r
-    result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();\r
+  result.type = ExprType::Method;\r
+  result._method.expr = std::make_unique<Expr>(getExpr(ctx->expr(0)));\r
+  result._method.methodName = ctx->funcName()->NAME()->toString();\r
+  if (ctx->genericInstantiation() != nullptr)\r
+  {\r
+    for (auto g : ctx->genericInstantiation()->type())\r
+    {\r
+      result._method.genericInstantiation.push_back(getType(g));\r
+    }\r
   }\r
+  for (int i = 1; i < ctx->expr().size(); i++)\r
+    result._method.arguments.push_back(getExpr(ctx->expr(i)));\r
   return result;\r
 }\r
-Expr getExpr(TocParser::ExprContext * ctx) {\r
+Expr getExpr(TocParser::LitExprContext * ctx)\r
+{\r
   Expr result;\r
-  if (ctx->funcCall() != nullptr) {\r
-    result.type = ExprType::Call;\r
-    for (auto e : ctx->funcCall()->expr())\r
-      result._call.arguments.push_back(getExpr(e));\r
-      result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();\r
+  result.type = ExprType::Lit;\r
+  if (ctx->literal()->INT_LIT() != nullptr)\r
+  {\r
+    result._lit.type = LitType::Int;\r
+    result._lit._int = atoi(ctx->literal()->INT_LIT()->toString().c_str());\r
   }\r
-  if (ctx->literal() != nullptr) {\r
-    result.type = ExprType::Literal;\r
-    result._literal.i = atoi(ctx->literal()->INTLIT()->toString().c_str());\r
+  else if (ctx->literal()->DECIMAL_LIT() != nullptr)\r
+  {\r
+    result._lit.type = LitType::Decimal;\r
+    result._lit._decimal = atof(ctx->literal()->DECIMAL_LIT()->toString().c_str());\r
   }\r
-  if (ctx->identifier() != nullptr) {\r
-    result.type = ExprType::Variable;\r
-    result._variable.name = ctx->identifier()->varName()->NAME()->toString();\r
+  else if (ctx->literal()->StringLit() != nullptr)\r
+  {\r
+    result._lit.type = LitType::String;\r
+    result._lit._string = ctx->literal()->StringLit()->toString();\r
   }\r
-  if (ctx->subscript() != nullptr) {\r
-    result.type = ExprType::Brackets;\r
-    result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->nonSubscriptExpr()));\r
-    result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));\r
-  }\r
-  if (ctx->memberAccess() != nullptr) {\r
-    result.type = ExprType::Dot;\r
-    Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();\r
-    result._dot.lexpr = std::make_unique<Expr>(e);\r
-    result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();\r
-  }\r
-  if (ctx->operatorExpr() != nullptr) {\r
-    result.type = ExprType::Operator;\r
-    result._operator = getOperatorExpr(ctx->operatorExpr());\r
+  else if (ctx->literal()->BOOL_LIT() != nullptr)\r
+  {\r
+    result._lit.type = LitType::Bool;\r
+    result._lit._bool = ctx->literal()->BOOL_LIT()->toString() == "true";\r
   }\r
   return result;\r
 }\r
-Stmt getStmt(TocParser::StmtContext * ctx) {\r
+Expr getExpr(TocParser::ParenExprContext * ctx)\r
+{\r
+  Expr result;\r
+  result.type = ExprType::Paren;\r
+  result._paren.expr = std::make_unique<Expr>(getExpr(ctx->expr()));\r
+  return result;\r
+}\r
+Expr getExpr(TocParser::DotExprContext * ctx)\r
+{\r
+  Expr result;\r
+  result.type = ExprType::Dot;\r
+  result._dot.expr = std::make_unique<Expr>(getExpr(ctx->expr()));\r
+  result._dot.identifier = ctx->varName()->getText();\r
+  result._dot.isPointer = ctx->arrow() != nullptr;\r
+  return result;\r
+}\r
+Expr getExpr(TocParser::PrefixOpExprContext * ctx)\r
+{\r
+  Expr result;\r
+  result.type = ExprType::PrefixOp;\r
+  result._prefixOp.expr = std::make_unique<Expr>(getExpr(ctx->expr()));\r
+  result._prefixOp.type = getOperatorType<PrefixOperatorType>(\r
+    ctx->prefix_op()->getText(),\r
+    PrefixOperatorTypeStrings);\r
+  return result;\r
+}\r
+Expr getExpr(TocParser::PostfixOpExprContext * ctx)\r
+{\r
+  Expr result;\r
+  result.type = ExprType::PostfixOp;\r
+  result._postfixOp.expr = std::make_unique<Expr>(getExpr(ctx->expr()));\r
+  result._postfixOp.type = getOperatorType<PostfixOperatorType>(\r
+    ctx->postfix_op()->getText(),\r
+    PostfixOperatorTypeStrings);\r
+  return result;\r
+}\r
+Expr getExpr(TocParser::BinaryOpExprContext * ctx)\r
+{\r
+  Expr result;\r
+  result.type = ExprType::BinaryOp;\r
+  result._binaryOp.lexpr = std::make_unique<Expr>(getExpr(ctx->expr(0)));\r
+  result._binaryOp.rexpr = std::make_unique<Expr>(getExpr(ctx->expr(1)));\r
+  result._binaryOp.type = getOperatorType<BinaryOperatorType>(\r
+    ctx->binary_op()->getText(),\r
+    BinaryOperatorTypeStrings);\r
+  return result;\r
+}\r
+Expr getExpr(TocParser::TernaryOpExprContext * ctx)\r
+{\r
+  Expr result;\r
+  result.type = ExprType::TernaryOp;\r
+  result._ternaryOp.lexpr = std::make_unique<Expr>(getExpr(ctx->expr(0)));\r
+  result._ternaryOp.rexprTrue = std::make_unique<Expr>(getExpr(ctx->expr(1)));\r
+  result._ternaryOp.rexprFalse = std::make_unique<Expr>(getExpr(ctx->expr(2)));\r
+  return result;\r
+}\r
+Expr getExpr(TocParser::BracketExprContext * ctx)\r
+{\r
+  Expr result;\r
+  result.type = ExprType::Bracket;\r
+  result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->expr(0)));\r
+  result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->expr(1)));\r
+  return result;\r
+}\r
+Expr getExpr(TocParser::IdentifierExprContext * ctx)\r
+{\r
+  Expr result;\r
+  result.type = ExprType::Identifier;\r
+  for (auto n : ctx->namespaceSpecifier())\r
+    result._identifier.namespacePrefixes.push_back(n->typeName()->getText());\r
+  result._identifier.identifier = ctx->varName()->getText();\r
+  return result;\r
+}\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+// this is always called for Expression rules\r
+// attempt dynamic_cast at runtime and call corresponding\r
+// function\r
+Expr getExpr(TocParser::ExprContext * ctx)\r
+{\r
+  Expr result;\r
+  if (dynamic_cast<TocParser::FuncExprContext *>(ctx) != nullptr)\r
+    result = getExpr(dynamic_cast<TocParser::FuncExprContext *>(ctx));\r
+  if (dynamic_cast<TocParser::MethodExprContext *>(ctx) != nullptr)\r
+    result = getExpr(dynamic_cast<TocParser::MethodExprContext *>(ctx));\r
+  if (dynamic_cast<TocParser::LitExprContext *>(ctx) != nullptr)\r
+    result = getExpr(dynamic_cast<TocParser::LitExprContext *>(ctx));\r
+  if (dynamic_cast<TocParser::ParenExprContext *>(ctx) != nullptr)\r
+    result = getExpr(dynamic_cast<TocParser::ParenExprContext *>(ctx));\r
+  if (dynamic_cast<TocParser::DotExprContext *>(ctx) != nullptr)\r
+    result = getExpr(dynamic_cast<TocParser::DotExprContext *>(ctx));\r
+  if (dynamic_cast<TocParser::PrefixOpExprContext *>(ctx) != nullptr)\r
+    result = getExpr(dynamic_cast<TocParser::PrefixOpExprContext *>(ctx));\r
+  if (dynamic_cast<TocParser::PostfixOpExprContext *>(ctx) != nullptr)\r
+    result = getExpr(dynamic_cast<TocParser::PostfixOpExprContext *>(ctx));\r
+  if (dynamic_cast<TocParser::BinaryOpExprContext *>(ctx) != nullptr)\r
+    result = getExpr(dynamic_cast<TocParser::BinaryOpExprContext *>(ctx));\r
+  if (dynamic_cast<TocParser::TernaryOpExprContext *>(ctx) != nullptr)\r
+    result = getExpr(dynamic_cast<TocParser::TernaryOpExprContext *>(ctx));\r
+  if (dynamic_cast<TocParser::BracketExprContext *>(ctx) != nullptr)\r
+    result = getExpr(dynamic_cast<TocParser::BracketExprContext *>(ctx));\r
+  if (dynamic_cast<TocParser::IdentifierExprContext *>(ctx) != nullptr)\r
+    result = getExpr(dynamic_cast<TocParser::IdentifierExprContext *>(ctx));\r
+  return result;\r
+}\r
+Stmt getStmt(TocParser::StmtContext * ctx, std::shared_ptr<Context> parent)\r
+{\r
   Stmt result;\r
-  if (ctx->conditional() != nullptr) {\r
+  if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr)\r
+  {\r
+    result.type = StmtType::Assign;\r
+    result._assign.lexpr.type = ExprType::Identifier;\r
+    result._assign.lexpr._identifier.identifier = ctx->varDecl()->var()->varName()->getText();\r
+    result._assign.rexpr = getExpr(ctx->varDecl()->var()->expr());\r
+  }\r
+  if (ctx->ifStmt() != nullptr)\r
+  {\r
     result.type = StmtType::If;\r
-    result._if.condition = getExpr(ctx->conditional()->ifCond()->expr());\r
-    result._if.body = getBody(ctx->conditional()->ifCond()->body());\r
+    result._if.condition = getExpr(ctx->ifStmt()->expr());\r
+    result._if.body = getBody(ctx->ifStmt()->body(), parent);\r
+    for (auto ei : ctx->ifStmt()->elseIfStmt())\r
+    {\r
+      result._if.elses.emplace_back(\r
+        true,\r
+        std::make_unique<Expr>(getExpr(ei->expr())),\r
+        getBody(ei->body(), parent)\r
+      );\r
+    }\r
+    if (ctx->ifStmt()->elseStmt() != nullptr)\r
+    {\r
+      result._if.elses.emplace_back(\r
+        false,\r
+        nullptr,\r
+        getBody(ctx->ifStmt()->elseStmt()->body(), parent)\r
+      );\r
+    }\r
   }\r
-  if (ctx->loop() != nullptr) {\r
+  if (ctx->switchStmt() != nullptr)\r
+  {\r
+    result.type = StmtType::Switch;\r
+    result._switch.ident = std::make_unique<Expr>(getExpr(ctx->switchStmt()->expr()));\r
+    for (auto c : ctx->switchStmt()->switchBody()->switchCase())\r
+    {\r
+      result._switch.cases.emplace_back(\r
+        std::make_unique<Expr>(getExpr(c->expr())),\r
+        getBody(c->body(), parent)\r
+      );\r
+    }\r
+  }\r
+  if (ctx->forStmt() != nullptr)\r
+  {\r
+    result.type = StmtType::For;\r
+    result._for.init = std::make_unique<AssignStmt>();\r
+    result._for.init->lexpr.type = ExprType::Identifier;\r
+    result._for.init->lexpr._identifier.identifier = ctx->forStmt()->varInit()->varName()->getText();\r
+    result._for.init->rexpr = getExpr(ctx->forStmt()->varInit()->expr());\r
+    result._for.condition = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(0)));\r
+    result._for.action = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(1)));\r
+    result._for.body = getBody(ctx->forStmt()->body(), parent);\r
+  }\r
+  if (ctx->whileStmt() != nullptr)\r
+  {\r
     result.type = StmtType::While;\r
-    result._while.condition = getExpr(ctx->loop()->whileLoop()->expr());\r
-    result._while.body = getBody(ctx->loop()->whileLoop()->body());\r
+    result._while.condition = getExpr(ctx->whileStmt()->expr());\r
+    result._while.body = getBody(ctx->whileStmt()->body(), parent);\r
   }\r
-  if (ctx->assignment() != nullptr) {\r
+  if (ctx->assignStmt() != nullptr)\r
+  {\r
     result.type = StmtType::Assign;\r
-    //result._assign.lexpr = getExpr(ctx->assignment()->);\r
-    result._assign.rexpr = getExpr(ctx->assignment()->expr());\r
+    result._assign.lexpr = getExpr(ctx->assignStmt()->expr(0));\r
+    result._assign.rexpr = getExpr(ctx->assignStmt()->expr(1));\r
   }\r
-  if (ctx->returnStmt() != nullptr) {\r
+  if (ctx->returnStmt() != nullptr)\r
+  {\r
     result.type = StmtType::Return;\r
     result._return.expr = getExpr(ctx->returnStmt()->expr());\r
   }\r
-  if (ctx->expr() != nullptr) {\r
+  if (ctx->expr() != nullptr)\r
+  {\r
     result.type = StmtType::Expr;\r
     result._expr = getExpr(ctx->expr());\r
   }\r
-  if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr) {\r
-    result.type = StmtType::Assign;\r
-    result._assign.rexpr = getExpr(ctx->varDecl()->var()->expr());\r
-  }\r
   return result;\r
 }
\ No newline at end of file