Further work on AST transformation
This commit is contained in:
@@ -18,10 +18,14 @@ enum ValueType {none, boolean, integer, floating, double_percision, char_string
|
|||||||
|
|
||||||
class ASTData {
|
class ASTData {
|
||||||
public:
|
public:
|
||||||
|
ASTData();
|
||||||
ASTData(ASTType type, ValueType valueType = none);
|
ASTData(ASTType type, ValueType valueType = none);
|
||||||
ASTData(ASTType type, Symbol symbol, ValueType valueType = none);
|
ASTData(ASTType type, Symbol symbol, ValueType valueType = none);
|
||||||
~ASTData();
|
~ASTData();
|
||||||
std::string toString();
|
std::string toString();
|
||||||
|
static std::string ASTTypeToString(ASTType type);
|
||||||
|
static std::string ValueTypeToString(ValueType type);
|
||||||
|
static ValueType strToType(std::string type);
|
||||||
ASTType type;
|
ASTType type;
|
||||||
ValueType valueType;
|
ValueType valueType;
|
||||||
Symbol symbol;
|
Symbol symbol;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ class ASTTransformation: public NodeTransformation<Symbol,ASTData> {
|
|||||||
ASTTransformation();
|
ASTTransformation();
|
||||||
~ASTTransformation();
|
~ASTTransformation();
|
||||||
virtual NodeTree<ASTData>* transform(NodeTree<Symbol>* from);
|
virtual NodeTree<ASTData>* transform(NodeTree<Symbol>* from);
|
||||||
|
std::string concatSymbolTree(NodeTree<Symbol>* root);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//Nothing
|
//Nothing
|
||||||
|
|||||||
48
include/DeleteTransformation.h
Normal file
48
include/DeleteTransformation.h
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#ifndef DELETETRANSFORMATION_H
|
||||||
|
#define DELETETRANSFORMATION_H
|
||||||
|
|
||||||
|
#include <queue>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "NodeTransformation.h"
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class DeleteTransformation: public NodeTransformation<T,T> {
|
||||||
|
public:
|
||||||
|
DeleteTransformation(T toDelete);
|
||||||
|
~DeleteTransformation();
|
||||||
|
virtual NodeTree<T>* transform(NodeTree<T>* from);
|
||||||
|
|
||||||
|
private:
|
||||||
|
T toRemove;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
DeleteTransformation<T>::DeleteTransformation(T toRemove) {
|
||||||
|
this->toRemove = toRemove;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
DeleteTransformation<T>::~DeleteTransformation() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
NodeTree<T>* DeleteTransformation<T>::transform(NodeTree<T>* from) {
|
||||||
|
std::queue<NodeTree<T>*> toProcess;
|
||||||
|
toProcess.push(from);
|
||||||
|
while(!toProcess.empty()) {
|
||||||
|
NodeTree<T>* node = toProcess.front();
|
||||||
|
toProcess.pop();
|
||||||
|
std::vector<NodeTree<T>*> children = node->getChildren();
|
||||||
|
for (int i = 0; i < children.size(); i++) {
|
||||||
|
if (children[i]->getData() == toRemove)
|
||||||
|
node->removeChild(children[i]);
|
||||||
|
else
|
||||||
|
toProcess.push(children[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return from;
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@ class Symbol {
|
|||||||
bool const operator<(const Symbol &other)const;
|
bool const operator<(const Symbol &other)const;
|
||||||
|
|
||||||
std::string getName() const;
|
std::string getName() const;
|
||||||
|
std::string getValue() const;
|
||||||
std::string toString() const;
|
std::string toString() const;
|
||||||
Symbol clone();
|
Symbol clone();
|
||||||
void setSubTree(NodeTree<Symbol>* tree);
|
void setSubTree(NodeTree<Symbol>* tree);
|
||||||
|
|||||||
@@ -60,4 +60,4 @@ double = sign numeric "." numeric | sign numeric "." numeric "d" ;
|
|||||||
bool = "true" | "false" | "True" | "False" ;
|
bool = "true" | "false" | "True" | "False" ;
|
||||||
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|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|_)+" ;
|
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|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|_)+" ;
|
||||||
numeric = "(0|1|2|3|4|5|6|7|8|9)+" ;
|
numeric = "(0|1|2|3|4|5|6|7|8|9)+" ;
|
||||||
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 = "\"(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)+\"" ;
|
||||||
9
main.cpp
9
main.cpp
@@ -102,14 +102,19 @@ int main(int argc, char* argv[]) {
|
|||||||
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("WS", false)));
|
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("WS", false)));
|
||||||
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("\\(", true)));
|
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("\\(", true)));
|
||||||
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("\\)", true)));
|
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("\\)", true)));
|
||||||
//preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("/", true)));
|
|
||||||
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("::", true)));
|
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("::", true)));
|
||||||
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol(";", true)));
|
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol(";", true)));
|
||||||
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("{", true)));
|
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("{", true)));
|
||||||
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("}", true)));
|
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("}", true)));
|
||||||
|
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("import", true))); //Don't need the actual text of the symbol
|
||||||
|
preASTTransforms.push_back(new RemovalTransformation<Symbol>(Symbol("interpreter_directive", false)));
|
||||||
//Collapse Transformations
|
//Collapse Transformations
|
||||||
preASTTransforms.push_back(new CollapseTransformation<Symbol>(Symbol("opt_typed_parameter_list", false)));
|
preASTTransforms.push_back(new CollapseTransformation<Symbol>(Symbol("opt_typed_parameter_list", false)));
|
||||||
preASTTransforms.push_back(new CollapseTransformation<Symbol>(Symbol("opt_parameter_list", false)));
|
preASTTransforms.push_back(new CollapseTransformation<Symbol>(Symbol("opt_parameter_list", false)));
|
||||||
|
preASTTransforms.push_back(new CollapseTransformation<Symbol>(Symbol("opt_import_list", false)));
|
||||||
|
preASTTransforms.push_back(new CollapseTransformation<Symbol>(Symbol("import_list", false)));
|
||||||
|
preASTTransforms.push_back(new CollapseTransformation<Symbol>(Symbol("function_list", false)));
|
||||||
|
|
||||||
for (int i = 0; i < preASTTransforms.size(); i++) {
|
for (int i = 0; i < preASTTransforms.size(); i++) {
|
||||||
parseTree = preASTTransforms[i]->transform(parseTree);
|
parseTree = preASTTransforms[i]->transform(parseTree);
|
||||||
}
|
}
|
||||||
@@ -126,7 +131,7 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
|
|
||||||
if (AST) {
|
if (AST) {
|
||||||
outFileTransformed << AST->DOTGraphString() << std::endl;
|
outFileAST << AST->DOTGraphString() << std::endl;
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Tree returned from ASTTransformation is NULL!" << std::endl;
|
std::cout << "Tree returned from ASTTransformation is NULL!" << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
#include "ASTData.h"
|
#include "ASTData.h"
|
||||||
|
|
||||||
|
ASTData::ASTData() {
|
||||||
|
}
|
||||||
|
|
||||||
ASTData::ASTData(ASTType type, ValueType valueType) {
|
ASTData::ASTData(ASTType type, ValueType valueType) {
|
||||||
this->type = type;
|
this->type = type;
|
||||||
this->valueType = valueType;
|
this->valueType = valueType;
|
||||||
@@ -16,5 +19,96 @@ ASTData::~ASTData() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string ASTData::toString() {
|
std::string ASTData::toString() {
|
||||||
return "ASTData!";
|
return ASTTypeToString(type) + (symbol.isTerminal() ? " " + symbol.toString() : "") + (valueType ? " " + ValueTypeToString(valueType) : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
ValueType ASTData::strToType(std::string type) {
|
||||||
|
if (type == "bool")
|
||||||
|
return boolean;
|
||||||
|
else if (type == "int")
|
||||||
|
return integer;
|
||||||
|
else if (type == "float")
|
||||||
|
return floating;
|
||||||
|
else if (type == "double")
|
||||||
|
return double_percision;
|
||||||
|
else if (type == "string")
|
||||||
|
return char_string;
|
||||||
|
else return none;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ASTData::ValueTypeToString(ValueType type) {
|
||||||
|
switch (type) {
|
||||||
|
case none:
|
||||||
|
return "none";
|
||||||
|
break;
|
||||||
|
case boolean:
|
||||||
|
return "boolean";
|
||||||
|
break;
|
||||||
|
case integer:
|
||||||
|
return "integer";
|
||||||
|
break;
|
||||||
|
case floating:
|
||||||
|
return "floating";
|
||||||
|
break;
|
||||||
|
case double_percision:
|
||||||
|
return "double_percision";
|
||||||
|
break;
|
||||||
|
case char_string:
|
||||||
|
return "char_string";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return "unknown_ValueType";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ASTData::ASTTypeToString(ASTType type) {
|
||||||
|
switch (type) {
|
||||||
|
case translation_unit:
|
||||||
|
return "translation_unit";
|
||||||
|
break;
|
||||||
|
case interpreter_directive:
|
||||||
|
return "interpreter_directive";
|
||||||
|
break;
|
||||||
|
case identifier:
|
||||||
|
return "identifier";
|
||||||
|
break;
|
||||||
|
case import:
|
||||||
|
return "import";
|
||||||
|
break;
|
||||||
|
case function:
|
||||||
|
return "function";
|
||||||
|
break;
|
||||||
|
case code_block:
|
||||||
|
return "code_block";
|
||||||
|
break;
|
||||||
|
case typed_parameter:
|
||||||
|
return "typed_parameter";
|
||||||
|
break;
|
||||||
|
case expression:
|
||||||
|
return "expression";
|
||||||
|
break;
|
||||||
|
case boolean_expression:
|
||||||
|
return "boolean_expression";
|
||||||
|
break;
|
||||||
|
case statement:
|
||||||
|
return "statement";
|
||||||
|
break;
|
||||||
|
case if_statement:
|
||||||
|
return "if_statement";
|
||||||
|
break;
|
||||||
|
case return_statement:
|
||||||
|
return "return_statement";
|
||||||
|
break;
|
||||||
|
case assignment_statement:
|
||||||
|
return "assignment_statement";
|
||||||
|
break;
|
||||||
|
case function_call:
|
||||||
|
return "function_call";
|
||||||
|
break;
|
||||||
|
case value:
|
||||||
|
return "value";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return "unknown_ASTType";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,5 +9,38 @@ ASTTransformation::~ASTTransformation() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
||||||
return NULL;
|
Symbol current = from->getData();
|
||||||
|
std::string name = current.getName();
|
||||||
|
NodeTree<ASTData>* newNode;
|
||||||
|
std::vector<NodeTree<Symbol>*> children = from->getChildren();
|
||||||
|
|
||||||
|
if (name == "translation_unit") {
|
||||||
|
newNode = new NodeTree<ASTData>(name, ASTData(translation_unit));
|
||||||
|
} else if (name == "import" && !current.isTerminal()) {
|
||||||
|
newNode = new NodeTree<ASTData>(name, ASTData(import, Symbol(concatSymbolTree(children[0]), true)));
|
||||||
|
return newNode; // Don't need children of import
|
||||||
|
} else if (name == "function") {
|
||||||
|
newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(concatSymbolTree(children[1]), true), ASTData::strToType(concatSymbolTree(children[0]))));
|
||||||
|
} else {
|
||||||
|
return new NodeTree<ASTData>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// In general, iterate through children and do them. Might not do this for all children.
|
||||||
|
for (int i = 0; i < children.size(); i++) {
|
||||||
|
newNode->addChild(transform(children[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return newNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ASTTransformation::concatSymbolTree(NodeTree<Symbol>* root) {
|
||||||
|
std::string concatString;
|
||||||
|
std::string ourValue = root->getData().getValue();
|
||||||
|
if (ourValue != "NoValue")
|
||||||
|
concatString += ourValue;
|
||||||
|
std::vector<NodeTree<Symbol>*> children = root->getChildren();
|
||||||
|
for (int i = 0; i < children.size(); i++) {
|
||||||
|
concatString = concatSymbolTree(children[i]);
|
||||||
|
}
|
||||||
|
return concatString;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,6 +47,10 @@ std::string Symbol::getName() const {
|
|||||||
return(name);
|
return(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Symbol::getValue() const {
|
||||||
|
return(value);
|
||||||
|
}
|
||||||
|
|
||||||
std::string Symbol::toString() const {
|
std::string Symbol::toString() const {
|
||||||
return(name + (terminal ? " " + value : ""));
|
return(name + (terminal ? " " + value : ""));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user