Added in structure for tree transformations.
This commit is contained in:
29
include/ASTData.h
Normal file
29
include/ASTData.h
Normal 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
|
||||
16
include/ASTTransformation.h
Normal file
16
include/ASTTransformation.h
Normal 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
|
||||
35
include/NodeTransformation.h
Normal file
35
include/NodeTransformation.h
Normal 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
|
||||
48
include/RemovalTransformation.h
Normal file
48
include/RemovalTransformation.h
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user