From 5f9668526491332f62c05ad831dbf6d5fdc2b6d0 Mon Sep 17 00:00:00 2001 From: Patrick Schönberger Date: Fri, 30 Jul 2021 18:32:33 +0200 Subject: complete grammar --- src/repr.h | 140 +++++++++++++++++++++++++++++++++++++++++++-------------- src/repr_get.h | 18 ++++---- src/toc.h | 32 +++++++------ 3 files changed, 135 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/repr.h b/src/repr.h index 6737ef4..c745ba1 100644 --- a/src/repr.h +++ b/src/repr.h @@ -14,22 +14,36 @@ struct Body; struct Function; struct Struct; struct Program; -struct CallExpr; -struct LiteralExpr; -struct VariableExpr; +struct FuncExpr; +struct LitExpr; +struct IdentifierExpr; struct BracketsExpr; -struct OperatorExpr; +struct UnaryOperatorExpr; +struct BinaryOperatorExpr; +struct TernaryOperatorExpr; struct DotExpr; struct Expr; struct IfStmt; +struct SwitchStmt; +struct ForStmt; struct WhileStmt; -struct ReturnStmt; struct AssignStmt; +struct ReturnStmt; struct Stmt; +enum class TypeModifierType { + Pointer, Array +}; + +struct TypeModifier { + TypeModifierType type; + + int _arraySize; +}; struct Type { std::string name; + std::vector modifiers; }; struct Variable { @@ -62,19 +76,28 @@ struct Program { }; enum class ExprType { - Call, Literal, Variable, Brackets, Operator, Dot + Func, Lit, Identifier, Brackets, UnaryOperator, BinaryOperator, TernaryOperator, Dot }; -struct CallExpr { +struct FuncExpr { std::string functionName; std::vector arguments; }; -struct LiteralExpr { - int i; +enum class LitType { + Int, Decimal, String, Bool +}; + +struct LitExpr { + LitType type; + + int _int; + double _decimal; + std::string _string; + bool _bool; }; -struct VariableExpr { +struct IdentifierExpr { std::string name; }; @@ -83,50 +106,95 @@ struct BracketsExpr { std::shared_ptr rexpr; }; -enum class OperatorType { - Plus, Minus, Multiply, Divide, - Equals, NotEquals, - LessThan, GreaterThan +enum class UnaryOperatorType { + Plus, Minus, IncrementPre, DecrementPre, IncrementPost, DecrementPost, LogicalNot, BitwiseNot, Dereference, AddressOf }; -struct OperatorExpr { +enum class BinaryOperatorType { + Plus, Minus, Multiply, Divide, Modulo, BitwiseAnd, BitwiseOr, BitwiseXor, LessThan, GreaterThan, + LeftShift, RightShift, LogicalAnd, LogicalOr, Equals, NotEquals, LessThanEquals, GreaterThanEquals, BitwiseAndEquals, BitwiseOrEquals, BitwiseXorEquals, + PlusEquals, MinusEquals, MultiplyEquals, DivideEquals, ModuloEquals, + LeftShiftEquals, RightShiftEquals +}; +static std::string UnaryOperatorTypeStrings[] = { + "+", "-", "++", "--", "++", "--", "!", "~", "*", "&" }; + +static std::string BinaryOperatorTypeStrings[] = { + "+", "-", "*", "/", "%", "&", "|", "^", "<", ">", + "<<",">>","&&","||","==","!=","<=",">=","&=","|=","^=", + "+=","-=","*=","/=","%=", + "<<=",">>=" }; + +struct UnaryOperatorExpr { + UnaryOperatorType type; + std::shared_ptr expr; +}; + +struct BinaryOperatorExpr { + BinaryOperatorType type; std::shared_ptr lexpr; std::shared_ptr rexpr; - OperatorType type; +}; + +struct TernaryOperatorExpr { + std::shared_ptr lexpr; + std::shared_ptr rexprTrue; + std::shared_ptr rexprFalse; }; struct DotExpr { std::shared_ptr lexpr; - std::string name; + IdentifierExpr ident; }; struct Expr { ExprType type; - CallExpr _call; - LiteralExpr _literal; - VariableExpr _variable; - BracketsExpr _brackets; - OperatorExpr _operator; - DotExpr _dot; + FuncExpr _func; + LitExpr _lit; + IdentifierExpr _identifier; + BracketsExpr _brackets; + UnaryOperatorExpr _unaryOperator; + BinaryOperatorExpr _binaryOperator; + TernaryOperatorExpr _ternaryOperator; + DotExpr _dot; }; enum class StmtType { - If, While, Return, Assign, Expr + If, Switch, For, While, Assign, Return, Expr }; +struct ElseStmt { + bool _if; + std::shared_ptr expr; + Body body; +}; struct IfStmt { Expr condition; Body body; + std::vector elses; }; -struct WhileStmt { - Expr condition; +struct SwitchCase { + std::shared_ptr expr; Body body; }; -struct ReturnStmt { - Expr expr; +struct SwitchStmt { + IdentifierExpr ident; + std::vector cases; +}; + +struct ForStmt { + AssignStmt assign; + std::shared_ptr condition; + std::shared_ptr action; + Body body; +}; + +struct WhileStmt { + Expr condition; + Body body; }; struct AssignStmt { @@ -134,14 +202,20 @@ struct AssignStmt { Expr rexpr; }; +struct ReturnStmt { + Expr expr; +}; + struct Stmt { StmtType type; - IfStmt _if; - WhileStmt _while; - ReturnStmt _return; - AssignStmt _assign; - Expr _expr; + IfStmt _if; + SwitchStmt _switch; + ForStmt _for; + WhileStmt _while; + AssignStmt _assign; + ReturnStmt _return; + Expr _expr; }; diff --git a/src/repr_get.h b/src/repr_get.h index 7dbeea3..afea882 100644 --- a/src/repr_get.h +++ b/src/repr_get.h @@ -8,9 +8,8 @@ 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::NonOpExprContext * ctx); +//Expr getExpr(TocParser::NonAccessExprContext * ctx); Expr getExpr(TocParser::ExprContext * ctx); Stmt getStmt(TocParser::StmtContext * ctx); @@ -76,12 +75,15 @@ Program getProgram(TocParser::ProgContext * ctx) { } return result; } -OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx) { +OperatorExpr getOperatorExpr(TocParser::OpExprContext * ctx) { OperatorExpr result; - result.lexpr = std::make_unique(getExpr(ctx->binaryOperator()->nonOpExpr(0))); - result.rexpr = std::make_unique(getExpr(ctx->binaryOperator()->nonOpExpr(1))); + result.lexpr = std::make_unique(getExpr(ctx->binaryOp()->nonOpExpr(0))); + result.rexpr = std::make_unique(getExpr(ctx->binaryOp()->nonOpExpr(1))); - std::string op = ctx->binaryOperator()->BINARY_OPERATOR(0)->toString(); + std::string op = ctx->binaryOp()->BINARY_OP(0)->toString(); + for (auto o : ops) { + + } if (op == "+") result.type = OperatorType::Plus; if (op == "-") result.type = OperatorType::Minus; if (op == "*") result.type = OperatorType::Multiply; @@ -121,7 +123,7 @@ Expr getExpr(TocParser::NonOpExprContext * ctx) { } return result; } -Expr getExpr(TocParser::NonSubscriptExprContext * ctx) { +Expr getExpr(TocParser::NonAccessExprContext * ctx) { Expr result; if (ctx->funcCall() != nullptr) { result.type = ExprType::Call; diff --git a/src/toc.h b/src/toc.h index 642bc37..67f92dc 100644 --- a/src/toc.h +++ b/src/toc.h @@ -18,7 +18,9 @@ std::ostream & operator<< (std::ostream & out, const std::vector & v) { std::ostream & operator<< (std::ostream & out, const Type & t); std::ostream & operator<< (std::ostream & out, const Variable & v); std::ostream & operator<< (std::ostream & out, const Body & b); -std::ostream & operator<< (std::ostream & out, const OperatorExpr & o); +std::ostream & operator<< (std::ostream & out, const UnaryOperatorExpr & o); +std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o); +std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o); std::ostream & operator<< (std::ostream & out, const Expr & e); std::ostream & operator<< (std::ostream & out, const Stmt & s); @@ -65,21 +67,23 @@ std::ostream & operator<< (std::ostream & out, const Body & b) { return out; } -std::ostream & operator<< (std::ostream & out, const OperatorExpr & o) { - out << *o.lexpr << " "; - - switch (o.type) { - case OperatorType::Plus: out << "+"; break; - case OperatorType::Minus: out << "-"; break; - case OperatorType::Multiply: out << "*"; break; - case OperatorType::Divide: out << "/"; break; - case OperatorType::Equals: out << "=="; break; - case OperatorType::NotEquals: out << "!="; break; - case OperatorType::LessThan: out << "<"; break; - case OperatorType::GreaterThan: out << ">"; break; +std::ostream & operator<< (std::ostream & out, const UnaryOperatorExpr & o) { + if (o.type == UnaryOperatorType::IncrementPost || o.type == UnaryOperatorType::DecrementPost) { + out << UnaryOperatorTypeStrings[(int)o.type] << *o.expr; + } + else { + out << *o.expr << UnaryOperatorTypeStrings[(int)o.type]; } - out << " " << *o.rexpr; + return out; +} +std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o) { + out << *o.lexpr << " " << BinaryOperatorTypeStrings[(int)o.type] << " " << *o.rexpr; + + return out; +} +std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o) { + out << *o.lexpr << " ? " << *o.rexprTrue << " : " << *o.rexprFalse; return out; } -- cgit v1.2.3