diff options
| author | Patrick Schönberger | 2021-08-03 12:08:12 +0200 |
|---|---|---|
| committer | Patrick Schönberger | 2021-08-03 12:08:12 +0200 |
| commit | 71a20a4f3d4e5f5278f7d004af710af89dfd7ebc (patch) | |
| tree | 4227c1cc0c0310a4b73f3b30b5ac1ee7c90b274e /src | |
| parent | 17fac686375c2823d22415e32e5b7e63bbfe7c54 (diff) | |
| download | toc-71a20a4f3d4e5f5278f7d004af710af89dfd7ebc.tar.gz toc-71a20a4f3d4e5f5278f7d004af710af89dfd7ebc.zip | |
add namespace, private struct member grammar, change bracket style
Diffstat (limited to 'src')
| -rw-r--r-- | src/check.h | 12 | ||||
| -rw-r--r-- | src/main.cpp | 6 | ||||
| -rw-r--r-- | src/repr.h | 99 | ||||
| -rw-r--r-- | src/repr_get.h | 240 | ||||
| -rw-r--r-- | src/toc.h | 115 |
5 files changed, 316 insertions, 156 deletions
diff --git a/src/check.h b/src/check.h index 4e52ccd..d85bd81 100644 --- a/src/check.h +++ b/src/check.h @@ -19,7 +19,8 @@ bool checkFunction( {
vars.insert(vars.end(), f.parameters.begin(), f.parameters.end());
vars.insert(vars.end(), f.body.variables.begin(), f.body.variables.end());
- for (auto s : f.body.statements) {
+ for (auto s : f.body.statements)
+ {
if (!checkStmt(s, structs, funcs, vars))
return false;
}
@@ -28,15 +29,18 @@ bool checkFunction( bool checkProgram(const Program & p)
{
- for (auto f : p.functions) {
+ for (auto f : p.functions)
+ {
if (!checkFunction(f, p.structs, p.functions, p.variables))
return false;
}
- for (auto s : p.structs) {
+ for (auto s : p.structs)
+ {
std::vector<Variable> vars = p.variables;
for (auto v : s.members)
vars.push_back(v);
- for (auto f : s.methods) {
+ for (auto f : s.methods)
+ {
if (!checkFunction(f, p.structs, p.functions, vars))
return false;
}
diff --git a/src/main.cpp b/src/main.cpp index 199f04d..4c78f5b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,8 @@ using namespace antlr4;
-int main(int argc, const char * argv[]) {
+int main(int argc, const char * argv[])
+{
std::ifstream ifs("test.toc");
ANTLRInputStream input(ifs);
@@ -24,7 +25,8 @@ int main(int argc, const char * argv[]) { TocParser::ProgContext * prog = parser.prog();
tree::ParseTree * tree = prog;
- if (parser.getNumberOfSyntaxErrors() > 0) {
+ if (parser.getNumberOfSyntaxErrors() > 0)
+ {
std::cerr << "Parsing error" << std::endl;
return 1;
}
@@ -33,65 +33,77 @@ struct AssignStmt; struct ReturnStmt;
struct Stmt;
-enum class TypeModifierType {
+enum class TypeModifierType
+{
Pointer, Array
};
-struct TypeModifier {
+struct TypeModifier
+{
TypeModifierType type;
bool _staticArray;
int _arraySize;
};
-struct Type {
+struct Type
+{
std::string name;
std::vector<TypeModifier> modifiers;
};
-struct Variable {
+struct Variable
+{
std::string name;
Type type;
};
-struct Body {
+struct Body
+{
std::vector<Variable> variables;
std::vector<Stmt> statements;
};
-struct Function {
+struct Function
+{
Type returnType;
std::string name;
std::vector<Variable> parameters;
Body body;
};
-struct Struct {
+struct Struct
+{
std::string name;
std::vector<Variable> members;
std::vector<Function> methods;
};
-struct Program {
+struct Program
+{
std::vector<Variable> variables;
std::vector<Struct> structs;
std::vector<Function> functions;
};
-enum class ExprType {
+enum class ExprType
+{
Func, Lit, Identifier, Brackets, UnaryOperator, BinaryOperator, TernaryOperator, Dot
};
-struct FuncExpr {
+struct FuncExpr
+{
std::string functionName;
std::vector<Expr> arguments;
};
-enum class LitType {
+enum class LitType
+{
Int, Decimal, String, Bool
};
-struct LitExpr {
+struct LitExpr
+{
LitType type;
int _int;
@@ -101,61 +113,72 @@ struct LitExpr { };
// TODO: accessExpr
-struct IdentifierExpr {
+struct IdentifierExpr
+{
std::string name;
};
-struct BracketsExpr {
+struct BracketsExpr
+{
std::shared_ptr<Expr> lexpr;
std::shared_ptr<Expr> rexpr;
};
-enum class UnaryOperatorType {
+enum class UnaryOperatorType
+{
Plus, Minus, IncrementPre, DecrementPre, IncrementPost, DecrementPost,
LogicalNot, BitwiseNot, Dereference, AddressOf,
COUNT
};
-enum class BinaryOperatorType {
+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,
COUNT
};
-static std::string UnaryOperatorTypeStrings[] = {
+static std::string UnaryOperatorTypeStrings[] =
+{
"+", "-", "++", "--", "++", "--", "!", "~", "*", "&" };
-static std::string BinaryOperatorTypeStrings[] = {
+static std::string BinaryOperatorTypeStrings[] =
+{
"+", "-", "*", "/", "%", "&", "|", "^", "<", ">",
"<<",">>","&&","||","==","!=","<=",">=","&=","|=","^=",
"+=","-=","*=","/=","%=",
"<<=",">>=" };
-struct UnaryOperatorExpr {
+struct UnaryOperatorExpr
+{
UnaryOperatorType type;
std::shared_ptr<Expr> expr;
};
-struct BinaryOperatorExpr {
+struct BinaryOperatorExpr
+{
BinaryOperatorType type;
std::shared_ptr<Expr> lexpr;
std::shared_ptr<Expr> rexpr;
};
-struct TernaryOperatorExpr {
+struct TernaryOperatorExpr
+{
std::shared_ptr<Expr> lexpr;
std::shared_ptr<Expr> rexprTrue;
std::shared_ptr<Expr> rexprFalse;
};
-struct DotExpr {
+struct DotExpr
+{
std::shared_ptr<Expr> expr;
IdentifierExpr ident;
};
// TODO: paren expr
-struct Expr {
+struct Expr
+{
ExprType type;
bool parenthesized;
@@ -170,33 +193,39 @@ struct Expr { DotExpr _dot;
};
-enum class StmtType {
+enum class StmtType
+{
If, Switch, For, While, Assign, Return, Expr
};
-struct ElseStmt {
+struct ElseStmt
+{
bool _if;
std::shared_ptr<Expr> expr;
Body body;
};
-struct IfStmt {
+struct IfStmt
+{
Expr condition;
Body body;
std::vector<ElseStmt> elses;
};
-struct SwitchCase {
+struct SwitchCase
+{
std::shared_ptr<Expr> expr;
Body body;
};
-struct SwitchStmt {
+struct SwitchStmt
+{
IdentifierExpr ident;
std::vector<SwitchCase> cases;
};
// TODO: int i = 0 (var decl)
-struct ForStmt {
+struct ForStmt
+{
std::string varName;
std::shared_ptr<Expr> initValue;
std::shared_ptr<Expr> condition;
@@ -204,21 +233,25 @@ struct ForStmt { Body body;
};
-struct WhileStmt {
+struct WhileStmt
+{
Expr condition;
Body body;
};
-struct AssignStmt {
+struct AssignStmt
+{
std::string name;
Expr expr;
};
-struct ReturnStmt {
+struct ReturnStmt
+{
Expr expr;
};
-struct Stmt {
+struct Stmt
+{
StmtType type;
IfStmt _if;
diff --git a/src/repr_get.h b/src/repr_get.h index 70ae3b1..118d7b3 100644 --- a/src/repr_get.h +++ b/src/repr_get.h @@ -18,10 +18,12 @@ Expr getExpr(TocParser::NonAccessExprContext * ctx); Expr getExpr(TocParser::ExprContext * ctx);
Stmt getStmt(TocParser::StmtContext * ctx);
-Type getType(TocParser::TypeContext * ctx) {
+Type getType(TocParser::TypeContext * ctx)
+{
Type result;
result.name = ctx->typeName()->NAME()->toString();
- for (auto m : ctx->typeModifier()) {
+ for (auto m : ctx->typeModifier())
+ {
bool isPointer = m->getText() == "*";
bool isStaticArray = m->INT_LIT() != nullptr;
@@ -33,82 +35,105 @@ Type getType(TocParser::TypeContext * ctx) { }
return result;
}
-Variable getVariable(TocParser::VarContext * ctx) {
+Variable getVariable(TocParser::VarContext * ctx)
+{
Variable result;
result.name = ctx->varName()->NAME()->toString();
result.type = getType(ctx->type());
return result;
}
-Body getBody(TocParser::BodyContext * ctx) {
+Body getBody(TocParser::BodyContext * ctx)
+{
Body result;
- for (auto s : ctx->stmt()) {
- if (s->varDecl() != nullptr) {
+ 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 {
+ else
+ {
result.statements.push_back(getStmt(s));
}
}
return result;
}
-Function getFunction(TocParser::FuncContext * ctx) {
+Function getFunction(TocParser::FuncContext * ctx)
+{
Function result;
result.name = ctx->funcName()->NAME()->toString();
result.returnType = getType(ctx->type());
- if (!ctx->parameter()->var().empty()) {
+ if (!ctx->parameter()->var().empty())
+ {
for (auto p : ctx->parameter()->var())
result.parameters.push_back(getVariable(p));
}
result.body = getBody(ctx->body());
return result;
}
-Struct getStruct(TocParser::StructDeclContext * ctx) {
+Struct getStruct(TocParser::StructDeclContext * ctx)
+{
Struct result;
result.name = ctx->structName()->NAME()->toString();
- for (auto m : ctx->structMember()) {
- if (m->structVar() != nullptr) {
+ for (auto m : ctx->structMember())
+ {
+ if (m->structVar() != nullptr)
+ {
result.members.push_back(getVariable(m->structVar()->var()));
}
- if (m->structMethod() != nullptr) {
+ if (m->structMethod() != nullptr)
+ {
result.methods.push_back(getFunction(m->structMethod()->func()));
}
}
return result;
}
-Program getProgram(TocParser::ProgContext * ctx) {
+Program getProgram(TocParser::ProgContext * ctx)
+{
Program result;
- for (auto d : ctx->decl()) {
- if (d->varDecl() != nullptr) {
+ for (auto d : ctx->decl())
+ {
+ if (d->varDecl() != nullptr)
+ {
result.variables.push_back(getVariable(d->varDecl()->var()));
}
- if (d->funcDecl() != nullptr) {
+ if (d->funcDecl() != nullptr)
+ {
result.functions.push_back(getFunction(d->funcDecl()->func()));
}
- if (d->structDecl() != nullptr) {
+ if (d->structDecl() != nullptr)
+ {
result.structs.push_back(getStruct(d->structDecl()));
}
}
return result;
}
-UnaryOperatorType getUnaryOperatorType(const std::string & s) {
- for (int i = 0; i < (int)UnaryOperatorType::COUNT; i++) {
- if (UnaryOperatorTypeStrings[i] == s) {
+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) {
+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 getUnaryOperatorExpr(TocParser::OpExprContext * ctx)
+{
UnaryOperatorExpr result;
if (ctx->prefixOp() != nullptr)
result.expr = std::make_unique<Expr>(getExpr(ctx->prefixOp()->nonOpExpr()));
@@ -127,7 +152,8 @@ UnaryOperatorExpr getUnaryOperatorExpr(TocParser::OpExprContext * ctx) { return result;
}
-BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx) {
+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)));
@@ -138,70 +164,86 @@ BinaryOperatorExpr getBinaryOperatorExpr(TocParser::OpExprContext * ctx) { return result;
}
-TernaryOperatorExpr getTernaryOperatorExpr(TocParser::OpExprContext * ctx) {
+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 getExpr(TocParser::NonOpExprContext * ctx)
+{
Expr result;
result.parenthesized = false;
- if (ctx->funcExpr() != nullptr) {
+ 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) {
+ if (ctx->litExpr() != nullptr)
+ {
result.type = ExprType::Lit;
- if (ctx->litExpr()->INT_LIT() != nullptr) {
+ 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) {
+ 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) {
+ 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) {
+ 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) {
+ if (ctx->identifierExpr() != nullptr)
+ {
result.type = ExprType::Identifier;
result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();
}
- if (ctx->parenExpr() != nullptr) {
+ if (ctx->parenExpr() != nullptr)
+ {
result = getExpr(ctx->parenExpr()->expr());
result.parenthesized = true;
}
- if (ctx->accessExpr() != nullptr) {
+ if (ctx->accessExpr() != nullptr)
+ {
auto firstSub = ctx->accessExpr()->accessSubExpr(0);
- if (firstSub->accessMember() != nullptr) {
+ if (firstSub->accessMember() != nullptr)
+ {
result.type = ExprType::Dot;
result._dot.expr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
result._dot.ident.name = firstSub->accessMember()->identifierExpr()->varName()->NAME()->toString();
}
- else {
+ else
+ {
result.type = ExprType::Brackets;
result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
result._brackets.rexpr = std::make_unique<Expr>(getExpr(firstSub->accessBrackets()->expr()));
}
- for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++) {
+ for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++)
+ {
Expr tmp = result;
auto sub = ctx->accessExpr()->accessSubExpr(i);
- if (sub->accessMember() != nullptr) {
+ if (sub->accessMember() != nullptr)
+ {
result.type = ExprType::Dot;
result._dot.expr = std::make_unique<Expr>(tmp);
result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString();
}
- else {
+ else
+ {
result.type = ExprType::Brackets;
result._brackets.lexpr = std::make_unique<Expr>(tmp);
result._brackets.rexpr = std::make_unique<Expr>(getExpr(sub->accessBrackets()->expr()));
@@ -210,129 +252,158 @@ Expr getExpr(TocParser::NonOpExprContext * ctx) { }
return result;
}
-Expr getExpr(TocParser::NonAccessExprContext * ctx) {
+Expr getExpr(TocParser::NonAccessExprContext * ctx)
+{
Expr result;
result.parenthesized = false;
- if (ctx->funcExpr() != nullptr) {
+ 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) {
+ if (ctx->identifierExpr() != nullptr)
+ {
result.type = ExprType::Identifier;
result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();
}
- if (ctx->parenExpr() != nullptr) {
+ if (ctx->parenExpr() != nullptr)
+ {
result = getExpr(ctx->parenExpr()->expr());
result.parenthesized = true;
}
return result;
}
-Expr getExpr(TocParser::ExprContext * ctx) {
+Expr getExpr(TocParser::ExprContext * ctx)
+{
Expr result;
result.parenthesized = false;
- if (ctx->funcExpr() != nullptr) {
+ 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) {
+ if (ctx->litExpr() != nullptr)
+ {
result.type = ExprType::Lit;
- if (ctx->litExpr()->INT_LIT() != nullptr) {
+ 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) {
+ 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) {
+ 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) {
+ 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) {
+ if (ctx->identifierExpr() != nullptr)
+ {
result.type = ExprType::Identifier;
result._identifier.name = ctx->identifierExpr()->varName()->NAME()->toString();
}
- if (ctx->parenExpr() != nullptr) {
+ if (ctx->parenExpr() != nullptr)
+ {
result = getExpr(ctx->parenExpr()->expr());
result.parenthesized = true;
}
- if (ctx->accessExpr() != nullptr) {
+ if (ctx->accessExpr() != nullptr)
+ {
auto firstSub = ctx->accessExpr()->accessSubExpr(0);
- if (firstSub->accessMember() != nullptr) {
+ if (firstSub->accessMember() != nullptr)
+ {
result.type = ExprType::Dot;
result._dot.expr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
result._dot.ident.name = firstSub->accessMember()->identifierExpr()->varName()->NAME()->toString();
}
- else {
+ else
+ {
result.type = ExprType::Brackets;
result._brackets.lexpr = std::make_unique<Expr>(getExpr(ctx->accessExpr()->nonAccessExpr()));
result._brackets.rexpr = std::make_unique<Expr>(getExpr(firstSub->accessBrackets()->expr()));
}
- for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++) {
+ for (int i = 1; i < ctx->accessExpr()->accessSubExpr().size(); i++)
+ {
Expr tmp = result;
auto sub = ctx->accessExpr()->accessSubExpr(i);
- if (sub->accessMember() != nullptr) {
+ if (sub->accessMember() != nullptr)
+ {
result.type = ExprType::Dot;
result._dot.expr = std::make_unique<Expr>(tmp);
result._dot.ident.name = sub->accessMember()->identifierExpr()->varName()->NAME()->toString();
}
- else {
+ else
+ {
result.type = ExprType::Brackets;
result._brackets.lexpr = std::make_unique<Expr>(tmp);
result._brackets.rexpr = std::make_unique<Expr>(getExpr(sub->accessBrackets()->expr()));
}
}
}
- if (ctx->opExpr() != nullptr) {
- if (ctx->opExpr()->prefixOp() != nullptr || ctx->opExpr()->postfixOp() != nullptr) {
+ 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) {
+ 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++) {
+ 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) {
+ else if (ctx->opExpr()->ternaryOp() != nullptr)
+ {
result.type = ExprType::TernaryOperator;
result._ternaryOperator = getTernaryOperatorExpr(ctx->opExpr());
}
}
return result;
}
-Stmt getStmt(TocParser::StmtContext * ctx) {
+Stmt getStmt(TocParser::StmtContext * ctx)
+{
Stmt result;
- if (ctx->varDecl() != nullptr && ctx->varDecl()->var()->expr() != 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) {
+ if (ctx->ifStmt() != nullptr)
+ {
result.type = StmtType::If;
result._if.condition = getExpr(ctx->ifStmt()->expr());
result._if.body = getBody(ctx->ifStmt()->body());
- for (auto ei : ctx->ifStmt()->elseIfStmt()) {
+ 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) {
+ if (ctx->ifStmt()->elseStmt() != nullptr)
+ {
result._if.elses.emplace_back(
false,
nullptr,
@@ -340,23 +411,28 @@ Stmt getStmt(TocParser::StmtContext * ctx) { );
}
}
- if (ctx->switchStmt() != 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()) {
+ 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) {
+ if (ctx->forStmt() != nullptr)
+ {
result.type = StmtType::For;
- if (ctx->forStmt()->varInit() != nullptr) {
+ 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 {
+ else
+ {
result._for.varName = ctx->forStmt()->assignStmt()->identifierExpr()->varName()->NAME()->toString();
result._for.initValue = std::make_unique<Expr>(getExpr(ctx->forStmt()->assignStmt()->expr()));
}
@@ -364,21 +440,25 @@ Stmt getStmt(TocParser::StmtContext * ctx) { result._for.action = std::make_unique<Expr>(getExpr(ctx->forStmt()->expr(1)));
result._for.body = getBody(ctx->forStmt()->body());
}
- if (ctx->whileStmt() != nullptr) {
+ if (ctx->whileStmt() != nullptr)
+ {
result.type = StmtType::While;
result._while.condition = getExpr(ctx->whileStmt()->expr());
result._while.body = getBody(ctx->whileStmt()->body());
}
- if (ctx->assignStmt() != nullptr) {
+ if (ctx->assignStmt() != nullptr)
+ {
result.type = StmtType::Assign;
result._assign.name = ctx->assignStmt()->identifierExpr()->varName()->NAME()->toString();
result._assign.expr = getExpr(ctx->assignStmt()->expr());
}
- if (ctx->returnStmt() != nullptr) {
+ if (ctx->returnStmt() != nullptr)
+ {
result.type = StmtType::Return;
result._return.expr = getExpr(ctx->returnStmt()->expr());
}
- if (ctx->expr() != nullptr) {
+ if (ctx->expr() != nullptr)
+ {
result.type = StmtType::Expr;
result._expr = getExpr(ctx->expr());
}
@@ -6,9 +6,11 @@ #include "repr.h"
template<typename T>
-std::ostream & operator<< (std::ostream & out, const std::vector<T> & v) {
+std::ostream & operator<< (std::ostream & out, const std::vector<T> & v)
+{
bool comma = false;
- for (auto t : v) {
+ for (auto t : v)
+ {
if (comma) out << ", ";
else comma = true;
out << t;
@@ -31,29 +33,35 @@ void tocProgram (std::ostream & out, const Program & p); static const int TAB_WIDTH = 2;
static int indentation = 0;
-static void indent(std::ostream & out, int change = 0) {
+static void indent(std::ostream & out, int change = 0)
+{
indentation += change;
out << std::string(indentation, ' ');
}
-std::ostream & operator<< (std::ostream & out, const Type & t) {
+std::ostream & operator<< (std::ostream & out, const Type & t)
+{
out << t.name;
return out;
}
-std::ostream & operator<< (std::ostream & out, const Variable & v) {
+std::ostream & operator<< (std::ostream & out, const Variable & v)
+{
out << v.type << " ";
std::stringstream sstr;
std::string s = v.name;
- for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++) {
- if (m->type == TypeModifierType::Pointer) {
+ for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++)
+ {
+ if (m->type == TypeModifierType::Pointer)
+ {
sstr.str(std::string());
sstr << "*(" << s << ")";
s = sstr.str();
}
- else {
+ else
+ {
sstr.str(std::string());
sstr << "(" << s << ")[";
if (m->_staticArray)
@@ -66,19 +74,22 @@ std::ostream & operator<< (std::ostream & out, const Variable & v) { return out;
}
-std::ostream & operator<< (std::ostream & out, const Body & b) {
+std::ostream & operator<< (std::ostream & out, const Body & b)
+{
indent(out);
out << "{\n";
indentation += 2;
- for (auto v : b.variables) {
+ for (auto v : b.variables)
+ {
indent(out);
out << v << ";\n";
}
out << "\n";
- for (auto s : b.statements) {
+ for (auto s : b.statements)
+ {
indent(out);
out << s << "\n";
}
@@ -88,31 +99,38 @@ std::ostream & operator<< (std::ostream & out, const Body & b) { return out;
}
-std::ostream & operator<< (std::ostream & out, const UnaryOperatorExpr & o) {
- if (o.type == UnaryOperatorType::IncrementPost || o.type == UnaryOperatorType::DecrementPost) {
+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 {
+ else
+ {
out << *o.expr << UnaryOperatorTypeStrings[(int)o.type];
}
return out;
}
-std::ostream & operator<< (std::ostream & out, const BinaryOperatorExpr & o) {
+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) {
+std::ostream & operator<< (std::ostream & out, const TernaryOperatorExpr & o)
+{
out << *o.lexpr << " ? " << *o.rexprTrue << " : " << *o.rexprFalse;
return out;
}
-std::ostream & operator<< (std::ostream & out, const Expr & e) {
+std::ostream & operator<< (std::ostream & out, const Expr & e)
+{
if (e.parenthesized)
out << "(";
- switch (e.type) {
+ switch (e.type)
+ {
case ExprType::Func:
out << e._func.functionName << "(" << e._func.arguments << ")"; break;
case ExprType::Lit:
@@ -140,13 +158,16 @@ std::ostream & operator<< (std::ostream & out, const Expr & e) { return out;
}
-std::ostream & operator<< (std::ostream & out, const Stmt & s) {
- switch (s.type) {
+std::ostream & operator<< (std::ostream & out, const Stmt & s)
+{
+ switch (s.type)
+ {
case StmtType::If:
out << "if (" << s._if.condition << ")\n" << s._if.body; break;
case StmtType::Switch:
out << "switch (" << s._switch.ident.name << ")\n{\n";
- for (auto c : s._switch.cases) {
+ for (auto c : s._switch.cases)
+ {
indent(out, 2);
out << "case " << *c.expr << ": " << c.body << "break;";
}
@@ -173,22 +194,31 @@ std::ostream & operator<< (std::ostream & out, const Stmt & s) { }
-void tocFunction (std::ostream & out, const Function & f, bool stub) {
+void tocFunction (std::ostream & out, const Function & f, bool stub)
+{
out << f.returnType << " " << f.name << " (" << f.parameters << ")";
- if (stub) {
+ if (stub)
+ {
out << ";\n";
}
- else {
+ else
+ {
out << "\n" << f.body;
}
}
-void tocStruct (std::ostream & out, const Struct & s, bool stub) {
+void tocStruct (std::ostream & out, const Struct & s, bool stub)
+{
out << "struct " << s.name;
- if (stub) {
+ if (stub)
+ {
out << ";\n";
- for (auto m : s.methods) {
- m.parameters.insert(m.parameters.begin(), {"this", {s.name, {{TypeModifierType::Pointer, false, -1}}}});
+ for (auto m : s.methods)
+ {
+ m.parameters.insert(m.parameters.begin(),
+ {"this",
+ {s.name,
+ {{TypeModifierType::Pointer, false, -1}}}});
out << m.returnType << " " <<
s.name << "_" << m.name <<
" (" << m.parameters << ");\n";
@@ -198,7 +228,8 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) { out << "\n{\n";
indentation += 2;
- for (auto m : s.members) {
+ for (auto m : s.members)
+ {
indent(out);
out << m << ";\n";
}
@@ -206,26 +237,36 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub) { indent(out, -2);
out << "};\n";
- for (auto m : s.methods) {
- m.parameters.insert(m.parameters.begin(), {"this", {s.name, {{TypeModifierType::Pointer, false, -1}}}});
+ for (auto m : s.methods)
+ {
+ m.parameters.insert(m.parameters.begin(),
+ {"this",
+ {s.name,
+ {{TypeModifierType::Pointer, false, -1}}}});
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) {
+void tocProgram (std::ostream & out, const Program & p)
+{
+ for (auto s : p.structs)
+ {
tocStruct(out, s, true);
}
- for (auto f : p.functions) {
+ for (auto f : p.functions)
+ {
tocFunction(out, f, true);
}
- for (auto v : p.variables) {
+ for (auto v : p.variables)
+ {
out << v << ";\n";
}
- for (auto s : p.structs) {
+ for (auto s : p.structs)
+ {
tocStruct(out, s, false);
}
- for (auto f : p.functions) {
+ for (auto f : p.functions)
+ {
tocFunction(out, f, false);
}
}
|
