-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractSyntaxTree.h
66 lines (61 loc) · 2.04 KB
/
AbstractSyntaxTree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef INCLUDE_AST_H
#define INCLUDE_AST_H
#include <map>
#include <string>
#include <vector>
#include "DataType.h"
enum KEYWORD_TYPE {
KEYWORD_UNKNOWN,
KEYWORD_OPERATOR,
KEYWORD_CONSTANT,
KEYWORD_CONDITIONAL,
KEYWORD_SYMBOL,
KEYWORD_LAMBDA_DEF,
KEYWORD_VARIABLE_DEF,
KEYWORD_EMPTY,
KEYWORD_STRING
};
class SyntaxTreeNode {
public:
class Symbol {
public:
enum SYMBOL_TYPE { SYMBOL_TYPE_VAR, SYMBOL_TYPE_LAMBDA };
enum SYMBOL_TYPE type;
DataType value;
SyntaxTreeNode* funcDef = nullptr; // Node containing definition of function.
Symbol& operator=(const Symbol& other) {
if (this != &other) {
this->type = other.type;
this->value = other.value;
}
return *this;
}
};
SyntaxTreeNode();
~SyntaxTreeNode();
// Create a child AST node.
SyntaxTreeNode* createChildNode();
// Clean up all children.
void cleanSyntaxTree();
SyntaxTreeNode* parent;
std::string token;
std::vector<SyntaxTreeNode*> childNodes;
enum KEYWORD_TYPE keywordType = KEYWORD_UNKNOWN;
std::map<std::string, SyntaxTreeNode::Symbol> symbolTable;
// Add symbol to all children's symbol table.
void propagateSymbol(std::pair<std::string, SyntaxTreeNode::Symbol> symbol);
void propagateSymbolTable(std::map<std::string, SyntaxTreeNode::Symbol> table);
void propagateSymbolTable_Lambdas(std::map<std::string, SyntaxTreeNode::Symbol> table);
bool evaluated = false;
DataType value;
std::size_t nodeid;
// Make a copy of another node, recursively duplicate all children. Copy everything except symbolTable.
// Preserve root pointer, throw away children.
void constructLambdaNode(const SyntaxTreeNode* defNode);
private:
static std::size_t allocated;
// Return root of new duplicated tree. Symbol table and is not copied because scoping.
// Must cleanSyntaxTree() before calling this.
SyntaxTreeNode* duplicate(const SyntaxTreeNode* other);
};
#endif