]> gitweb.ps.run Git - toc/blobdiff - src/repr_get.h
add namespace, private struct member grammar, change bracket style
[toc] / src / repr_get.h
index 70ae3b1c2a2fe77bfca563d4160a9e45ec4b6e11..118d7b33b156aa02db8c1ad41c881d9a6cea7183 100644 (file)
@@ -18,10 +18,12 @@ Expr                getExpr(TocParser::NonAccessExprContext * ctx);
 Expr                getExpr(TocParser::ExprContext * ctx);\r
 Stmt                getStmt(TocParser::StmtContext * ctx);\r
 \r
-Type getType(TocParser::TypeContext * ctx) {\r
+Type getType(TocParser::TypeContext * ctx)\r
+{\r
   Type result;\r
   result.name = ctx->typeName()->NAME()->toString();\r
-  for (auto m : ctx->typeModifier()) {\r
+  for (auto m : ctx->typeModifier())\r
+  {\r
     bool isPointer = m->getText() == "*";\r
     bool isStaticArray = m->INT_LIT() != nullptr;\r
 \r
@@ -33,82 +35,105 @@ Type getType(TocParser::TypeContext * ctx) {
   }\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)\r
+{\r
   Body result;\r
-  for (auto s : ctx->stmt()) {\r
-    if (s->varDecl() != nullptr) {\r
+  for (auto s : ctx->stmt())\r
+  {\r
+    if (s->varDecl() != nullptr)\r
+    {\r
       result.variables.push_back(getVariable(s->varDecl()->var()));\r
       if (s->varDecl()->var()->expr() != nullptr)\r
         result.statements.push_back(getStmt(s));\r
     }\r
-    else {\r
+    else\r
+    {\r
       result.statements.push_back(getStmt(s));\r
     }\r
   }\r
   return result;\r
 }\r
-Function getFunction(TocParser::FuncContext * ctx) {\r
+Function getFunction(TocParser::FuncContext * ctx)\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->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
   return result;\r
 }\r
-Struct getStruct(TocParser::StructDeclContext * ctx) {\r
+Struct getStruct(TocParser::StructDeclContext * ctx)\r
+{\r
   Struct result;\r
   result.name = ctx->structName()->NAME()->toString();\r
-  for (auto m : ctx->structMember()) {\r
-    if (m->structVar() != nullptr) {\r
+  for (auto m : ctx->structMember())\r
+  {\r
+    if (m->structVar() != nullptr)\r
+    {\r
       result.members.push_back(getVariable(m->structVar()->var()));\r
     }\r
-    if (m->structMethod() != nullptr) {\r
+    if (m->structMethod() != nullptr)\r
+    {\r
       result.methods.push_back(getFunction(m->structMethod()->func()));\r
     }\r
   }\r
   return result;\r
 }\r
-Program getProgram(TocParser::ProgContext * ctx) {\r
+Program getProgram(TocParser::ProgContext * ctx)\r
+{\r
   Program result;\r
-  for (auto d : ctx->decl()) {\r
-    if (d->varDecl() != nullptr) {\r
+  for (auto d : ctx->decl())\r
+  {\r
+    if (d->varDecl() != nullptr)\r
+    {\r
       result.variables.push_back(getVariable(d->varDecl()->var()));\r
     }\r
-    if (d->funcDecl() != nullptr) {\r
+    if (d->funcDecl() != nullptr)\r
+    {\r
       result.functions.push_back(getFunction(d->funcDecl()->func()));\r
     }\r
-    if (d->structDecl() != nullptr) {\r
+    if (d->structDecl() != nullptr)\r
+    {\r
       result.structs.push_back(getStruct(d->structDecl()));\r
     }\r
   }\r
   return result;\r
 }\r
-UnaryOperatorType getUnaryOperatorType(const std::string & s) {\r
-  for (int i = 0; i < (int)UnaryOperatorType::COUNT; i++) {\r
-    if (UnaryOperatorTypeStrings[i] == s) {\r
+UnaryOperatorType getUnaryOperatorType(const std::string & s)\r
+{\r
+  for (int i = 0; i < (int)UnaryOperatorType::COUNT; i++)\r
+  {\r
+    if (UnaryOperatorTypeStrings[i] == s)\r
+    {\r
       return (UnaryOperatorType)i;\r
     }\r
   }\r
   return UnaryOperatorType::COUNT;\r
 }\r
-BinaryOperatorType getBinaryOperatorType(const std::string & s) {\r
-  for (int i = 0; i < (int)BinaryOperatorType::COUNT; i++) {\r
-    if (BinaryOperatorTypeStrings[i] == s) {\r
+BinaryOperatorType getBinaryOperatorType(const std::string & s)\r
+{\r
+  for (int i = 0; i < (int)BinaryOperatorType::COUNT; i++)\r
+  {\r
+    if (BinaryOperatorTypeStrings[i] == s)\r
+    {\r
       return (BinaryOperatorType)i;\r
     }\r
   }\r
   return BinaryOperatorType::COUNT;\r
 }\r
-UnaryOperatorExpr getUnaryOperatorExpr(TocParser::OpExprContext * ctx) {\r
+UnaryOperatorExpr getUnaryOperatorExpr(TocParser::OpExprContext * ctx)\r
+{\r
   UnaryOperatorExpr result;\r
   if (ctx->prefixOp() != nullptr)\r
     result.expr = std::make_unique<Expr>(getExpr(ctx->prefixOp()->nonOpExpr()));\r
@@ -127,7 +152,8 @@ UnaryOperatorExpr getUnaryOperatorExpr(TocParser::OpExprContext * ctx) {
 \r
   return result;\r
 }\r
-BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx) {\r
+BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx)\r
+{\r
   BinaryOperatorExpr result;\r
   result.lexpr = std::make_unique<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(0)));\r
   result.rexpr = std::make_unique<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(1)));\r
@@ -138,70 +164,86 @@ BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx) {
 \r
   return result;\r
 }\r
-TernaryOperatorExpr getTernaryOperatorExpr(TocParser::OpExprContext * ctx) {\r
+TernaryOperatorExpr getTernaryOperatorExpr(TocParser::OpExprContext * ctx)\r
+{\r
   TernaryOperatorExpr result;\r
   result.lexpr = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->nonOpExpr()));\r
   result.rexprTrue = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->expr(0)));\r
   result.rexprFalse = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->expr(1)));\r
   return result;\r
 }\r
-Expr getExpr(TocParser::NonOpExprContext * ctx) {\r
+Expr getExpr(TocParser::NonOpExprContext * ctx)\r
+{\r
   Expr result;\r
   result.parenthesized = false;\r
-  if (ctx->funcExpr() != nullptr) {\r
+  if (ctx->funcExpr() != nullptr)\r
+  {\r
     result.type = ExprType::Func;\r
     result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString();\r
     for (auto e : ctx->funcExpr()->expr())\r
       result._func.arguments.push_back(getExpr(e));\r
   }\r
-  if (ctx->litExpr() != nullptr) {\r
+  if (ctx->litExpr() != nullptr)\r
+  {\r
     result.type = ExprType::Lit;\r
-    if (ctx->litExpr()->INT_LIT() != nullptr) {\r
+    if (ctx->litExpr()->INT_LIT() != nullptr)\r
+    {\r
       result._lit.type = LitType::Int;\r
       result._lit._int = atoi(ctx->litExpr()->INT_LIT()->toString().c_str());\r
     }\r
-    else if (ctx->litExpr()->DECIMAL_LIT() != nullptr) {\r
+    else if (ctx->litExpr()->DECIMAL_LIT() != nullptr)\r
+    {\r
       result._lit.type = LitType::Decimal;\r
       result._lit._decimal = atof(ctx->litExpr()->DECIMAL_LIT()->toString().c_str());\r
     }\r
-    else if (ctx->litExpr()->STRING_LIT() != nullptr) {\r
+    else if (ctx->litExpr()->STRING_LIT() != nullptr)\r
+    {\r
       result._lit.type = LitType::String;\r
       result._lit._string = ctx->litExpr()->STRING_LIT()->toString();\r
     }\r
-    else if (ctx->litExpr()->BOOL_LIT() != nullptr) {\r
+    else if (ctx->litExpr()->BOOL_LIT() != nullptr)\r
+    {\r
       result._lit.type = LitType::Bool;\r
       result._lit._bool = ctx->litExpr()->BOOL_LIT()->toString() == "true";\r
     }\r
   }\r
-  if (ctx->identifierExpr() != nullptr) {\r
+  if (ctx->identifierExpr() != nullptr)\r
+  {\r
     result.type = ExprType::Identifier;\r
     result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
   }\r
-  if (ctx->parenExpr() != nullptr) {\r
+  if (ctx->parenExpr() != nullptr)\r
+  {\r
     result = getExpr(ctx->parenExpr()->expr());\r
     result.parenthesized = true;\r
   }\r
-  if (ctx->accessExpr() != nullptr) {\r
+  if (ctx->accessExpr() != nullptr)\r
+  {\r
     auto firstSub = ctx->accessExpr()->accessSubExpr(0);\r
-    if (firstSub->accessMember() != nullptr) {\r
+    if (firstSub->accessMember() != nullptr)\r
+    {\r
       result.type = ExprType::Dot;\r
       result._dot.expr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));\r
       result._dot.ident.name = firstSub->accessMember()->identifierExpr()->varName()->NAME()->toString();\r
     }\r
-    else {\r
+    else\r
+    {\r
       result.type = ExprType::Brackets;\r
       result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));\r
       result._brackets.rexpr = std::make_unique<Expr>(getExpr(firstSub->accessBrackets()->expr()));\r
     }\r
-    for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++) {\r
+    for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++)\r
+    {\r
       Expr tmp = result;\r
       auto sub = ctx->accessExpr()->accessSubExpr(i);\r
-      if (sub->accessMember() != nullptr) {\r
+      if (sub->accessMember() != nullptr)\r
+      {\r
         result.type = ExprType::Dot;\r
         result._dot.expr = std::make_unique<Expr>(tmp);\r
         result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString();\r
       }\r
-      else {\r
+      else\r
+      {\r
         result.type = ExprType::Brackets;\r
         result._brackets.lexpr = std::make_unique<Expr>(tmp);\r
         result._brackets.rexpr = std::make_unique<Expr>(getExpr(sub->accessBrackets()->expr()));\r
@@ -210,129 +252,158 @@ Expr getExpr(TocParser::NonOpExprContext * ctx) {
   }\r
   return result;\r
 }\r
-Expr getExpr(TocParser::NonAccessExprContext * ctx) {\r
+Expr getExpr(TocParser::NonAccessExprContext * ctx)\r
+{\r
   Expr result;\r
   result.parenthesized = false;\r
-  if (ctx->funcExpr() != nullptr) {\r
+  if (ctx->funcExpr() != nullptr)\r
+  {\r
     result.type = ExprType::Func;\r
     result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString();\r
     for (auto e : ctx->funcExpr()->expr())\r
       result._func.arguments.push_back(getExpr(e));\r
   }\r
-  if (ctx->identifierExpr() != nullptr) {\r
+  if (ctx->identifierExpr() != nullptr)\r
+  {\r
     result.type = ExprType::Identifier;\r
     result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
   }\r
-  if (ctx->parenExpr() != nullptr) {\r
+  if (ctx->parenExpr() != nullptr)\r
+  {\r
     result = getExpr(ctx->parenExpr()->expr());\r
     result.parenthesized = true;\r
   }\r
   return result;\r
 }\r
-Expr getExpr(TocParser::ExprContext * ctx) {\r
+Expr getExpr(TocParser::ExprContext * ctx)\r
+{\r
   Expr result;\r
   result.parenthesized = false;\r
-  if (ctx->funcExpr() != nullptr) {\r
+  if (ctx->funcExpr() != nullptr)\r
+  {\r
     result.type = ExprType::Func;\r
     result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString();\r
     for (auto e : ctx->funcExpr()->expr())\r
       result._func.arguments.push_back(getExpr(e));\r
   }\r
-  if (ctx->litExpr() != nullptr) {\r
+  if (ctx->litExpr() != nullptr)\r
+  {\r
     result.type = ExprType::Lit;\r
-    if (ctx->litExpr()->INT_LIT() != nullptr) {\r
+    if (ctx->litExpr()->INT_LIT() != nullptr)\r
+    {\r
       result._lit.type = LitType::Int;\r
       result._lit._int = atoi(ctx->litExpr()->INT_LIT()->toString().c_str());\r
     }\r
-    else if (ctx->litExpr()->DECIMAL_LIT() != nullptr) {\r
+    else if (ctx->litExpr()->DECIMAL_LIT() != nullptr)\r
+    {\r
       result._lit.type = LitType::Decimal;\r
       result._lit._decimal = atof(ctx->litExpr()->DECIMAL_LIT()->toString().c_str());\r
     }\r
-    else if (ctx->litExpr()->STRING_LIT() != nullptr) {\r
+    else if (ctx->litExpr()->STRING_LIT() != nullptr)\r
+    {\r
       result._lit.type = LitType::String;\r
       result._lit._string = ctx->litExpr()->STRING_LIT()->toString();\r
     }\r
-    else if (ctx->litExpr()->BOOL_LIT() != nullptr) {\r
+    else if (ctx->litExpr()->BOOL_LIT() != nullptr)\r
+    {\r
       result._lit.type = LitType::Bool;\r
       result._lit._bool = ctx->litExpr()->BOOL_LIT()->toString() == "true";\r
     }\r
   }\r
-  if (ctx->identifierExpr() != nullptr) {\r
+  if (ctx->identifierExpr() != nullptr)\r
+  {\r
     result.type = ExprType::Identifier;\r
     result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();\r
   }\r
-  if (ctx->parenExpr() != nullptr) {\r
+  if (ctx->parenExpr() != nullptr)\r
+  {\r
     result = getExpr(ctx->parenExpr()->expr());\r
     result.parenthesized = true;\r
   }\r
-  if (ctx->accessExpr() != nullptr) {\r
+  if (ctx->accessExpr() != nullptr)\r
+  {\r
     auto firstSub = ctx->accessExpr()->accessSubExpr(0);\r
-    if (firstSub->accessMember() != nullptr) {\r
+    if (firstSub->accessMember() != nullptr)\r
+    {\r
       result.type = ExprType::Dot;\r
       result._dot.expr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));\r
       result._dot.ident.name = firstSub->accessMember()->identifierExpr()->varName()->NAME()->toString();\r
     }\r
-    else {\r
+    else\r
+    {\r
       result.type = ExprType::Brackets;\r
       result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));\r
       result._brackets.rexpr = std::make_unique<Expr>(getExpr(firstSub->accessBrackets()->expr()));\r
     }\r
-    for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++) {\r
+    for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++)\r
+    {\r
       Expr tmp = result;\r
       auto sub = ctx->accessExpr()->accessSubExpr(i);\r
-      if (sub->accessMember() != nullptr) {\r
+      if (sub->accessMember() != nullptr)\r
+      {\r
         result.type = ExprType::Dot;\r
         result._dot.expr = std::make_unique<Expr>(tmp);\r
         result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString();\r
       }\r
-      else {\r
+      else\r
+      {\r
         result.type = ExprType::Brackets;\r
         result._brackets.lexpr = std::make_unique<Expr>(tmp);\r
         result._brackets.rexpr = std::make_unique<Expr>(getExpr(sub->accessBrackets()->expr()));\r
       }\r
     }\r
   }\r
-  if (ctx->opExpr() != nullptr) {\r
-    if (ctx->opExpr()->prefixOp() != nullptr || ctx->opExpr()->postfixOp() != nullptr) {\r
+  if (ctx->opExpr() != nullptr)\r
+  {\r
+    if (ctx->opExpr()->prefixOp() != nullptr || ctx->opExpr()->postfixOp() != nullptr)\r
+    {\r
       result.type = ExprType::UnaryOperator;\r
       result._unaryOperator = getUnaryOperatorExpr(ctx->opExpr());\r
     }\r
-    else if (ctx->opExpr()->binaryOp() != nullptr) {\r
+    else if (ctx->opExpr()->binaryOp() != nullptr)\r
+    {\r
       result.type = ExprType::BinaryOperator;\r
       result._binaryOperator = getBinaryOperatorExpr(ctx->opExpr());\r
-      for (int i = 1; i < ctx->opExpr()->binaryOp()->binary_op().size(); i++) {\r
+      for (int i = 1; i < ctx->opExpr()->binaryOp()->binary_op().size(); i++)\r
+      {\r
         Expr tmp = result;\r
         result._binaryOperator.lexpr = std::make_unique<Expr>(tmp);\r
         result._binaryOperator.type = getBinaryOperatorType(ctx->opExpr()->binaryOp()->binary_op(i)->getText());\r
         result._binaryOperator.rexpr = std::make_unique<Expr>(getExpr(ctx->opExpr()->binaryOp()->nonOpExpr(i+1)));\r
       }\r
     }\r
-    else if (ctx->opExpr()->ternaryOp() != nullptr) {\r
+    else if (ctx->opExpr()->ternaryOp() != nullptr)\r
+    {\r
       result.type = ExprType::TernaryOperator;\r
       result._ternaryOperator = getTernaryOperatorExpr(ctx->opExpr());\r
     }\r
   }\r
   return result;\r
 }\r
-Stmt getStmt(TocParser::StmtContext * ctx) {\r
+Stmt getStmt(TocParser::StmtContext * ctx)\r
+{\r
   Stmt result;\r
-  if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr) {\r
+  if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr)\r
+  {\r
     result.type = StmtType::Assign;\r
     result._assign.name = ctx->varDecl()->var()->varName()->NAME()->toString();\r
     result._assign.expr = getExpr(ctx->varDecl()->var()->expr());\r
   }\r
-  if (ctx->ifStmt() != nullptr) {\r
+  if (ctx->ifStmt() != nullptr)\r
+  {\r
     result.type = StmtType::If;\r
     result._if.condition = getExpr(ctx->ifStmt()->expr());\r
     result._if.body = getBody(ctx->ifStmt()->body());\r
-    for (auto ei : ctx->ifStmt()->elseIfStmt()) {\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())\r
       );\r
     }\r
-    if (ctx->ifStmt()->elseStmt() != nullptr) {\r
+    if (ctx->ifStmt()->elseStmt() != nullptr)\r
+    {\r
       result._if.elses.emplace_back(\r
         false,\r
         nullptr,\r
@@ -340,23 +411,28 @@ Stmt getStmt(TocParser::StmtContext * ctx) {
       );\r
     }\r
   }\r
-  if (ctx->switchStmt() != nullptr) {\r
+  if (ctx->switchStmt() != nullptr)\r
+  {\r
     result.type = StmtType::Switch;\r
     result._switch.ident.name = ctx->switchStmt()->identifierExpr()->varName()->NAME()->toString();\r
-    for (auto c : ctx->switchStmt()->switchBody()->switchCase()) {\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())\r
       );\r
     }\r
   }\r
-  if (ctx->forStmt() != nullptr) {\r
+  if (ctx->forStmt() != nullptr)\r
+  {\r
     result.type = StmtType::For;\r
-    if (ctx->forStmt()->varInit() != nullptr) {\r
+    if (ctx->forStmt()->varInit() != nullptr)\r
+    {\r
       result._for.varName = ctx->forStmt()->varInit()->varName()->NAME()->toString();\r
       result._for.initValue = std::make_unique<Expr>(getExpr(ctx->forStmt()->varInit()->expr()));\r
     }\r
-    else {\r
+    else\r
+    {\r
       result._for.varName = ctx->forStmt()->assignStmt()->identifierExpr()->varName()->NAME()->toString();\r
       result._for.initValue = std::make_unique<Expr>(getExpr(ctx->forStmt()->assignStmt()->expr()));\r
     }\r
@@ -364,21 +440,25 @@ Stmt getStmt(TocParser::StmtContext * ctx) {
     result._for.action = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(1)));\r
     result._for.body = getBody(ctx->forStmt()->body());\r
   }\r
-  if (ctx->whileStmt() != nullptr) {\r
+  if (ctx->whileStmt() != nullptr)\r
+  {\r
     result.type = StmtType::While;\r
     result._while.condition = getExpr(ctx->whileStmt()->expr());\r
     result._while.body = getBody(ctx->whileStmt()->body());\r
   }\r
-  if (ctx->assignStmt() != nullptr) {\r
+  if (ctx->assignStmt() != nullptr)\r
+  {\r
     result.type = StmtType::Assign;\r
     result._assign.name = ctx->assignStmt()->identifierExpr()->varName()->NAME()->toString();\r
     result._assign.expr = getExpr(ctx->assignStmt()->expr());\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