/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk
64 by Gustav Hartvigsson
[General] Major refactoring. Utils -> Vee nenaming.
1
using Vee;
1 by Gustav Hartvigsson
* Initial code - far from done
2
using GLib;
3
4
namespace VQDR.Expression {
5
  
6
  public abstract class Token : GLib.Object {
19 by Gustav Hartvigsson
Added Round Up and Round Down function classes.
7
    
1 by Gustav Hartvigsson
* Initial code - far from done
8
    /** Max size of a single token */
52.1.3 by Gustav Hartvigsson
int -> int32
9
    public const int32 MAX_TOKEN_STRING_LENGTH = 64;
1 by Gustav Hartvigsson
* Initial code - far from done
10
    /** Max size of an expression */
52.1.3 by Gustav Hartvigsson
int -> int32
11
    public const int32 MAX_TOTAL_STRING_LENGTH = 200;
1 by Gustav Hartvigsson
* Initial code - far from done
12
13
    /** Max iteration number for a token (function) */
52.1.3 by Gustav Hartvigsson
int -> int32
14
    public const int32 MAX_TOKEN_ITERATIONS = 500;
1 by Gustav Hartvigsson
* Initial code - far from done
15
    /** Max iteration number for the expression */
52.1.3 by Gustav Hartvigsson
int -> int32
16
    public const int32 MAX_TOTAL_ITERATIONS = 5000;
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
17
    
18
    
24 by Gustav Hartvigsson
* Fixed valadoc stuff.
19
    /**
20
     * Operator precedence and associativity
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
21
     * 
22
     * higher number is higher priority, and is to be done befroe those
23
     * with lower number.
24
     */
23 by Gustav Hartvigsson
* Added DiceOperatiorToken
25
    public enum Prio {
43 by Gustav Hartvigsson
* Added err_print_ln.
26
      /** invalid case. Prevents null-trap. */
27
      INVALID = 0,
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
28
      /** Priority for assignment "=" operator */
43 by Gustav Hartvigsson
* Added err_print_ln.
29
      ASSIGNMENT = 1,
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
30
      /** Priority for conditional OR "||" operator */
31
      CONDITIONAL_OR,
32
      /** Priority for conditional AND "&&" operator */
33
      CONDITIONAL_AND,
34
      /** Priority for equality "==" and "!=" operators */
35
      EQUALITY,
36
      /** Priority for comparison ">", "<", ">=", etc operators */
37
      COMPARISON,
38
      /** Priority for addictive "+" and "-" operators */
39
      ADDICTIVE,
40
      /** Priority for multiplicative "*" and "/" operators */
41
      MULTIPLICATIVE,
42
      /** Priority for unary "+" and "-" and "!" operators */
43
      UNARY,
44
      /** Priority for label assignment ":" operator */
45
      LABEL,
46
      /** Priority for dice "d" operator */
47
      DICE,
48
      /** Priority for functions */
49
      FUNCTION,
50
      /** Priority for values */
44 by Gustav Hartvigsson
* Added skeleton for upcoming parser.
51
      VALUE,
52
      /** The number of values. */
45 by Gustav Hartvigsson
* woopes.
53
      _NUM_VAL;
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
54
      
55
      /** get the name of the priority */
23 by Gustav Hartvigsson
* Added DiceOperatiorToken
56
      public string to_string () {
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
57
        // We have to add 1 as we have an invalid case at zero.
58
        static_assert (_NUM_VAL == 12 + 1);
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
59
        switch (this) {
60
          case ASSIGNMENT:
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
61
            return "ASSIGNMENT";
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
62
          case CONDITIONAL_OR:
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
63
            return "CONDITIONAL_OR";
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
64
          case CONDITIONAL_AND:
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
65
            return "CONDITIONAL_AND";
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
66
          case EQUALITY:
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
67
            return "EQUALITY";
68
          case COMPARISON:
69
            return "COMPARISON";
70
          case ADDICTIVE:
71
            return "ADDICTIVE";
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
72
          case MULTIPLICATIVE:
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
73
            return "MULTIPLICATIVE";
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
74
          case UNARY:
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
75
            return "UNARY";
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
76
          case LABEL:
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
77
            return "LABEL";
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
78
          case DICE:
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
79
            return "DICE";
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
80
          case FUNCTION:
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
81
            return "FUNCTION";
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
82
          case VALUE:
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
83
            return "VALUE";
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
84
          default:
85
            assert_not_reached ();
86
        }
87
      }
43 by Gustav Hartvigsson
* Added err_print_ln.
88
89
      public Prio from_string (string name) {
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
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 ();
43 by Gustav Hartvigsson
* Added err_print_ln.
93
        switch (collected) {
94
          case "ASSIGMENT":
95
            return ASSIGNMENT;
96
          case "CONDITIONAL_OR":
97
            return CONDITIONAL_OR;
98
          case "CONDITIONAL_AND":
99
            return CONDITIONAL_AND;
100
          case "EQUALITY":
101
            return EQUALITY;
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
102
          case "COMPARISON":
103
            return COMPARISON;
104
          case "ADDICTIVE":
105
            return ADDICTIVE;
43 by Gustav Hartvigsson
* Added err_print_ln.
106
          case "MULTIPLICATIVE":
107
            return MULTIPLICATIVE;
108
          case "UNARY":
109
            return UNARY;
110
          case "LABEL":
111
            return LABEL;
112
          case "DICE":
113
            return DICE;
114
          case "FUNCTON":
115
            return FUNCTION;
116
          case "VALUE":
117
            return VALUE;
118
          default:
119
            assert_not_reached ();
120
        }
121
      }
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
122
    }
1 by Gustav Hartvigsson
* Initial code - far from done
123
24 by Gustav Hartvigsson
* Fixed valadoc stuff.
124
    /** Generation to use to get the root with @link parent */
52.1.3 by Gustav Hartvigsson
int -> int32
125
    protected const int32 ROOT_GENERATION = int.MAX;
1 by Gustav Hartvigsson
* Initial code - far from done
126
    
127
    /* ********************************************************************** */
128
    
129
    /**
23 by Gustav Hartvigsson
* Added DiceOperatiorToken
130
     * tells whether the token is right associative or not.
1 by Gustav Hartvigsson
* Initial code - far from done
131
     */
132
    public bool right_assosiative {get;set; default = false;}
133
    
134
    /** The parent token of this token*/
135
    protected unowned Token? parent {protected get; protected set;}
136
    
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
137
    public virtual Prio priority {public get; protected construct set;}
1 by Gustav Hartvigsson
* Initial code - far from done
138
    
139
    /** Starting position of the token in the expression */
57 by Gustav Hartvigsson
Made sure that no text rows exeed 80 columns.
140
    public int32 position {public get; protected set;}
1 by Gustav Hartvigsson
* Initial code - far from done
141
    
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
142
    private int internal_next_child_num;
1 by Gustav Hartvigsson
* Initial code - far from done
143
    /** The index of the next child */
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
144
    public int next_child_num {get {return internal_next_child_num;}
145
                 protected set{internal_next_child_num = value;}}
1 by Gustav Hartvigsson
* Initial code - far from done
146
    
147
    /** 
148
     * The optional that this child represents.
149
     * 
150
     * 
151
     */
52.1.3 by Gustav Hartvigsson
int -> int32
152
    public int32 optional_num_child {public get; protected construct set;}
1 by Gustav Hartvigsson
* Initial code - far from done
153
    
154
    /** The mandatory number of child this token can have */
52.1.3 by Gustav Hartvigsson
int -> int32
155
    public int32 mandatory_num_child {public get; protected construct set;}
1 by Gustav Hartvigsson
* Initial code - far from done
156
    
157
    /** 
158
     * The maximum number of chidren 
159
     * 
160
     * This is calculated at runtime as
161
     * ''optional_num_child + mandatory_num_child''.
162
     * 
163
     * Can not be set.
164
     */
52.1.3 by Gustav Hartvigsson
int -> int32
165
    public int32 max_num_child {get {
1 by Gustav Hartvigsson
* Initial code - far from done
166
      return optional_num_child + mandatory_num_child;
167
     }}
168
     
169
    /** all children of this token */
170
    private (unowned Token?)[] children;
171
    
12.1.2 by Gustav Hartvigsson
* Fastnumber is now a struct,
172
    /*
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.
175
     */
176
    public FastNumber result_value;
177
    public FastNumber result_max_value;
178
    public FastNumber result_min_value;
1 by Gustav Hartvigsson
* Initial code - far from done
179
    public string result_string {public get; protected set; default = "";}
180
    
181
    construct {
182
      children = new Token[max_num_child];
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
183
      next_child_num = 0;
12.1.1 by Gustav Hartvigsson
* Switch to using FastNumber instead of something else.
184
      
12.1.2 by Gustav Hartvigsson
* Fastnumber is now a struct,
185
      result_value = FastNumber ();
186
      result_max_value = FastNumber ();
187
      result_min_value = FastNumber ();
1 by Gustav Hartvigsson
* Initial code - far from done
188
    }
189
    
190
    
52.1.3 by Gustav Hartvigsson
int -> int32
191
    protected Token (int32 position) {
1 by Gustav Hartvigsson
* Initial code - far from done
192
      this.position = position;
193
    }
194
    
23 by Gustav Hartvigsson
* Added DiceOperatiorToken
195
    
1 by Gustav Hartvigsson
* Initial code - far from done
196
    /**
197
     * Reorders result_min_value and result_max_value, so that
23 by Gustav Hartvigsson
* Added DiceOperatiorToken
198
     * result_max_value is the bigger of the two, and
1 by Gustav Hartvigsson
* Initial code - far from done
199
     * result_min_value is the smaller.
200
     */
201
    protected void reorder_max_min_values () {
12.1.1 by Gustav Hartvigsson
* Switch to using FastNumber instead of something else.
202
      if (result_max_value.compare (result_min_value) <= 0) {
203
        FastNumber tmp = result_max_value;
1 by Gustav Hartvigsson
* Initial code - far from done
204
        result_max_value = result_min_value;
205
        result_min_value = tmp;
206
      }
207
    }
208
    
209
    /**
210
     * Get a child token to this token.
211
     * 
212
     * Child positions are between 1 and max_num_child.
213
     * 
214
     * and index of 0 is illegal.
215
     */
57 by Gustav Hartvigsson
Made sure that no text rows exeed 80 columns.
216
    public unowned Token? get_child (int32 index)
217
                          requires (index > 0 && index <= max_num_child) {
1 by Gustav Hartvigsson
* Initial code - far from done
218
      
219
      return children[index -1 ];
220
    }
221
    
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
222
    public void set_next_child (Token child) throws ArgError {
223
      next_child_num++;
224
      set_child (next_child_num, child);
225
    }
1 by Gustav Hartvigsson
* Initial code - far from done
226
    
227
    /**
228
     * Set a child token to this this token.
229
     * 
230
     * Child positions are between 1 and max_num_child.
231
     * 
232
     * and index of 0 is illegal.
233
     */
57 by Gustav Hartvigsson
Made sure that no text rows exeed 80 columns.
234
    public void set_child (int32 index, Token? child)
235
                requires (index > 0 && index <= max_num_child) {
1 by Gustav Hartvigsson
* Initial code - far from done
236
      
237
      children[index - 1] = child;
238
      
239
      if (children[index - 1] != null) {
240
        children[index -1].parent = this;
241
        
242
      }
243
    }
244
    
245
    /**
246
     * is functionally equal to get_child (1);
247
     */
248
    public Token get_left_child () {
249
      return get_child (1);
250
    }
251
    
252
    /**
253
     * is functionally equal to get_child (2);
254
     */
255
    public Token get_right_child () {
256
      return get_child (2);
257
    }
258
    
259
    /**
260
     * is functionally equal to set_child (1, token);
261
     */
262
    public void set_left_child (Token token)  {
263
      set_child (1, token);
264
    }
265
    
266
    /**
267
     * is functionally equal to set_child (2, token);
268
     */
269
    public void set_right_child (Token token)  {
270
      set_child (2, token);
271
    }
272
    
273
    public void evaluate (Context instance) throws GLib.Error {
274
      evaluate_self (instance);
275
    }
276
    
277
    /**
278
     * Evalutates current token tree.
279
     * 
280
     * This Method must evaluate the token subtree, and assign proper values to
281
     * the following properties:
282
     * 
283
     *  * result_value
284
     *  * result_max_value
285
     *  * result_min_value
286
     *  * result_string
287
     * 
288
     * @param instance The context to be used valfor this tree.
289
     * @throws GLib.Error an error if an error has orrured in the evaluation of the tree.
290
     */
291
    protected abstract void evaluate_self (Context instance) throws GLib.Error;
292
  }
293
  
294
}