Added in structure for tree transformations.

This commit is contained in:
Nathan Braswell
2013-09-26 15:16:58 -04:00
parent 7cfdc1e66b
commit 0110672f50
11 changed files with 192 additions and 6 deletions

29
include/ASTData.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef ASTDATA_H
#define ASTDATA_H
#ifndef NULL
#define NULL 0
#endif
#include "Symbol.h"
#include <vector>
class ASTData {
public:
enum ASTType {translation_unit, interpreter_directive, identifier,
import, interpreter_directive, function, code_block,
typed_parameter, expression, boolean_expression, statement,
if_statement, return_statement, assignment_statement, function_call,
value};
enum ValueType {none, boolean, integer, floating, double_percision, char_string }
ASTData(ASTType type, ValueType valueType = none);
ASTData(ASTType type, Symbol* symbol, ValueType valueType = none);
ASTType type;
Symbol* symbol;
private:
};
#endif

View File

@@ -0,0 +1,16 @@
#ifndef ASTTRANSFORMATION_H
#define ASTTRANSFORMATION_H
#include "NodeTransformation.h"
class ASTTransformation: public Transformation<Symbol*,ASTData> {
public:
ASTTransformation();
~ASTTransformation();
virtual NodeTree<Symbol*>* transform(NodeTree<ASTData>* from);
private:
//Nothing
};
#endif

View File

@@ -0,0 +1,35 @@
#ifndef NODETRANSFORMATION_H
#define NODETRANSFORMATION_H
#include "NodeTree.h"
#ifndef NULL
#define NULL 0
#endif
template <class FROM, class TO>
class NodeTransformation {
public:
NodeTransformation();
virtual ~NodeTransformation();
virtual NodeTree<TO>* transform(NodeTree<FROM>* from)=0;
private:
};
template <class FROM, class TO>
NodeTransformation<FROM,TO>::NodeTransformation() {
//Nothing
}
template <class FROM, class TO>
NodeTransformation<FROM,TO>::~NodeTransformation() {
//Nothing
}
// template <class FROM, class TO>
// NodeTree<TO>* NodeTransformation<FROM,TO>::transform(NodeTree<FROM>* from) {
// return (NodeTree<TO>*)0x1234;
// }
#endif

View File

@@ -0,0 +1,48 @@
#ifndef ASTTRANSFORMATION_H
#define ASTTRANSFORMATION_H
#include <queue>
#include <vector>
#include "NodeTransformation.h"
template<class T>
class RemovalTransformation: public NodeTransformation<T,T> {
public:
RemovalTransformation(T toRemove);
~RemovalTransformation();
virtual NodeTree<T>* transform(NodeTree<T>* from);
private:
T toRemove;
};
#endif
template<class T>
RemovalTransformation<T>::RemovalTransformation(T toRemove) {
this->toRemove = toRemove;
}
template<class T>
RemovalTransformation<T>::~RemovalTransformation() {
//
}
template<class T>
NodeTree<T>* RemovalTransformation<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;
}