-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.cpp
More file actions
234 lines (192 loc) · 4.23 KB
/
ast.cpp
File metadata and controls
234 lines (192 loc) · 4.23 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <cctype>
#define NUMBER "NUMBER"
#define ADDITION "ADDITION"
#define DIVISION "DIVISION"
#define MULTIPLICATION "MULTIPLICATION"
#define SUBSTRACTION "SUBSCTRACTION"
#define MODULUS "MODULUS"
using namespace std;
class String {
public:
string value;
int len;
String(){
this->value = "";
this->len = 0;
}
String(string value){
this->value = value;
this->len = value.length();
}
string sub_string(int start , int end){
string sub = "";
for(int i = start ; i < end ; i++){
sub = sub + value[i];
}
return sub;
}
char at(int i){
return value[i];
}
};
template <typename T>
class LinkedList {
T *head;
public:
LinkedList(){
this->head = NULL;
}
bool is_empty() {
return head == NULL;
}
void add_node(T *newNode){
if(is_empty()){
head = newNode;
} else{
T *tail = head;
while(tail->next){
tail = tail->next;
}
tail->next = newNode;
newNode->last = tail;
}
}
void print_nodes(){
cout << endl;
if(is_empty()) {
cout << "Linked list is empty" << endl;
return;
}
T *temp = this->head;
while(temp != NULL){
temp->print();
temp = temp->next;
}
}
};
class Token{
public:
Token *last;
Token *next;
char lexeme;
float value;
string token;
Token(char lexeme, int value, string token){
this->last = NULL;
this->next = NULL;
this->lexeme =lexeme;
this->value = value;
this->token = token;
}
void delete_from_list(){
Token *temp = this;
if(temp->next == NULL){
free(this);
return;
}
temp->last->next = temp->next;
temp->next->last = temp->last;
free(temp);
}
void print(){
cout <<"Lexeme: " << this->lexeme << "\tValue: " << this->value << "\tToken: " << this->token << endl;
}
};
namespace logger {
void error(string message, int pos){
cout << "Error on line 1 character " << pos << " :: " << "\"" << message << "\"" << endl;
}
void error(string message) {
cout << "Error " << "\"" << message << "\"" << endl;
}
}
class Scanner{
public:
int start = 0;
int current = 0;
int end;
LinkedList<Token> *tokens = new LinkedList<Token>();
String source;
Scanner(string source){
cout << "New scanner: " << source << endl;
this->source = String(source);
this->end = this->source.len;
}
// returns current character and increases the current;
char advance(){
current ++;
cout << current << ":" << source.at(current -1) << "\t";
return source.at(current -1);
}
// returns current character;
char peek(){
if(current == end) return '\0';
return source.at(current);
}
char peek_next(){
if(current+1 == end) return '\0';
return source.at(current +1);
}
void add_token(string type){
tokens->add_node(new Token(source.at(current-1), 0, type));
}
void add_token(string type, int value){
Token *token = new Token(source.at(current-1),value, type);
tokens->add_node(token);
}
void number(){
// checks if the 1st character is not the end of source and is also a digit
while(current != end && isdigit(peek())) advance();
// verifies if the next character is . and is also followed by a digit to make a valid floating number
if(peek() == '.' && isdigit(peek_next())) advance();
// keep moving the current ( pointer) unti we reach the end of the number
while(isdigit(peek())) advance();
add_token(NUMBER, stoi(String(source).sub_string(start, current)));
}
void scan_tokens(){
while(current != end){
start = current;
char c = advance();
switch(c){
case ' ':
break;
case '-':
this->add_token(SUBSTRACTION);
break;
case '/':
this->add_token(DIVISION);
break;
case '+':
this->add_token(ADDITION);
break;
case '*':
this->add_token(MULTIPLICATION);
break;
case '%':
this->add_token(MODULUS);
break;
default:
if(isdigit(c)){
this->number();
} else{
logger::error("Unexpected character ", current);
}
}
}
// Token *token = new Token('\o',0, EOF);
// tokens->add_node(token) ;
}
};
//class BinaryTree {
// public:
//
// BinaryTree()
//};
//float walk_the_tree
// calculate the value of 1+2*3+4;
int main(){
Scanner *scanner = new Scanner("22.4+ 2*3-5");
scanner->scan_tokens();
scanner->tokens->print_nodes();
}