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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/*
* Copyright (C) 2012-2024 Robin Haberkorn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#include "sciteco.h"
#include "error.h"
#include "undo.h"
#include "qreg.h"
#include "expressions.h"
/*
* Number and operator stacks are static, so
* they can be passed to the undo token constructors.
* This is OK since we're currently singleton.
*/
static GArray *teco_numbers;
TECO_DEFINE_ARRAY_UNDO_INSERT_VAL(teco_numbers, teco_int_t);
TECO_DEFINE_ARRAY_UNDO_REMOVE_INDEX(teco_numbers);
static GArray *teco_operators;
TECO_DEFINE_ARRAY_UNDO_INSERT_VAL(teco_operators, teco_operator_t);
TECO_DEFINE_ARRAY_UNDO_REMOVE_INDEX(teco_operators);
static gboolean teco_expressions_calc(GError **error);
static void __attribute__((constructor))
teco_expressions_init(void)
{
teco_numbers = g_array_sized_new(FALSE, FALSE, sizeof(teco_int_t), 1024);
teco_operators = g_array_sized_new(FALSE, FALSE, sizeof(teco_operator_t), 1024);
}
/** Get operator precedence */
static inline gint
teco_expressions_precedence(teco_operator_t op)
{
return op >> 4;
}
gint teco_num_sign = 1;
void
teco_expressions_push_int(teco_int_t number)
{
while (teco_operators->len > 0 && teco_expressions_peek_op(0) == TECO_OP_NEW)
teco_expressions_pop_op(0);
teco_expressions_push_op(TECO_OP_NUMBER);
if (teco_num_sign < 0) {
teco_set_num_sign(1);
number *= -1;
}
g_array_append_val(teco_numbers, number);
undo__remove_index__teco_numbers(teco_numbers->len-1);
}
teco_int_t
teco_expressions_peek_num(guint index)
{
return g_array_index(teco_numbers, teco_int_t, teco_numbers->len - 1 - index);
}
teco_int_t
teco_expressions_pop_num(guint index)
{
teco_int_t n = 0;
G_GNUC_UNUSED teco_operator_t op = teco_expressions_pop_op(0);
g_assert(op == TECO_OP_NUMBER);
if (teco_numbers->len > 0) {
n = teco_expressions_peek_num(index);
undo__insert_val__teco_numbers(teco_numbers->len - 1 - index, n);
g_array_remove_index(teco_numbers, teco_numbers->len - 1 - index);
}
return n;
}
gboolean
teco_expressions_pop_num_calc(teco_int_t *ret, teco_int_t imply, GError **error)
{
if (!teco_expressions_eval(FALSE, error))
return FALSE;
if (teco_num_sign < 0)
teco_set_num_sign(1);
teco_int_t v = teco_expressions_args() > 0 ? teco_expressions_pop_num(0) : imply;
if (ret)
*ret = v;
return TRUE;
}
void
teco_expressions_add_digit(gunichar digit, teco_qreg_t *qreg)
{
/*
* FIXME: We could just access qreg->integer here since
* we can assume that "^R" is a plain register.
*/
assert(qreg != NULL);
teco_int_t radix = 10;
qreg->vtable->get_integer(qreg, &radix, NULL);
teco_int_t n = teco_expressions_args() > 0 ? teco_expressions_pop_num(0) : 0;
/* use g_unichar_digit_value()? */
teco_expressions_push(n*radix + (n < 0 ? -1 : 1)*((gint)digit - '0'));
}
void
teco_expressions_push_op(teco_operator_t op)
{
g_array_append_val(teco_operators, op);
undo__remove_index__teco_operators(teco_operators->len-1);
}
gboolean
teco_expressions_push_calc(teco_operator_t op, GError **error)
{
gint first = teco_expressions_first_op();
/* calculate if op has lower precedence than op on stack */
if (first >= 0 &&
teco_expressions_precedence(op) <= teco_expressions_precedence(teco_expressions_peek_op(first)) &&
!teco_expressions_calc(error))
return FALSE;
teco_expressions_push_op(op);
return TRUE;
}
teco_operator_t
teco_expressions_peek_op(guint index)
{
return g_array_index(teco_operators, teco_operator_t, teco_operators->len - 1 - index);
}
teco_operator_t
teco_expressions_pop_op(guint index)
{
teco_operator_t op = TECO_OP_NIL;
if (teco_operators->len > 0) {
op = teco_expressions_peek_op(index);
undo__insert_val__teco_operators(teco_operators->len - 1 - index, op);
g_array_remove_index(teco_operators, teco_operators->len - 1 - index);
}
return op;
}
static gboolean
teco_expressions_calc(GError **error)
{
teco_int_t result;
if (!teco_operators->len || teco_expressions_peek_op(0) != TECO_OP_NUMBER) {
g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED,
"Missing right operand");
return FALSE;
}
teco_int_t vright = teco_expressions_pop_num(0);
teco_operator_t op = teco_expressions_pop_op(0);
if (!teco_operators->len || teco_expressions_peek_op(0) != TECO_OP_NUMBER) {
g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED,
"Missing left operand");
return FALSE;
}
teco_int_t vleft = teco_expressions_pop_num(0);
switch (op) {
case TECO_OP_POW:
if (!vright) {
result = vleft < 0 ? -1 : 1;
break;
}
if (vright < 0) {
if (!vleft) {
g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED,
"Negative power of 0 is not defined");
return FALSE;
}
result = ABS(vleft) == 1 ? vleft : 0;
break;
}
result = 1;
for (;;) {
if (vright & 1)
result *= vleft;
vright >>= 1;
if (!vright)
break;
vleft *= vleft;
}
break;
case TECO_OP_MUL:
result = vleft * vright;
break;
case TECO_OP_DIV:
if (!vright) {
g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED,
"Division by zero");
return FALSE;
}
result = vleft / vright;
break;
case TECO_OP_MOD:
if (!vright) {
g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED,
"Remainder of division by zero");
return FALSE;
}
result = vleft % vright;
break;
case TECO_OP_ADD:
result = vleft + vright;
break;
case TECO_OP_SUB:
result = vleft - vright;
break;
case TECO_OP_AND:
result = vleft & vright;
break;
case TECO_OP_XOR:
result = vleft ^ vright;
break;
case TECO_OP_OR:
result = vleft | vright;
break;
default:
/* shouldn't happen */
g_assert_not_reached();
}
teco_expressions_push(result);
return TRUE;
}
gboolean
teco_expressions_eval(gboolean pop_brace, GError **error)
{
for (;;) {
gint n = teco_expressions_first_op();
if (n < 0)
break;
teco_operator_t op = teco_expressions_peek_op(n);
if (op == TECO_OP_BRACE) {
if (pop_brace)
teco_expressions_pop_op(n);
break;
}
if (n < 1)
break;
if (!teco_expressions_calc(error))
return FALSE;
}
return TRUE;
}
guint
teco_expressions_args(void)
{
guint n = 0;
while (n < teco_operators->len && teco_expressions_peek_op(n) == TECO_OP_NUMBER)
n++;
return n;
}
gint
teco_expressions_first_op(void)
{
for (guint i = 0; i < teco_operators->len; i++) {
switch (teco_expressions_peek_op(i)) {
case TECO_OP_NUMBER:
case TECO_OP_NEW:
break;
default:
return i;
}
}
return -1; /* no operator */
}
gboolean
teco_expressions_discard_args(GError **error)
{
if (!teco_expressions_eval(FALSE, error))
return FALSE;
for (guint i = teco_expressions_args(); i; i--)
if (!teco_expressions_pop_num_calc(NULL, 0, error))
return FALSE;
return TRUE;
}
/** The nesting level of braces */
guint teco_brace_level = 0;
void
teco_expressions_brace_open(void)
{
while (teco_operators->len > 0 && teco_expressions_peek_op(0) == TECO_OP_NEW)
teco_expressions_pop_op(0);
teco_expressions_push_op(TECO_OP_BRACE);
teco_undo_guint(teco_brace_level)++;
}
gboolean
teco_expressions_brace_return(guint keep_braces, guint args, GError **error)
{
/*
* FIXME: Allocating on the stack might be dangerous.
*/
teco_int_t return_numbers[args];
for (guint i = args; i; i--)
return_numbers[i-1] = teco_expressions_pop_num(0);
teco_undo_guint(teco_brace_level);
while (teco_brace_level > keep_braces) {
if (!teco_expressions_discard_args(error) ||
!teco_expressions_eval(TRUE, error))
return FALSE;
teco_brace_level--;
}
for (guint i = 0; i < args; i++)
teco_expressions_push(return_numbers[i]);
return TRUE;
}
gboolean
teco_expressions_brace_close(GError **error)
{
if (!teco_brace_level) {
g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED,
"Missing opening brace");
return FALSE;
}
teco_undo_guint(teco_brace_level)--;
return teco_expressions_eval(TRUE, error);
}
void
teco_expressions_clear(void)
{
g_array_set_size(teco_numbers, 0);
g_array_set_size(teco_operators, 0);
teco_brace_level = 0;
}
/**
* Format a TECO integer as the `\` command would.
*
* @param buffer The output buffer of at least TECO_EXPRESSIONS_FORMAT_LEN characters.
* The output string will be null-terminated.
* @param number The number to format.
* @param table The local Q-Register table that contains the appropriate radix register (^R).
* @return A pointer into buffer to the beginning of the formatted number.
*/
gchar *
teco_expressions_format(gchar *buffer, teco_int_t number, teco_qreg_t *qreg)
{
assert(qreg != NULL);
teco_int_t radix = 10;
qreg->vtable->get_integer(qreg, &radix, NULL);
gchar *p = buffer + TECO_EXPRESSIONS_FORMAT_LEN;
teco_int_t v = ABS(number);
*--p = '\0';
do {
*--p = '0' + (v % radix);
if (*p > '9')
*p += 'A' - '9' - 1;
} while ((v /= radix));
if (number < 0)
*--p = '-';
return p;
}
static void TECO_DEBUG_CLEANUP
teco_expressions_cleanup(void)
{
g_array_free(teco_numbers, TRUE);
g_array_free(teco_operators, TRUE);
}
|