abouttreesummaryrefslogcommitdiff
path: root/src
diff options
context:
space:
mode:
authorPatrick Schönberger2021-07-30 18:32:33 +0200
committerPatrick Schönberger2021-07-30 18:32:33 +0200
commit5f9668526491332f62c05ad831dbf6d5fdc2b6d0 (patch)
treebed333edc9b4e0dc166b0a957d4587ff086307d8 /src
parentb64d16088b29615d222d33450cf0315467400e59 (diff)
downloadtoc-5f9668526491332f62c05ad831dbf6d5fdc2b6d0.tar.gz
toc-5f9668526491332f62c05ad831dbf6d5fdc2b6d0.zip
complete grammar
Diffstat (limited to 'src')
-rw-r--r--src/repr.h140
-rw-r--r--src/repr_get.h18
-rw-r--r--src/toc.h32
3 files changed, 135 insertions, 55 deletions
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<TypeModifier> 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<Expr> 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<Expr> 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> expr;
+};
+
+struct BinaryOperatorExpr {
+ BinaryOperatorType type;
std::shared_ptr<Expr> lexpr;
std::shared_ptr<Expr> rexpr;
- OperatorType type;
+};
+
+struct TernaryOperatorExpr {
+ std::shared_ptr<Expr> lexpr;
+ std::shared_ptr<Expr> rexprTrue;
+ std::shared_ptr<Expr> rexprFalse;
};
struct DotExpr {
std::shared_ptr<Expr> 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> expr;
+ Body body;
+};
struct IfStmt {
Expr condition;
Body body;
+ std::vector<ElseStmt> elses;
};
-struct WhileStmt {
- Expr condition;
+struct SwitchCase {
+ std::shared_ptr<Expr> expr;
Body body;
};
-struct ReturnStmt {
- Expr expr;
+struct SwitchStmt {
+ IdentifierExpr ident;
+ std::vector<SwitchCase> cases;
+};
+
+struct ForStmt {
+ AssignStmt assign;
+ std::shared_ptr<Expr> condition;
+ std::shared_ptr<Expr> 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<Expr>(getExpr(ctx->binaryOperator()->nonOpExpr(0)));
- result.rexpr = std::make_unique<Expr>(getExpr(ctx->binaryOperator()->nonOpExpr(1)));
+ result.lexpr = std::make_unique<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(0)));
+ result.rexpr = std::make_unique<Expr>(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<T> & 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;
}