-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ypp
224 lines (198 loc) · 3.76 KB
/
parser.ypp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
%{
#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
#include<map>
#include<cstring>
#include<cstdio>
#include"userinfo.hpp"
#include"matrix.hpp"
#define show_powerline {std::cout << *username << "::" << *wdir << ":->";}
extern int yylex();
void yyerror(std::string s){
std::cerr << "Error occurred: " << s << std::endl;
}
bool undefined_var = false;
std::string* username;
std::string* wdir;
bool matlab_mode = false;
std::map<std::string, Matrix*> variable;
void is_matlab_mode_on(){
if (!matlab_mode){
std::cout << "Matlab mode active. Please see quick start in README.md for matlab."
<< std::endl
<< std::endl;
wdir = new std::string("matlab-mode");
matlab_mode = true;
}
}
%}
%union{
int int_type;
double double_type;
char* s;
Matrix* matrix_type;
std::vector<double>* p_vector;
std::string* string_type;
}
%left '+' '-'
%left '*' '/' '%'
%left UNMIN
%type<int_type> E
%type<double_type> number
%type<matrix_type> T array_rows
%type<p_vector> array_num row
%token<double_type> d_num_tok
%token<int_type> num_tok
%token<s> ls_flags_tok id_tok
%token<string_type> id_matrix_tok
%token clear_tok ls_tok pwd_tok chusername_tok exit_tok matlab_tok
%%
Program
: Niz_linija
;
Niz_linija
: Niz_linija wLinija
| wLinija
;
wLinija
: Linija '\n'{
show_powerline
}
| '\n'{
show_powerline
}
| error '\n' {
/* TODO - show more userfriendly message */
std::cout << "It has come to an error. Please rephrase.." << std::endl;
show_powerline
}
| exit_tok {
if (matlab_mode){
std::cout << "Exit matlab mode."
<< std::endl
<< std::endl;
wdir = getworkingdir(username);
matlab_mode = false;
}else{
std::cerr << "exit" << std::endl;
exit(EXIT_SUCCESS);
}
}
| matlab_tok '\n' {
if (!matlab_mode){
std::cout << "Matlab mode active. Please see quick start in README.md for matlab."
<< std::endl
<< std::endl;
wdir = new std::string("matlab-mode");
matlab_mode = true;
}
show_powerline
}
;
Linija
: E {std::cout << $1 << std::endl;}
| clear_tok {clear();}
| ls_tok ls_flags_tok {
/* TODO - implement with system functions without system()*/
if (strlen($2) > 10){
std::cout << "Too many flags sent.."<< std::endl;
}else{
char buffer[11];
snprintf(buffer, sizeof(buffer), "ls %s", $2);
system(buffer);
}
}
| ls_tok {system("ls");}
| pwd_tok {system("pwd");}
| chusername_tok id_tok {
delete username;
username = new std::string($2);
}
| T {
// this is matrix line recognition
is_matlab_mode_on();
if (!undefined_var){
std::cout << *$1 << std::endl;
delete $1;
}
}
| id_matrix_tok '=' T {
is_matlab_mode_on();
variable[*$1] = $3;
}
;
E
: E '+' E {$$ = $1 + $3;}
| E '-' E {$$ = $1 - $3;}
| E '*' E {$$ = $1 * $3;}
| E '/' E {$$ = $1 / $3;}
| E '%' E {$$ = $1 % $3;}
| '(' E ')' {
$$ = $2;
}
| '-' E %prec UNMIN {$$ = -$2;}
| num_tok {
$$ = $1;
}
;
T
: '[' array_rows ']' {
$$ = $2;
}
| id_matrix_tok {
auto finder = variable.find(*$1);
if (finder == variable.end()){
std::cerr << "Variable '" << *$1 << "' is not defined." << std::endl;
undefined_var = true;
}else{
$$ = new Matrix(*finder->second);
undefined_var = false;
}
}
;
array_rows
: array_rows ';' row {
$$->add_to_matrix(*$3);
delete $3;
}
| row {
Matrix* m = new Matrix();
m->add_to_matrix(*$1);
$$ = m;
}
;
row
: '[' array_num ']' {
$$ = $2;
}
;
array_num
: array_num ',' number {
$$->push_back($3);
}
| number {
std::vector<double>* new_vector = new std::vector<double>();
new_vector->push_back($1);
$$ = new_vector;
}
;
number
: num_tok {
$$ = $1;
}
| d_num_tok {
$$ = $1;
}
;
%%
int main(){
clear();
username = getuserinfo();
wdir = getworkingdir(username);
show_powerline
yyparse();
delete username;
exit(EXIT_SUCCESS);
}