-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharr_lib.c
More file actions
287 lines (226 loc) · 4.79 KB
/
arr_lib.c
File metadata and controls
287 lines (226 loc) · 4.79 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Saol interpreter library
* v1.4
* 02.01.2020
* by Centrix
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "arr_lib.h"
#include "unitok/unitok.h"
#define MAX 2048
int arr[MAX];
int pos = 0;
int arg_type = 0; // 0 - integer, 1 - character
int start = 0, end = MAX, len = 32, pred = 1, stop = 0;
void ini(char *arg) {
if (!strcmp(arg, "il")) {
arr[pos] = end - start - 1;
return ;
}
else if (isint(arg))
arr[pos] = atoi(arg);
else
arr[pos] = arg[0];
}
void addr(char *arg) {
if (isint(arg) && atoi(arg) < MAX)
pos = atoi(arg);
}
void shr(char *arg) {
pos++;
}
void shl(char *arg) {
pos--;
}
void show_elm(char *arg) {
if (!strcmp(arg, "_!")) {
if (!arg_type)
printf("%d", arr[pos]);
else
putc(arr[pos], stdout);
}
}
void show_arr(char *arg) {
if (!strcmp(arg, "-!")) {
for (int i = start; i < end; i++) {
if (!arg_type)
printf("%d", arr[i]);
else
putc(arr[i], stdout);
}
}
}
void comment(char *arg) {}
void filler(char *arg) {
int i = 0;
if (isint(arg)) arr[pos++] = atoi(arg);
else {
if (!strcmp(arg, "|")) return ;
while (arg[i]) {
arr[pos++] = arg[i++];
}
}
}
void int_mode(char *arg) {
arg_type = 0;
}
void char_mode(char *arg) {
arg_type = 1;
}
void sum(char *arg) {
for (int i = start; i < end; i++)
arr[MAX-1] += arr[i];
}
void subt(char *arg) {
for (int i = start; i < end; i++)
arr[MAX-1] -= arr[i];
}
void mult(char *arg) {
for (int i = start; i < end; i++)
arr[MAX-1] *= arr[i];
}
void idiv(char *arg) {
for (int i = start; i < end; i++) {
if (arr[i])
arr[MAX-1] /= arr[i];
}
}
void cla(char *arg) {
if (!strcmp(arg, "#")) return ;
for (int i = start; i < end; i++) {
if (!arg_type)
arr[i] = atoi(arg);
else
arr[i] = arg[0];
}
}
void pstart(char *arg) {
if (isint(arg)) start = atoi(arg);
}
void pend(char *arg) {
if (isint(arg)) end = atoi(arg);
}
void assig(char *arg) {
if (!strcmp(arg, ":-")) return ;
if (isint(arg)) arr[atoi(arg)] = arr[pos];
}
void notassig(char *arg) {
if (!strcmp(arg, "!-")) return ;
if (isint(arg)) arr[atoi(arg)] = !arr[pos];
}
void andf(char *arg) {
for (int i = start; i < end; i++) {
arr[MAX-1] &= arr[i];
}
}
void orf(char *arg) {
for (int i = start; i < end; i++) {
arr[MAX-1] |= arr[i];
}
}
void predf(char *arg) {
pred = arr[MAX-1];
}
void unpred(char *arg) {
pred = !pred;
}
void iter(char *arg) {
char *tok = NULL;
int code = -1, indx = 0;
if (!strcmp(arg, "|~")) return ;
for (int i = start; i < end; i++) {
pos = i;
indx = 0;
tok = get_token(arg, &indx);
while (tok != NULL && !stop) {
if (is_kw(arg) >= 0)
code = is_kw(arg);
if (!is_empty(arg))
exec(code, arg);
tok = get_token(arg, &indx);
}
}
stop = 0;
}
void swap(char *arg) {
int indx = 0;
if (!strcmp(arg, "<->")) return ;
if (isint(arg)) {
indx = atoi(arg);
arr[pos] ^= arr[indx];
arr[indx] ^= arr[pos];
arr[pos] ^= arr[indx];
}
}
void inc(char *arg) {
int c = getc(stdin);
if (c == '\n') stop = 1;
arr[pos] = c;
}
void is_bigger(char *arg) {
if (!strcmp(arg, ">?")) return ;
if (isint(arg)) {
if (arr[pos] > atoi(arg)) pred = 1;
else pred = 0;
}
}
void is_smaller(char *arg) {
if (!strcmp(arg, "<?")) return ;
if (isint(arg)) {
if (arr[pos] < atoi(arg)) pred = 1;
else pred = 0;
}
}
void is_bigger_or_eq(char *arg) {
if (!strcmp(arg, ">=?")) return ;
if (isint(arg)) {
if (arr[pos] >= atoi(arg)) pred = 1;
else pred = 0;
}
}
void is_smaller_or_eq(char *arg) {
if (!strcmp(arg, "<=?")) return ;
if (isint(arg)) {
if (arr[pos] <= atoi(arg)) pred = 1;
else pred = 0;
}
}
void is_eq(char *arg) {
if (!strcmp(arg, "=?")) return ;
if (isint(arg)) {
if (arr[pos] == atoi(arg)) pred = 1;
else pred = 0;
}
}
void is_not_eq(char *arg) {
if (!strcmp(arg, "!=?")) return ;
if (isint(arg)) {
if (arr[pos] != atoi(arg)) pred = 1;
else pred = 0;
}
}
int need_exec(int code) {
if ((code > 9 && code < 14) || code > 18) return 1;
else if (pred) return 1;
else return 0;
}
int is_empty(char *word) {
int i = 0;
while (word[i])
if (!isspace(word[i++])) return 0;
return 1;
}
int is_kw(char *word) {
char *kws[] = {":", "^", "<", ">", "_!", "~", "|", "-!", ";", "@", "+", "\'", "*", "/", "#", "[", "]", ":-", "!-", "&", "\\", "?~", "?!", "|~", "<->", "\"", ">?", "<?", ">=?", "<=?", "=?", "!=?"};
for (int i = 0; i < len; i++) {
if (!strcmp(kws[i], word)) return i;
}
return -1;
}
void exec(int arr_index, char *arg) {
void (*arr[])(char *) = {ini, addr, shl, shr, show_elm, comment, filler, show_arr, int_mode, char_mode, sum, subt, mult, idiv, cla, pstart, pend, assig, notassig, andf, orf, predf, unpred, iter, swap, inc, is_bigger, is_smaller, is_bigger_or_eq, is_smaller_or_eq, is_eq, is_not_eq};
if (arr_index >= 0 && arr_index < len && need_exec(arr_index)) (*arr[arr_index])(arg);
}