Added rough but working scoping.
This commit is contained in:
@@ -2,6 +2,8 @@ cmake_minimum_required (VERSION 2.6)
|
|||||||
|
|
||||||
project(Kraken)
|
project(Kraken)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||||
|
|
||||||
set( MY_INCLUDES ${PROJECT_SOURCE_DIR}/include)
|
set( MY_INCLUDES ${PROJECT_SOURCE_DIR}/include)
|
||||||
|
|
||||||
set( MY_SOURCES main.cpp src/Parser.cpp src/LALRParser.cpp src/GraphStructuredStack.cpp src/RNGLRParser.cpp src/ParseAction.cpp src/ParseRule.cpp src/Symbol.cpp src/StringReader.cpp src/State.cpp src/util.cpp src/Lexer.cpp src/RegEx.cpp src/RegExState.cpp src/Table.cpp src/ASTData.cpp src/ASTTransformation.cpp src/CGenerator.cpp src/Type.cpp )
|
set( MY_SOURCES main.cpp src/Parser.cpp src/LALRParser.cpp src/GraphStructuredStack.cpp src/RNGLRParser.cpp src/ParseAction.cpp src/ParseRule.cpp src/Symbol.cpp src/StringReader.cpp src/State.cpp src/util.cpp src/Lexer.cpp src/RegEx.cpp src/RegExState.cpp src/Table.cpp src/ASTData.cpp src/ASTTransformation.cpp src/CGenerator.cpp src/Type.cpp )
|
||||||
|
|||||||
+2
-1
@@ -2,7 +2,7 @@
|
|||||||
#define ASTDATA_H
|
#define ASTDATA_H
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <set>
|
#include <map>
|
||||||
|
|
||||||
#include "Symbol.h"
|
#include "Symbol.h"
|
||||||
#include "Type.h"
|
#include "Type.h"
|
||||||
@@ -28,6 +28,7 @@ class ASTData {
|
|||||||
ASTType type;
|
ASTType type;
|
||||||
Type valueType;
|
Type valueType;
|
||||||
Symbol symbol;
|
Symbol symbol;
|
||||||
|
std::map<std::string, NodeTree<ASTData>*> scope;
|
||||||
private:
|
private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
#ifndef ASTTRANSFORMATION_H
|
#ifndef ASTTRANSFORMATION_H
|
||||||
#define ASTTRANSFORMATION_H
|
#define ASTTRANSFORMATION_H
|
||||||
|
|
||||||
|
#include <set>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
#include "ASTData.h"
|
#include "ASTData.h"
|
||||||
#include "NodeTransformation.h"
|
#include "NodeTransformation.h"
|
||||||
|
|
||||||
@@ -9,8 +12,9 @@ class ASTTransformation: public NodeTransformation<Symbol,ASTData> {
|
|||||||
ASTTransformation();
|
ASTTransformation();
|
||||||
~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);
|
||||||
std::string concatSymbolTree(NodeTree<Symbol>* root);
|
std::string concatSymbolTree(NodeTree<Symbol>* root);
|
||||||
|
NodeTree<ASTData>* scopeLookup(NodeTree<ASTData>* scope, std::string lookup);
|
||||||
private:
|
private:
|
||||||
//Nothing
|
//Nothing
|
||||||
};
|
};
|
||||||
|
|||||||
+2
-2
@@ -76,5 +76,5 @@ alpha = "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|
|
|||||||
numeric = "(0|1|2|3|4|5|6|7|8|9)+" ;
|
numeric = "(0|1|2|3|4|5|6|7|8|9)+" ;
|
||||||
string = triple_quoted_string | "\"(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|,|!|\?|_|-|:|%| | |\\|/|\||\(|\)|0|1|2|3|4|5|6|7|8|9)+\"" ;
|
string = triple_quoted_string | "\"(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|,|!|\?|_|-|:|%| | |\\|/|\||\(|\)|0|1|2|3|4|5|6|7|8|9)+\"" ;
|
||||||
|
|
||||||
comment = "//(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|,|!|\?|_|-|:|;|%|=|\+| | |\\|/|\||\(|\)|\*|0|1|2|3|4|5|6|7|8|9)+
|
comment = "//(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|,|!|\?|_|-|:|;|%|=|\+| | |\\|/|\||\(|\)|\*|\"|0|1|2|3|4|5|6|7|8|9)+
|
||||||
" | "/\*(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|,|!|\?|_|-|:|%|=|\+| | |\\|/|\||\(|\)|0|1|2|3|4|5|6|7|8|9)+\*/" ;
|
" | "/\*(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|,|!|\?|_|-|:|%|=|\+| | |\\|/|\||\(|\)|\"|0|1|2|3|4|5|6|7|8|9)+\*/" ;
|
||||||
@@ -128,6 +128,8 @@ int main(int argc, char* argv[]) {
|
|||||||
removeSymbols.push_back(Symbol("interpreter_directive", false));
|
removeSymbols.push_back(Symbol("interpreter_directive", false));
|
||||||
removeSymbols.push_back(Symbol("if", true));
|
removeSymbols.push_back(Symbol("if", true));
|
||||||
removeSymbols.push_back(Symbol("while", true));
|
removeSymbols.push_back(Symbol("while", true));
|
||||||
|
removeSymbols.push_back(Symbol("__if_comp__", true));
|
||||||
|
removeSymbols.push_back(Symbol("comp_simple_passthrough", true));
|
||||||
|
|
||||||
for (int i = 0; i < removeSymbols.size(); i++)
|
for (int i = 0; i < removeSymbols.size(); i++)
|
||||||
parseTree = RemovalTransformation<Symbol>(removeSymbols[i]).transform(parseTree);
|
parseTree = RemovalTransformation<Symbol>(removeSymbols[i]).transform(parseTree);
|
||||||
@@ -148,9 +150,6 @@ int main(int argc, char* argv[]) {
|
|||||||
for (int i = 0; i < collapseSymbols.size(); i++)
|
for (int i = 0; i < collapseSymbols.size(); i++)
|
||||||
parseTree = CollapseTransformation<Symbol>(collapseSymbols[i]).transform(parseTree);
|
parseTree = CollapseTransformation<Symbol>(collapseSymbols[i]).transform(parseTree);
|
||||||
|
|
||||||
|
|
||||||
NodeTree<ASTData>* AST = ASTTransformation().transform(parseTree);
|
|
||||||
|
|
||||||
if (parseTree) {
|
if (parseTree) {
|
||||||
outFileTransformed << parseTree->DOTGraphString() << std::endl;
|
outFileTransformed << parseTree->DOTGraphString() << std::endl;
|
||||||
} else {
|
} else {
|
||||||
@@ -158,6 +157,7 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
outFileTransformed.close();
|
outFileTransformed.close();
|
||||||
|
|
||||||
|
NodeTree<ASTData>* AST = ASTTransformation().transform(parseTree);
|
||||||
if (AST) {
|
if (AST) {
|
||||||
outFileAST << AST->DOTGraphString() << std::endl;
|
outFileAST << AST->DOTGraphString() << std::endl;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+131
-39
@@ -9,6 +9,11 @@ ASTTransformation::~ASTTransformation() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
||||||
|
//Set up top scope
|
||||||
|
return transform(from, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope) {
|
||||||
Symbol current = from->getData();
|
Symbol current = from->getData();
|
||||||
std::string name = current.getName();
|
std::string name = current.getName();
|
||||||
NodeTree<ASTData>* newNode;
|
NodeTree<ASTData>* newNode;
|
||||||
@@ -17,59 +22,93 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
|||||||
|
|
||||||
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;
|
||||||
|
//Temporary scope fix
|
||||||
|
scope->getDataRef()->scope["+"] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope["-"] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope["*"] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope["&"] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope["=="] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope["--"] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope["++"] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope["<="] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope[">="] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope["*="] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope["+="] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope["-="] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope["<"] = new NodeTree<ASTData>();
|
||||||
|
scope->getDataRef()->scope[">"] = new NodeTree<ASTData>();
|
||||||
} 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()) {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(import, Symbol(concatSymbolTree(children[0]), true)));
|
newNode = new NodeTree<ASTData>(name, ASTData(import, Symbol(concatSymbolTree(children[0]), true)));
|
||||||
|
//Add to scope?
|
||||||
|
//
|
||||||
|
//
|
||||||
return newNode; // Don't need children of import
|
return newNode; // Don't need children of import
|
||||||
} else if (name == "identifier") {
|
} else if (name == "identifier") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(identifier, Symbol(concatSymbolTree(children[0]), true)));
|
std::string lookupName = concatSymbolTree(children[0]);
|
||||||
|
std::cout << "scope lookup from identifier" << std::endl;
|
||||||
|
newNode = scopeLookup(scope, lookupName);
|
||||||
|
if (newNode == NULL) {
|
||||||
|
std::cout << "scope lookup error! Could not find " << lookupName << std::endl;
|
||||||
|
throw "LOOKUP ERROR: " + lookupName;
|
||||||
|
}
|
||||||
|
//newNode = new NodeTree<ASTData>(name, ASTData(identifier, Symbol(concatSymbolTree(children[0]), true)));
|
||||||
} else if (name == "function") {
|
} else if (name == "function") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(concatSymbolTree(children[1]), true), Type(concatSymbolTree(children[0]))));
|
std::string functionName = concatSymbolTree(children[1]);
|
||||||
|
newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(functionName, true), Type(concatSymbolTree(children[0]))));
|
||||||
skipChildren.insert(0);
|
skipChildren.insert(0);
|
||||||
skipChildren.insert(1);
|
skipChildren.insert(1);
|
||||||
|
scope->getDataRef()->scope[functionName] = newNode;
|
||||||
|
newNode->getDataRef()->scope["~enclosing_scope"] = scope;
|
||||||
|
scope = newNode;
|
||||||
} 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;
|
||||||
|
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
|
||||||
newNode->getDataRef()->valueType = Type(concatSymbolTree(children[0])); //Get the type (left child) and set our new identifer to be that type
|
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
|
||||||
|
newNode = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(parameterName, true), Type(typeString)));
|
||||||
|
scope->getDataRef()->scope[parameterName] = newNode;
|
||||||
return newNode;
|
return newNode;
|
||||||
} else if (name == "boolean_expression") {
|
} 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) {
|
||||||
std::string functionCallName = concatSymbolTree(children[1]);
|
std::string functionCallName = concatSymbolTree(children[1]);
|
||||||
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
std::cout << "scope lookup from boolen_expression or similar" << std::endl;
|
||||||
skipChildren.insert(1);
|
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
||||||
} else {
|
if (function == NULL) {
|
||||||
return transform(children[0]); //Just a promoted term, so do child
|
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
|
||||||
|
throw "LOOKUP ERROR: " + functionCallName;
|
||||||
}
|
}
|
||||||
} else if (name == "and_boolean_expression") {
|
|
||||||
//If this is an actual part of an expression, not just a premoted bool_exp
|
|
||||||
if (children.size() > 1) {
|
|
||||||
std::string functionCallName = concatSymbolTree(children[1]);
|
|
||||||
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
|
||||||
skipChildren.insert(1);
|
skipChildren.insert(1);
|
||||||
} else {
|
} else {
|
||||||
return transform(children[0]); //Just a promoted bool_exp, so do child
|
std::cout << children.size() << std::endl;
|
||||||
}
|
if (children.size() == 0)
|
||||||
} else if (name == "bool_exp") {
|
return new NodeTree<ASTData>();
|
||||||
//If this is an actual part of an expression, not just a premoted bool_exp.
|
return transform(children[0], scope); //Just a promoted term, so do child
|
||||||
if (children.size() > 1) {
|
|
||||||
std::string functionCallName = concatSymbolTree(children[1]);
|
|
||||||
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
|
||||||
skipChildren.insert(1);
|
|
||||||
} else {
|
|
||||||
return transform(children[0]); //Just a promoted bool_exp, 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") { //unarad can ride through, it should always just be a promoted child
|
} else if (name == "expression" || name == "shiftand" || name == "term" || name == "unarad") { //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) {
|
||||||
std::string functionCallName = concatSymbolTree(children[1]);
|
std::string functionCallName = concatSymbolTree(children[1]);
|
||||||
|
std::cout << "scope lookup from expression or similar" << std::endl;
|
||||||
|
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
||||||
|
if (function == NULL) {
|
||||||
|
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
|
||||||
|
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
|
||||||
skipChildren.insert(1);
|
skipChildren.insert(1);
|
||||||
} else {
|
} else {
|
||||||
return transform(children[0]); //Just a promoted child, so do it instead
|
return transform(children[0], scope); //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
|
||||||
@@ -82,10 +121,17 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
|||||||
else
|
else
|
||||||
funcName = concatSymbolTree(children[1]), funcNum = 1;
|
funcName = concatSymbolTree(children[1]), funcNum = 1;
|
||||||
|
|
||||||
|
std::cout << "scope lookup from factor" << std::endl;
|
||||||
|
NodeTree<ASTData>* function = scopeLookup(scope, funcName);
|
||||||
|
if (function == NULL) {
|
||||||
|
std::cout << "scope lookup error! Could not find " << funcName << std::endl;
|
||||||
|
throw "LOOKUP ERROR: " + funcName;
|
||||||
|
}
|
||||||
newNode = new NodeTree<ASTData>(funcName, ASTData(function_call, Symbol(funcName, true)));
|
newNode = new NodeTree<ASTData>(funcName, ASTData(function_call, Symbol(funcName, true)));
|
||||||
|
newNode->addChild(function);
|
||||||
skipChildren.insert(funcNum);
|
skipChildren.insert(funcNum);
|
||||||
} else {
|
} else {
|
||||||
return transform(children[0]); //Just a promoted child, so do it instead
|
return transform(children[0], scope); //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));
|
||||||
@@ -101,43 +147,66 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
|||||||
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]));
|
newNode->addChild(transform(children[0], scope));
|
||||||
newNode->addChild(transform(children[2]));
|
newNode->addChild(transform(children[2], scope));
|
||||||
} else {
|
} else {
|
||||||
//For assignments like += or *=, expand the syntatic sugar.
|
//For assignments like += or *=, expand the syntatic sugar.
|
||||||
NodeTree<ASTData>* lhs = transform(children[0]);
|
NodeTree<ASTData>* lhs = transform(children[0], scope);
|
||||||
NodeTree<ASTData>* childCall = new NodeTree<ASTData>(assignFuncName.substr(0,1), ASTData(function_call, Symbol(assignFuncName.substr(0,1), true)));
|
std::string functionName = assignFuncName.substr(0,1);
|
||||||
|
NodeTree<ASTData>* childCall = new NodeTree<ASTData>(functionName, ASTData(function_call, Symbol(functionName, true)));
|
||||||
|
NodeTree<ASTData>* functionDef = scopeLookup(scope, functionName);
|
||||||
|
if (functionDef == NULL) {
|
||||||
|
std::cout << "scope lookup error! Could not find " << functionName << std::endl;
|
||||||
|
throw "LOOKUP ERROR: " + functionName;
|
||||||
|
}
|
||||||
|
childCall->addChild(functionDef); //First child of function call is definition of the function
|
||||||
childCall->addChild(lhs);
|
childCall->addChild(lhs);
|
||||||
childCall->addChild(transform(children[2]));
|
childCall->addChild(transform(children[2], scope));
|
||||||
newNode->addChild(lhs);
|
newNode->addChild(lhs);
|
||||||
newNode->addChild(childCall);
|
newNode->addChild(childCall);
|
||||||
}
|
}
|
||||||
return newNode;
|
return newNode;
|
||||||
} else if (name == "declaration_statement") {
|
} else if (name == "declaration_statement") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(declaration_statement));
|
newNode = new NodeTree<ASTData>(name, ASTData(declaration_statement));
|
||||||
NodeTree<ASTData>* newIdentifier = transform(children[1]); //Transform the identifier
|
|
||||||
newIdentifier->getDataRef()->valueType = Type(concatSymbolTree(children[0]));//set the type of the identifier
|
// NodeTree<ASTData>* newIdentifier = transform(children[1], scope); //Transform the identifier
|
||||||
|
// newIdentifier->getDataRef()->valueType = Type(concatSymbolTree(children[0]));//set the type of the identifier
|
||||||
|
|
||||||
|
std::string newIdentifierStr = concatSymbolTree(children[1]);
|
||||||
|
std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type
|
||||||
|
NodeTree<ASTData>* newIdentifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), Type(typeString)));
|
||||||
|
scope->getDataRef()->scope[newIdentifierStr] = newIdentifier;
|
||||||
|
|
||||||
newNode->addChild(newIdentifier);
|
newNode->addChild(newIdentifier);
|
||||||
skipChildren.insert(0); //These, the type and the identifier, have been taken care of.
|
skipChildren.insert(0); //These, the type and the identifier, have been taken care of.
|
||||||
skipChildren.insert(1);
|
skipChildren.insert(1);
|
||||||
} else if (name == "if_comp") {
|
} else if (name == "if_comp") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(if_comp));
|
newNode = new NodeTree<ASTData>(name, ASTData(if_comp));
|
||||||
|
newNode->addChild(new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(concatSymbolTree(children[0]),true))));
|
||||||
|
skipChildren.insert(0); //Don't do the identifier. The identifier lookup will fail. That's why we do it here.
|
||||||
} else if (name == "simple_passthrough") {
|
} else if (name == "simple_passthrough") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(simple_passthrough));
|
newNode = new NodeTree<ASTData>(name, ASTData(simple_passthrough));
|
||||||
} else if (name == "function_call") {
|
} else if (name == "function_call") {
|
||||||
//children[0] is scope
|
//children[0] is scope
|
||||||
std::string functionCallName = concatSymbolTree(children[1]);
|
std::string functionCallName = concatSymbolTree(children[1]);
|
||||||
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" << std::endl;
|
||||||
|
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
||||||
|
if (function == NULL) {
|
||||||
|
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
|
||||||
|
throw "LOOKUP ERROR: " + functionCallName;
|
||||||
|
}
|
||||||
|
newNode->addChild(function);
|
||||||
skipChildren.insert(1);
|
skipChildren.insert(1);
|
||||||
} else if (name == "parameter") {
|
} else if (name == "parameter") {
|
||||||
return transform(children[0]); //Don't need a parameter node, just the value
|
return transform(children[0], scope); //Don't need a parameter node, just the value
|
||||||
} else if (name == "parameter") {
|
} else if (name == "parameter") {
|
||||||
return transform(children[0]); //Don't need a parameter node, just the value
|
return transform(children[0], scope); //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), Type(theConcat)));
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(theConcat, true), Type(theConcat)));
|
||||||
} else if (name == "number") {
|
} else if (name == "number") {
|
||||||
return transform(children[0]);
|
return transform(children[0], scope);
|
||||||
} else if (name == "integer") {
|
} else if (name == "integer") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), Type(integer)));
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), Type(integer)));
|
||||||
} else if (name == "float") {
|
} else if (name == "float") {
|
||||||
@@ -145,7 +214,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
|||||||
} else if (name == "double") {
|
} else if (name == "double") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), Type(double_percision)));
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), Type(double_percision)));
|
||||||
} else if (name == "char") {
|
} else if (name == "char") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), Type(character))); //Indirection of 1 for array
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), Type(character, 1))); //Indirection of 1 for array
|
||||||
} else if (name == "string" || name == "triple_quoted_string") {
|
} else if (name == "string" || name == "triple_quoted_string") {
|
||||||
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), Type(character, 1))); //Indirection of 1 for array
|
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), Type(character, 1))); //Indirection of 1 for array
|
||||||
} else {
|
} else {
|
||||||
@@ -155,8 +224,8 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
|||||||
// In general, iterate through children and do them. Might not do this for all children.
|
// In general, iterate through children and do them. Might not do this for all children.
|
||||||
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]);
|
NodeTree<ASTData>* transChild = transform(children[i], scope);
|
||||||
if (transChild->getData().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;
|
||||||
@@ -168,7 +237,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
|||||||
|
|
||||||
std::string ASTTransformation::concatSymbolTree(NodeTree<Symbol>* root) {
|
std::string ASTTransformation::concatSymbolTree(NodeTree<Symbol>* root) {
|
||||||
std::string concatString;
|
std::string concatString;
|
||||||
std::string ourValue = root->getData().getValue();
|
std::string ourValue = root->getDataRef()->getValue();
|
||||||
if (ourValue != "NoValue")
|
if (ourValue != "NoValue")
|
||||||
concatString += ourValue;
|
concatString += ourValue;
|
||||||
std::vector<NodeTree<Symbol>*> children = root->getChildren();
|
std::vector<NodeTree<Symbol>*> children = root->getChildren();
|
||||||
@@ -177,3 +246,26 @@ std::string ASTTransformation::concatSymbolTree(NodeTree<Symbol>* root) {
|
|||||||
}
|
}
|
||||||
return concatString;
|
return concatString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NodeTree<ASTData>* ASTTransformation::scopeLookup(NodeTree<ASTData>* scope, std::string lookup) {
|
||||||
|
//Seach the map
|
||||||
|
auto scopeMap = scope->getDataRef()->scope;
|
||||||
|
std::cout << "scope size: " << scopeMap.size() << ", scope from " << scope->getName() << std::endl;
|
||||||
|
for (auto i = scopeMap.begin(); i != scopeMap.end(); i++)
|
||||||
|
std::cout << i->first << " : " << i-> second << " - " << i->second->getName() << std::endl;
|
||||||
|
|
||||||
|
auto elementIterator = scopeMap.find(lookup);
|
||||||
|
if (elementIterator != scopeMap.end()) {
|
||||||
|
std::cout << "lookup of " << lookup << " succeded in first scope!" << std::endl;
|
||||||
|
return elementIterator->second;
|
||||||
|
}
|
||||||
|
std::cout << "lookup of " << lookup << " failed in first scope, checking for upper scope" << std::endl;
|
||||||
|
//if it doesn't exist, try the enclosing scope if it exists.
|
||||||
|
auto enclosingIterator = scopeMap.find("~enclosing_scope");
|
||||||
|
if (enclosingIterator != scopeMap.end()) {
|
||||||
|
std::cout << "upper scope exists, searching it for " << lookup << std::endl;
|
||||||
|
return scopeLookup(enclosingIterator->second, lookup);
|
||||||
|
}
|
||||||
|
std::cout << "upper scope does not exist" << std::endl;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|||||||
+24
-6
@@ -20,7 +20,22 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
|
|||||||
std::string output = "";
|
std::string output = "";
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case translation_unit:
|
case translation_unit:
|
||||||
//Do nothing
|
//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++) {
|
||||||
|
NodeTree<ASTData>* declaration = i->second;
|
||||||
|
ASTData declarationData = i->second->getData();
|
||||||
|
switch(declarationData.type) {
|
||||||
|
case identifier:
|
||||||
|
output += ValueTypeToCType(declarationData.valueType) + " " + declarationData.symbol.getName() + "; /*identifier*/\n";
|
||||||
|
break;
|
||||||
|
case function:
|
||||||
|
output += "/*func*/\n";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
std::cout << "Declaration? of unknown type " << ASTData::ASTTypeToString(declarationData.type) << " in translation unit scope" << std::endl;
|
||||||
|
output += "/*unknown declaration*/\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case interpreter_directive:
|
case interpreter_directive:
|
||||||
//Do nothing
|
//Do nothing
|
||||||
@@ -82,18 +97,21 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
|
|||||||
return strSlice(generate(children[0]), 3, -4);
|
return strSlice(generate(children[0]), 3, -4);
|
||||||
case function_call:
|
case function_call:
|
||||||
{
|
{
|
||||||
|
//NOTE: The first (0th) child of a function call node is the declaration of the function
|
||||||
|
|
||||||
//Handle operators specially for now. Will later replace with
|
//Handle operators specially for now. Will later replace with
|
||||||
//Inlined functions in the standard library
|
//Inlined functions in the standard library
|
||||||
std::string name = data.symbol.getName();
|
std::string name = data.symbol.getName();
|
||||||
|
std::cout << name << " == " << children[0]->getData().symbol.getName() << std::endl;
|
||||||
if (name == "++" || name == "--")
|
if (name == "++" || name == "--")
|
||||||
return generate(children[0]) + name;
|
return generate(children[1]) + name;
|
||||||
if (name == "*" && children.size() == 1) //Is dereference, not multiplication
|
if (name == "*" && children.size() == 2) //Is dereference, not multiplication
|
||||||
return "*(" + generate(children[0]) + ")";
|
return "*(" + generate(children[1]) + ")";
|
||||||
if (name == "+" || name == "-" || name == "*" || name == "/" || name == "==" || name == ">=" || name == "<=" || name == "!=" || name == "<" || name == ">" || name == "%" || name == "+=" || name == "-=" || name == "*=" || name == "/=") {
|
if (name == "+" || name == "-" || name == "*" || name == "/" || name == "==" || name == ">=" || name == "<=" || name == "!=" || name == "<" || name == ">" || name == "%" || name == "+=" || name == "-=" || name == "*=" || name == "/=") {
|
||||||
return "((" + generate(children[0]) + ")" + name + "(" + generate(children[1]) + "))";
|
return "((" + generate(children[1]) + ")" + name + "(" + generate(children[2]) + "))";
|
||||||
}
|
}
|
||||||
output += data.symbol.getName() + "(";
|
output += data.symbol.getName() + "(";
|
||||||
for (int i = 0; i < children.size(); i++)
|
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]) + ", ";
|
output += generate(children[i]) + ", ";
|
||||||
else output += generate(children[i]);
|
else output += generate(children[i]);
|
||||||
|
|||||||
Reference in New Issue
Block a user