-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.c
More file actions
189 lines (146 loc) · 3.49 KB
/
misc.c
File metadata and controls
189 lines (146 loc) · 3.49 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
#include <time.h>
#include "cy.h"
static int eval_random(struct cy_token *t, struct cy_file *f)
{
t->v.t = CY_V_NUMBER;
t->v.v_i = random();
return 1;
}
static struct cy_type cy_random = { .ts = "random number", .eval = eval_random, };
static struct cy_type cy_const = { .ts = "constant", };
int try_resolve_cvalue(struct cy_token *t)
{
const char *val = t->val;
if (val[1] == '+' && val[2] == '\0') {
t->typ = &cy_const;
t->v.t = CY_V_BOOL;
t->v.v_bool = true;
return 1;
}
if (val[1] == '-' && val[2] == '\0') {
t->typ = &cy_const;
t->v.t = CY_V_BOOL;
t->v.v_bool = false;
return 1;
}
if (val[1] == '?' && val[2] == '\0') {
t->typ = &cy_random;
return 1;
}
return 0;
}
static int eval_in(struct cy_token *t, struct cy_file *f)
{
struct cy_token it, ct;
if (cy_eval_next(f, &it) <= 0)
return -1;
if (cy_eval_next(f, &ct) <= 0)
return -1;
t->v.t = CY_V_BOOL;
if (it.v.t == CY_V_LIST) {
t->v.v_bool = check_in_list(it.v.v_list, &ct.v);
return 1;
}
if (it.v.t == CY_V_MAP) {
;
/*
* What to check in map? Key or value? Py does check key,
* but that's equivalent to just dereferencing the map
* element. Also, checking value makes it similar to
* checking the list, by re-using the cy_compare() thing.
*/
}
show_token_err(t, "Bad object %s to check value in", vtype2s(it.v.t));
return -1;
}
static int eval_sizeof(struct cy_token *t, struct cy_file *f)
{
struct cy_token at;
if (cy_eval_next(f, &at) <= 0)
return -1;
t->v.t = CY_V_NUMBER;
if (at.v.t == CY_V_STRING) {
t->v.v_i = strlen(at.v.v_str);
return 1;
}
if (at.v.t == CY_V_LIST) {
struct cy_list_value *lv;
t->v.v_i = 0;
list_for_each_entry(lv, &at.v.v_list->h, l)
t->v.v_i++;
return 1;
}
if (at.v.t == CY_V_MAP) {
struct rb_node *n;
t->v.v_i = 0;
for (n = rb_first(&at.v.v_map->r); n; n = rb_next(n))
t->v.v_i++;
return 1;
}
show_token_err(t, "Can't get size of a %s", vtype2s(at.v.t));
return -1;
}
static int eval_default_value(struct cy_token *t, struct cy_file *f)
{
struct cy_token a, b;
if (cy_eval_next(f, &a) <= 0)
return -1;
if (cy_eval_next(f, &b) <= 0)
return -1;
t->v = (a.v.t != CY_V_NOVALUE ? a.v : b.v);
return 1;
}
static bool is_empty_value(struct cy_value *v)
{
switch (v->t) {
case CY_V_NOVALUE:
return true;
case CY_V_BOOL:
return !v->v_bool;
case CY_V_LIST:
return list_empty(&v->v_list->h);
case CY_V_MAP:
return RB_EMPTY_ROOT(&v->v_map->r);
case CY_V_STRING:
return v->v_str[0] == '\0';
case CY_V_NUMBER:
return v->v_i == 0;
}
return false;
}
static int eval_empty_is_novalue(struct cy_token *t, struct cy_file *f)
{
struct cy_token vt;
if (cy_eval_next(f, &vt) <= 0)
return -1;
if (!is_empty_value(&vt.v))
t->v = vt.v;
return 1;
}
static int eval_eval(struct cy_token *t, struct cy_file *f)
{
struct cy_token st;
if (cy_eval_next_x(f, &st, CY_V_STRING) <= 0)
return -1;
st.val = st.v.v_str;
st.v.t = CY_V_NOVALUE;
if (cy_resolve(&st) < 0)
return -1;
if (st.typ->eval && st.typ->eval(&st, f) <= 0)
return -1;
t->v = st.v;
return 1;
}
static struct cy_command cmd_misc[] = {
{ .name = "@", { .ts = "in", .eval = eval_in, }, },
{ .name = "$", .t = { .ts = "sizeof", .eval = eval_sizeof, }, },
{ .name = ".|", .t = { .ts = "default value", .eval = eval_default_value, }, },
{ .name = "-.", .t = { .ts = "no empty", .eval = eval_empty_is_novalue, }, },
{ .name = "`", .t = { .ts = "eval token", .eval = eval_eval, }, },
{}
};
void init_misc(void)
{
srand(time(NULL));
add_commands(cmd_misc);
}