Just got paranoid about saving all this work. Almost finished operator overloading, but everything is slightly broken right now.
This commit is contained in:
+2
-2
@@ -10,7 +10,7 @@ class Type;
|
|||||||
#include "Type.h"
|
#include "Type.h"
|
||||||
|
|
||||||
#ifndef NULL
|
#ifndef NULL
|
||||||
#define NULL 0
|
#define NULL ((void*)0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
enum ASTType {undef, translation_unit, interpreter_directive, import, identifier, type_def,
|
enum ASTType {undef, translation_unit, interpreter_directive, import, identifier, type_def,
|
||||||
@@ -29,7 +29,7 @@ class ASTData {
|
|||||||
ASTType type;
|
ASTType type;
|
||||||
Type* valueType;
|
Type* valueType;
|
||||||
Symbol symbol;
|
Symbol symbol;
|
||||||
std::map<std::string, NodeTree<ASTData>*> scope;
|
std::map<std::string, std::vector<NodeTree<ASTData>*>> scope;
|
||||||
private:
|
private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -16,12 +16,15 @@ class ASTTransformation: public NodeTransformation<Symbol,ASTData> {
|
|||||||
ASTTransformation(Importer* importerIn);
|
ASTTransformation(Importer* importerIn);
|
||||||
~ASTTransformation();
|
~ASTTransformation();
|
||||||
virtual NodeTree<ASTData>* transform(NodeTree<Symbol>* from);
|
virtual NodeTree<ASTData>* transform(NodeTree<Symbol>* from);
|
||||||
NodeTree<ASTData>* transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope);
|
NodeTree<ASTData>* transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope, std::vector<Type> types = std::vector<Type>());
|
||||||
|
std::vector<NodeTree<ASTData>*> transformChildren(std::vector<NodeTree<Symbol>*> children, std::set<int> skipChildren, NodeTree<ASTData>* scope, std::vector<Type> types);
|
||||||
|
std::vector<Type> mapNodesToTypes(std::vector<NodeTree<ASTData>*> nodes);
|
||||||
std::string concatSymbolTree(NodeTree<Symbol>* root);
|
std::string concatSymbolTree(NodeTree<Symbol>* root);
|
||||||
NodeTree<ASTData>* scopeLookup(NodeTree<ASTData>* scope, std::string lookup);
|
NodeTree<ASTData>* scopeLookup(NodeTree<ASTData>* scope, std::string lookup, std::vector<Type> types = std::vector<Type>());
|
||||||
Type* typeFromString(std::string type, NodeTree<ASTData>* scope);
|
Type* typeFromString(std::string type, NodeTree<ASTData>* scope);
|
||||||
private:
|
private:
|
||||||
Importer * importer;
|
Importer * importer;
|
||||||
|
std::map<std::string, std::vector<NodeTree<ASTData>*>> languageLevelScope;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ class CGenerator {
|
|||||||
void generateCompSet(std::map<std::string, NodeTree<ASTData>*> ASTs, std::string outputName);
|
void generateCompSet(std::map<std::string, NodeTree<ASTData>*> ASTs, std::string outputName);
|
||||||
std::string generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enclosingObject = NULL);
|
std::string generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enclosingObject = NULL);
|
||||||
static std::string ValueTypeToCType(Type *type);
|
static std::string ValueTypeToCType(Type *type);
|
||||||
|
static std::string ValueTypeToCTypeDecoration(Type *type);
|
||||||
std::string generateObjectMethod(NodeTree<ASTData>* enclosingObject, NodeTree<ASTData>* from);
|
std::string generateObjectMethod(NodeTree<ASTData>* enclosingObject, NodeTree<ASTData>* from);
|
||||||
|
|
||||||
std::string generatorString;
|
std::string generatorString;
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include "NodeTree.h"
|
#include "NodeTree.h"
|
||||||
|
|
||||||
#ifndef NULL
|
#ifndef NULL
|
||||||
#define NULL 0
|
#define NULL ((void*)0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <class FROM, class TO>
|
template <class FROM, class TO>
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
#define NODETREE_H
|
#define NODETREE_H
|
||||||
|
|
||||||
#ifndef NULL
|
#ifndef NULL
|
||||||
#define NULL 0
|
#define NULL ((void*)0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
#define PARSE_ACTION_H
|
#define PARSE_ACTION_H
|
||||||
|
|
||||||
#ifndef NULL
|
#ifndef NULL
|
||||||
#define NULL 0
|
#define NULL ((void*)0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
#define PARSERULE_H
|
#define PARSERULE_H
|
||||||
|
|
||||||
#ifndef NULL
|
#ifndef NULL
|
||||||
#define NULL 0
|
#define NULL ((void*)0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "Symbol.h"
|
#include "Symbol.h"
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
#define STATE_H
|
#define STATE_H
|
||||||
|
|
||||||
#ifndef NULL
|
#ifndef NULL
|
||||||
#define NULL 0
|
#define NULL ((void*)0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
#define SYMBOL_H
|
#define SYMBOL_H
|
||||||
|
|
||||||
#ifndef NULL
|
#ifndef NULL
|
||||||
#define NULL 0
|
#define NULL ((void*)0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "NodeTree.h"
|
#include "NodeTree.h"
|
||||||
|
|||||||
+3
-1
@@ -2,7 +2,7 @@
|
|||||||
#define TYPE_H
|
#define TYPE_H
|
||||||
|
|
||||||
#ifndef NULL
|
#ifndef NULL
|
||||||
#define NULL 0
|
#define NULL ((void*)0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -25,6 +25,8 @@ class Type {
|
|||||||
Type(NodeTree<ASTData>* typeDefinitionIn, int indirectionIn);
|
Type(NodeTree<ASTData>* typeDefinitionIn, int indirectionIn);
|
||||||
Type(ValueType typeIn, NodeTree<ASTData>* typeDefinitionIn, int indirectionIn);
|
Type(ValueType typeIn, NodeTree<ASTData>* typeDefinitionIn, int indirectionIn);
|
||||||
~Type();
|
~Type();
|
||||||
|
bool const operator==(const Type &other)const;
|
||||||
|
bool const operator!=(const Type &other)const;
|
||||||
std::string toString();
|
std::string toString();
|
||||||
ValueType baseType;
|
ValueType baseType;
|
||||||
NodeTree<ASTData>* typeDefinition;
|
NodeTree<ASTData>* typeDefinition;
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
#define UTIL_H
|
#define UTIL_H
|
||||||
|
|
||||||
#ifndef NULL
|
#ifndef NULL
|
||||||
#define NULL 0
|
#define NULL ((void*)0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|||||||
+152
-91
@@ -1,8 +1,28 @@
|
|||||||
#include "ASTTransformation.h"
|
#include "ASTTransformation.h"
|
||||||
|
|
||||||
ASTTransformation::ASTTransformation(Importer *importerIn) {
|
ASTTransformation::ASTTransformation(Importer *importerIn) {
|
||||||
//
|
|
||||||
importer = importerIn;
|
importer = importerIn;
|
||||||
|
//Set up language level special scope. (the final scope checked)
|
||||||
|
//Note the NULL type
|
||||||
|
languageLevelScope["+"].push_back( new NodeTree<ASTData>("function", ASTData(function, Symbol("+", true), NULL)));
|
||||||
|
languageLevelScope["-"].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("-", true), NULL)));
|
||||||
|
languageLevelScope["*"].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("*", true), NULL)));
|
||||||
|
languageLevelScope["&"].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("&", true), NULL)));
|
||||||
|
languageLevelScope["--"].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("--", true), NULL)));
|
||||||
|
languageLevelScope["++"].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("++", true), NULL)));
|
||||||
|
languageLevelScope["=="].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("==", true), NULL)));
|
||||||
|
languageLevelScope["<="].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("<=", true), NULL)));
|
||||||
|
languageLevelScope[">="].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol(">=", true), NULL)));
|
||||||
|
languageLevelScope["<"].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("<", true), NULL)));
|
||||||
|
languageLevelScope[">"].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol(">", true), NULL)));
|
||||||
|
languageLevelScope["&&"].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("&&", true), NULL)));
|
||||||
|
languageLevelScope["||"].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("||", true), NULL)));
|
||||||
|
languageLevelScope["!"].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("!", true), NULL)));
|
||||||
|
languageLevelScope["*="].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("*=", true), NULL)));
|
||||||
|
languageLevelScope["+="].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("+=", true), NULL)));
|
||||||
|
languageLevelScope["-="].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("-=", true), NULL)));
|
||||||
|
languageLevelScope["."].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol(".", true), NULL)));
|
||||||
|
languageLevelScope["->"].push_back(new NodeTree<ASTData>("function", ASTData(function, Symbol("->", true), NULL)));
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTTransformation::~ASTTransformation() {
|
ASTTransformation::~ASTTransformation() {
|
||||||
@@ -11,10 +31,10 @@ ASTTransformation::~ASTTransformation() {
|
|||||||
|
|
||||||
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
||||||
//Set up top scope
|
//Set up top scope
|
||||||
return transform(from, NULL);
|
return transform(from, NULL, std::vector<Type>());
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope) {
|
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope, std::vector<Type> types) {
|
||||||
Symbol current = from->getData();
|
Symbol current = from->getData();
|
||||||
std::string name = current.getName();
|
std::string name = current.getName();
|
||||||
NodeTree<ASTData>* newNode = NULL;
|
NodeTree<ASTData>* newNode = NULL;
|
||||||
@@ -24,27 +44,6 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
if (name == "translation_unit") {
|
if (name == "translation_unit") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(translation_unit));
|
newNode = new NodeTree<ASTData>(name, ASTData(translation_unit));
|
||||||
scope = newNode;
|
scope = newNode;
|
||||||
//Temporary scope fix, use placeholder type
|
|
||||||
scope->getDataRef()->scope["+"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("+", true), NULL));
|
|
||||||
scope->getDataRef()->scope["-"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("-", true), NULL));
|
|
||||||
scope->getDataRef()->scope["*"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("*", true), NULL));
|
|
||||||
scope->getDataRef()->scope["&"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("&", true), NULL));
|
|
||||||
scope->getDataRef()->scope["--"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("--", true), NULL));
|
|
||||||
scope->getDataRef()->scope["++"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("++", true), NULL));
|
|
||||||
scope->getDataRef()->scope["=="] = new NodeTree<ASTData>("function", ASTData(function, Symbol("==", true), NULL));
|
|
||||||
scope->getDataRef()->scope["<="] = new NodeTree<ASTData>("function", ASTData(function, Symbol("<=", true), NULL));
|
|
||||||
scope->getDataRef()->scope[">="] = new NodeTree<ASTData>("function", ASTData(function, Symbol(">=", true), NULL));
|
|
||||||
scope->getDataRef()->scope["<"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("<", true), NULL));
|
|
||||||
scope->getDataRef()->scope[">"] = new NodeTree<ASTData>("function", ASTData(function, Symbol(">", true), NULL));
|
|
||||||
scope->getDataRef()->scope["&&"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("&&", true), NULL));
|
|
||||||
scope->getDataRef()->scope["||"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("||", true), NULL));
|
|
||||||
scope->getDataRef()->scope["!"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("!", true), NULL));
|
|
||||||
scope->getDataRef()->scope["*="] = new NodeTree<ASTData>("function", ASTData(function, Symbol("*=", true), NULL));
|
|
||||||
scope->getDataRef()->scope["+="] = new NodeTree<ASTData>("function", ASTData(function, Symbol("+=", true), NULL));
|
|
||||||
scope->getDataRef()->scope["-="] = new NodeTree<ASTData>("function", ASTData(function, Symbol("-=", true), NULL));
|
|
||||||
scope->getDataRef()->scope["."] = new NodeTree<ASTData>("function", ASTData(function, Symbol(".", true), NULL));
|
|
||||||
scope->getDataRef()->scope["->"] = new NodeTree<ASTData>("function", ASTData(function, Symbol("->", true), NULL));
|
|
||||||
|
|
||||||
} else if (name == "interpreter_directive") {
|
} else if (name == "interpreter_directive") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(interpreter_directive));
|
newNode = new NodeTree<ASTData>(name, ASTData(interpreter_directive));
|
||||||
} else if (name == "import" && !current.isTerminal()) {
|
} else if (name == "import" && !current.isTerminal()) {
|
||||||
@@ -52,16 +51,17 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
newNode = new NodeTree<ASTData>(name, ASTData(import, Symbol(toImport, true)));
|
newNode = new NodeTree<ASTData>(name, ASTData(import, Symbol(toImport, true)));
|
||||||
//Do the imported file too
|
//Do the imported file too
|
||||||
NodeTree<ASTData>* outsideTranslationUnit = importer->import(toImport + ".krak");
|
NodeTree<ASTData>* outsideTranslationUnit = importer->import(toImport + ".krak");
|
||||||
scope->getDataRef()->scope[toImport] = outsideTranslationUnit; //Put this transation_unit in the scope as it's files name
|
scope->getDataRef()->scope[toImport].push_back(outsideTranslationUnit); //Put this transation_unit in the scope as it's files name
|
||||||
//Now add it to scope
|
//Now add it to scope
|
||||||
for (auto i = outsideTranslationUnit->getDataRef()->scope.begin(); i != outsideTranslationUnit->getDataRef()->scope.end(); i++)
|
for (auto i = outsideTranslationUnit->getDataRef()->scope.begin(); i != outsideTranslationUnit->getDataRef()->scope.end(); i++)
|
||||||
scope->getDataRef()->scope[i->first] = i->second;
|
for (auto j : i->second)
|
||||||
|
scope->getDataRef()->scope[i->first].push_back(j);
|
||||||
return newNode; // Don't need children of import
|
return newNode; // Don't need children of import
|
||||||
} else if (name == "identifier") {
|
} else if (name == "identifier") {
|
||||||
//Make sure we get the entire name
|
//Make sure we get the entire name
|
||||||
std::string lookupName = concatSymbolTree(from);
|
std::string lookupName = concatSymbolTree(from);
|
||||||
//std::cout << "scope lookup from identifier" << std::endl;
|
std::cout << "Looking up: " << lookupName << std::endl;
|
||||||
newNode = scopeLookup(scope, lookupName);
|
newNode = scopeLookup(scope, lookupName, types);
|
||||||
if (newNode == NULL) {
|
if (newNode == NULL) {
|
||||||
std::cout << "scope lookup error! Could not find " << lookupName << " in identifier " << std::endl;
|
std::cout << "scope lookup error! Could not find " << lookupName << " in identifier " << std::endl;
|
||||||
throw "LOOKUP ERROR: " + lookupName;
|
throw "LOOKUP ERROR: " + lookupName;
|
||||||
@@ -80,8 +80,8 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
newNode = new NodeTree<ASTData>(name, ASTData(type_def, Symbol(typeAlias, true, typeAlias)));
|
newNode = new NodeTree<ASTData>(name, ASTData(type_def, Symbol(typeAlias, true, typeAlias)));
|
||||||
newNode->getDataRef()->valueType = new Type(newNode); //Type is self-referential since this is the definition
|
newNode->getDataRef()->valueType = new Type(newNode); //Type is self-referential since this is the definition
|
||||||
}
|
}
|
||||||
scope->getDataRef()->scope[typeAlias] = newNode;
|
scope->getDataRef()->scope[typeAlias].push_back(newNode);
|
||||||
newNode->getDataRef()->scope["~enclosing_scope"] = scope;
|
newNode->getDataRef()->scope["~enclosing_scope"].push_back(scope);
|
||||||
scope = newNode;
|
scope = newNode;
|
||||||
skipChildren.insert(0); //Identifier lookup will be ourselves, as we just added ourselves to the scope
|
skipChildren.insert(0); //Identifier lookup will be ourselves, as we just added ourselves to the scope
|
||||||
//return newNode;
|
//return newNode;
|
||||||
@@ -90,66 +90,80 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(functionName, true), typeFromString(concatSymbolTree(children[0]), scope)));
|
newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(functionName, true), typeFromString(concatSymbolTree(children[0]), scope)));
|
||||||
skipChildren.insert(0);
|
skipChildren.insert(0);
|
||||||
skipChildren.insert(1);
|
skipChildren.insert(1);
|
||||||
scope->getDataRef()->scope[functionName] = newNode;
|
scope->getDataRef()->scope[functionName].push_back(newNode);
|
||||||
newNode->getDataRef()->scope["~enclosing_scope"] = scope;
|
newNode->getDataRef()->scope["~enclosing_scope"].push_back(scope);
|
||||||
scope = newNode;
|
scope = newNode;
|
||||||
|
|
||||||
|
// auto transChildren = transformChildren(children, skipChildren, scope, types);
|
||||||
|
// std::cout << functionName << " ";
|
||||||
|
// for (auto i : transChildren)
|
||||||
|
// std::cout << "||" << i->getDataRef()->toString() << "|| ";
|
||||||
|
// std::cout << "??||" << std::endl;
|
||||||
|
// newNode->addChildren(transChildren);
|
||||||
|
// return newNode;
|
||||||
|
|
||||||
std::cout << "finished function " << functionName << std::endl;
|
std::cout << "finished function " << functionName << std::endl;
|
||||||
} else if (name == "code_block") {
|
} else if (name == "code_block") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(code_block));
|
newNode = new NodeTree<ASTData>(name, ASTData(code_block));
|
||||||
newNode->getDataRef()->scope["~enclosing_scope"] = scope;
|
newNode->getDataRef()->scope["~enclosing_scope"].push_back(scope);
|
||||||
scope = newNode;
|
scope = newNode;
|
||||||
} else if (name == "typed_parameter") {
|
} else if (name == "typed_parameter") {
|
||||||
//newNode = transform(children[1]); //Transform to get the identifier
|
//newNode = transform(children[1]); //Transform to get the identifier
|
||||||
std::string parameterName = concatSymbolTree(children[1]);
|
std::string parameterName = concatSymbolTree(children[1]);
|
||||||
std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type
|
std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type
|
||||||
newNode = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(parameterName, true), typeFromString(typeString, scope)));
|
newNode = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(parameterName, true), typeFromString(typeString, scope)));
|
||||||
scope->getDataRef()->scope[parameterName] = newNode;
|
scope->getDataRef()->scope[parameterName].push_back(newNode);
|
||||||
newNode->getDataRef()->scope["~enclosing_scope"] = scope;
|
newNode->getDataRef()->scope["~enclosing_scope"].push_back(scope);
|
||||||
return newNode;
|
return newNode;
|
||||||
} else if (name == "boolean_expression" || name == "and_boolean_expression" || name == "bool_exp") {
|
} else if (name == "boolean_expression" || name == "and_boolean_expression" || name == "bool_exp") {
|
||||||
//If this is an actual part of an expression, not just a premoted term
|
//If this is an actual part of an expression, not just a premoted term
|
||||||
if (children.size() > 1) {
|
if (children.size() > 1) {
|
||||||
|
//We do children first so we can do appropriate scope searching with types (yay operator overloading!)
|
||||||
|
skipChildren.insert(1);
|
||||||
|
std::vector<NodeTree<ASTData>*> transformedChildren = transformChildren(children, skipChildren, scope, types);
|
||||||
std::string functionCallString = concatSymbolTree(children[1]);
|
std::string functionCallString = concatSymbolTree(children[1]);
|
||||||
NodeTree<ASTData>* function = scopeLookup(scope, functionCallString);
|
NodeTree<ASTData>* function = scopeLookup(scope, functionCallString, mapNodesToTypes(transformedChildren));
|
||||||
if (function == NULL) {
|
if (function == NULL) {
|
||||||
std::cout << "scope lookup error! Could not find " << functionCallString << " in boolean stuff " << std::endl;
|
std::cout << "scope lookup error! Could not find " << functionCallString << " in boolean stuff " << std::endl;
|
||||||
throw "LOOKUP ERROR: " + functionCallString;
|
throw "LOOKUP ERROR: " + functionCallString;
|
||||||
}
|
}
|
||||||
newNode = new NodeTree<ASTData>(functionCallString, ASTData(function_call, function->getDataRef()->valueType));
|
newNode = new NodeTree<ASTData>(functionCallString, ASTData(function_call, function->getDataRef()->valueType));
|
||||||
newNode->addChild(function); // First child of function call is a link to the function
|
newNode->addChild(function); // First child of function call is a link to the function
|
||||||
skipChildren.insert(1);
|
newNode->addChildren(transformedChildren);
|
||||||
} else {
|
} else {
|
||||||
//std::cout << children.size() << std::endl;
|
//std::cout << children.size() << std::endl;
|
||||||
if (children.size() == 0)
|
if (children.size() == 0)
|
||||||
return new NodeTree<ASTData>();
|
return new NodeTree<ASTData>();
|
||||||
return transform(children[0], scope); //Just a promoted term, so do child
|
return transform(children[0], scope, types); //Just a promoted term, so do child
|
||||||
}
|
}
|
||||||
//Here's the order of ops stuff
|
//Here's the order of ops stuff
|
||||||
} else if (name == "expression" || name == "shiftand" || name == "term" || name == "unarad" || name == "access_operation") { //unarad can ride through, it should always just be a promoted child
|
} else if (name == "expression" || name == "shiftand" || name == "term" || name == "unarad" || name == "access_operation") { //unarad can ride through, it should always just be a promoted child
|
||||||
//If this is an actual part of an expression, not just a premoted child
|
//If this is an actual part of an expression, not just a premoted child
|
||||||
if (children.size() > 2) {
|
if (children.size() > 2) {
|
||||||
|
NodeTree<ASTData>* lhs = transform(children[0], scope); //LHS does not inherit types
|
||||||
|
NodeTree<ASTData>* rhs;
|
||||||
|
if (name == "access_operation")
|
||||||
|
rhs = transform(children[2], lhs->getDataRef()->valueType->typeDefinition, types); //If an access operation, then the right side will be in the lhs's type's scope
|
||||||
|
else
|
||||||
|
rhs = transform(children[2], scope, types);
|
||||||
|
|
||||||
std::string functionCallName = concatSymbolTree(children[1]);
|
std::string functionCallName = concatSymbolTree(children[1]);
|
||||||
//std::cout << "scope lookup from expression or similar" << std::endl;
|
//std::cout << "scope lookup from expression or similar" << std::endl;
|
||||||
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
std::vector<NodeTree<ASTData>*> transformedChildren; transformedChildren.push_back(lhs); transformedChildren.push_back(rhs);
|
||||||
|
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName, mapNodesToTypes(transformedChildren));
|
||||||
if (function == NULL) {
|
if (function == NULL) {
|
||||||
std::cout << "scope lookup error! Could not find " << functionCallName << " in expression " << std::endl;
|
std::cout << "scope lookup error! Could not find " << functionCallName << " in expression " << std::endl;
|
||||||
throw "LOOKUP ERROR: " + functionCallName;
|
throw "LOOKUP ERROR: " + functionCallName;
|
||||||
}
|
}
|
||||||
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
||||||
newNode->addChild(function); // First child of function call is a link to the function definition
|
newNode->addChild(function); // First child of function call is a link to the function definition
|
||||||
NodeTree<ASTData>* lhs = transform(children[0], scope);
|
newNode->addChild(lhs);
|
||||||
NodeTree<ASTData>* rhs;// = transform(children[2], scope);
|
newNode->addChild(rhs);
|
||||||
if (name == "access_operation")
|
|
||||||
rhs = transform(children[2], lhs->getDataRef()->valueType->typeDefinition); //If an access operation, then the right side will be in the lhs's type's scope
|
|
||||||
else
|
|
||||||
rhs = transform(children[2], scope);
|
|
||||||
|
|
||||||
if (name == "access_operation")
|
if (name == "access_operation")
|
||||||
std::cout << "Access Operation: " << lhs->getDataRef()->symbol.getName() << " : " << rhs->getDataRef()->symbol.getName() << std::endl;
|
std::cout << "Access Operation: " << lhs->getDataRef()->symbol.getName() << " : " << rhs->getDataRef()->symbol.getName() << std::endl;
|
||||||
|
|
||||||
newNode->addChild(lhs);
|
|
||||||
newNode->addChild(rhs);
|
|
||||||
std::cout << functionCallName << " - " << function->getName() << " has value type " << function->getDataRef()->valueType << " and rhs " << rhs->getDataRef()->valueType << std::endl;
|
std::cout << functionCallName << " - " << function->getName() << " has value type " << function->getDataRef()->valueType << " and rhs " << rhs->getDataRef()->valueType << std::endl;
|
||||||
|
//Set the value of this function call
|
||||||
if (function->getDataRef()->valueType)
|
if (function->getDataRef()->valueType)
|
||||||
newNode->getDataRef()->valueType = function->getDataRef()->valueType;
|
newNode->getDataRef()->valueType = function->getDataRef()->valueType;
|
||||||
else if (rhs->getDataRef()->valueType)
|
else if (rhs->getDataRef()->valueType)
|
||||||
@@ -160,7 +174,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
return newNode;
|
return newNode;
|
||||||
//skipChildren.insert(1);
|
//skipChildren.insert(1);
|
||||||
} else {
|
} else {
|
||||||
return transform(children[0], scope); //Just a promoted child, so do it instead
|
return transform(children[0], scope, types); //Just a promoted child, so do it instead
|
||||||
}
|
}
|
||||||
} else if (name == "factor") { //Do factor here, as it has all the weird unary operators
|
} else if (name == "factor") { //Do factor here, as it has all the weird unary operators
|
||||||
//If this is an actual part of an expression, not just a premoted child
|
//If this is an actual part of an expression, not just a premoted child
|
||||||
@@ -169,12 +183,13 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
std::string funcName = concatSymbolTree(children[0]);
|
std::string funcName = concatSymbolTree(children[0]);
|
||||||
NodeTree<ASTData>* param;
|
NodeTree<ASTData>* param;
|
||||||
if (funcName == "*" || funcName == "&" || funcName == "++" || funcName == "--" || funcName == "-" || funcName == "!" || funcName == "~")
|
if (funcName == "*" || funcName == "&" || funcName == "++" || funcName == "--" || funcName == "-" || funcName == "!" || funcName == "~")
|
||||||
param = transform(children[1], scope);
|
param = transform(children[1], scope, types);
|
||||||
else
|
else
|
||||||
funcName = concatSymbolTree(children[1]), param = transform(children[0], scope);
|
funcName = concatSymbolTree(children[1]), param = transform(children[0], scope, types);
|
||||||
|
|
||||||
//std::cout << "scope lookup from factor" << std::endl;
|
//std::cout << "scope lookup from factor" << std::endl;
|
||||||
NodeTree<ASTData>* function = scopeLookup(scope, funcName);
|
std::vector<NodeTree<ASTData>*> transformedChildren; transformedChildren.push_back(param);
|
||||||
|
NodeTree<ASTData>* function = scopeLookup(scope, funcName, mapNodesToTypes(transformedChildren));
|
||||||
if (function == NULL) {
|
if (function == NULL) {
|
||||||
std::cout << "scope lookup error! Could not find " << funcName << " in factor " << std::endl;
|
std::cout << "scope lookup error! Could not find " << funcName << " in factor " << std::endl;
|
||||||
throw "LOOKUP ERROR: " + funcName;
|
throw "LOOKUP ERROR: " + funcName;
|
||||||
@@ -189,7 +204,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
|
|
||||||
return newNode;
|
return newNode;
|
||||||
} else {
|
} else {
|
||||||
return transform(children[0], scope); //Just a promoted child, so do it instead
|
return transform(children[0], scope, types); //Just a promoted child, so do it instead
|
||||||
}
|
}
|
||||||
} else if (name == "statement") {
|
} else if (name == "statement") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(statement));
|
newNode = new NodeTree<ASTData>(name, ASTData(statement));
|
||||||
@@ -205,21 +220,23 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
newNode = new NodeTree<ASTData>(name, ASTData(assignment_statement));
|
newNode = new NodeTree<ASTData>(name, ASTData(assignment_statement));
|
||||||
std::string assignFuncName = concatSymbolTree(children[1]);
|
std::string assignFuncName = concatSymbolTree(children[1]);
|
||||||
if (assignFuncName == "=") {
|
if (assignFuncName == "=") {
|
||||||
newNode->addChild(transform(children[0], scope));
|
newNode->addChild(transform(children[0], scope, types));
|
||||||
newNode->addChild(transform(children[2], scope));
|
newNode->addChild(transform(children[2], scope, types));
|
||||||
} else {
|
} else {
|
||||||
//For assignments like += or *=, expand the syntatic sugar.
|
//For assignments like += or *=, expand the syntatic sugar.
|
||||||
NodeTree<ASTData>* lhs = transform(children[0], scope);
|
NodeTree<ASTData>* lhs = transform(children[0], scope, types);
|
||||||
|
NodeTree<ASTData>* rhs = transform(children[2], scope, types);
|
||||||
|
std::vector<NodeTree<ASTData>*> transformedChildren; transformedChildren.push_back(lhs); transformedChildren.push_back(rhs);
|
||||||
std::string functionName = assignFuncName.substr(0,1);
|
std::string functionName = assignFuncName.substr(0,1);
|
||||||
NodeTree<ASTData>* childCall = new NodeTree<ASTData>(functionName, ASTData(function_call, Symbol(functionName, true)));
|
NodeTree<ASTData>* childCall = new NodeTree<ASTData>(functionName, ASTData(function_call, Symbol(functionName, true)));
|
||||||
NodeTree<ASTData>* functionDef = scopeLookup(scope, functionName);
|
NodeTree<ASTData>* functionDef = scopeLookup(scope, functionName, mapNodesToTypes(transformedChildren));
|
||||||
if (functionDef == NULL) {
|
if (functionDef == NULL) {
|
||||||
std::cout << "scope lookup error! Could not find " << functionName << " in assignment_statement " << std::endl;
|
std::cout << "scope lookup error! Could not find " << functionName << " in assignment_statement " << std::endl;
|
||||||
throw "LOOKUP ERROR: " + functionName;
|
throw "LOOKUP ERROR: " + functionName;
|
||||||
}
|
}
|
||||||
childCall->addChild(functionDef); //First child of function call is definition of the function
|
childCall->addChild(functionDef); //First child of function call is definition of the function
|
||||||
childCall->addChild(lhs);
|
childCall->addChild(lhs);
|
||||||
childCall->addChild(transform(children[2], scope));
|
childCall->addChild(rhs);
|
||||||
newNode->addChild(lhs);
|
newNode->addChild(lhs);
|
||||||
newNode->addChild(childCall);
|
newNode->addChild(childCall);
|
||||||
}
|
}
|
||||||
@@ -233,8 +250,8 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type
|
std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type
|
||||||
Type* identifierType = typeFromString(typeString, scope);
|
Type* identifierType = typeFromString(typeString, scope);
|
||||||
NodeTree<ASTData>* newIdentifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), identifierType));
|
NodeTree<ASTData>* newIdentifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), identifierType));
|
||||||
scope->getDataRef()->scope[newIdentifierStr] = newIdentifier;
|
scope->getDataRef()->scope[newIdentifierStr].push_back(newIdentifier);
|
||||||
newNode->getDataRef()->scope["~enclosing_scope"] = scope;
|
newNode->getDataRef()->scope["~enclosing_scope"].push_back(scope);
|
||||||
//Now we don't do this thing
|
//Now we don't do this thing
|
||||||
// if (identifierType->typeDefinition) {
|
// if (identifierType->typeDefinition) {
|
||||||
// //Is a custom type. Populate this declaration's scope with it's inner declarations
|
// //Is a custom type. Populate this declaration's scope with it's inner declarations
|
||||||
@@ -257,29 +274,32 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
} else if (name == "function_call") {
|
} else if (name == "function_call") {
|
||||||
std::string functionCallName = concatSymbolTree(children[0]);
|
std::string functionCallName = concatSymbolTree(children[0]);
|
||||||
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
||||||
std::cout << "scope lookup from function_call: " << functionCallName << std::endl;
|
|
||||||
for (auto i : children)
|
|
||||||
std::cout << i << " : " << i->getName() << " : " << i->getDataRef()->getName() << std::endl;
|
|
||||||
//NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
|
||||||
NodeTree<ASTData>* function = transform(children[0], scope);/* scopeLookup(scope, functionCallName);*/
|
|
||||||
std::cout << "The thing: " << function << " : " << function->getName() << std::endl;
|
|
||||||
for (auto i : function->getChildren())
|
|
||||||
std::cout << i->getName() << " ";
|
|
||||||
std::cout << std::endl;
|
|
||||||
// if (function == NULL) {
|
// if (function == NULL) {
|
||||||
// std::cout << "scope lookup error! Could not find " << functionCallName << " in function_call " << std::endl;
|
// std::cout << "scope lookup error! Could not find " << functionCallName << " in function_call " << std::endl;
|
||||||
// throw "LOOKUP ERROR: " + functionCallName;
|
// throw "LOOKUP ERROR: " + functionCallName;
|
||||||
// }
|
// }
|
||||||
|
skipChildren.insert(0);
|
||||||
|
std::vector<NodeTree<ASTData>*> transformedChildren = transformChildren(children, skipChildren, scope, types);
|
||||||
|
std::cout << "scope lookup from function_call: " << functionCallName << std::endl;
|
||||||
|
for (auto i : children)
|
||||||
|
std::cout << i << " : " << i->getName() << " : " << i->getDataRef()->getName() << std::endl;
|
||||||
|
|
||||||
|
NodeTree<ASTData>* function = transform(children[0], scope, mapNodesToTypes(transformedChildren));
|
||||||
|
std::cout << "The thing: " << function << " : " << function->getName() << std::endl;
|
||||||
|
for (auto i : function->getChildren())
|
||||||
|
std::cout << i->getName() << " ";
|
||||||
|
std::cout << std::endl;
|
||||||
newNode->addChild(function);
|
newNode->addChild(function);
|
||||||
newNode->getDataRef()->valueType = function->getDataRef()->valueType;
|
newNode->getDataRef()->valueType = function->getDataRef()->valueType;
|
||||||
skipChildren.insert(0);
|
newNode->addChildren(transformedChildren);
|
||||||
|
return newNode;
|
||||||
} else if (name == "parameter") {
|
} else if (name == "parameter") {
|
||||||
return transform(children[0], scope); //Don't need a parameter node, just the value
|
return transform(children[0], scope, types); //Don't need a parameter node, just the value
|
||||||
} else if (name == "type") {
|
} else if (name == "type") {
|
||||||
std::string theConcat = concatSymbolTree(from); //We have no symbol, so this will concat our children
|
std::string theConcat = concatSymbolTree(from); //We have no symbol, so this will concat our children
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(theConcat, true), typeFromString(theConcat, scope)));
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(theConcat, true), typeFromString(theConcat, scope)));
|
||||||
} else if (name == "number") {
|
} else if (name == "number") {
|
||||||
return transform(children[0], scope);
|
return transform(children[0], scope, types);
|
||||||
} else if (name == "integer") {
|
} else if (name == "integer") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), new Type(integer)));
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), new Type(integer)));
|
||||||
} else if (name == "float") {
|
} else if (name == "float") {
|
||||||
@@ -294,20 +314,42 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
return new NodeTree<ASTData>();
|
return new NodeTree<ASTData>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// In general, iterate through children and do them. Might not do this for all children.
|
//Do all children but the ones we skip
|
||||||
for (int i = 0; i < children.size(); i++) {
|
for (int i = 0; i < children.size(); i++) {
|
||||||
if (skipChildren.find(i) == skipChildren.end()) {
|
if (skipChildren.find(i) == skipChildren.end()) {
|
||||||
NodeTree<ASTData>* transChild = transform(children[i], scope);
|
NodeTree<ASTData>* transChild = transform(children[i], scope, types);
|
||||||
if (transChild->getDataRef()->type) //Only add the children that have a real ASTData::ASTType, that is, legit ASTData.
|
if (transChild->getDataRef()->type) //Only add the children that have a real ASTData::ASTType, that is, legit ASTData.
|
||||||
newNode->addChild(transChild);
|
newNode->addChild(transChild);
|
||||||
else
|
else
|
||||||
delete transChild;
|
delete transChild;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return newNode;
|
return newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//We use this functionality a lot at different places
|
||||||
|
std::vector<NodeTree<ASTData>*> ASTTransformation::transformChildren(std::vector<NodeTree<Symbol>*> children, std::set<int> skipChildren, NodeTree<ASTData>* scope, std::vector<Type> types) {
|
||||||
|
std::vector<NodeTree<ASTData>*> transformedChildren;
|
||||||
|
// In general, iterate through children and do them. Might not do this for all children.
|
||||||
|
for (int i = 0; i < children.size(); i++) {
|
||||||
|
if (skipChildren.find(i) == skipChildren.end()) {
|
||||||
|
NodeTree<ASTData>* transChild = transform(children[i], scope, types);
|
||||||
|
if (transChild->getDataRef()->type) //Only add the children that have a real ASTData::ASTType, that is, legit ASTData.
|
||||||
|
transformedChildren.push_back(transChild);
|
||||||
|
else
|
||||||
|
delete transChild;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return transformedChildren;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Type> ASTTransformation::mapNodesToTypes(std::vector<NodeTree<ASTData>*> nodes) {
|
||||||
|
std::vector<Type> types;
|
||||||
|
for (auto i : nodes)
|
||||||
|
types.push_back(*(i->getDataRef()->valueType));
|
||||||
|
return types;
|
||||||
|
}
|
||||||
|
|
||||||
std::string ASTTransformation::concatSymbolTree(NodeTree<Symbol>* root) {
|
std::string ASTTransformation::concatSymbolTree(NodeTree<Symbol>* root) {
|
||||||
std::string concatString;
|
std::string concatString;
|
||||||
std::string ourValue = root->getDataRef()->getValue();
|
std::string ourValue = root->getDataRef()->getValue();
|
||||||
@@ -320,33 +362,52 @@ std::string ASTTransformation::concatSymbolTree(NodeTree<Symbol>* root) {
|
|||||||
return concatString;
|
return concatString;
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeTree<ASTData>* ASTTransformation::scopeLookup(NodeTree<ASTData>* scope, std::string lookup) {
|
NodeTree<ASTData>* ASTTransformation::scopeLookup(NodeTree<ASTData>* scope, std::string lookup, std::vector<Type> types) {
|
||||||
//First, if it is a struct or object, get it's base.
|
|
||||||
std::vector<std::string> splitString = split(lookup, '.');
|
|
||||||
if (splitString.size() > 1) {
|
|
||||||
std::string base = splitString[0];
|
|
||||||
// NodeTree<ASTData>* baseDef = scopeLookup(scope, base);
|
|
||||||
// splitString.erase(splitString.begin()); //Get rid of the base in the split str
|
|
||||||
// //Now the base is the scope.
|
|
||||||
// return scopeLookup(baseDef, join(splitString, ".")); //So the joined version doesn't have the base.
|
|
||||||
return scopeLookup(scope, base);
|
|
||||||
}
|
|
||||||
//Search the map
|
//Search the map
|
||||||
auto scopeMap = scope->getDataRef()->scope;
|
auto scopeMap = scope->getDataRef()->scope;
|
||||||
auto elementIterator = scopeMap.find(lookup);
|
auto elementIterator = scopeMap.find(lookup);
|
||||||
|
//
|
||||||
if (elementIterator != scopeMap.end()) {
|
if (elementIterator != scopeMap.end()) {
|
||||||
// std::cout << "lookup of " << lookup << " succeded in first scope!" << std::endl;
|
for (auto i = elementIterator->second.begin(); i != elementIterator->second.end(); i++) {
|
||||||
return elementIterator->second;
|
//Types and functions cannot have the same name, and types very apparently do not have parameter types, so check and short-circuit
|
||||||
|
if ((*i)->getDataRef()->type == type_def)
|
||||||
|
return *i;
|
||||||
|
//return *i;
|
||||||
|
std::vector<NodeTree<ASTData>*> children = (*i)->getChildren();
|
||||||
|
if (types.size() != (children.size() > 0) ? children.size()-1 : 0) {
|
||||||
|
std::cout << "Type sizes do not match between two " << lookup << "(" << types.size() << "," << ((children.size() > 0) ? children.size()-1 : 0) << "), types are: ";
|
||||||
|
for (auto j : types)
|
||||||
|
std::cout << j.toString() << " ";
|
||||||
|
std::cout << std::endl;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
//std::cout << "lookup of " << lookup << " failed in first scope, checking for upper scope" << std::endl;
|
bool typesMatch = true;
|
||||||
|
for (int j = 0; j < types.size(); j++) {
|
||||||
|
if (types[j] != *(children[j]->getDataRef()->valueType)) {
|
||||||
|
typesMatch = false;
|
||||||
|
std::cout << "Types do not match between two " << lookup << std::endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (typesMatch)
|
||||||
|
return *i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//if it doesn't exist, try the enclosing scope if it exists.
|
//if it doesn't exist, try the enclosing scope if it exists.
|
||||||
auto enclosingIterator = scopeMap.find("~enclosing_scope");
|
auto enclosingIterator = scopeMap.find("~enclosing_scope");
|
||||||
if (enclosingIterator != scopeMap.end()) {
|
if (enclosingIterator != scopeMap.end()) {
|
||||||
// std::cout << "upper scope exists, searching it for " << lookup << std::endl;
|
// std::cout << "upper scope exists, searching it for " << lookup << std::endl;
|
||||||
return scopeLookup(enclosingIterator->second, lookup);
|
return scopeLookup(enclosingIterator->second[0], lookup, types);
|
||||||
}
|
}
|
||||||
//std::cout << "upper scope does not exist" << std::endl;
|
//std::cout << "upper scope does not exist" << std::endl;
|
||||||
std::cout << "could not find " << lookup << std::endl;
|
std::cout << "could not find " << lookup << " in standard scope, checking for operator" << std::endl;
|
||||||
|
//Note that we don't check for types. At some point we should, as we don't know how to add objects/structs without overloaded operators, etc
|
||||||
|
elementIterator = languageLevelScope.find(lookup);
|
||||||
|
if (elementIterator != languageLevelScope.end()) {
|
||||||
|
std::cout << "found it at language level as operator." << std::endl;
|
||||||
|
return elementIterator->second[0];
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+73
-23
@@ -48,26 +48,31 @@ std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
|||||||
output += generate(children[i], enclosingObject) + "\n";
|
output += generate(children[i], enclosingObject) + "\n";
|
||||||
//Declare everything in translation unit scope here. (allows stuff from other files, automatic forward declarations)
|
//Declare everything in translation unit scope here. (allows stuff from other files, automatic forward declarations)
|
||||||
for (auto i = data.scope.begin(); i != data.scope.end(); i++) {
|
for (auto i = data.scope.begin(); i != data.scope.end(); i++) {
|
||||||
NodeTree<ASTData>* declaration = i->second;
|
for (auto overloadedMembers : i->second) {
|
||||||
|
NodeTree<ASTData>* declaration = overloadedMembers;
|
||||||
std::vector<NodeTree<ASTData>*> decChildren = declaration->getChildren();
|
std::vector<NodeTree<ASTData>*> decChildren = declaration->getChildren();
|
||||||
ASTData declarationData = i->second->getData();
|
ASTData declarationData = declaration->getData();
|
||||||
switch(declarationData.type) {
|
switch(declarationData.type) {
|
||||||
case identifier:
|
case identifier:
|
||||||
output += ValueTypeToCType(declarationData.valueType) + " " + declarationData.symbol.getName() + "; /*identifier*/\n";
|
output += ValueTypeToCType(declarationData.valueType) + " " + declarationData.symbol.getName() + "; /*identifier*/\n";
|
||||||
break;
|
break;
|
||||||
case function:
|
case function:
|
||||||
|
{
|
||||||
if (decChildren.size() == 0) { //Not a real function, must be a built in passthrough {
|
if (decChildren.size() == 0) { //Not a real function, must be a built in passthrough {
|
||||||
output += "/* built in function: " + declarationData.toString() + " */\n";
|
output += "/* built in function: " + declarationData.toString() + " */\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
output += "\n" + ValueTypeToCType(declarationData.valueType) + " " + declarationData.symbol.getName() + "(";
|
output += "\n" + ValueTypeToCType(declarationData.valueType) + " ";
|
||||||
|
std::string nameDecoration, parameters;
|
||||||
for (int j = 0; j < decChildren.size()-1; j++) {
|
for (int j = 0; j < decChildren.size()-1; j++) {
|
||||||
if (j > 0)
|
if (j > 0)
|
||||||
output += ", ";
|
parameters += ", ";
|
||||||
output += ValueTypeToCType(decChildren[j]->getData().valueType) + " " + generate(decChildren[j], enclosingObject);
|
parameters += ValueTypeToCType(decChildren[j]->getData().valueType) + " " + generate(decChildren[j], enclosingObject);
|
||||||
|
nameDecoration += ValueTypeToCTypeDecoration(decChildren[j]->getData().valueType) + "_";
|
||||||
}
|
}
|
||||||
output += "); /*func*/\n";
|
output += nameDecoration + declarationData.symbol.getName() + "(" + parameters + "); /*func*/\n";
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case type_def:
|
case type_def:
|
||||||
//type
|
//type
|
||||||
output += "/*typedef " + declarationData.symbol.getName() + " */\n";
|
output += "/*typedef " + declarationData.symbol.getName() + " */\n";
|
||||||
@@ -77,6 +82,7 @@ std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
|||||||
output += "/*unknown declaration named " + declaration->getName() + "*/\n";
|
output += "/*unknown declaration named " + declaration->getName() + "*/\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
//Do here because we need the newlines
|
//Do here because we need the newlines
|
||||||
for (int i = 0; i < children.size(); i++)
|
for (int i = 0; i < children.size(); i++)
|
||||||
if (children[i]->getDataRef()->type != type_def)
|
if (children[i]->getDataRef()->type != type_def)
|
||||||
@@ -92,9 +98,12 @@ std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
|||||||
case identifier:
|
case identifier:
|
||||||
{
|
{
|
||||||
//If we're in an object method, and our enclosing scope is that object, we're a member of the object and should use the self reference.
|
//If we're in an object method, and our enclosing scope is that object, we're a member of the object and should use the self reference.
|
||||||
if (enclosingObject && enclosingObject->getDataRef()->scope.find(data.symbol.getName()) != enclosingObject->getDataRef()->scope.end()) {
|
std::string preName;
|
||||||
return "self->" + data.symbol.getName();
|
if (enclosingObject && enclosingObject->getDataRef()->scope.find(data.symbol.getName()) != enclosingObject->getDataRef()->scope.end())
|
||||||
}
|
preName += "self->";
|
||||||
|
if (false)
|
||||||
|
for (int j = 0; j < children.size()-1; j++)
|
||||||
|
preName += ValueTypeToCType(children[j]->getData().valueType) + "_";
|
||||||
return data.symbol.getName();
|
return data.symbol.getName();
|
||||||
}
|
}
|
||||||
case type_def:
|
case type_def:
|
||||||
@@ -114,21 +123,25 @@ std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
|||||||
return objectString + postString; //Functions come after the declaration of the struct
|
return objectString + postString; //Functions come after the declaration of the struct
|
||||||
}
|
}
|
||||||
case function:
|
case function:
|
||||||
output += "\n" + ValueTypeToCType(data.valueType) + " " + data.symbol.getName() + "(";
|
{
|
||||||
for (int i = 0; i < children.size()-1; i++) {
|
output += "\n" + ValueTypeToCType(data.valueType) + " ";
|
||||||
if (i > 0)
|
std::string nameDecoration, parameters;
|
||||||
output += ", ";
|
for (int j = 0; j < children.size()-1; j++) {
|
||||||
output += ValueTypeToCType(children[i]->getData().valueType) + " " + generate(children[i], enclosingObject);
|
if (j > 0)
|
||||||
|
parameters += ", ";
|
||||||
|
parameters += ValueTypeToCType(children[j]->getData().valueType) + " " + generate(children[j], enclosingObject);
|
||||||
|
nameDecoration += ValueTypeToCTypeDecoration(children[j]->getData().valueType) + "_";
|
||||||
}
|
}
|
||||||
output+= ")\n" + generate(children[children.size()-1], enclosingObject);
|
output += nameDecoration + data.symbol.getName() + "(" + parameters + ")\n" + generate(children[children.size()-1], enclosingObject);
|
||||||
return output;
|
return output;
|
||||||
|
}
|
||||||
case code_block:
|
case code_block:
|
||||||
output += "{\n";
|
output += "{\n";
|
||||||
tabLevel++;
|
tabLevel++;
|
||||||
for (int i = 0; i < children.size(); i++) {
|
for (int i = 0; i < children.size(); i++) {
|
||||||
std::cout << "Line " << i << std::endl;
|
//std::cout << "Line " << i << std::endl;
|
||||||
std::string line = generate(children[i], enclosingObject);
|
std::string line = generate(children[i], enclosingObject);
|
||||||
std::cout << line << std::endl;
|
//std::cout << line << std::endl;
|
||||||
output += line;
|
output += line;
|
||||||
}
|
}
|
||||||
tabLevel--;
|
tabLevel--;
|
||||||
@@ -200,19 +213,19 @@ std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
|||||||
NodeTree<ASTData>* possibleObjectType = children[1]->getDataRef()->valueType->typeDefinition;
|
NodeTree<ASTData>* possibleObjectType = children[1]->getDataRef()->valueType->typeDefinition;
|
||||||
//If is an object method, generate it like one. Needs extension/modification for inheritence
|
//If is an object method, generate it like one. Needs extension/modification for inheritence
|
||||||
if (possibleObjectType && possibleObjectType->getDataRef()->scope.find(functionName) != possibleObjectType->getDataRef()->scope.end()) {
|
if (possibleObjectType && possibleObjectType->getDataRef()->scope.find(functionName) != possibleObjectType->getDataRef()->scope.end()) {
|
||||||
return possibleObjectType->getDataRef()->symbol.getName() +"__" + functionName + "(" + (name == "." ? "&" : "") + generate(children[1], enclosingObject) + ",";
|
HERE return possibleObjectType->getDataRef()->symbol.getName() +"__" + functionName + "(" + (name == "." ? "&" : "") + generate(children[1], enclosingObject) + ",";
|
||||||
//The comma lets the upper function call know we already started the param list
|
//The comma lets the upper function call know we already started the param list
|
||||||
//Note that we got here from a function call. We just pass up this special case and let them finish with the perentheses
|
//Note that we got here from a function call. We just pass up this special case and let them finish with the perentheses
|
||||||
std::cout << "Is in scope or not type!" << std::endl;
|
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Is not in scope or not type" << std::endl;
|
std::cout << "Is not in scope or not type" << std::endl;
|
||||||
return "((" + generate(children[1], enclosingObject) + ")" + name + functionName + ")";
|
WTHISTHIS return "((" + generate(children[1], enclosingObject) + ")" + name + functionName + ")";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return "((" + generate(children[1], enclosingObject) + ")" + name + generate(children[2], enclosingObject) + ")";
|
ALSOWTH return "((" + generate(children[1], enclosingObject) + ")" + name + generate(children[2], enclosingObject) + ")";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
output += name + "(";
|
//It's a normal function call, not a special one or a method or anything
|
||||||
|
HERE output += name + "(";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
std::string functionCallSource = generate(children[0], enclosingObject);
|
std::string functionCallSource = generate(children[0], enclosingObject);
|
||||||
@@ -224,7 +237,8 @@ std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
|||||||
for (int i = 1; i < children.size(); i++) //children[0] is the declaration
|
for (int i = 1; i < children.size(); i++) //children[0] is the declaration
|
||||||
if (i < children.size()-1)
|
if (i < children.size()-1)
|
||||||
output += generate(children[i], enclosingObject) + ", ";
|
output += generate(children[i], enclosingObject) + ", ";
|
||||||
else output += generate(children[i], enclosingObject);
|
else
|
||||||
|
output += generate(children[i], enclosingObject);
|
||||||
output += ") ";
|
output += ") ";
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
@@ -288,3 +302,39 @@ std::string CGenerator::ValueTypeToCType(Type *type) {
|
|||||||
return_type += "*";
|
return_type += "*";
|
||||||
return return_type;
|
return return_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string CGenerator::ValueTypeToCTypeDecoration(Type *type) {
|
||||||
|
std::string return_type;
|
||||||
|
switch (type->baseType) {
|
||||||
|
case none:
|
||||||
|
if (type->typeDefinition)
|
||||||
|
return_type = type->typeDefinition->getDataRef()->symbol.getName();
|
||||||
|
else
|
||||||
|
return_type = "none";
|
||||||
|
break;
|
||||||
|
case void_type:
|
||||||
|
return_type = "void";
|
||||||
|
break;
|
||||||
|
case boolean:
|
||||||
|
return_type = "bool";
|
||||||
|
break;
|
||||||
|
case integer:
|
||||||
|
return_type = "int";
|
||||||
|
break;
|
||||||
|
case floating:
|
||||||
|
return_type = "float";
|
||||||
|
break;
|
||||||
|
case double_percision:
|
||||||
|
return_type = "double";
|
||||||
|
break;
|
||||||
|
case character:
|
||||||
|
return_type = "char";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return_type = "unknown_ValueType";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < type->indirection; i++)
|
||||||
|
return_type += "_P__";
|
||||||
|
return return_type;
|
||||||
|
}
|
||||||
|
|||||||
@@ -106,6 +106,7 @@ NodeTree<ASTData>* Importer::import(std::string fileName) {
|
|||||||
}
|
}
|
||||||
outFileTransformed.close();
|
outFileTransformed.close();
|
||||||
|
|
||||||
|
//Call with ourself to allow the transformation to call us to import files that it needs
|
||||||
NodeTree<ASTData>* AST = ASTTransformation(this).transform(parseTree);
|
NodeTree<ASTData>* AST = ASTTransformation(this).transform(parseTree);
|
||||||
|
|
||||||
if (AST) {
|
if (AST) {
|
||||||
|
|||||||
@@ -38,6 +38,14 @@ Type::Type(ValueType typeIn, NodeTree<ASTData>* typeDefinitionIn, int indirectio
|
|||||||
Type::~Type() {
|
Type::~Type() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const bool Type::operator==(const Type &other) const {
|
||||||
|
return( baseType == other.baseType && indirection == other.indirection && typeDefinition == other.typeDefinition);
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool Type::operator!=(const Type &other) const {
|
||||||
|
return(!this->operator==(other));
|
||||||
|
}
|
||||||
|
|
||||||
std::string Type::toString() {
|
std::string Type::toString() {
|
||||||
std::string typeString;
|
std::string typeString;
|
||||||
switch (baseType) {
|
switch (baseType) {
|
||||||
|
|||||||
Reference in New Issue
Block a user