-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
65 lines (54 loc) · 1.01 KB
/
main.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
/*
* Ricky Palaguachi
* CS280
* Program 4
* main.h
*/
#include "lex.h"
#include "parse.h"
#include <fstream>
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
map<string, Val> myMap;
void runtime_err(int linenum, string errmsg){
string message = "RUNTIME ERROR at " + to_string(linenum) + ": " + errmsg;
throw message;
}
int main(int argc, char *argv[]) {
ifstream file;
istream *in;
int linenum = 0;
if( argc == 1 ) {
in = &cin;
}
else if( argc == 2 ) {
file.open(argv[1]);
if( file.is_open() == false ) {
cout << "COULD NOT OPEN " << argv[1] << endl;
return 1;
}
in = &file;
}
else {
cout << "TOO MANY FILENAMES" << endl;
return 1;
}
ParseTree *prog = Prog(*in, linenum);
if( prog == 0 )
return 0;
//check for undeclared identifiers
map<string,bool> declaredId;
if (prog->CheckLetBeforeUse(declaredId) > 0){
return 0;
}
//execute interpeter
try {
prog->Eval();
}
catch(string& e) {
cout << e << endl;
}
return 0;
}