abouttreesummaryrefslogcommitdiff
path: root/src
diff options
context:
space:
mode:
authorPatrick Schönberger2021-07-31 23:18:40 +0200
committerPatrick Schönberger2021-07-31 23:18:40 +0200
commit9f5457a18f551d261e4bd380ea16a52dc5b04cf9 (patch)
treea0db0635e21039374a022d1f0619c88dcb5d0dd8 /src
parent5f9668526491332f62c05ad831dbf6d5fdc2b6d0 (diff)
downloadtoc-9f5457a18f551d261e4bd380ea16a52dc5b04cf9.tar.gz
toc-9f5457a18f551d261e4bd380ea16a52dc5b04cf9.zip
compile again
Diffstat (limited to 'src')
-rw-r--r--src/repr.h19
-rw-r--r--src/repr_get.h353
-rw-r--r--src/toc.h74
3 files changed, 306 insertions, 140 deletions
diff --git a/src/repr.h b/src/repr.h
index c745ba1..cc28746 100644
--- a/src/repr.h
+++ b/src/repr.h
@@ -97,6 +97,7 @@ struct LitExpr {
bool _bool;
};
+// TODO: accessExpr
struct IdentifierExpr {
std::string name;
};
@@ -107,14 +108,17 @@ struct BracketsExpr {
};
enum class UnaryOperatorType {
- Plus, Minus, IncrementPre, DecrementPre, IncrementPost, DecrementPost, LogicalNot, BitwiseNot, Dereference, AddressOf
+ Plus, Minus, IncrementPre, DecrementPre, IncrementPost, DecrementPost,
+ LogicalNot, BitwiseNot, Dereference, AddressOf,
+ COUNT
};
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
+ LeftShiftEquals, RightShiftEquals,
+ COUNT
};
static std::string UnaryOperatorTypeStrings[] = {
"+", "-", "++", "--", "++", "--", "!", "~", "*", "&" };
@@ -143,10 +147,11 @@ struct TernaryOperatorExpr {
};
struct DotExpr {
- std::shared_ptr<Expr> lexpr;
+ std::shared_ptr<Expr> expr;
IdentifierExpr ident;
};
+// TODO: paren expr
struct Expr {
ExprType type;
@@ -185,8 +190,10 @@ struct SwitchStmt {
std::vector<SwitchCase> cases;
};
+// TODO: int i = 0 (var decl)
struct ForStmt {
- AssignStmt assign;
+ std::string varName;
+ std::shared_ptr<Expr> initValue;
std::shared_ptr<Expr> condition;
std::shared_ptr<Expr> action;
Body body;
@@ -198,8 +205,8 @@ struct WhileStmt {
};
struct AssignStmt {
- Expr lexpr;
- Expr rexpr;
+ std::string name;
+ Expr expr;
};
struct ReturnStmt {
diff --git a/src/repr_get.h b/src/repr_get.h
index afea882..b624e4c 100644
--- a/src/repr_get.h
+++ b/src/repr_get.h
@@ -2,20 +2,31 @@
#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);
-//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);
+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()) {
+ result.modifiers.emplace_back(
+ m->toString() == "*" ? TypeModifierType::Pointer : TypeModifierType::Array,
+ m->toString() == "*" ? -1 : atoi(m->NUMBER()->toString().c_str())
+ );
+ }
return result;
}
Variable getVariable(TocParser::VarContext * ctx) {
@@ -29,6 +40,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));
@@ -75,127 +88,257 @@ Program getProgram(TocParser::ProgContext * ctx) {
}
return result;
}
-OperatorExpr getOperatorExpr(TocParser::OpExprContext * ctx) {
- OperatorExpr result;
+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<Expr>(getExpr(ctx->prefixOp()->nonOpExpr()));
+ else
+ result.expr = std::make_unique<Expr>(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<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(0)));
result.rexpr = std::make_unique<Expr>(getExpr(ctx->binaryOp()->nonOpExpr(1)));
- std::string op = ctx->binaryOp()->BINARY_OP(0)->toString();
- for (auto o : ops) {
+ std::string op = ctx->binaryOp()->binary_op(0)->getText();
+ std::cout << op << std::endl;
- }
- 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;
+ result.type = getBinaryOperatorType(op);
+
+ return result;
+}
+TernaryOperatorExpr getTernaryOperatorExpr(TocParser::OpExprContext * ctx) {
+ TernaryOperatorExpr result;
+ result.lexpr = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->nonOpExpr()));
+ result.rexprTrue = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->expr(0)));
+ result.rexprFalse = std::make_unique<Expr>(getExpr(ctx->ternaryOp()->expr(1)));
return result;
}
Expr getExpr(TocParser::NonOpExprContext * 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.functionName = ctx->funcCall()->funcName()->NAME()->toString();
- }
- 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 = std::make_unique<Expr>(getExpr(ctx->subscript()->nonSubscriptExpr()));
- result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));
- }
- if (ctx->memberAccess() != nullptr) {
- result.type = ExprType::Dot;
- Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();
- result._dot.lexpr = std::make_unique<Expr>(e);
- result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();
+ 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());
+ }
+ if (ctx->accessExpr() != nullptr) {
+ // TODO: access chain
+ for (auto sub : ctx->accessExpr()->accessSubExpr()) {
+ if (sub->accessMember() != nullptr) {
+ result.type = ExprType::Dot;
+ result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString();
+ }
+ else {
+ result.type = ExprType::Brackets;
+ result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
+ result._brackets.rexpr = std::make_unique<Expr>(getExpr(sub->accessBrackets()->expr()));
+ }
+ }
}
return result;
}
Expr getExpr(TocParser::NonAccessExprContext * 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.functionName = ctx->funcCall()->funcName()->NAME()->toString();
- }
- 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->memberAccess() != nullptr) {
- result.type = ExprType::Dot;
- Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();
- result._dot.lexpr = std::make_unique<Expr>(e);
- result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();
+ 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());
}
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.functionName = ctx->funcCall()->funcName()->NAME()->toString();
- }
- 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 = std::make_unique<Expr>(getExpr(ctx->subscript()->nonSubscriptExpr()));
- result._brackets.rexpr = std::make_unique<Expr>(getExpr(ctx->subscript()->expr()));
- }
- if (ctx->memberAccess() != nullptr) {
- result.type = ExprType::Dot;
- Expr e; e.type = ExprType::Variable; e._variable.name = ctx->memberAccess()->identifier(0)->varName()->NAME()->toString();
- result._dot.lexpr = std::make_unique<Expr>(e);
- result._dot.name = ctx->memberAccess()->identifier(1)->varName()->NAME()->toString();
- }
- if (ctx->operatorExpr() != nullptr) {
- result.type = ExprType::Operator;
- result._operator = getOperatorExpr(ctx->operatorExpr());
+ 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());
+ }
+ if (ctx->accessExpr() != nullptr) {
+ // TODO: access chain
+ for (auto sub : ctx->accessExpr()->accessSubExpr()) {
+ if (sub->accessMember() != nullptr) {
+ result.type = ExprType::Dot;
+ result._dot.expr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
+ result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString();
+ }
+ else {
+ result.type = ExprType::Brackets;
+ result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
+ result._brackets.rexpr = std::make_unique<Expr>(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<Expr>(tmp);
+ result._binaryOperator.type = getBinaryOperatorType(ctx->opExpr()->binaryOp()->binary_op(i)->getText());
+ result._binaryOperator.rexpr = std::make_unique<Expr>(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<Expr>(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<Expr>(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<Expr>(getExpr(ctx->forStmt()->varInit()->expr()));
+ }
+ else {
+ result._for.varName = ctx->forStmt()->assignStmt()->identifierExpr()->varName()->NAME()->toString();
+ result._for.initValue = std::make_unique<Expr>(getExpr(ctx->forStmt()->assignStmt()->expr()));
+ }
+ result._for.condition = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(0)));
+ result._for.action = std::make_unique<Expr>(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;
@@ -205,9 +348,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
diff --git a/src/toc.h b/src/toc.h
index 67f92dc..6dd611d 100644
--- a/src/toc.h
+++ b/src/toc.h
@@ -59,7 +59,7 @@ std::ostream & operator<< (std::ostream & out, const Body & b) {
for (auto s : b.statements) {
indent(out);
- out << s << ";\n";
+ out << s << "\n";
}
indent(out, -2);
@@ -89,34 +89,57 @@ std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o) {
}
std::ostream & operator<< (std::ostream & out, const Expr & e) {
switch (e.type) {
+ case ExprType::Func:
+ out << e._func.functionName << "(" << e._func.arguments << ")"; break;
+ case ExprType::Lit:
+ /**/ if (e._lit.type == LitType::Int) out << e._lit._int;
+ else if (e._lit.type == LitType::Decimal) out << e._lit._decimal;
+ else if (e._lit.type == LitType::String) out << e._lit._string;
+ else if (e._lit.type == LitType::Bool) out << e._lit._bool;
+ break;
+ case ExprType::Identifier:
+ out << e._identifier.name; break;
case ExprType::Brackets:
out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;
- case ExprType::Call:
- out << e._call.functionName << "(" << e._call.arguments << ")"; break;
case ExprType::Dot:
- out << e._dot.name << "." << *e._dot.lexpr; break;
- case ExprType::Literal:
- out << e._literal.i; break;
- case ExprType::Operator:
- out << e._operator; break;
- case ExprType::Variable:
- out << e._variable.name; break;
+ out << *e._dot.expr << "." << e._dot.ident.name; break;
+ case ExprType::UnaryOperator:
+ out << e._unaryOperator; break;
+ case ExprType::BinaryOperator:
+ out << e._binaryOperator; break;
+ case ExprType::TernaryOperator:
+ out << e._ternaryOperator; break;
}
return out;
}
std::ostream & operator<< (std::ostream & out, const Stmt & s) {
switch (s.type) {
- case StmtType::Assign:
- out << s._assign.lexpr << "=" << s._assign.rexpr; break;
- case StmtType::Expr:
- out << s._expr; break;
case StmtType::If:
out << "if (" << s._if.condition << ")\n" << s._if.body; break;
- case StmtType::Return:
- out << "return " << s._return.expr; break;
+ case StmtType::Switch:
+ out << "switch (" << s._switch.ident.name << ")\n{\n";
+ for (auto c : s._switch.cases) {
+ indent(out, 2);
+ out << "case " << *c.expr << ": " << c.body << "break;";
+ }
+ indent(out, -2);
+ out << "}\n";
+ break;
+ case StmtType::For:
+ out << "for (" <<
+ s._for.varName << " = " << *s._for.initValue << "; " <<
+ *s._for.condition << "; " <<
+ *s._for.action <<
+ ")\n" << s._for.body; break;
case StmtType::While:
out << "while (" << s._while.condition << ")\n" << s._while.body; break;
+ case StmtType::Assign:
+ out << s._assign.name << "=" << s._assign.expr << ";"; break;
+ case StmtType::Return:
+ out << "return " << s._return.expr << ";"; break;
+ case StmtType::Expr:
+ out << s._expr << ";"; break;
}
return out;
@@ -124,17 +147,7 @@ std::ostream & operator<< (std::ostream & out, const Stmt & s) {
void tocFunction (std::ostream & out, const Function & f, bool stub) {
- out << f.returnType << " " << f.name << " (";
-
- bool comma = false;
- for (auto p : f.parameters) {
- if (comma) out << ", ";
- else comma = true;
-
- out << p.type << " " << p.name;
- }
-
- out << ")";
+ out << f.returnType << " " << f.name << " (" << f.parameters << ")";
if (stub) {
out << ";\n";
@@ -147,6 +160,9 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) {
out << "struct " << s.name;
if (stub) {
out << ";\n";
+ for (auto m : s.methods) {
+ out << m.returnType << " " << s.name << "_" << m.name << " (" << m.parameters << ");\n";
+ }
return;
}
out << "\n{\n";
@@ -159,6 +175,10 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) {
indent(out, -2);
out << "};\n";
+
+ for (auto m : s.methods) {
+ out << m.returnType << " " << s.name << "_" << m.name << " (" << m.parameters << ")\n" << m.body;
+ }
}
void tocProgram (std::ostream & out, const Program & p) {
for (auto s : p.structs) {