/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk
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
using VQDR.Common;
using GLib;

namespace VQDR.Expression {
  
  public abstract class Token : GLib.Object {
    
    //public static int VALUES_OUTPUT_PRECISION_FACTOR = (int)Math.pow(10, VALUES_OUTPUT_PRECISION_DIGITS);
    
    
    /** Max size of a single token */
    public const int MAX_TOKEN_STRING_LENGTH = 64;
    /** Max size of an expression */
    public const int MAX_TOTAL_STRING_LENGTH = 200;

    /** Max iteration number for a token (function) */
    public const int MAX_TOKEN_ITERATIONS = 500;
    /** Max iteration number for the expression */
    public const int MAX_TOTAL_ITERATIONS = 5000;
    
    
    /** Operator precedence and associativity
     * 
     * higher number is higher priority, and is to be done befroe those
     * with lower number.
     */
    enum Prio {
      /** Priority for assignment "=" operator */
      ASSIGNMENT = 0,
      /** Priority for conditional OR "||" operator */
      CONDITIONAL_OR,
      /** Priority for conditional AND "&&" operator */
      CONDITIONAL_AND,
      /** Priority for equality "==" and "!=" operators */
      EQUALITY,
      /** Priority for comparison ">", "<", ">=", etc operators */
      COMPARISON,
      /** Priority for addictive "+" and "-" operators */
      ADDICTIVE,
      /** Priority for multiplicative "*" and "/" operators */
      MULTIPLICATIVE,
      /** Priority for unary "+" and "-" and "!" operators */
      UNARY,
      /** Priority for label assignment ":" operator */
      LABEL,
      /** Priority for dice "d" operator */
      DICE,
      /** Priority for functions */
      FUNCTION,
      /** Priority for values */
      VALUE;
      
      /** get the name of the priority */
      string to_string () {
        switch (this) {
          case ASSIGNMENT:
            return "prio: assigment";
          case CONDITIONAL_OR:
            return "prio: conditonal or";
          case CONDITIONAL_AND:
            return "prio: conidonal and";
          case EQUALITY:
            return "prio: equality";
          case MULTIPLICATIVE:
            return "prio: multiplicative";
          case UNARY:
            return "prio: unary";
          case LABEL:
            return "prio: label";
          case DICE:
            return "prio: dice";
          case FUNCTION:
            return "prio: function";
          case VALUE:
            return "prio: value";
          default:
            assert_not_reached ();
        }
      }
    }

    /** Generation to use to get the root with {@link #getParent} */
    protected const int ROOT_GENERATION = int.MAX;
    
    /* ********************************************************************** */
    
    /**
     * tells weather the token is right associative or not.
     */
    public bool right_assosiative {get;set; default = false;}
    
    
    private int real_priority;
    
    /** The parent token of this token*/
    protected unowned Token? parent {protected get; protected set;}
    
    public virtual int priority {public get {
      return __get_priority ();
     } protected construct set {
       __set_priority (value);
     }}
    
    /** Starting position of the token in the expression */
    protected int position;
    
    /** The index of the next child */
    private int next_child;
    
    /** 
     * The optional that this child represents.
     * 
     * 
     */
    public int optional_num_child {public get; protected construct set;}
    
    /** The mandatory number of child this token can have */
    public int mandatory_num_child {public get; protected construct set;}
    
    /** 
     * The maximum number of chidren 
     * 
     * This is calculated at runtime as
     * ''optional_num_child + mandatory_num_child''.
     * 
     * Can not be set.
     */
    public int max_num_child {get {
      return optional_num_child + mandatory_num_child;
     }}
     
    /** all children of this token */
    private (unowned Token?)[] children;
    
    /*
     * These values should have a protected setter, but I could not get it to
     * work. So we will have to live with this.
     */
    public FastNumber result_value;
    public FastNumber result_max_value;
    public FastNumber result_min_value;
    public string result_string {public get; protected set; default = "";}
    
    construct {
      children = new Token[max_num_child];
      next_child = 0;
      
      result_value = FastNumber ();
      result_max_value = FastNumber ();
      result_min_value = FastNumber ();
    }
    
    
    protected Token (int position) {
      this.position = position;
    }
    
    /**
     * Reorders result_min_value and result_max_value, so that
     * result_max varue is the bigger of the two, and
     * result_min_value is the smaller.
     */
    protected void reorder_max_min_values () {
      if (result_max_value.compare (result_min_value) <= 0) {
        FastNumber tmp = result_max_value;
        result_max_value = result_min_value;
        result_min_value = tmp;
      }
    }
    
    /**
     * Get a child token to this token.
     * 
     * Child positions are between 1 and max_num_child.
     * 
     * and index of 0 is illegal.
     */
    public unowned Token? get_child (int index) requires (index > 0 && index <= max_num_child) {
      
      return children[index -1 ];
    }
    
    
    /**
     * Set a child token to this this token.
     * 
     * Child positions are between 1 and max_num_child.
     * 
     * and index of 0 is illegal.
     */
    public void set_child (int index, Token? child) requires (index > 0 && index <= max_num_child) {
      
      children[index - 1] = child;
      
      if (children[index - 1] != null) {
        children[index -1].parent = this;
        
      }
    }
    
    /**
     * is functionally equal to get_child (1);
     */
    public Token get_left_child () {
      return get_child (1);
    }
    
    /**
     * is functionally equal to get_child (2);
     */
    public Token get_right_child () {
      return get_child (2);
    }
    
    /**
     * is functionally equal to set_child (1, token);
     */
    public void set_left_child (Token token)  {
      set_child (1, token);
    }
    
    /**
     * is functionally equal to set_child (2, token);
     */
    public void set_right_child (Token token)  {
      set_child (2, token);
    }
    
    public void evaluate (Context instance) throws GLib.Error {
      evaluate_self (instance);
    }
    
    /**
     * Evalutates current token tree.
     * 
     * This Method must evaluate the token subtree, and assign proper values to
     * the following properties:
     * 
     *  * result_value
     *  * result_max_value
     *  * result_min_value
     *  * result_string
     * 
     * @param instance The context to be used valfor this tree.
     * @throws GLib.Error an error if an error has orrured in the evaluation of the tree.
     */
    protected abstract void evaluate_self (Context instance) throws GLib.Error;
    
    /* *********** IGNORE THE MESS I HAVE CREATED FOR MY SELF *************** */
    /** IGNORE ME */
    protected void __set_priority (int prio) {
      real_priority = prio;
    }
    
    /** IGNORE ME*/
    protected int __get_priority () {
      return real_priority;
    }
    
    
  }
  
}