X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/45409c781a9e35df68c43b1e2f028d30bf90c0a0..8aeae09e74b46ca52866f22b48f55fecdf27b849:/src/repr_get.h diff --git a/src/repr_get.h b/src/repr_get.h index 67ff40f..70ae3b1 100644 --- a/src/repr_get.h +++ b/src/repr_get.h @@ -2,19 +2,35 @@ #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); +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); +UnaryOperatorType getUnaryOperatorType(const std::string & s); +BinaryOperatorType getBinaryOperatorType(const std::string & s); +UnaryOperatorExpr getUnaryOperatorExpr(TocParser::OpExprContext * ctx); +BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx); +TernaryOperatorExpr getTernaryOperatorExpr(TocParser::OpExprContext * ctx); +Expr getExpr(TocParser::NonOpExprContext * ctx); +Expr getExpr(TocParser::NonAccessExprContext * ctx); +Expr getExpr(TocParser::ExprContext * ctx); +Stmt getStmt(TocParser::StmtContext * ctx); Type getType(TocParser::TypeContext * ctx) { Type result; result.name = ctx->typeName()->NAME()->toString(); + for (auto m : ctx->typeModifier()) { + bool isPointer = m->getText() == "*"; + bool isStaticArray = m->INT_LIT() != nullptr; + + result.modifiers.emplace_back( + isPointer ? TypeModifierType::Pointer : TypeModifierType::Array, + isStaticArray, + isStaticArray ? atoi(m->INT_LIT()->toString().c_str()) : -1 + ); + } return result; } Variable getVariable(TocParser::VarContext * ctx) { @@ -28,6 +44,8 @@ Body getBody(TocParser::BodyContext * ctx) { for (auto s : ctx->stmt()) { if (s->varDecl() != nullptr) { result.variables.push_back(getVariable(s->varDecl()->var())); + if (s->varDecl()->var()->expr() != nullptr) + result.statements.push_back(getStmt(s)); } else { result.statements.push_back(getStmt(s)); @@ -38,10 +56,10 @@ Body getBody(TocParser::BodyContext * ctx) { 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.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; @@ -74,69 +92,287 @@ Program getProgram(TocParser::ProgContext * ctx) { } 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; +UnaryOperatorType getUnaryOperatorType(const std::string & s) { + for (int i = 0; i < (int)UnaryOperatorType::COUNT; i++) { + if (UnaryOperatorTypeStrings[i] == s) { + return (UnaryOperatorType)i; + } + } + return UnaryOperatorType::COUNT; +} +BinaryOperatorType getBinaryOperatorType(const std::string & s) { + for (int i = 0; i < (int)BinaryOperatorType::COUNT; i++) { + if (BinaryOperatorTypeStrings[i] == s) { + return (BinaryOperatorType)i; + } + } + return BinaryOperatorType::COUNT; +} +UnaryOperatorExpr getUnaryOperatorExpr(TocParser::OpExprContext * ctx) { + UnaryOperatorExpr result; + if (ctx->prefixOp() != nullptr) + result.expr = std::make_unique(getExpr(ctx->prefixOp()->nonOpExpr())); + else + result.expr = std::make_unique(getExpr(ctx->postfixOp()->nonOpExpr())); + + std::string op; + if (ctx->prefixOp() != nullptr) + op = ctx->prefixOp()->prefix_op()->getText(); + else + op = ctx->postfixOp()->postfix_op()->getText(); + + // TODO: postfix type + + result.type = getUnaryOperatorType(op); + + return result; +} +BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx) { + BinaryOperatorExpr result; + result.lexpr = std::make_unique(getExpr(ctx->binaryOp()->nonOpExpr(0))); + result.rexpr = std::make_unique(getExpr(ctx->binaryOp()->nonOpExpr(1))); + + std::string op = ctx->binaryOp()->binary_op(0)->getText(); + + result.type = getBinaryOperatorType(op); + + return result; +} +TernaryOperatorExpr getTernaryOperatorExpr(TocParser::OpExprContext * ctx) { + TernaryOperatorExpr result; + result.lexpr = std::make_unique(getExpr(ctx->ternaryOp()->nonOpExpr())); + result.rexprTrue = std::make_unique(getExpr(ctx->ternaryOp()->expr(0))); + result.rexprFalse = std::make_unique(getExpr(ctx->ternaryOp()->expr(1))); + return result; +} +Expr getExpr(TocParser::NonOpExprContext * ctx) { + Expr result; + result.parenthesized = false; + if (ctx->funcExpr() != nullptr) { + result.type = ExprType::Func; + result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString(); + for (auto e : ctx->funcExpr()->expr()) + result._func.arguments.push_back(getExpr(e)); + } + if (ctx->litExpr() != nullptr) { + result.type = ExprType::Lit; + if (ctx->litExpr()->INT_LIT() != nullptr) { + result._lit.type = LitType::Int; + result._lit._int = atoi(ctx->litExpr()->INT_LIT()->toString().c_str()); + } + else if (ctx->litExpr()->DECIMAL_LIT() != nullptr) { + result._lit.type = LitType::Decimal; + result._lit._decimal = atof(ctx->litExpr()->DECIMAL_LIT()->toString().c_str()); + } + else if (ctx->litExpr()->STRING_LIT() != nullptr) { + result._lit.type = LitType::String; + result._lit._string = ctx->litExpr()->STRING_LIT()->toString(); + } + else if (ctx->litExpr()->BOOL_LIT() != nullptr) { + result._lit.type = LitType::Bool; + result._lit._bool = ctx->litExpr()->BOOL_LIT()->toString() == "true"; + } + } + if (ctx->identifierExpr() != nullptr) { + result.type = ExprType::Identifier; + result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString(); + } + if (ctx->parenExpr() != nullptr) { + result = getExpr(ctx->parenExpr()->expr()); + result.parenthesized = true; + } + if (ctx->accessExpr() != nullptr) { + auto firstSub = ctx->accessExpr()->accessSubExpr(0); + if (firstSub->accessMember() != nullptr) { + result.type = ExprType::Dot; + result._dot.expr = std::make_unique(getExpr(ctx->accessExpr()->nonAccessExpr())); + result._dot.ident.name = firstSub->accessMember()->identifierExpr()->varName()->NAME()->toString(); + } + else { + result.type = ExprType::Brackets; + result._brackets.lexpr = std::make_unique(getExpr(ctx->accessExpr()->nonAccessExpr())); + result._brackets.rexpr = std::make_unique(getExpr(firstSub->accessBrackets()->expr())); + } + for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++) { + Expr tmp = result; + auto sub = ctx->accessExpr()->accessSubExpr(i); + if (sub->accessMember() != nullptr) { + result.type = ExprType::Dot; + result._dot.expr = std::make_unique(tmp); + result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString(); + } + else { + result.type = ExprType::Brackets; + result._brackets.lexpr = std::make_unique(tmp); + result._brackets.rexpr = std::make_unique(getExpr(sub->accessBrackets()->expr())); + } + } + } + return result; +} +Expr getExpr(TocParser::NonAccessExprContext * ctx) { + Expr result; + result.parenthesized = false; + if (ctx->funcExpr() != nullptr) { + result.type = ExprType::Func; + result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString(); + for (auto e : ctx->funcExpr()->expr()) + result._func.arguments.push_back(getExpr(e)); + } + if (ctx->identifierExpr() != nullptr) { + result.type = ExprType::Identifier; + result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString(); + } + if (ctx->parenExpr() != nullptr) { + result = getExpr(ctx->parenExpr()->expr()); + result.parenthesized = true; + } 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(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()); + result.parenthesized = false; + if (ctx->funcExpr() != nullptr) { + result.type = ExprType::Func; + result._func.functionName = ctx->funcExpr()->funcName()->NAME()->toString(); + for (auto e : ctx->funcExpr()->expr()) + result._func.arguments.push_back(getExpr(e)); + } + if (ctx->litExpr() != nullptr) { + result.type = ExprType::Lit; + if (ctx->litExpr()->INT_LIT() != nullptr) { + result._lit.type = LitType::Int; + result._lit._int = atoi(ctx->litExpr()->INT_LIT()->toString().c_str()); + } + else if (ctx->litExpr()->DECIMAL_LIT() != nullptr) { + result._lit.type = LitType::Decimal; + result._lit._decimal = atof(ctx->litExpr()->DECIMAL_LIT()->toString().c_str()); + } + else if (ctx->litExpr()->STRING_LIT() != nullptr) { + result._lit.type = LitType::String; + result._lit._string = ctx->litExpr()->STRING_LIT()->toString(); + } + else if (ctx->litExpr()->BOOL_LIT() != nullptr) { + result._lit.type = LitType::Bool; + result._lit._bool = ctx->litExpr()->BOOL_LIT()->toString() == "true"; + } + } + if (ctx->identifierExpr() != nullptr) { + result.type = ExprType::Identifier; + result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString(); + } + if (ctx->parenExpr() != nullptr) { + result = getExpr(ctx->parenExpr()->expr()); + result.parenthesized = true; + } + if (ctx->accessExpr() != nullptr) { + auto firstSub = ctx->accessExpr()->accessSubExpr(0); + if (firstSub->accessMember() != nullptr) { + result.type = ExprType::Dot; + result._dot.expr = std::make_unique(getExpr(ctx->accessExpr()->nonAccessExpr())); + result._dot.ident.name = firstSub->accessMember()->identifierExpr()->varName()->NAME()->toString(); + } + else { + result.type = ExprType::Brackets; + result._brackets.lexpr = std::make_unique(getExpr(ctx->accessExpr()->nonAccessExpr())); + result._brackets.rexpr = std::make_unique(getExpr(firstSub->accessBrackets()->expr())); + } + for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++) { + Expr tmp = result; + auto sub = ctx->accessExpr()->accessSubExpr(i); + if (sub->accessMember() != nullptr) { + result.type = ExprType::Dot; + result._dot.expr = std::make_unique(tmp); + result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString(); + } + else { + result.type = ExprType::Brackets; + result._brackets.lexpr = std::make_unique(tmp); + result._brackets.rexpr = std::make_unique(getExpr(sub->accessBrackets()->expr())); + } + } + } + if (ctx->opExpr() != nullptr) { + if (ctx->opExpr()->prefixOp() != nullptr || ctx->opExpr()->postfixOp() != nullptr) { + result.type = ExprType::UnaryOperator; + result._unaryOperator = getUnaryOperatorExpr(ctx->opExpr()); + } + else if (ctx->opExpr()->binaryOp() != nullptr) { + result.type = ExprType::BinaryOperator; + result._binaryOperator = getBinaryOperatorExpr(ctx->opExpr()); + for (int i = 1; i < ctx->opExpr()->binaryOp()->binary_op().size(); i++) { + Expr tmp = result; + result._binaryOperator.lexpr = std::make_unique(tmp); + result._binaryOperator.type = getBinaryOperatorType(ctx->opExpr()->binaryOp()->binary_op(i)->getText()); + result._binaryOperator.rexpr = std::make_unique(getExpr(ctx->opExpr()->binaryOp()->nonOpExpr(i+1))); + } + } + else if (ctx->opExpr()->ternaryOp() != nullptr) { + result.type = ExprType::TernaryOperator; + result._ternaryOperator = getTernaryOperatorExpr(ctx->opExpr()); + } } return result; } Stmt getStmt(TocParser::StmtContext * ctx) { Stmt result; - if (ctx->conditional() != nullptr) { + if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != nullptr) { + result.type = StmtType::Assign; + result._assign.name = ctx->varDecl()->var()->varName()->NAME()->toString(); + result._assign.expr = getExpr(ctx->varDecl()->var()->expr()); + } + if (ctx->ifStmt() != nullptr) { result.type = StmtType::If; - result._if.condition = getExpr(ctx->conditional()->ifCond()->expr()); - result._if.body = getBody(ctx->conditional()->ifCond()->body()); + result._if.condition = getExpr(ctx->ifStmt()->expr()); + result._if.body = getBody(ctx->ifStmt()->body()); + for (auto ei : ctx->ifStmt()->elseIfStmt()) { + result._if.elses.emplace_back( + true, + std::make_unique(getExpr(ei->expr())), + getBody(ei->body()) + ); + } + if (ctx->ifStmt()->elseStmt() != nullptr) { + result._if.elses.emplace_back( + false, + nullptr, + getBody(ctx->ifStmt()->elseStmt()->body()) + ); + } } - if (ctx->loop() != nullptr) { + if (ctx->switchStmt() != nullptr) { + result.type = StmtType::Switch; + result._switch.ident.name = ctx->switchStmt()->identifierExpr()->varName()->NAME()->toString(); + for (auto c : ctx->switchStmt()->switchBody()->switchCase()) { + result._switch.cases.emplace_back( + std::make_unique(getExpr(c->expr())), + getBody(c->body()) + ); + } + } + if (ctx->forStmt() != nullptr) { + result.type = StmtType::For; + if (ctx->forStmt()->varInit() != nullptr) { + result._for.varName = ctx->forStmt()->varInit()->varName()->NAME()->toString(); + result._for.initValue = std::make_unique(getExpr(ctx->forStmt()->varInit()->expr())); + } + else { + result._for.varName = ctx->forStmt()->assignStmt()->identifierExpr()->varName()->NAME()->toString(); + result._for.initValue = std::make_unique(getExpr(ctx->forStmt()->assignStmt()->expr())); + } + result._for.condition = std::make_unique(getExpr(ctx->forStmt()->expr(0))); + result._for.action = std::make_unique(getExpr(ctx->forStmt()->expr(1))); + result._for.body = getBody(ctx->forStmt()->body()); + } + if (ctx->whileStmt() != nullptr) { result.type = StmtType::While; - result._while.condition = getExpr(ctx->loop()->whileLoop()->expr()); - result._while.body = getBody(ctx->loop()->whileLoop()->body()); + result._while.condition = getExpr(ctx->whileStmt()->expr()); + result._while.body = getBody(ctx->whileStmt()->body()); } - if (ctx->assignment() != nullptr) { + if (ctx->assignStmt() != nullptr) { result.type = StmtType::Assign; - //result._assign.lexpr = getExpr(ctx->assignment()->); - result._assign.rexpr = getExpr(ctx->assignment()->expr()); + result._assign.name = ctx->assignStmt()->identifierExpr()->varName()->NAME()->toString(); + result._assign.expr = getExpr(ctx->assignStmt()->expr()); } if (ctx->returnStmt() != nullptr) { result.type = StmtType::Return; @@ -146,9 +382,5 @@ Stmt getStmt(TocParser::StmtContext * ctx) { 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; } \ No newline at end of file