-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
120 lines (103 loc) · 2.48 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
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
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "TypedClient.h"
//#include "ImpLexicalParser.h"
//#include "ImpSyntaxParser.h"
#include "SchLexicalParser.h"
#include "SchSyntaxParser.h"
#include "SpecialObjects.h"
#include "settings.h"
struct args_t * args;
void network_test() {
timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0.2;
TypedClient client(args->ip, args->port);
while(true){
sleep(1);
client.send("help\n");
client.check(timeout);
client.act();
}
}
int main(int argc, char ** argv) {
SchSyntaxParser sparser;
timeval timeout;
int fd;
timeout.tv_sec = 0;
timeout.tv_usec = 0.1;
args = parse_args(argc, argv);
if(args->script != NULL){
fd = open(args->script, O_RDONLY);
} else {
fd = STDIN_FILENO;
char tmp[] = "stdin";
args->script = (char *) malloc(sizeof(tmp));
strcpy(args->script, tmp);
}
try{
SchemeMachine vm(args->ip, args->port, timeout);
sparser.receive(fd);
sparser.reset();
SchObjectRef ref(SchObjectRef(sparser.trueParse()));
// network_test();
if(args->memtest){
int cnt = 0;
const int infinity = true;
while(infinity || cnt < 10000){
++cnt;
fprintf(stderr, "%d%c", cnt, (cnt%8)?'\t':'\n');
if(args->evaluate){
vm.addTask(new PrintTask(1));
vm.addTask(new EvalTask(ref));
} else {
vm.addTask(new ExecTask(ref, 1));
}
vm.launch();
} // memory leaks test
} else {
if(args->verbose){
printf("The following object will be %s:\n", (args->evaluate) ? "evaluated" : "executed");
ref->print();
printf("\n");
}
if(args->evaluate){
vm.addTask(new PrintTask(1));
vm.addTask(new EvalTask(ref));
} else {
vm.addTask(new ExecTask(ref, 1));
}
printf("SchemeMachine launch:\n");
vm.launch(args->verbose);
printf("\n");
}
}
catch(UnexpectedLexemError &e){
e.print(args->script, sparser.getLexString(e.getExpected()), sparser.getLexString(e.getGot()));
fprintf(stderr, "\n");
}
catch(UndefinedSymbolError &e){
e.print(args->script, sparser.getLexString(e.getId()));
fprintf(stderr, "\n");
}
catch(SomethingExpectedError &e){
e.print(args->script, sparser.getLexString(e.getLex()->id));
fprintf(stderr, "\n");
}
catch(Error &e){
e.print(args->script);
fprintf(stderr, "\n");
}
catch(...){
throw;
fprintf(stderr, "\nSomething was unexpected in the project. Contact depeloper\n");
}
/* Destructors */
delete args->ip;
delete args->script;
delete args;
return 0;
}