diff options
| author | Patrick Schönberger | 2021-07-29 09:58:14 +0200 |
|---|---|---|
| committer | Patrick Schönberger | 2021-07-29 09:58:14 +0200 |
| commit | b64d16088b29615d222d33450cf0315467400e59 (patch) | |
| tree | 7fe3a5bdbe33fe286ad25282ce955bd906097755 /src | |
| parent | 45409c781a9e35df68c43b1e2f028d30bf90c0a0 (diff) | |
| download | toc-b64d16088b29615d222d33450cf0315467400e59.tar.gz toc-b64d16088b29615d222d33450cf0315467400e59.zip | |
toc now uses internal representation instead of ast
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 12 | ||||
| -rw-r--r-- | src/repr.h | 22 | ||||
| -rw-r--r-- | src/repr_get.h | 91 | ||||
| -rw-r--r-- | src/toc.h | 646 |
4 files changed, 485 insertions, 286 deletions
diff --git a/src/main.cpp b/src/main.cpp index a7742ac..7d8d5c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,12 +32,6 @@ int main(int argc, const char * argv[]) { //std::cout << "Parse Tree: " << s << std::endl;
- //toc(std::cout, prog);
-
- //std::ofstream ofs("output.c");
- //toc(ofs, prog);
- //ofs.close();
-
Program prg = getProgram(prog);
std::cout << "Variables:\n";
for (auto v : prg.variables)
@@ -46,5 +40,11 @@ int main(int argc, const char * argv[]) { for (auto f : prg.functions)
std::cout << " " << f.name << endl;
+ tocProgram(std::cout, prg);
+
+ //std::ofstream ofs("output.c");
+ //tocProg(ofs, prg);
+ //ofs.close();
+
return 0;
}
\ No newline at end of file @@ -43,6 +43,7 @@ struct Body { };
struct Function {
+ Type returnType;
std::string name;
std::vector<Variable> parameters;
Body body;
@@ -65,7 +66,7 @@ enum class ExprType { };
struct CallExpr {
- Function function;
+ std::string functionName;
std::vector<Expr> arguments;
};
@@ -78,11 +79,8 @@ struct VariableExpr { };
struct BracketsExpr {
- BracketsExpr() {}
- BracketsExpr(const BracketsExpr &) {}
- BracketsExpr & operator=(const BracketsExpr &) {return *this;};
- std::unique_ptr<Expr> lexpr;
- std::unique_ptr<Expr> rexpr;
+ std::shared_ptr<Expr> lexpr;
+ std::shared_ptr<Expr> rexpr;
};
enum class OperatorType {
@@ -92,19 +90,13 @@ enum class OperatorType { };
struct OperatorExpr {
- OperatorExpr() {}
- OperatorExpr(const OperatorExpr &) {}
- OperatorExpr & operator=(const OperatorExpr &) {return *this;};
- std::unique_ptr<Expr> lexpr;
- std::unique_ptr<Expr> rexpr;
+ std::shared_ptr<Expr> lexpr;
+ std::shared_ptr<Expr> rexpr;
OperatorType type;
};
struct DotExpr {
- DotExpr() {}
- DotExpr(const DotExpr &) {}
- DotExpr & operator=(const DotExpr &) {return *this;};
- std::unique_ptr<Expr> lexpr;
+ std::shared_ptr<Expr> lexpr;
std::string name;
};
diff --git a/src/repr_get.h b/src/repr_get.h index 67ff40f..7dbeea3 100644 --- a/src/repr_get.h +++ b/src/repr_get.h @@ -2,15 +2,17 @@ #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);
+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);
+Expr getExpr(TocParser::NonOpExprContext * ctx);
+Expr getExpr(TocParser::NonSubscriptExprContext * ctx);
+Expr getExpr(TocParser::ExprContext * ctx);
+Stmt getStmt(TocParser::StmtContext * ctx);
Type getType(TocParser::TypeContext * ctx) {
Type result;
@@ -38,10 +40,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;
@@ -76,8 +78,9 @@ Program getProgram(TocParser::ProgContext * ctx) { }
OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx) {
OperatorExpr result;
- //result.lexpr = getExpr(ctx->binaryOperator()->nonOpExpr(0));
- //result.rexpr = getExpr(ctx->binaryOperator()->nonOpExpr(1));
+ result.lexpr = std::make_unique<Expr>(getExpr(ctx->binaryOperator()->nonOpExpr(0)));
+ result.rexpr = std::make_unique<Expr>(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;
@@ -89,13 +92,66 @@ OperatorExpr getOperatorExpr(TocParser::OperatorExprContext * ctx) { if (op == ">") result.type = OperatorType::GreaterThan;
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();
+ }
+ return result;
+}
+Expr getExpr(TocParser::NonSubscriptExprContext * 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();
+ }
+ 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();
+ result._call.functionName = ctx->funcCall()->funcName()->NAME()->toString();
}
if (ctx->literal() != nullptr) {
result.type = ExprType::Literal;
@@ -107,12 +163,13 @@ Expr getExpr(TocParser::ExprContext * ctx) { }
if (ctx->subscript() != nullptr) {
result.type = ExprType::Brackets;
- //result._brackets.lexpr = getExpr(ctx->subscript()->nonSubscriptExpr());
+ 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;
- //result._dot.lexpr = ctx->memberAccess()->identifier(0);
+ 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) {
@@ -2,273 +2,423 @@ #include <iostream>
-#include "TocParser.h"
-
-void toc(std::ostream & o, TocParser::ProgContext * ctx);
-void toc(std::ostream & o, TocParser::VarDeclContext * ctx);
-void toc(std::ostream & o, TocParser::FuncContext * ctx);
-void toc(std::ostream & o, TocParser::StructDeclContext * ctx);
-void toc(std::ostream & o, TocParser::BodyContext * ctx);
-void toc(std::ostream & o, TocParser::StmtContext * ctx);
-void toc(std::ostream & o, TocParser::IfCondContext * ctx);
-void toc(std::ostream & o, TocParser::WhileLoopContext * ctx);
-void toc(std::ostream & o, TocParser::AssignmentContext * ctx);
-void toc(std::ostream & o, TocParser::ReturnStmtContext * ctx);
-void toc(std::ostream & o, TocParser::ExprContext * ctx);
-void toc(std::ostream & o, TocParser::NonOpExprContext * ctx);
-void toc(std::ostream & o, TocParser::NonSubscriptExprContext * ctx);
-void toc(std::ostream & o, TocParser::FuncCallContext * ctx);
-void toc(std::ostream & o, TocParser::IdentifierContext * ctx);
-void toc(std::ostream & o, TocParser::LiteralContext * ctx);
-void toc(std::ostream & o, TocParser::SubscriptContext * ctx);
-void toc(std::ostream & o, TocParser::MemberAccessContext * ctx);
-void toc(std::ostream & o, TocParser::ParenExprContext * ctx);
-void toc(std::ostream & o, TocParser::BinaryOperatorContext * ctx);
-
-void toc_stub(std::ostream & o, TocParser::FuncContext * ctx);
-void toc_stub(std::ostream & o, TocParser::StructDeclContext * ctx);
-
-
-void toc(std::ostream & o, TocParser::ProgContext * ctx) {
- for (auto * decl : ctx->decl()) {
- /**/ if (decl->structDecl() != nullptr) toc_stub(o, decl->structDecl());
- else if (decl->funcDecl() != nullptr) toc_stub(o, decl->funcDecl()->func());
- }
- for (auto * decl : ctx->decl()) {
- if (decl->varDecl() != nullptr) {
- toc(o, decl->varDecl());
- o << ";\n";
- }
- else if (decl->structDecl() != nullptr) toc(o, decl->structDecl());
- else if (decl->funcDecl() != nullptr) toc(o, decl->funcDecl()->func());
+#include "repr.h"
+
+template<typename T>
+std::ostream & operator<< (std::ostream & out, const std::vector<T> & v) {
+ bool comma = false;
+ for (auto t : v) {
+ if (comma) out << ", ";
+ else comma = true;
+ out << t;
}
+ return out;
}
-void toc(std::ostream & o, TocParser::VarDeclContext * ctx) {
- o
- << ctx->var()->type()->getText()
- << " "
- << ctx->var()->varName()->getText();
-
- if (ctx->var()->expr() != nullptr) {
- o << " = ";
- toc(o, ctx->var()->expr());
- }
+
+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 Expr & e);
+std::ostream & operator<< (std::ostream & out, const Stmt & s);
+
+void tocFunction (std::ostream & out, const Function & f, bool stub);
+void tocStruct (std::ostream & out, const Struct & s, bool stub);
+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) {
+ indentation += change;
+ out << std::string(indentation, ' ');
}
-void toc(std::ostream & o, TocParser::FuncContext * ctx) {
- o
- << ctx->type()->getText()
- << " "
- << ctx->funcName()->getText()
- << "(";
-
- if (ctx->parameter()->firstParameter() != nullptr) {
- o
- << ctx->parameter()->firstParameter()->var()->type()->getText()
- << " "
- << ctx->parameter()->firstParameter()->var()->varName()->getText();
-
- for (auto * par : ctx->parameter()->additionalParameter()) {
- o
- << ", "
- << par->var()->type()->getText()
- << " "
- << par->var()->varName()->getText();
- }
- }
- o << ")\n{\n";
+std::ostream & operator<< (std::ostream & out, const Type & t) {
+ out << t.name;
- toc(o, ctx->body());
+ return out;
+}
+std::ostream & operator<< (std::ostream & out, const Variable & v) {
+ out << v.type << " " << v.name;
- o << "}\n";
+ return out;
}
-void toc(std::ostream & o, TocParser::StructDeclContext * ctx) {
- o
- << "typedef struct "
- << ctx->structName()->getText()
- << "\n{\n";
-
- for (auto * member : ctx->structMember()) {
- if (member->structVar() != nullptr) {
- o
- << member->structVar()->var()->type()->getText()
- << " "
- << member->structVar()->var()->varName()->getText()
- << ";\n";
- }
+std::ostream & operator<< (std::ostream & out, const Body & b) {
+ indent(out);
+ out << "{\n";
+ indentation += 2;
+
+ for (auto v : b.variables) {
+ indent(out);
+ out << v << ";\n";
}
- o << "} "
- << ctx->structName()->getText()
- << ";\n";
- for (auto * member : ctx->structMember()) {
- if (member->structMethod() != nullptr) {
- o
- << member->structMethod()->func()->type()->getText()
- << " "
- << ctx->structName()->getText()
- << "_"
- << member->structMethod()->func()->funcName()->getText()
- << "("
- << ctx->structName()->getText()
- << " * this";
-
- if (member->structMethod()->func()->parameter()->firstParameter() != nullptr) {
- o
- << ", "
- << member->structMethod()->func()->parameter()->firstParameter()->var()->type()->getText()
- << " "
- << member->structMethod()->func()->parameter()->firstParameter()->var()->varName()->getText();
-
- for (auto * par : member->structMethod()->func()->parameter()->additionalParameter()) {
- o
- << ", "
- << par->var()->type()->getText()
- << " "
- << par->var()->varName()->getText();
- }
- }
-
- o << ")\n{\n";
-
- toc(o, member->structMethod()->func()->body());
-
- o << "}\n";
- }
+
+ out << "\n";
+
+ for (auto s : b.statements) {
+ indent(out);
+ out << s << ";\n";
}
+
+ indent(out, -2);
+ out << "}\n";
+
+ return out;
}
-void toc(std::ostream & o, TocParser::BodyContext * ctx) {
- for (auto * stmt : ctx->stmt()) {
- toc(o, stmt);
- o << "\n";
+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;
}
+
+ out << " " << *o.rexpr;
+
+ return out;
}
-void toc(std::ostream & o, TocParser::StmtContext * ctx) {
- /**/ if (ctx->varDecl() != nullptr) toc(o, ctx->varDecl());
- else if (ctx->conditional() != nullptr) toc(o, ctx->conditional()->ifCond());
- else if (ctx->loop() != nullptr) toc(o, ctx->loop()->whileLoop());
- else if (ctx->assignment() != nullptr) toc(o, ctx->assignment());
- else if (ctx->returnStmt() != nullptr) toc(o, ctx->returnStmt());
- else if (ctx->expr() != nullptr) toc(o, ctx->expr());
-
- if (ctx->conditional() == nullptr && ctx->loop() == nullptr)
- o << ";";
-}
-void toc(std::ostream & o, TocParser::IfCondContext * ctx) {
- o << "if (";
- toc(o, ctx->expr());
- o << ")\n{\n";
- toc(o, ctx->body());
- o << "}\n";
-}
-void toc(std::ostream & o, TocParser::WhileLoopContext * ctx) {
- o << "while (";
- toc(o, ctx->expr());
- o << ")\n{\n";
- toc(o, ctx->body());
- o << "}\n";
-}
-void toc(std::ostream & o, TocParser::AssignmentContext * ctx) {
- toc(o, ctx->identifier());
- o << " = ";
- toc(o, ctx->expr());
-}
-void toc(std::ostream & o, TocParser::ReturnStmtContext * ctx) {
- o << "return ";
- toc(o, ctx->expr());
-}
-void toc(std::ostream & o, TocParser::ExprContext * ctx) {
- /**/ if (ctx->funcCall() != nullptr) toc(o, ctx->funcCall());
- else if (ctx->identifier() != nullptr) toc(o, ctx->identifier());
- else if (ctx->literal() != nullptr) toc(o, ctx->literal());
- else if (ctx->subscript() != nullptr) toc(o, ctx->subscript());
- else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());
- else if (ctx->parenExpr() != nullptr) toc(o, ctx->parenExpr());
- else if (ctx->operatorExpr() != nullptr) toc(o, ctx->operatorExpr()->binaryOperator());
-}
-void toc(std::ostream & o, TocParser::NonOpExprContext * ctx) {
- /**/ if (ctx->funcCall() != nullptr) toc(o, ctx->funcCall());
- else if (ctx->identifier() != nullptr) toc(o, ctx->identifier());
- else if (ctx->literal() != nullptr) toc(o, ctx->literal());
- else if (ctx->subscript() != nullptr) toc(o, ctx->subscript());
- else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());
- else if (ctx->parenExpr() != nullptr) toc(o, ctx->parenExpr());
-}
-void toc(std::ostream & o, TocParser::NonSubscriptExprContext * ctx) {
- /**/ if (ctx->funcCall() != nullptr) toc(o, ctx->funcCall());
- else if (ctx->identifier() != nullptr) toc(o, ctx->identifier());
- else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());
- else if (ctx->parenExpr() != nullptr) toc(o, ctx->parenExpr());
-}
-void toc(std::ostream & o, TocParser::FuncCallContext * ctx) {
- o
- << ctx->funcName()->getText()
- << "(";
- for (int i = 0; i < ctx->expr().size(); i++) {
- if (i != 0) o << ", ";
- toc(o, ctx->expr(i));
+std::ostream & operator<< (std::ostream & out, const Expr & e) {
+ switch (e.type) {
+ 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;
}
- o << ")";
-}
-void toc(std::ostream & o, TocParser::IdentifierContext * ctx) {
- o << ctx->getText();
-}
-void toc(std::ostream & o, TocParser::LiteralContext * ctx) {
- if (ctx->INTLIT() != nullptr) o << ctx->INTLIT()->getText();
-}
-void toc(std::ostream & o, TocParser::SubscriptContext * ctx) {
- toc(o, ctx->nonSubscriptExpr());
- o << "[";
- toc(o, ctx->expr());
- o << "]";
-}
-void toc(std::ostream & o, TocParser::MemberAccessContext * ctx) {
- toc(o, ctx->identifier(0));
- o << ".";
- toc(o, ctx->identifier(1));
+
+ return out;
}
-void toc(std::ostream & o, TocParser::ParenExprContext * ctx) {
- o << "(";
- toc(o, ctx->expr());
- o << ")";
+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::While:
+ out << "while (" << s._while.condition << ")\n" << s._while.body; break;
+ }
+
+ return out;
}
-void toc(std::ostream & o, TocParser::BinaryOperatorContext * ctx) {
- for (int i = 0; i < ctx->BINARY_OPERATOR().size(); i++) {
- toc(o, ctx->nonOpExpr(i));
- o
- << " "
- << ctx->BINARY_OPERATOR(i)->getText()
- << " ";
- toc(o, ctx->nonOpExpr(i + 1));
+
+
+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 << ")";
+
+ if (stub) {
+ out << ";\n";
+ }
+ else {
+ out << "\n" << f.body;
}
}
+void tocStruct (std::ostream & out, const Struct & s, bool stub) {
+ out << "struct " << s.name;
+ if (stub) {
+ out << ";\n";
+ return;
+ }
+ out << "\n{\n";
+ indentation += 2;
-void toc_stub(std::ostream & o, TocParser::FuncContext * ctx) {
- o
- << ctx->type()->getText()
- << " "
- << ctx->funcName()->getText()
- << "(";
-
- if (ctx->parameter()->firstParameter() != nullptr) {
- o
- << ctx->parameter()->firstParameter()->var()->type()->getText()
- << " "
- << ctx->parameter()->firstParameter()->var()->varName()->getText();
-
- for (auto * par : ctx->parameter()->additionalParameter()) {
- o
- << ", "
- << par->var()->type()->getText()
- << " "
- << par->var()->varName()->getText();
- }
+ for (auto m : s.members) {
+ indent(out);
+ out << m << ";\n";
}
- o << ");\n";
+ indent(out, -2);
+ out << "};\n";
}
-void toc_stub(std::ostream & o, TocParser::StructDeclContext * ctx) {
- o
- << "struct "
- << ctx->structName()->getText()
- << ";\n";
+void tocProgram (std::ostream & out, const Program & p) {
+ for (auto s : p.structs) {
+ tocStruct(out, s, true);
+ }
+ for (auto s : p.structs) {
+ tocStruct(out, s, false);
+ }
+
+ for (auto v : p.variables) {
+ out << v << ";\n";
+ }
+
+ for (auto f : p.functions) {
+ tocFunction(out, f, true);
+ }
+ for (auto f : p.functions) {
+ tocFunction(out, f, false);
+ }
}
+
+
+
+
+// void toc(std::ostream & o, TocParser::ProgContext * ctx) {
+// for (auto * decl : ctx->decl()) {
+// /**/ if (decl->structDecl() != nullptr) toc_stub(o, decl->structDecl());
+// else if (decl->funcDecl() != nullptr) toc_stub(o, decl->funcDecl()->func());
+// }
+// for (auto * decl : ctx->decl()) {
+// if (decl->varDecl() != nullptr) {
+// toc(o, decl->varDecl());
+// out << ";\n";
+// }
+// else if (decl->structDecl() != nullptr) toc(o, decl->structDecl());
+// else if (decl->funcDecl() != nullptr) toc(o, decl->funcDecl()->func());
+// }
+// }
+// void toc(std::ostream & o, TocParser::VarDeclContext * ctx) {
+// o
+// << ctx->var()->type()->getText()
+// << " "
+// << ctx->var()->varName()->getText();
+
+// if (ctx->var()->expr() != nullptr) {
+// out << " = ";
+// toc(o, ctx->var()->expr());
+// }
+// }
+// void toc(std::ostream & o, TocParser::FuncContext * ctx) {
+// o
+// << ctx->type()->getText()
+// << " "
+// << ctx->funcName()->getText()
+// << "(";
+
+// if (ctx->parameter()->firstParameter() != nullptr) {
+// o
+// << ctx->parameter()->firstParameter()->var()->type()->getText()
+// << " "
+// << ctx->parameter()->firstParameter()->var()->varName()->getText();
+
+// for (auto * par : ctx->parameter()->additionalParameter()) {
+// o
+// << ", "
+// << par->var()->type()->getText()
+// << " "
+// << par->var()->varName()->getText();
+// }
+// }
+
+// out << ")\n{\n";
+
+// toc(o, ctx->body());
+
+// out << "}\n";
+// }
+// void toc(std::ostream & o, TocParser::StructDeclContext * ctx) {
+// o
+// << "typedef struct "
+// << ctx->structName()->getText()
+// << "\n{\n";
+
+// for (auto * member : ctx->structMember()) {
+// if (member->structVar() != nullptr) {
+// o
+// << member->structVar()->var()->type()->getText()
+// << " "
+// << member->structVar()->var()->varName()->getText()
+// << ";\n";
+// }
+// }
+// out << "} "
+// << ctx->structName()->getText()
+// << ";\n";
+// for (auto * member : ctx->structMember()) {
+// if (member->structMethod() != nullptr) {
+// o
+// << member->structMethod()->func()->type()->getText()
+// << " "
+// << ctx->structName()->getText()
+// << "_"
+// << member->structMethod()->func()->funcName()->getText()
+// << "("
+// << ctx->structName()->getText()
+// << " * this";
+
+// if (member->structMethod()->func()->parameter()->firstParameter() != nullptr) {
+// o
+// << ", "
+// << member->structMethod()->func()->parameter()->firstParameter()->var()->type()->getText()
+// << " "
+// << member->structMethod()->func()->parameter()->firstParameter()->var()->varName()->getText();
+
+// for (auto * par : member->structMethod()->func()->parameter()->additionalParameter()) {
+// o
+// << ", "
+// << par->var()->type()->getText()
+// << " "
+// << par->var()->varName()->getText();
+// }
+// }
+
+// out << ")\n{\n";
+
+// toc(o, member->structMethod()->func()->body());
+
+// out << "}\n";
+// }
+// }
+// }
+// void toc(std::ostream & o, TocParser::BodyContext * ctx) {
+// for (auto * stmt : ctx->stmt()) {
+// toc(o, stmt);
+// out << "\n";
+// }
+// }
+// void toc(std::ostream & o, TocParser::StmtContext * ctx) {
+// /**/ if (ctx->varDecl() != nullptr) toc(o, ctx->varDecl());
+// else if (ctx->conditional() != nullptr) toc(o, ctx->conditional()->ifCond());
+// else if (ctx->loop() != nullptr) toc(o, ctx->loop()->whileLoop());
+// else if (ctx->assignment() != nullptr) toc(o, ctx->assignment());
+// else if (ctx->returnStmt() != nullptr) toc(o, ctx->returnStmt());
+// else if (ctx->expr() != nullptr) toc(o, ctx->expr());
+
+// if (ctx->conditional() == nullptr && ctx->loop() == nullptr)
+// out << ";";
+// }
+// void toc(std::ostream & o, TocParser::IfCondContext * ctx) {
+// out << "if (";
+// toc(o, ctx->expr());
+// out << ")\n{\n";
+// toc(o, ctx->body());
+// out << "}\n";
+// }
+// void toc(std::ostream & o, TocParser::WhileLoopContext * ctx) {
+// out << "while (";
+// toc(o, ctx->expr());
+// out << ")\n{\n";
+// toc(o, ctx->body());
+// out << "}\n";
+// }
+// void toc(std::ostream & o, TocParser::AssignmentContext * ctx) {
+// toc(o, ctx->identifier());
+// out << " = ";
+// toc(o, ctx->expr());
+// }
+// void toc(std::ostream & o, TocParser::ReturnStmtContext * ctx) {
+// out << "return ";
+// toc(o, ctx->expr());
+// }
+// void toc(std::ostream & o, TocParser::ExprContext * ctx) {
+// /**/ if (ctx->funcCall() != nullptr) toc(o, ctx->funcCall());
+// else if (ctx->identifier() != nullptr) toc(o, ctx->identifier());
+// else if (ctx->literal() != nullptr) toc(o, ctx->literal());
+// else if (ctx->subscript() != nullptr) toc(o, ctx->subscript());
+// else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());
+// else if (ctx->parenExpr() != nullptr) toc(o, ctx->parenExpr());
+// else if (ctx->operatorExpr() != nullptr) toc(o, ctx->operatorExpr()->binaryOperator());
+// }
+// void toc(std::ostream & o, TocParser::NonOpExprContext * ctx) {
+// /**/ if (ctx->funcCall() != nullptr) toc(o, ctx->funcCall());
+// else if (ctx->identifier() != nullptr) toc(o, ctx->identifier());
+// else if (ctx->literal() != nullptr) toc(o, ctx->literal());
+// else if (ctx->subscript() != nullptr) toc(o, ctx->subscript());
+// else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());
+// else if (ctx->parenExpr() != nullptr) toc(o, ctx->parenExpr());
+// }
+// void toc(std::ostream & o, TocParser::NonSubscriptExprContext * ctx) {
+// /**/ if (ctx->funcCall() != nullptr) toc(o, ctx->funcCall());
+// else if (ctx->identifier() != nullptr) toc(o, ctx->identifier());
+// else if (ctx->memberAccess() != nullptr) toc(o, ctx->memberAccess());
+// else if (ctx->parenExpr() != nullptr) toc(o, ctx->parenExpr());
+// }
+// void toc(std::ostream & o, TocParser::FuncCallContext * ctx) {
+// o
+// << ctx->funcName()->getText()
+// << "(";
+// for (int i = 0; i < ctx->expr().size(); i++) {
+// if (i != 0) out << ", ";
+// toc(o, ctx->expr(i));
+// }
+// out << ")";
+// }
+// void toc(std::ostream & o, TocParser::IdentifierContext * ctx) {
+// out << ctx->getText();
+// }
+// void toc(std::ostream & o, TocParser::LiteralContext * ctx) {
+// if (ctx->INTLIT() != nullptr) out << ctx->INTLIT()->getText();
+// }
+// void toc(std::ostream & o, TocParser::SubscriptContext * ctx) {
+// toc(o, ctx->nonSubscriptExpr());
+// out << "[";
+// toc(o, ctx->expr());
+// out << "]";
+// }
+// void toc(std::ostream & o, TocParser::MemberAccessContext * ctx) {
+// toc(o, ctx->identifier(0));
+// out << ".";
+// toc(o, ctx->identifier(1));
+// }
+// void toc(std::ostream & o, TocParser::ParenExprContext * ctx) {
+// out << "(";
+// toc(o, ctx->expr());
+// out << ")";
+// }
+// void toc(std::ostream & o, TocParser::BinaryOperatorContext * ctx) {
+// for (int i = 0; i < ctx->BINARY_OPERATOR().size(); i++) {
+// toc(o, ctx->nonOpExpr(i));
+// o
+// << " "
+// << ctx->BINARY_OPERATOR(i)->getText()
+// << " ";
+// toc(o, ctx->nonOpExpr(i + 1));
+// }
+// }
+
+// void toc_stub(std::ostream & o, TocParser::FuncContext * ctx) {
+// o
+// << ctx->type()->getText()
+// << " "
+// << ctx->funcName()->getText()
+// << "(";
+
+// if (ctx->parameter()->firstParameter() != nullptr) {
+// o
+// << ctx->parameter()->firstParameter()->var()->type()->getText()
+// << " "
+// << ctx->parameter()->firstParameter()->var()->varName()->getText();
+
+// for (auto * par : ctx->parameter()->additionalParameter()) {
+// o
+// << ", "
+// << par->var()->type()->getText()
+// << " "
+// << par->var()->varName()->getText();
+// }
+// }
+
+// out << ");\n";
+// }
+// void toc_stub(std::ostream & o, TocParser::StructDeclContext * ctx) {
+// o
+// << "struct "
+// << ctx->structName()->getText()
+// << ";\n";
+// }
|
