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
|
#ifndef __EXPRESSIONS_H
#define __EXPRESSIONS_H
#include <glib.h>
#include "undo.h"
template <typename Type>
class ValueStack {
int size;
Type *stack;
Type *top;
public:
ValueStack(int _size = 1024) : size(_size)
{
top = stack = new Type[size];
}
~ValueStack()
{
delete stack;
}
inline int
items(void)
{
return (top - stack)/sizeof(Type);
}
inline Type &
push(Type value, int index = 1)
{
for (int i = -index + 1; i; i++)
top[i+1] = top[i];
top++;
return peek(index) = value;
}
inline Type
pop(int index = 1)
{
Type v = peek(index);
top--;
while (--index)
top[-index] = top[-index + 1];
return v;
}
inline Type &
peek(int index = 1)
{
return top[-index];
}
};
template <typename Type>
class UndoTokenPush : public UndoToken {
ValueStack<Type> *stack;
Type value;
int index;
public:
UndoTokenPush(ValueStack<Type> &_stack, Type _value, int _index = 1)
: stack(&_stack), value(_value), index(_index) {}
void
run(void)
{
stack->push(value, index);
}
};
template <typename Type>
class UndoTokenPop : public UndoToken {
ValueStack<Type> *stack;
int index;
public:
UndoTokenPop(ValueStack<Type> &_stack, int _index = 1)
: stack(&_stack), index(_index) {}
void
run(void)
{
stack->pop(index);
}
};
/*
* Arithmetic expression stacks
*/
extern class Expressions {
/* reflects also operator precedence */
enum Operator {
OP_NIL = 0,
OP_POW, // ^*
OP_MUL, // *
OP_DIV, // /
OP_MOD, // ^/
OP_ADD, // +
OP_SUB, // -
OP_AND, // &
OP_OR, // #
// pseudo operators:
OP_BRACE,
OP_LOOP,
OP_NUMBER
};
ValueStack<gint64> numbers;
ValueStack<Operator> operators;
gint num_sign;
gint radix;
public:
Expressions() : num_sign(1), radix(10) {}
void set_num_sign(gint sign);
void set_radix(gint r);
gint64 push(gint64 number);
gint64 pop_num(int index = 1);
gint64 pop_num_calc(int index, gint64 imply);
inline gint64
pop_num_calc(int index = 1)
{
return pop_num_calc(index, num_sign);
}
gint64 add_digit(gchar digit);
Operator push(Operator op);
Operator push_calc(Operator op);
Operator pop_op(int index = 1);
void calc(void);
void eval(bool pop_brace = false);
int args(void);
inline int
first_op(void)
{
return args() + 1;
}
void discard_args(void);
} expressions;
#endif
|