-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonInterpreterSkeleton.cpp
More file actions
247 lines (198 loc) · 5.46 KB
/
Copy pathpythonInterpreterSkeleton.cpp
File metadata and controls
247 lines (198 loc) · 5.46 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
235
236
237
238
239
240
241
242
243
244
245
246
247
//Unsure if both streams are needed
#include<iostream>
#include<fstream>
#include<string>
#include<regex>
#include<vector>
#include<map>
#include<fstream>
using namespace std;
typedef enum { /*Types of statement*/ ASSIGN, PRINT, INVALID } StatementType;
typedef enum { /*Types of terminal*/KEYWORD, EQUALS, RELATIONAL_OP, ADDITIVE_OP, UNSIGNED_REAL, UNSIGNED_INT, QUOTE, OPEN_PAREN, CLOSE_PAREN, IDENTIFIER, EOL, BAD_TOKEN } TokenType;
typedef enum { /*valid dataTypes*/ NONE } DataType;
class Token{
public:
TokenType type;
string value;
Token(TokenType newType = EOL, string newValue = ""){
type = newType;
value = newValue;
}
};
class Statement{
StatementType Statetype;
string line;
map<string,string> Values;
unsigned int pos;
public:
Statement (StatementType state = INVALID){Statetype = state;}
// Changes what the line we are working on is and resets pos to 0
void nextLine(string newline){
Statetype = INVALID;
line = newline;
pos = 0;
}
// Get values from the map
auto getValue(string key){ return Values.find(key); }
// Add values to map
void addValue(string key,string value){ Values.insert({key,value}); }
// initialize first statement line
void start(string newline){
Statetype = INVALID;
line = newline;
pos = 0;
}
StatementType getType(){ return Statetype; }
void setPos(int newPos){ pos = newPos; }
void setType(StatementType type) {Statetype = type;}
Token peek(){
if (pos == line.length()) return Token(EOL);
smatch match;
string remaining = line.substr(pos);
while(regex_match(remaining, match, regex(" .*"))){
pos++;
remaining = line.substr(pos);
}
/*if(regex_match(remaining, match, regex("(<|<=|=|=>|>|<>).*")))
return Token(RELATIONAL_OP, match[1]);*/
if(regex_match(remaining, match, regex("(print|and|or|not|elif|else|if|while|break).*"))){
setType(PRINT);
return Token(KEYWORD, match[1]);
}
if(regex_match(remaining, match, regex("(=).*"))){
setType(ASSIGN);
return Token(EQUALS, match[1]);
}
if(regex_match(remaining, match, regex("(\\+|-|or).*")))
return Token(ADDITIVE_OP, match[1]);
if(regex_match(remaining, match, regex("([0-9]+\\.[0-9]+([eE][0-9]+)?).*")))
return Token(UNSIGNED_REAL, match[1]);
if(regex_match(remaining, match, regex("([0-9]+).*")))
return Token(UNSIGNED_INT, match[1]);
if(regex_match(remaining, match, regex("(\").*")))
return Token(QUOTE, match[1]);
if(regex_match(remaining, match, regex("(\\().*")))
return Token(OPEN_PAREN, match[1]);
if(regex_match(remaining, match, regex("(\\)).*")))
return Token(CLOSE_PAREN, match[1]);
if(regex_match(remaining, match, regex("(([a-z]|[A-Z])+).*")))
return Token(IDENTIFIER, match[1]);
return Token(BAD_TOKEN);
}
// peek ahead no increment no added token
Token peekyboi(){
Token t;
t = peek();
return t;
}
// Get next token and increment pos while adding token to vector
Token getToken(){
Token t;
t = peek();
pos += t.value.size();
addToken(t);
return t;
}
// find the previous token. HAVE NOT TESTED THIS YET
Token lastToken(Token t){
pos -= t.value.size();
t = peek();
return t;
}
};
class Parser{
Statement statement;
string error;
Token token, tempToken;
int parenDepth;
public:
void equals(){
token = statement.getToken();
Token t;
t = statement.peekyboi();
if (token.type == IDENTIFIER){
statement.addValue(tempToken.value,token.value);
}
if (t.type == EOL){
statement.nextLine("print(ourstring)");
token = statement.getToken();
if (token.type == KEYWORD){ keyword(); }
if (token.type == IDENTIFIER){ identifier(); }
}
}
void keyword (){
if(token.value == "print"){
token = statement.getToken();
if (token.type == OPEN_PAREN){ open_paren(); }
else error = "expected open paren";
}
}
void identifier (){
token = statement.getToken();
if (token.type == EQUALS){ equals(); }
else if (token.type == CLOSE_PAREN){ close_paren(); }
}
void string_literal(){
token = statement.getToken();
}
void open_paren (){
token = statement.getToken();
Token t;
t = statement.peekyboi();
if(token.type == QUOTE){}
if (t.type == IDENTIFIER){
tempToken = t;
identifier();
}
}
void close_paren (){
// TO DO FINISH THIS FUNCTION
if (statement.getType() == PRINT){
cout << statement.getValue(tempToken.value)->second << endl;
}
}
void parse(string input){
statement.start(input);
token = statement.getToken();
if (token.type == KEYWORD){ keyword(); }
if (token.type == IDENTIFIER){
tempToken = token;
identifier();
}
}
};
vector<string> testExpressions;
void loadTests(){
string test;
string tests[13] = {
"tests/test1.txt",
"tests/test2.txt",
"tests/test3.txt",
"tests/test4.txt",
"tests/test5.txt",
"tests/test6.txt",
"tests/test7.txt",
"tests/test8.txt",
"tests/test9.txt",
"tests/test10.txt",
"tests/test11.txt",
"tests/test12.txt"
};
ifstream inf;
for(int i = 0; i < 12; i++){
inf.open(tests[i]);
getline(inf,test);
testExpressions.push_back(test);
inf.close();
}
}
int main(){
Parser parser;
Interpreter interpreter;
loadTests();
for(int i = 0; i < testExpressions.size(); i++){
cout << "test " << i + 1 << ": " << testExpressions[i] << endl;
parser.parse(testExpressions[i]);
}
return 0;
}