4
4
namespace VQDR.Expression {
6
6
public abstract class Token : GLib.Object {
7
/** Precision used to perform evaluation */
8
public const int VALUES_PRECISION_DIGITS = 3;
9
/** Precision factor used to convert raw values to actual ones */
10
public const int VALUES_PRECISION_FACTOR = 1000;
11
//public static int VALUES_PRECISION_FACTOR = (int)Math.pow(10, VALUES_PRECISION_DIGITS);
13
/** Precision used to output values */
14
public const int VALUES_OUTPUT_PRECISION_DIGITS = 2;
15
/** Precision factor used to evaluate output */
16
public const int VALUES_OUTPUT_PRECISION_FACTOR = 100;
17
//public static int VALUES_OUTPUT_PRECISION_FACTOR = (int)Math.pow(10, VALUES_OUTPUT_PRECISION_DIGITS);
8
20
/** Max size of a single token */
9
public const int32 MAX_TOKEN_STRING_LENGTH = 64;
21
public const int MAX_TOKEN_STRING_LENGTH = 64;
10
22
/** Max size of an expression */
11
public const int32 MAX_TOTAL_STRING_LENGTH = 200;
23
public const int MAX_TOTAL_STRING_LENGTH = 200;
13
25
/** Max iteration number for a token (function) */
14
public const int32 MAX_TOKEN_ITERATIONS = 500;
26
public const int MAX_TOKEN_ITERATIONS = 500;
15
27
/** Max iteration number for the expression */
16
public const int32 MAX_TOTAL_ITERATIONS = 5000;
20
* Operator precedence and associativity
22
* higher number is higher priority, and is to be done befroe those
26
/** invalid case. Prevents null-trap. */
28
/** Priority for assignment "=" operator */
30
/** Priority for conditional OR "||" operator */
32
/** Priority for conditional AND "&&" operator */
34
/** Priority for equality "==" and "!=" operators */
36
/** Priority for comparison ">", "<", ">=", etc operators */
38
/** Priority for addictive "+" and "-" operators */
40
/** Priority for multiplicative "*" and "/" operators */
42
/** Priority for unary "+" and "-" and "!" operators */
44
/** Priority for label assignment ":" operator */
46
/** Priority for dice "d" operator */
48
/** Priority for functions */
50
/** Priority for values */
52
/** The number of values. */
55
/** get the name of the priority */
56
public string to_string () {
57
// We have to add 1 as we have an invalid case at zero.
58
static_assert (_NUM_VAL == 12 + 1);
63
return "CONDITIONAL_OR";
65
return "CONDITIONAL_AND";
73
return "MULTIPLICATIVE";
85
assert_not_reached ();
89
public Prio from_string (string name) {
90
// We have to add 1 as we have an invalid case at zero.
91
static_assert (_NUM_VAL == 12 + 1);
92
var collected = name.up ();
96
case "CONDITIONAL_OR":
97
return CONDITIONAL_OR;
98
case "CONDITIONAL_AND":
99
return CONDITIONAL_AND;
106
case "MULTIPLICATIVE":
107
return MULTIPLICATIVE;
119
assert_not_reached ();
124
/** Generation to use to get the root with @link parent */
125
protected const int32 ROOT_GENERATION = int.MAX;
28
public const int MAX_TOTAL_ITERATIONS = 5000;
30
/* ************************************* */
31
/* Operator precedence and associativity */
32
/* ************************************* */
33
/** Priority for assignment "=" operator */
34
public const int PRIO_ASSIGNMENT = 0;
35
/** Priority for conditional OR "||" operator */
36
public const int PRIO_CONDITIONAL_OR = 2;
37
/** Priority for conditional AND "&&" operator */
38
public const int PRIO_CONDITIONAL_AND = 3;
39
/** Priority for equality "==" and "!=" operators */
40
public const int PRIO_EQUALITY = 4;
41
/** Priority for comparison ">", "<", ">=", etc operators */
42
public const int PRIO_COMPARISON = 5;
43
/** Priority for addictive "+" and "-" operators */
44
public const int PRIO_ADDICTIVE = 6;
45
/** Priority for multiplicative "*" and "/" operators */
46
public const int PRIO_MULTIPLICATIVE = 7;
47
/** Priority for unary "+" and "-" and "!" operators */
48
public const int PRIO_UNARY = 8;
49
/** Priority for label assignment ":" operator */
50
public const int PRIO_LABEL = 9;
51
/** Priority for dice "d" operator */
52
public const int PRIO_DICE = 10;
53
/** Priority for functions */
54
public const int PRIO_FUNCTION = 11;
55
/** Priority for values */
56
public const int PRIO_VALUE = 12;
58
/** Generation to use to get the root with {@link #getParent} */
59
protected const int ROOT_GENERATION = int.MAX;
127
61
/* ********************************************************************** */
130
* tells whether the token is right associative or not.
64
* tells weather the token is right associative or not.
132
66
public bool right_assosiative {get;set; default = false;}
69
private int real_priority;
134
71
/** The parent token of this token*/
135
72
protected unowned Token? parent {protected get; protected set;}
137
public virtual Prio priority {public get; protected construct set;}
74
public virtual int priority {public get {
75
return __get_priority ();
76
} protected construct set {
77
__set_priority (value);
139
80
/** Starting position of the token in the expression */
140
public int32 position {public get; protected set;}
81
protected int position;
142
private int internal_next_child_num;
143
83
/** The index of the next child */
144
public int next_child_num {get {return internal_next_child_num;}
145
protected set{internal_next_child_num = value;}}
84
private int next_child;
148
87
* The optional that this child represents.
152
public int32 optional_num_child {public get; protected construct set;}
91
public int optional_num_child {public get; protected construct set;}
154
93
/** The mandatory number of child this token can have */
155
public int32 mandatory_num_child {public get; protected construct set;}
94
public int mandatory_num_child {public get; protected construct set;}
158
97
* The maximum number of chidren
163
102
* Can not be set.
165
public int32 max_num_child {get {
104
public int max_num_child {get {
166
105
return optional_num_child + mandatory_num_child;
169
108
/** all children of this token */
170
109
private (unowned Token?)[] children;
173
* These values should have a protected setter, but I could not get it to
174
* work. So we will have to live with this.
176
public FastNumber result_value;
177
public FastNumber result_max_value;
178
public FastNumber result_min_value;
112
public long result_value {public get; protected set; default = 0;}
113
public long result_max_value {public get; protected set; default = 0;}
114
public long result_min_value {public get; protected set; default = 0;}
179
115
public string result_string {public get; protected set; default = "";}
118
// Valgrind says there is a memory leak here... But it's actually
119
// GObject's constructor that is leaking.
182
120
children = new Token[max_num_child];
185
result_value = FastNumber ();
186
result_max_value = FastNumber ();
187
result_min_value = FastNumber ();
191
protected Token (int32 position) {
125
protected Token (int position) {
192
126
this.position = position;
197
130
* Reorders result_min_value and result_max_value, so that
198
* result_max_value is the bigger of the two, and
131
* result_max varue is the bigger of the two, and
199
132
* result_min_value is the smaller.
201
134
protected void reorder_max_min_values () {
202
if (result_max_value.compare (result_min_value) <= 0) {
203
FastNumber tmp = result_max_value;
135
if (result_max_value < result_min_value) {
136
long tmp = result_max_value;
204
137
result_max_value = result_min_value;
205
138
result_min_value = tmp;