-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractSyntaxTree.cpp
141 lines (127 loc) · 4.01 KB
/
AbstractSyntaxTree.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "AbstractSyntaxTree.hpp"
#include <cstdio>
#include <cstdlib>
AbstractSyntaxTree::AbstractSyntaxTree()
: root(nullptr)
{
}
void AbstractSyntaxTree::AbstractSyntaxTreeDtor()
{
if (root != nullptr) {
deleteRecursively(&root);
}
}
void AbstractSyntaxTree::deleteRecursively(Node **node)
{
if ((*node)->left != nullptr) {
deleteRecursively(&(*node)->left);
}
if ((*node)->right != nullptr) {
deleteRecursively(&(*node)->right);
}
std::free(*node);
*node = nullptr;
}
void AbstractSyntaxTree::dumpToText(const char *outputFileName) const
{
std::FILE *outputFile = std::fopen(outputFileName, "w");
std::fprintf(outputFile, "{\n");
dumpToTextRecursively(outputFile, root);
std::fprintf(outputFile, "}\n");
std::fclose(outputFile);
}
void AbstractSyntaxTree::dumpToTextRecursively(std::FILE *outputFile, Node *node)
{
if (node == nullptr) {
std::fprintf(outputFile, "nil\n");
return;
}
switch (node->type) {
case Node::Type::FunctionDeclaration: {
std::fprintf(outputFile, "function-declaration\n"
"{\n");
break;
}
case Node::Type::FunctionImplementation: {
std::fprintf(outputFile, "%s\n"
"{\n", node->data.identifier);
break;
}
case Node::Type::FunctionImplementationParameter: {
std::fprintf(outputFile, "function-implementation-parameter\n"
"{\n");
break;
}
case Node::Type::Variable: {
std::fprintf(outputFile, "%s\n"
"{\n", node->data.identifier);
break;
}
case Node::Type::Concatenation: {
std::fprintf(outputFile, "concatenation\n"
"{\n");
break;
}
case Node::Type::If: {
std::fprintf(outputFile, "if\n"
"{\n");
break;
}
case Node::Type::While: {
std::fprintf(outputFile, "while\n"
"{\n");
break;
}
case Node::Type::AssignmentOperator: {
std::fprintf(outputFile, "=\n"
"{\n");
break;
}
case Node::Type::FunctionCallOperator: {
std::fprintf(outputFile, "$%s\n"
"{\n", node->data.identifier);
break;
}
case Node::Type::FunctionCallParameter: {
std::fprintf(outputFile, "function-call-parameter\n"
"{\n");
break;
}
case Node::Type::ReturnOperator: {
std::fprintf(outputFile, "return\n"
"{\n");
break;
}
case Node::Type::MathematicalOperator: {
std::fprintf(outputFile, "%s\n"
"{\n", node->data.mathematicalOperator);
break;
}
case Node::Type::Number: {
std::fprintf(outputFile, "%lg\n"
"{\n", node->data.number);
break;
}
}
dumpToTextRecursively(outputFile, node->left);
std::fprintf(outputFile, "}\n"
"{\n");
dumpToTextRecursively(outputFile, node->right);
std::fprintf(outputFile, "}\n");
}
AbstractSyntaxTree::Node *AbstractSyntaxTree::copySubtreeRecursively(const Node *node)
{
if (node == nullptr) return nullptr;
auto nodeCopy = (Node *) std::calloc(1, sizeof(Node));
nodeCopy->type = node->type;
nodeCopy->data = node->data;
nodeCopy->left = copySubtreeRecursively(node->left);
nodeCopy->right = copySubtreeRecursively(node->right);
return nodeCopy;
}
#if 0
AbstractSyntaxTree::Node *AbstractSyntaxTree::createNumberNode(double number)
{
auto node = (Node *) std::calloc(1, sizeof(Node));
}
#endif