-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
435 lines (386 loc) · 13.6 KB
/
main.cpp
File metadata and controls
435 lines (386 loc) · 13.6 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
* @file main.cpp
* @brief GLSL Compiler for educational purposes
* @date 2024-06-16
* @license MIT License
*
* This program simulates a simple compiler that interprets and executes
* commands from a .code file based on a custom language specification,
* using input values from a .input file.
*
* @author techotaku@zs, WHU
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <unordered_map>
#include <sstream>
#include <cctype>
#include <stdexcept>
#include <memory>
#include <filesystem>
// Token types enumeration: Defines the types of tokens in the source language
enum class TokenType {
IDENTIFIER, NUMBER,
ASSIGN, PRINT, INPUT,
IF, THEN, ENDIF,
COMPARE_OP, CALCULATE_OP,
SEMICOLON, LPAREN, RPAREN,
END
};
// Token structure: Represents a lexical token with a type and value
struct Token {
TokenType type;
std::string value;
};
// Lexer class: Tokenizes input source code
class Lexer {
public:
explicit Lexer(const std::string& source) : sourceCode(source), position(0) {}
std::vector<Token> tokenize() {
std::vector<Token> tokens;
while (position < sourceCode.size()) {
char current = sourceCode[position];
if (std::isspace(current)) {
++position;
}
else if (std::isalpha(current)) {
tokens.push_back(readIdentifier());
}
else if (std::isdigit(current)) {
tokens.push_back(readNumber());
}
else {
tokens.push_back(readSingleCharToken(current));
}
}
tokens.push_back({ TokenType::END, "" });
return tokens;
}
private:
std::string sourceCode;
size_t position;
Token readIdentifier() {
size_t start = position;
while (position < sourceCode.size() && std::isalnum(sourceCode[position])) {
++position;
}
std::string value = sourceCode.substr(start, position - start);
TokenType type = TokenType::IDENTIFIER;
if (value == "print") type = TokenType::PRINT;
else if (value == "input") type = TokenType::INPUT;
else if (value == "if") type = TokenType::IF;
else if (value == "then") type = TokenType::THEN;
else if (value == "endif") type = TokenType::ENDIF;
return { type, value };
}
Token readNumber() {
size_t start = position;
while (position < sourceCode.size() && (std::isdigit(sourceCode[position]) || sourceCode[position] == '.')) {
++position;
}
return { TokenType::NUMBER, sourceCode.substr(start, position - start) };
}
Token readSingleCharToken(char current) {
switch (current) {
case '=':
return handleCompareOrAssign();
case '>': case '<': case '!':
return handleCompareOperator();
case '+': case '-': case '*':
++position;
return { TokenType::CALCULATE_OP, std::string(1, current) };
case '(':
++position;
return { TokenType::LPAREN, "(" };
case ')':
++position;
return { TokenType::RPAREN, ")" };
case ';':
++position;
return { TokenType::SEMICOLON, ";" };
default:
throw std::runtime_error("Unexpected character: " + std::string(1, current));
}
}
Token handleCompareOrAssign() {
if (sourceCode[position + 1] == '=') {
position += 2;
return { TokenType::COMPARE_OP, "==" };
}
++position;
return { TokenType::ASSIGN, "=" };
}
Token handleCompareOperator() {
char op = sourceCode[position];
++position;
if (sourceCode[position] == '=') {
++position;
return { TokenType::COMPARE_OP, std::string(1, op) + "=" };
}
return { TokenType::COMPARE_OP, std::string(1, op) };
}
};
// AST nodes: Define the structure of the AST, with each node type corresponding to constructs like statements and expressions.
struct ASTNode {
virtual ~ASTNode() = default;
};
struct Expression : ASTNode {};
struct Statement : ASTNode {};
using ASTNodePtr = std::unique_ptr<ASTNode>;
using ExprPtr = std::unique_ptr<Expression>;
using StmtPtr = std::unique_ptr<Statement>;
struct Program : ASTNode {
std::vector<StmtPtr> statements;
};
struct AssignStatement : Statement {
std::string identifier;
ExprPtr expression;
AssignStatement(std::string id, ExprPtr expr)
: identifier(std::move(id)), expression(std::move(expr)) {}
};
struct PrintStatement : Statement {
ExprPtr expression;
explicit PrintStatement(ExprPtr expr) : expression(std::move(expr)) {}
};
struct InputStatement : Statement {
std::string identifier;
explicit InputStatement(std::string id) : identifier(std::move(id)) {}
};
struct IfStatement : Statement {
ExprPtr compareExpression;
std::vector<StmtPtr> thenStatements;
IfStatement(ExprPtr compExpr, std::vector<StmtPtr> thenStmts)
: compareExpression(std::move(compExpr)), thenStatements(std::move(thenStmts)) {}
};
struct BinaryOperation : Expression {
std::string op;
ExprPtr left;
ExprPtr right;
BinaryOperation(std::string oper, ExprPtr lhs, ExprPtr rhs)
: op(std::move(oper)), left(std::move(lhs)), right(std::move(rhs)) {}
};
struct Identifier : Expression {
std::string name;
explicit Identifier(std::string id) : name(std::move(id)) {}
};
struct Number : Expression {
std::string value;
explicit Number(std::string val) : value(std::move(val)) {}
};
// Parser class: Parses tokens into an AST
class Parser {
public:
explicit Parser(const std::vector<Token>& tokens) : tokens(tokens), position(0) {}
std::unique_ptr<Program> parse() {
auto program = std::make_unique<Program>();
while (currentToken().type != TokenType::END) {
program->statements.push_back(parseStatement());
}
return program;
}
private:
const std::vector<Token>& tokens;
size_t position;
Token currentToken() const {
return tokens[position];
}
Token consume(TokenType type) {
if (currentToken().type != type) {
throw std::runtime_error("Unexpected token: " + currentToken().value);
}
return tokens[position++];
}
// Parse a statement
StmtPtr parseStatement() {
if (currentToken().type == TokenType::IF) {
return parseIfStatement();
}
else {
auto stmt = parseSimpleStatement();
consume(TokenType::SEMICOLON);
return stmt;
}
}
// Parse an if statement
StmtPtr parseIfStatement() {
consume(TokenType::IF);
auto condition = parseExpression();
consume(TokenType::THEN);
std::vector<StmtPtr> thenStatements;
while (currentToken().type != TokenType::ENDIF) {
thenStatements.push_back(parseStatement());
}
consume(TokenType::ENDIF);
consume(TokenType::SEMICOLON);
return std::make_unique<IfStatement>(IfStatement{ std::move(condition), std::move(thenStatements) });
}
// Parse a simple statement
StmtPtr parseSimpleStatement() {
if (currentToken().type == TokenType::IDENTIFIER) {
return parseAssignStatement();
}
else if (currentToken().type == TokenType::PRINT) {
return parsePrintStatement();
}
else if (currentToken().type == TokenType::INPUT) {
return parseInputStatement();
}
else {
throw std::runtime_error("Unexpected simple statement");
}
}
// Parse an assignment statement
StmtPtr parseAssignStatement() {
std::string identifier = consume(TokenType::IDENTIFIER).value;
consume(TokenType::ASSIGN);
auto expression = parseExpression();
return std::make_unique<AssignStatement>(AssignStatement{ identifier, std::move(expression) });
}
// Parse a print statement
StmtPtr parsePrintStatement() {
consume(TokenType::PRINT);
consume(TokenType::LPAREN);
auto expression = parseExpression();
consume(TokenType::RPAREN);
return std::make_unique<PrintStatement>(PrintStatement{ std::move(expression) });
}
// Parse an input statement
StmtPtr parseInputStatement() {
consume(TokenType::INPUT);
consume(TokenType::LPAREN);
std::string identifier = consume(TokenType::IDENTIFIER).value;
consume(TokenType::RPAREN);
return std::make_unique<InputStatement>(InputStatement{ identifier });
}
// Parse an expression
ExprPtr parseExpression() {
auto left = parsePrimary();
while (currentToken().type == TokenType::COMPARE_OP || currentToken().type == TokenType::CALCULATE_OP) {
std::string op = consume(currentToken().type).value;
auto right = parsePrimary();
left = std::make_unique<BinaryOperation>(BinaryOperation{ op, std::move(left), std::move(right) });
}
return left;
}
// Parse a primary expression
ExprPtr parsePrimary() {
if (currentToken().type == TokenType::IDENTIFIER) {
return std::make_unique<Identifier>(Identifier{ consume(TokenType::IDENTIFIER).value });
}
else if (currentToken().type == TokenType::NUMBER) {
return std::make_unique<Number>(Number{ consume(TokenType::NUMBER).value });
}
else if (currentToken().type == TokenType::LPAREN) {
consume(TokenType::LPAREN);
auto expression = parseExpression();
consume(TokenType::RPAREN);
return expression;
}
else {
throw std::runtime_error("Unexpected primary expression");
}
}
};
// Interpreter class: Executes the AST
class Interpreter {
public:
Interpreter(Program* program, const std::vector<int>& inputs)
: program(program), inputs(inputs), inputIndex(0) {}
void interpret() {
for (auto& statement : program->statements) {
execute(statement.get());
}
}
private:
Program* program;
std::vector<int> inputs;
size_t inputIndex;
std::unordered_map<std::string, int> variables;
// Execute a statement
void execute(Statement* statement) {
if (auto assignStmt = dynamic_cast<AssignStatement*>(statement)) {
int value = evaluate(assignStmt->expression.get());
variables[assignStmt->identifier] = value;
}
else if (auto printStmt = dynamic_cast<PrintStatement*>(statement)) {
int value = evaluate(printStmt->expression.get());
std::cout << value << std::endl;
}
else if (auto inputStmt = dynamic_cast<InputStatement*>(statement)) {
variables[inputStmt->identifier] = inputs[inputIndex++];
}
else if (auto ifStmt = dynamic_cast<IfStatement*>(statement)) {
if (evaluate(ifStmt->compareExpression.get())) {
for (auto& stmt : ifStmt->thenStatements) {
execute(stmt.get());
}
}
}
else {
throw std::runtime_error("Unexpected statement");
}
}
// Evaluate an expression
int evaluate(Expression* expression) {
if (auto binOp = dynamic_cast<BinaryOperation*>(expression)) {
int left = evaluate(binOp->left.get());
int right = evaluate(binOp->right.get());
if (binOp->op == "+") return left + right;
if (binOp->op == "-") return left - right;
if (binOp->op == "*") return left * right;
if (binOp->op == ">") return left > right;
if (binOp->op == "<") return left < right;
if (binOp->op == "==") return left == right;
if (binOp->op == "!=") return left != right;
if (binOp->op == ">=") return left >= right;
if (binOp->op == "<=") return left <= right;
throw std::runtime_error("Unexpected binary operator: " + binOp->op);
}
else if (auto ident = dynamic_cast<Identifier*>(expression)) {
return variables.at(ident->name);
}
else if (auto num = dynamic_cast<Number*>(expression)) {
return std::stoi(num->value);
}
else {
throw std::runtime_error("Unexpected expression");
}
}
};
// Main function: Entry point of the program
int main() {
// Commented out for deployment; Uncomment for debugging purposes
// std::cout << "Current working directory: " << std::filesystem::current_path() << std::endl;
// Open code and input files
// Note: Ensure that test.code and test.input are in the build directory when using cmake for compilation.
std::ifstream codeFile("test.code");
std::ifstream inputFile("test.input");
// Check if files are opened successfully
if (!codeFile) {
std::cerr << "Error opening 'test.code'." << std::endl;
return 1;
}
if (!inputFile) {
std::cerr << "Error opening 'test.input'." << std::endl;
return 1;
}
// Read code and input files
std::string code((std::istreambuf_iterator<char>(codeFile)), std::istreambuf_iterator<char>());
std::string inputLine;
std::vector<int> inputs;
while (std::getline(inputFile, inputLine)) {
inputs.push_back(std::stoi(inputLine));
}
// Tokenize the code
Lexer lexer(code);
auto tokens = lexer.tokenize();
// Parse the tokens into an AST
Parser parser(tokens);
auto program = parser.parse();
// Interpret the AST
Interpreter interpreter(program.get(), inputs);
interpreter.interpret();
return 0;
}