-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
88 lines (73 loc) · 1.59 KB
/
main.cpp
File metadata and controls
88 lines (73 loc) · 1.59 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
#include <iostream>
#include <fstream>
#include "lexer.h"
#include "token.h"
#include "parser.h"
#include "interpreter.h"
using namespace std;
extern Memory memory;
extern short programExit;
int executeLine(char s[]){
Lexeme *lexlist = createLexemeList(s); //make lexeme list
Token *toklist = createTokenList(lexlist); //make token list
//unrecognized token present then toklist[0] has tokErr
if (toklist[0].name != tn_error){
treeNode *root = NULL;
//create parse tree
for (int i = 0; toklist[i].isValid(); i++)
root = insertToken(root, toklist[i]);
/* cout<<"Tok list:\n";
printTokenList(toklist);
cout<<"Post order:\n";
postOrder(root);
cout<<'\n';
*/
//check if there's a syntax error
//if there is, don't execute
if (syntax(root) != tn_error){
solve(root); //add .print() for debugging purpose
cout<<'\n';
}
else {
cout<<"syntax error\n";
return -1;
}
deleteTree(root);
}
else
return -1;
//clean up
destroyLexemeList(lexlist);
destroyTokenList(toklist);
}
int readfile(const char *filename){
char buffer1[1023];
ifstream file;
file.open(filename, ios::in);
if (!file.is_open()) {
cout << "File " << filename << " does not exist.\n";
return -1;
}
while(!file.eof()) {
file.getline(buffer1, 1023);
executeLine(buffer1);
}
return 0;
}
int main(){
cout<<"Do you want to run "<<filename<<" y/n: ";
char c;
cin>>c;
if (c == 'y'){
readfile(filename);
return 0;
}
cout<<"Running command line interpreter mode...\n\n";
while (!programExit){
char s[100];
cout<<"bell$ ";
cin.getline(s, 100);
executeLine(s);
}
return 0;
}