+++ /dev/null
-#pragma once\r
-\r
-#include "repr.h"\r
-\r
-bool checkStmt(\r
- const Stmt & s,\r
- std::vector<Namespace> namespaces,\r
- std::vector<Variable> vars)\r
-{\r
- return true;\r
-}\r
-\r
-bool checkFunction(\r
- const Function & f,\r
- std::vector<Namespace> namespaces,\r
- std::vector<Variable> vars)\r
-{\r
- vars.insert(vars.end(), f.parameters.begin(), f.parameters.end());\r
- vars.insert(vars.end(), f.body.variables.begin(), f.body.variables.end());\r
- for (auto s : f.body.statements)\r
- {\r
- if (!checkStmt(s, namespaces, vars))\r
- return false;\r
- }\r
- return true;\r
-}\r
-\r
-bool checkProgram(const Program & p)\r
-{\r
- for (auto f : p.functions)\r
- {\r
- if (!checkFunction(f, p.namespaces, p.variables))\r
- return false;\r
- }\r
- for (auto s : p.structs)\r
- {\r
- std::vector<Variable> vars = p.variables;\r
- for (auto v : s.members)\r
- vars.push_back(v);\r
- for (auto f : s.methods)\r
- {\r
- if (!checkFunction(f, p.namespaces, vars))\r
- return false;\r
- }\r
- }\r
- return true;\r
-}
\ No newline at end of file
\r
template<typename T>\r
using opt = std::optional<T>;\r
+template<typename ... Ts>\r
+using tup = std::tuple<Ts ...>;\r
\r
+// find an item in a vector by predicate\r
template<typename T>\r
opt<T> find(const std::vector<T> & ts, std::function<bool(T)> f)\r
{\r
return nullopt;\r
}\r
\r
+// same as above but return pointer into raw array held by vector\r
template<typename T>\r
opt<T *> findPtr(const std::vector<T> & ts, std::function<bool(T)> f)\r
{\r
return nullopt;\r
}\r
\r
-std::optional<\r
- std::tuple<\r
- std::shared_ptr<Context>,\r
- std::vector<std::string>>>\r
+opt<tup<\r
+ std::shared_ptr<Context>,\r
+ std::vector<std::string>>>\r
getContext(std::shared_ptr<Context> ctx, const std::vector<std::string> & namespacePrefix)\r
{\r
+ // try finding a continuos series of namespaces in a given context\r
auto result = ctx;\r
\r
for (auto name : namespacePrefix)\r
}\r
}\r
\r
+ // if the found context is the end of a series of namespaces, also return\r
+ // a vector of namespace names\r
std::vector<std::string> namespaces;\r
for (auto it = result; it != nullptr; it = it->parent)\r
{\r
return std::make_tuple(result, namespaces);\r
}\r
\r
+// all of the following functions work the same way,\r
+// walking up the context hierarchy until the global context.\r
+// return the first found instance that matches provided criteria\r
+// theres also a variant to get a pointer instead for functions and\r
+// structs used for generic instantiation\r
\r
-\r
-opt<std::tuple<Function, std::vector<std::string>>> findFunction(\r
+opt<tup<Function, std::vector<std::string>>> findFunction(\r
const std::string & name,\r
const std::vector<std::string> & namespacePrefix,\r
std::shared_ptr<Context> ctx)\r
return nullopt;\r
}\r
\r
-opt<std::tuple<Function *, std::vector<std::string>>> findFunctionPtr(\r
+opt<tup<Function *, std::vector<std::string>>> findFunctionPtr(\r
const std::string & name,\r
const std::vector<std::string> & namespacePrefix,\r
std::shared_ptr<Context> ctx)\r
\r
\r
\r
-opt<std::tuple<Struct, std::vector<std::string>>> findStruct(\r
+opt<tup<Struct, std::vector<std::string>>> findStruct(\r
const std::string & name,\r
const std::vector<std::string> & namespacePrefix,\r
std::shared_ptr<Context> ctx)\r
return nullopt;\r
}\r
\r
-opt<std::tuple<Struct *, std::vector<std::string>>> findStructPtr(\r
+opt<tup<Struct *, std::vector<std::string>>> findStructPtr(\r
const std::string & name,\r
const std::vector<std::string> & namespacePrefix,\r
std::shared_ptr<Context> ctx)\r
\r
\r
\r
-opt<std::tuple<Variable, std::vector<std::string>>> findVariable(\r
+opt<tup<Variable, std::vector<std::string>>> findVariable(\r
const std::string & name,\r
const std::vector<std::string> & namespacePrefix,\r
std::shared_ptr<Context> ctx)\r
\r
\r
\r
+// find struct members and pointer variants\r
+\r
opt<StructMember<Function>> findStructMethod(\r
const std::string & name,\r
const Struct & s)\r
#include "typeInfo.h"\r
#include "visit.h"\r
\r
+// add a generic instantiation if its not in the vector already\r
void addGenericInstantiation(\r
std::vector<std::vector<Type>> & insts,\r
const std::vector<Type> & newInst)\r
\r
// Find generic instantiations\r
\r
+ // visit expressions (only function calls are considered) and types,\r
+ // find the function/struct by pointer and add an instantiation\r
Visitor findGenericInstantiations;\r
findGenericInstantiations.onExpr =\r
[&](const Expr & e, const std::shared_ptr<Context> ctx)\r
addGenericInstantiation(std::get<0>(*f)->genericInstantiations, e._func.genericInstantiation);\r
}\r
}\r
- // TODO: generic methods\r
};\r
findGenericInstantiations.onType =\r
[&](const Type & t, const std::shared_ptr<Context> ctx)\r
return result;\r
}\r
\r
+// generate the appendix for C struct/function names\r
+// including array/pointer indicators because\r
+// there might be distinct instantiations\r
+// for int and int* for example\r
std::string genericAppendix(const std::vector<Type> & ts)\r
{\r
std::stringstream sstr;\r
{\r
std::ifstream ifs("test/test.toc");\r
\r
+ // create ANTLR input from filestream\r
ANTLRInputStream input(ifs);\r
\r
+ // lex input\r
TocLexer lexer(&input);\r
CommonTokenStream tokens(&lexer);\r
\r
+ // parse\r
TocParser parser(&tokens);\r
+\r
+ // get Prog (root node)\r
TocParser::ProgContext * prog = parser.prog();\r
- tree::ParseTree * tree = prog;\r
\r
+ // dont continue on parse error\r
if (parser.getNumberOfSyntaxErrors() > 0)\r
{\r
std::cerr << "Parsing error" << std::endl;\r
return 1;\r
}\r
\r
+ // print raw parse tree\r
+ //tree::ParseTree * tree = prog;\r
//std::string s = tree->toStringTree(&parser) + "\n";\r
//std::cout << "Parse Tree: " << s << std::endl;\r
\r
+ // generate IR from tree and instantiate generics\r
Program prg = getProgram(prog, nullptr);\r
instantiateGenerics(prg);\r
\r
+ // print to cout and file\r
try\r
{\r
tocProgram(std::cout, prg);\r
\r
using namespace std;\r
\r
+// This contains a 1 to 1 representation of the defined language\r
+\r
struct Type;\r
struct Variable;\r
struct Body;\r
struct ReturnStmt;\r
struct Stmt;\r
\r
+// Context is a collection of everything that can be defined in a namespace\r
+// that is reused for bodies so that the hierarchy can be walked uniformly\r
+// both up and down using the parent variable\r
struct Context\r
{\r
std::optional<std::string> name;\r
std::string identifier;\r
};\r
\r
+// OperatorType enum with corresponding string array to lookup\r
+// enum from string and the other way round\r
enum class PrefixOperatorType\r
{\r
Plus, Minus, Increment, Decrement,\r
\r
#include "repr.h"\r
\r
+// Transform ANTLR-generated types to corresponding IR types recursively\r
+\r
Type getType(TocParser::TypeContext * ctx);\r
Variable getVariable(TocParser::VarContext * ctx);\r
Body getBody(TocParser::BodyContext * ctx, std::shared_ptr<Context> parent);\r
\r
Stmt getStmt(TocParser::StmtContext * ctx, std::shared_ptr<Context> parent);\r
\r
+// all of these functions get the relevant information\r
+// from the parse tree and call each other for sub expressions\r
+// the getVariable is called for variable declarations and parameter definitions\r
+// for example, because they have the same rule in the grammar file\r
+\r
Type getType(TocParser::TypeContext * ctx)\r
{\r
Type result;\r
\r
\r
\r
-\r
+// Expressions are somewhat of an exception, because some of their\r
+// grammar rules are recursive, so they have to be defined\r
+// in a single rule using Labels (https://github.com/antlr/antlr4/blob/master/doc/parser-rules.md#alternative-labels)\r
+// Because this results in a polymorphic type, getExpr for the base expression type\r
+// is always called and from there the polymorphic type is determined at runtime\r
Expr getExpr(TocParser::FuncExprContext * ctx)\r
{\r
Expr result;\r
\r
\r
\r
-\r
+// this is always called for Expression rules\r
+// attempt dynamic_cast at runtime and call corresponding\r
+// function\r
Expr getExpr(TocParser::ExprContext * ctx)\r
{\r
Expr result;\r
#include "generic.h"\r
#include "typeInfo.h"\r
\r
+// print a generic vector with specified separator, optionally printing the separator at the end aswell\r
template<typename T>\r
std::string vectorStr (const std::vector<T> & v, const std::string & separator, bool end = false)\r
{\r
return sstr.str();\r
}\r
\r
+// mapping from generic typenames (which are just names)\r
+// to actual instantiated types\r
static std::map<std::string, Type> currentInstantiation;\r
\r
-static Program globalPrg;\r
+// set current context so that lookups can be made correctly\r
static std::shared_ptr<Context> globalCtx;\r
\r
\r
-\r
-// std::string getPrefix(std::shared_ptr<Context> ctx)\r
-// {\r
-// std::string result;\r
-// for (auto it = ctx; it != nullptr; it = it->parent)\r
-// {\r
-// if (it->name.has_value())\r
-// {\r
-// result = it->name.value() + "_" + result;\r
-// }\r
-// }\r
-// return result;\r
-// }\r
-\r
std::ostream & operator<< (std::ostream & out, const Type & t)\r
{\r
+ // if the typename equals one of the current generic instantiations\r
+ // print instantiated type instead\r
for (auto kv : currentInstantiation)\r
{\r
if (t.name == kv.first)\r
TypeInfo ti = typeType(globalCtx, t);\r
if (ti.isStruct)\r
out << "struct ";\r
+ // try finding type in current context\r
auto s = findStruct(t.name, t.namespacePrefixes, globalCtx);\r
+ // print prefix for either found type or the specified \r
+ // prefix if type is not found (shouldn't happen)\r
if (s.has_value())\r
out << vectorStr(std::get<1>(*s), "_", true) << t.name; \r
else\r
out << vectorStr(t.namespacePrefixes, "_", true) << t.name;\r
+\r
+ // print generic appendix\r
if (!t.genericInstantiation.empty())\r
out << genericAppendix(t.genericInstantiation);\r
\r
std::stringstream sstr;\r
std::string s = v.name;\r
\r
+ // lookup variable and change name to reflect containing namespace\r
auto var = findVariable(v.name, namespaces, globalCtx);\r
if (var.has_value())\r
s = vectorStr(std::get<1>(*var), "_", true) + s;\r
\r
+ // nest modifiers, inverted because C defines them\r
+ // the opposite direction\r
for (auto m = v.type.modifiers.rbegin(); m != v.type.modifiers.rend(); m++)\r
{\r
if (m->type == TypeModifierType::Pointer)\r
{\r
case ExprType::Func:\r
{\r
+ // print function call\r
auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx);\r
\r
if (std::get<0>(*f).defined)\r
}\r
case ExprType::Method:\r
{\r
+ // get TypeInfo on the Expression that the method is called on\r
+ // then print method call\r
TypeInfo ti = typeExpr(globalCtx, *e._method.expr);\r
out <<\r
vectorStr(ti.type.namespacePrefixes, "_", true) <<\r
case ExprType::Bracket:\r
out << *e._brackets.lexpr << "[" << *e._brackets.rexpr << "]"; break;\r
case ExprType::Identifier:\r
+ // try variable lookup\r
auto v = findVariable(e._identifier.identifier, e._identifier.namespacePrefixes, globalCtx);\r
if (v.has_value())\r
out << vectorStr(std::get<1>(*v), "_", true);\r
\r
void tocFunction (std::ostream & out, const Function & f, bool stub)\r
{\r
- if (!stub && !f.defined) return;\r
+ // for a function that is not defined, only the stub can be printed\r
+ if (!f.defined && !stub) return;\r
\r
+ // regular function\r
if (f.genericTypeNames.empty())\r
{\r
out << f.returnType << " " << namespacePrefix() << f.name << " (" << vectorStr(f.parameters, ", ") << ")";\r
out << "\n" << f.body;\r
}\r
}\r
+ // generic function\r
else\r
{\r
+ // print one instance per instantiation\r
for (auto instantiation : f.genericInstantiations)\r
{\r
+ // set global type mapping\r
for (int i = 0; i < f.genericTypeNames.size(); i++)\r
{\r
currentInstantiation[f.genericTypeNames[i]] = instantiation[i];\r
}\r
void tocStruct (std::ostream & out, const Struct & s, bool stub)\r
{\r
+ // regular struct\r
if (s.genericTypeNames.empty())\r
{\r
out << "struct " << namespacePrefix() << s.name;\r
{\r
Function f = m;\r
\r
+ // add implicit this parameter\r
f.parameters.insert(f.parameters.begin(),\r
{"this",\r
{\r
for (auto m : s.methods)\r
{\r
Function f = m;\r
+ \r
+ // add implicit this parameter\r
f.parameters.insert(f.parameters.begin(),\r
{"this",\r
{\r
" (" << vectorStr(f.parameters, ", ") << ")\n" << f.body;\r
}\r
}\r
+ // generic struct\r
else\r
{\r
for (auto instantiation : s.genericInstantiations)\r
{\r
Function f = m;\r
\r
+ // add implicit this parameter\r
f.parameters.insert(f.parameters.begin(),\r
{"this",\r
{\r
for (auto m : s.methods)\r
{\r
Function f = m;\r
+ \r
+ // add implicit this parameter\r
f.parameters.insert(f.parameters.begin(),\r
{"this",\r
{\r
{\r
globalCtx = p.ctx;\r
\r
- globalPrg = p;\r
for (auto n : p.ctx->namespaces)\r
{\r
tocNamespace(out, n, true);\r
\r
TypeInfo typeType(std::shared_ptr<Context> globalCtx, Type t)\r
{\r
+ // used to differentiate basic types from user defined types\r
TypeInfo result;\r
result.isStruct = true;\r
if (t.name == "int" || t.name == "float" || t.name == "double" ||\r
{\r
case ExprType::Func:\r
{\r
+ // get type info from return type\r
auto f = findFunction(e._func.functionName, e._func.namespacePrefixes, globalCtx);\r
if (!f.has_value())\r
throw "Unknown function";\r
}\r
case ExprType::Method:\r
{\r
+ // get type info from return type\r
TypeInfo tiCaller = typeExpr(globalCtx, *e._method.expr);\r
if (!tiCaller.isStruct)\r
throw "Calling method on non-struct";\r
break;\r
}\r
case ExprType::Lit:\r
+ // literal types are defined\r
result.isStruct = false;\r
switch (e._lit.type)\r
{\r
break;\r
case ExprType::Dot:\r
{\r
+ // assume dot access is always member access\r
+ // and lookup struct variable\r
auto tiCaller = typeExpr(globalCtx, *e._dot.expr);\r
if (!tiCaller.isStruct)\r
throw "Accessing member of non-struct";\r
break;\r
case ExprType::Bracket:\r
{\r
+ // get type of expr and remove array/ptr modifier to get\r
+ // type of [] access\r
TypeInfo ti = typeExpr(globalCtx, *e._brackets.lexpr);\r
if (!ti.type.modifiers.empty())\r
{\r
}\r
case ExprType::Identifier:\r
{\r
+ // var lookup and return var type\r
auto v = findVariable(e._identifier.identifier, e._identifier.namespacePrefixes, globalCtx);\r
if (!v.has_value())\r
throw "Unknown variable";\r
\r
#include <functional>\r
\r
+// struct with callback functions for all relevant types\r
+// tree can be walked selectively by providing only\r
+// needed callbacks\r
struct Visitor {\r
std::function<void(const Type &, const std::shared_ptr<Context> ctx)> onType = [](auto, auto){};\r
std::function<void(const Expr &, const std::shared_ptr<Context> ctx)> onExpr = [](auto, auto){};\r
\r
#define VISIT(XS) for (auto x : XS) visit(x);\r
\r
+// simply walk IR by recursively calling functions for all children\r
struct Visit {\r
private:\r
Visitor v;\r