abouttreesummaryrefslogcommitdiff
path: root/src/toc.h
diff options
context:
space:
mode:
authorPatrick Schönberger2021-08-04 14:51:23 +0200
committerPatrick Schönberger2021-08-04 14:51:23 +0200
commitdbc4a22d3c8c4189459f0361cb9da06415ec2dc9 (patch)
tree7eabe3910c0a6d56b3d7c2b2a082e95a61a71c58 /src/toc.h
parent71a20a4f3d4e5f5278f7d004af710af89dfd7ebc (diff)
downloadtoc-dbc4a22d3c8c4189459f0361cb9da06415ec2dc9.tar.gz
toc-dbc4a22d3c8c4189459f0361cb9da06415ec2dc9.zip
pre change
Diffstat (limited to 'src/toc.h')
-rw-r--r--src/toc.h190
1 files changed, 121 insertions, 69 deletions
diff --git a/src/toc.h b/src/toc.h
index e4f36e5..95c24d8 100644
--- a/src/toc.h
+++ b/src/toc.h
@@ -4,32 +4,36 @@
#include <sstream>
#include "repr.h"
+#include "typeInfo.h"
template<typename T>
-std::ostream & operator<< (std::ostream & out, const std::vector<T> & v)
+std::string vectorStr (const std::vector<T> & v, const std::string & separator, bool end = false)
{
- bool comma = false;
+ std::stringstream sstr;
+
+ bool putSeparator = false;
for (auto t : v)
{
- if (comma) out << ", ";
- else comma = true;
- out << t;
+ if (putSeparator) sstr << separator;
+ else putSeparator = true;
+ sstr << t;
}
- return out;
+ if (end && !v.empty())
+ sstr << separator;
+
+ return sstr.str();
}
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 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);
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);
+void tocNamespace (std::ostream & out, const Namespace & n, bool stub);
static const int TAB_WIDTH = 2;
static int indentation = 0;
@@ -39,9 +43,21 @@ static void indent(std::ostream & out, int change = 0)
out << std::string(indentation, ' ');
}
+static std::vector<std::string> namespaces;
+static std::string namespacePrefix() {
+ std::stringstream sstr;
+ for (auto n : namespaces)
+ {
+ sstr << n << "_";
+ }
+ return sstr.str();
+}
+
+static Program globalPrg;
+
std::ostream & operator<< (std::ostream & out, const Type & t)
{
- out << t.name;
+ out << vectorStr(t.namespacePrefixes, "_", true) << t.name;
return out;
}
@@ -99,62 +115,46 @@ 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)
- {
- out << UnaryOperatorTypeStrings[(int)o.type] << *o.expr;
- }
- else
- {
- out << *o.expr << UnaryOperatorTypeStrings[(int)o.type];
- }
-
- 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;
-}
std::ostream & operator<< (std::ostream & out, const Expr & e)
{
- if (e.parenthesized)
- out << "(";
-
switch (e.type)
{
case ExprType::Func:
- out << e._func.functionName << "(" << e._func.arguments << ")"; break;
+ out << vectorStr(e._func.namespacePrefixes, "_", true) << e._func.functionName << "(" << vectorStr(e._func.arguments, ", ") << ")"; break;
+ case ExprType::Method:
+ {
+ TypeInfo ti = typeExpr(globalPrg, namespaces, *e._method.expr);
+ out <<
+ vectorStr(ti.type.namespacePrefixes, "_", true) <<
+ ti.type.name << "_" << e._method.methodName << "(" << *e._method.expr << vectorStr(e._method.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::Paren:
+ out << "(" << e._paren.expr << ")"; break;
case ExprType::Dot:
- 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;
+ out << *e._dot.expr << "." << e._dot.identifier; break;
+ case ExprType::PrefixOp:
+ out << PrefixOperatorTypeStrings[(int)e._prefixOp.type] << *e._prefixOp.expr; break;
+ case ExprType::PostfixOp:
+ out << *e._postfixOp.expr << PostfixOperatorTypeStrings[(int)e._postfixOp.type]; break;
+ case ExprType::BinaryOp:
+ out << *e._binaryOp.lexpr <<
+ " " << BinaryOperatorTypeStrings[(int)e._binaryOp.type] << " " <<
+ *e._binaryOp.rexpr; break;
+ case ExprType::TernaryOp:
+ out << *e._ternaryOp.lexpr <<
+ " ? " << *e._ternaryOp.rexprTrue <<
+ " : " << *e._ternaryOp.rexprFalse; break;
+ case ExprType::Bracket:
+ out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;
+ case ExprType::Identifier:
+ out << vectorStr(e._identifier.namespacePrefixes, "_", true) << e._identifier.identifier; break;
}
-
- if (e.parenthesized)
- out << ")";
return out;
}
@@ -165,7 +165,7 @@ std::ostream & operator<< (std::ostream & out, const Stmt & s)
case StmtType::If:
out << "if (" << s._if.condition << ")\n" << s._if.body; break;
case StmtType::Switch:
- out << "switch (" << s._switch.ident.name << ")\n{\n";
+ out << "switch (" << s._switch.ident << ")\n{\n";
for (auto c : s._switch.cases)
{
indent(out, 2);
@@ -176,14 +176,14 @@ std::ostream & operator<< (std::ostream & out, const Stmt & s)
break;
case StmtType::For:
out << "for (" <<
- s._for.varName << " = " << *s._for.initValue << "; " <<
+ s._for.init << "; " <<
*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;
+ out << s._assign.lexpr << " = " << s._assign.rexpr << ";"; break;
case StmtType::Return:
out << "return " << s._return.expr << ";"; break;
case StmtType::Expr:
@@ -196,7 +196,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 << " (" << f.parameters << ")";
+ out << f.returnType << " " << namespacePrefix() << f.name << " (" << vectorStr(f.parameters, ", ") << ")";
if (stub)
{
@@ -209,19 +209,27 @@ void tocFunction (std::ostream & out, const Function & f, bool stub)
}
void tocStruct (std::ostream & out, const Struct & s, bool stub)
{
- out << "struct " << s.name;
+ out << "struct " << namespacePrefix() << s.name;
if (stub)
{
out << ";\n";
for (auto m : s.methods)
{
- m.parameters.insert(m.parameters.begin(),
+ Function f = m;
+
+ f.parameters.insert(f.parameters.begin(),
{"this",
- {s.name,
- {{TypeModifierType::Pointer, false, -1}}}});
- out << m.returnType << " " <<
- s.name << "_" << m.name <<
- " (" << m.parameters << ");\n";
+ {
+ namespaces,
+ s.name,
+ {
+ {TypeModifierType::Pointer, false, -1}
+ }
+ }
+ });
+ out << f.returnType << " " <<
+ namespacePrefix() << s.name << "_" << f.name <<
+ " (" << vectorStr(f.parameters, ", ") << ");\n";
}
return;
}
@@ -239,15 +247,29 @@ void tocStruct (std::ostream & out, const Struct & s, bool stub)
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;
+ Function f = m;
+ f.parameters.insert(f.parameters.begin(),
+ {"this",
+ {
+ namespaces,
+ s.name,
+ {
+ {TypeModifierType::Pointer, false, -1}
+ }
+ }
+ });
+ out << f.returnType << " " <<
+ namespacePrefix() << s.name << "_" << f.name <<
+ " (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;
}
}
void tocProgram (std::ostream & out, const Program & p)
{
+ globalPrg = p;
+ for (auto n : p.namespaces)
+ {
+ tocNamespace(out, n, true);
+ }
for (auto s : p.structs)
{
tocStruct(out, s, true);
@@ -261,6 +283,10 @@ void tocProgram (std::ostream & out, const Program & p)
{
out << v << ";\n";
}
+ for (auto n : p.namespaces)
+ {
+ tocNamespace(out, n, false);
+ }
for (auto s : p.structs)
{
tocStruct(out, s, false);
@@ -270,3 +296,29 @@ void tocProgram (std::ostream & out, const Program & p)
tocFunction(out, f, false);
}
}
+
+
+void tocNamespace (std::ostream & out, const Namespace & n, bool stub)
+{
+ namespaces.push_back(n.name);
+ if (!stub)
+ {
+ for (auto v : n.variables)
+ {
+ out << v << ";\n";
+ }
+ }
+ for (auto n : n.namespaces)
+ {
+ tocNamespace(out, n, stub);
+ }
+ for (auto s : n.structs)
+ {
+ tocStruct(out, s, stub);
+ }
+ for (auto f : n.functions)
+ {
+ tocFunction(out, f, stub);
+ }
+ namespaces.pop_back();
+} \ No newline at end of file