-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcond.c
More file actions
332 lines (264 loc) · 6.28 KB
/
cond.c
File metadata and controls
332 lines (264 loc) · 6.28 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "cy.h"
#define OP_EQ 1
#define OP_NE 2
#define OP_GE 3
#define OP_GT 4
#define OP_LE 5
#define OP_LT 6
static int cy_compare_t(struct cy_value *v1, struct cy_value *v2, int *cr)
{
switch (v1->t) {
case CY_V_NUMBER:
*cr = v1->v_i - v2->v_i;
return 0;
case CY_V_STRING:
*cr = strcmp(v1->v_str, v2->v_str);
return 0;
default:
return -1;
}
}
bool cy_compare(struct cy_value *v1, struct cy_value *v2)
{
int cr;
if (v1->t != v2->t)
return false;
if (cy_compare_t(v1, v2, &cr) < 0)
return false;
return cr == 0 ? true : false;
}
static int eval_compare(struct cy_token *t, struct cy_file *f)
{
int cr;
struct cy_token a, b;
if (cy_eval_next(f, &a) <= 0)
return -1;
if (cy_eval_next_x(f, &b, a.v.t) <= 0)
return -1;
t->v.t = CY_V_BOOL;
if (cy_compare_t(&a.v, &b.v, &cr) < 0) {
show_token_err(t, "Comparison of %ss not possible", vtype2s(a.v.t));
return -1;
}
switch (t->typ->priv) {
case OP_EQ: t->v.v_bool = (cr == 0); break;
case OP_NE: t->v.v_bool = (cr != 0); break;
case OP_GT: t->v.v_bool = (cr > 0); break;
case OP_GE: t->v.v_bool = (cr >= 0); break;
case OP_LT: t->v.v_bool = (cr < 0); break;
case OP_LE: t->v.v_bool = (cr <= 0); break;
}
return 1;
}
static int get_branch(struct cy_token *t, struct cy_file *f)
{
if (cy_eval_next(f, t) <= 0)
return -1;
if ((t->v.t != CY_V_CBLOCK) && !is_nop_token(t)) {
show_token_err(t, "Expected cblock/nop branch, got %s", vtype2s(t->v.t));
return -1;
}
return 1;
}
static int call_branch(struct cy_token *t, struct cy_file *f, struct cy_value *rv)
{
if (is_nop_token(t))
return 1;
else
return cy_call_cblock(t, f, rv);
}
static int eval_if(struct cy_token *t, struct cy_token *t_cond, struct cy_file *f)
{
int ret;
struct cy_token t_then, t_else;
if (get_branch(&t_then, f) <= 0)
return -1;
if (get_branch(&t_else, f) <= 0)
return -1;
if (t_cond->v.v_bool)
ret = call_branch(&t_then, f, &t->v);
else
ret = call_branch(&t_else, f, &t->v);
if (ret < 0)
return ret;
return 1;
}
static int cy_call_sblock(struct cy_token *ct, struct cy_file *f, struct cy_value *rv)
{
int ret;
struct cy_cblock *c_list;
struct cy_ctoken *c_nxt;
c_list = f->main;
c_nxt = f->nxt;
init_tokenizer(f, ct->v.v_cblk);
while (1) {
struct cy_token ct = {};
struct cy_token bt = {};
ret = cy_eval_next_x(f, &ct, CY_V_BOOL);
if (ret <= 0)
break;
ret = get_branch(&bt, f);
if (ret <= 0)
break;
if (ct.v.v_bool) {
ret = call_branch(&bt, f, rv);
break;
}
}
f->main = c_list;
f->nxt = c_nxt;
if (ret < 0)
return ret;
return 1;
}
static int eval_select(struct cy_token *t, struct cy_token *sbt, struct cy_file *f)
{
return cy_call_sblock(sbt, f, &t->v);
}
static int eval_cond(struct cy_token *t, struct cy_file *f)
{
struct cy_token tn;
if (cy_eval_next(f, &tn) <= 0)
return -1;
if (tn.v.t == CY_V_BOOL)
return eval_if(t, &tn, f);
if (tn.v.t == CY_V_CBLOCK)
return eval_select(t, &tn, f);
show_token_err(t, "Bad argument for condition");
return -1;
}
static int eval_list_loop(struct cy_token *t, struct cy_token *lt, struct cy_token *bt, struct cy_file *f)
{
int ret;
struct cy_list_value *lv;
list_for_each_entry(lv, <->v.v_list->h, l) {
struct cy_value rv = {};
set_cursor(&lv->v);
ret = call_branch(bt, f, &rv);
if (ret < 0)
return ret;
if (ret == 2) {
t->v = rv;
break;
}
/* XXX if someone removes an element from the list while
* we iterate it :(
*/
if (list_empty(&lv->l))
break;
}
set_cursor(NULL);
return 1;
}
static int eval_map_loop(struct cy_token *t, struct cy_token *mt, struct cy_token *bt, struct cy_file *f)
{
int ret;
struct rb_node *n;
for (n = rb_first(&mt->v.v_map->r); n != NULL; n = rb_next(n)) {
struct cy_map_value *mv;
struct cy_value rv = {};
struct cy_value cv = { .t = CY_V_STRING, };
mv = rb_entry(n, struct cy_map_value, n);
cv.v_str = mv->key;
set_cursor(&cv);
ret = call_branch(bt, f, &rv);
if (ret < 0)
return ret;
if (ret == 2) {
t->v = rv;
break;
}
}
set_cursor(NULL);
return 1;
}
static int eval_cblock_loop(struct cy_token *t, struct cy_token *bt, struct cy_file *f)
{
int ret;
while (1) {
struct cy_value rv = {};
ret = cy_call_cblock(bt, f, &rv);
if (ret <= 0)
return -1;
if (ret == 2) {
t->v = rv;
break;
}
}
return 1;
}
static int eval_nuber_loop(struct cy_token *t, struct cy_token *nt, struct cy_token *bt, struct cy_file *f)
{
int ret, start, stop, inc;
struct cy_value lc = { .t = CY_V_NUMBER, };
struct cy_value rv = {};
if (nt->v.v_i > 0) {
start = 0;
stop = nt->v.v_i;
inc = 1;
} else if (nt->v.v_i < 0) {
start = -nt->v.v_i;
stop = 0;
inc = -1;
} else
return 1;
for (lc.v_i = start; lc.v_i != stop; lc.v_i += inc) {
set_cursor(&lc);
ret = call_branch(bt, f, &rv);
if (ret < 0)
return ret;
if (ret == 2) {
t->v = rv;
break;
}
}
return 1;
}
static int eval_loop(struct cy_token *t, struct cy_file *f)
{
struct cy_token lt, bt;
if (cy_eval_next(f, <) <= 0)
return -1;
if (lt.v.t == CY_V_CBLOCK)
return eval_cblock_loop(t, <, f);
if (get_branch(&bt, f) <= 0)
return -1;
switch (lt.v.t) {
case CY_V_LIST:
return eval_list_loop(t, <, &bt, f);
case CY_V_MAP:
return eval_map_loop(t, <, &bt, f);
case CY_V_NUMBER:
return eval_nuber_loop(t, <, &bt, f);
}
show_token_err(t, "Bad loop object");
return -1;
}
static int eval_is_novalue(struct cy_token *t, struct cy_file *f)
{
struct cy_token nt;
if (cy_eval_next(f, &nt) <= 0)
return -1;
t->v.t = CY_V_BOOL;
t->v.v_bool = (nt.v.t == CY_V_NOVALUE);
return 1;
}
static struct cy_command cmd_compare[] = {
/* Checks */
{ .name = "==", { .ts = "eq", .eval = eval_compare, .priv = OP_EQ, }, },
{ .name = "!=", { .ts = "ne", .eval = eval_compare, .priv = OP_NE, }, },
{ .name = ">", { .ts = "gt", .eval = eval_compare, .priv = OP_GT, }, },
{ .name = ">=", { .ts = "ge", .eval = eval_compare, .priv = OP_GE, }, },
{ .name = "<", { .ts = "lt", .eval = eval_compare, .priv = OP_LT, }, },
{ .name = "<=", { .ts = "le", .eval = eval_compare, .priv = OP_LE, }, },
{ .name = "=.", .t = { .ts = "is novalue", .eval = eval_is_novalue, }, },
/* If-s */
{ .name = "?", { .ts = "condition", .eval = eval_cond, }, },
/* Loops */
{ .name = "~", { .ts = "loop", .eval = eval_loop, }, },
{},
};
void init_cond(void)
{
add_commands(cmd_compare);
}