This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminipython.cpp
82 lines (68 loc) · 1.69 KB
/
minipython.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
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include "tokens.h"
#include "lexer.h"
#include "parser.h"
#include "ast.h"
#include "global_scope.h"
#include "interpreter.h"
#include "error.h"
#include "DebugFuncs.h"
using namespace std;
bool inBlock = false;
int main(int argc, char *argv[]) {
/*====file input====*/
//check if input file is provided
if(argc < 2) {
cout << "minipython: no input file provided" << endl;
return 0;
}
string inFile = argv[1];
ifstream inputProgram(inFile);
//check if file exists
if(!inputProgram.is_open()) {
cout << "minipython: can't open file \'" << inFile << "\', no such file in directory" << endl;
inputProgram.close();
return 0;
}
/*==end file input==*/
/*====Interpreter====*/
try {
LexicalAnalyzer lexer;
Parser parse;
Interpreter interpret;
string line;
int lineCtr = 1;
while(getline(inputProgram, line)) {
/*====Lexical Analysis====*/
lexer.initialize(line, lineCtr);
lexer.tokenize();
if(inputProgram.peek() == EOF) {
lexer.addEndStmntTokenIfNecessary(true);
}
vector<Token> tokens = lexer.getTokens();
//representTokenList(tokens); //debug function
/*==end Lexical Analysis*/
/*====Parser====*/
ASTNode* tree = nullptr;
parse.initialize(tokens);
parse.parseAndCreateAST();
tree = parse.getAST();
//representAST(tree); cout << endl; //debug function
/*==end Parser==*/
/*====Code Interpreter====*/
interpret.initialize(tree);
interpret.evaluate();
/*==end Code Interpreter==*/
lineCtr++;
}
inputProgram.close();
}
catch(CreateProgramError& e) {
cout << e.what() << endl;
return -1;
}
/*==end Interpreter==*/
}