-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.cpp
86 lines (81 loc) · 2.09 KB
/
interpreter.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "psl.hpp"
#include "lexerstr.hpp"
#include "syntaxerstr.hpp"
#include "rpn.hpp"
#include "intpretstr.hpp"
int lexer_work(
int fd,
const char *serv_ip,
int port,
const char *mode,
int lob_num,
const char *nick)
{
Lexer lxr;
lex_list *lexfirst;
RPNItem *RPNfirst = 0;
Bot mark(nick);
int c;
do {
c = getchar();
lxr.Run(c);
} while (c != -1);
lexfirst = lxr.GetList();
add_null_lexem(&lexfirst);
print_lexems(lexfirst);
if (lxr.GetMachineState() == error)
return 6;
try {
Syntaxer sntx(lexfirst);
sntx.Run();
RPNfirst = sntx.GetRPN();
mark.ConnectGame(serv_ip, port, mode, lob_num);
Interpreter intpr(&mark, RPNfirst);
intpr.Interpretation();
delete_RPNlist(RPNfirst);
delete_lexlist(lexfirst);
}
catch (SyntaxError syn_err) {
syn_err.Report();
}
catch (RPNError rpn_elem) {
rpn_elem.Report();
}
close (fd);
return 0;
}
int main(int argc, char **argv)
{
int fd, port, num;
if (argc != 7) {
fprintf(stderr, "Not enough arguments.\n"
"Note: %s <script> <server_ip> <port> "
"<mode> <lobby/num_players> "
"<nick>.\n", argv[0]);
return 1;
}
if ((port = is_number(argv[3])) == -1) {
fprintf(stderr, "Incorrect port.\n");
return 2;
}
if ((fd = use_file(argv[1])) == -1) {
fprintf(stderr, "Cannot use this file\n");
return 3;
}
if ((num = is_number(argv[5])) == -1) {
fprintf(stderr, "Invalid format of lobby/num_players.\n");
return 4;
}
if (strcmp("create", argv[4]) && strcmp("join", argv[4])) {
fprintf(stderr, "Invalid format of mode.\n");
return 5;
}
if (!correct_nick(argv[6])) {
fprintf(stderr, "Invalid format of nick.\n");
return 6;
}
return lexer_work(fd, argv[2], port, argv[4], num, argv[6]);
}