/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk

« back to all changes in this revision

Viewing changes to src/libvqdr/token.vala

  • Committer: Gustav Hartvigsson
  • Date: 2022-05-30 18:35:38 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20220530183538-xlixbth43j8k3s42
Added NamedVector

Added Pair

Removed debugs from FastNumber

Added foreach_pop and foreach_peek to Stak.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using VQDR.Common;
 
1
using Utils;
2
2
using GLib;
3
3
 
4
4
namespace VQDR.Expression {
5
5
  
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);
12
7
    
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
8
    //public static int VALUES_OUTPUT_PRECISION_FACTOR = (int)Math.pow(10, VALUES_OUTPUT_PRECISION_DIGITS);
18
9
    
19
10
    
26
17
    public const int MAX_TOKEN_ITERATIONS = 500;
27
18
    /** Max iteration number for the expression */
28
19
    public const int MAX_TOTAL_ITERATIONS = 5000;
29
 
 
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;
57
 
 
58
 
    /** Generation to use to get the root with {@link #getParent} */
 
20
    
 
21
    
 
22
    /**
 
23
     * Operator precedence and associativity
 
24
     * 
 
25
     * higher number is higher priority, and is to be done befroe those
 
26
     * with lower number.
 
27
     */
 
28
    public enum Prio {
 
29
      /** invalid case. Prevents null-trap. */
 
30
      INVALID = 0,
 
31
      /** Priority for assignment "=" operator */
 
32
      ASSIGNMENT = 1,
 
33
      /** Priority for conditional OR "||" operator */
 
34
      CONDITIONAL_OR,
 
35
      /** Priority for conditional AND "&&" operator */
 
36
      CONDITIONAL_AND,
 
37
      /** Priority for equality "==" and "!=" operators */
 
38
      EQUALITY,
 
39
      /** Priority for comparison ">", "<", ">=", etc operators */
 
40
      COMPARISON,
 
41
      /** Priority for addictive "+" and "-" operators */
 
42
      ADDICTIVE,
 
43
      /** Priority for multiplicative "*" and "/" operators */
 
44
      MULTIPLICATIVE,
 
45
      /** Priority for unary "+" and "-" and "!" operators */
 
46
      UNARY,
 
47
      /** Priority for label assignment ":" operator */
 
48
      LABEL,
 
49
      /** Priority for dice "d" operator */
 
50
      DICE,
 
51
      /** Priority for functions */
 
52
      FUNCTION,
 
53
      /** Priority for values */
 
54
      VALUE,
 
55
      /** The number of values. */
 
56
      _NUM_VAL;
 
57
      
 
58
      /** get the name of the priority */
 
59
      public string to_string () {
 
60
        switch (this) {
 
61
          case ASSIGNMENT:
 
62
            return "prio: assigment";
 
63
          case CONDITIONAL_OR:
 
64
            return "prio: conditonal OR";
 
65
          case CONDITIONAL_AND:
 
66
            return "prio: conidonal AND";
 
67
          case EQUALITY:
 
68
            return "prio: equality";
 
69
          case MULTIPLICATIVE:
 
70
            return "prio: multiplicative";
 
71
          case UNARY:
 
72
            return "prio: unary";
 
73
          case LABEL:
 
74
            return "prio: label";
 
75
          case DICE:
 
76
            return "prio: dice";
 
77
          case FUNCTION:
 
78
            return "prio: function";
 
79
          case VALUE:
 
80
            return "prio: value";
 
81
          default:
 
82
            assert_not_reached ();
 
83
        }
 
84
      }
 
85
 
 
86
      public Prio from_string (string name) {
 
87
        var collected = collect_string (name.split (" ")).up ();
 
88
        switch (collected) {
 
89
          case "ASSIGMENT":
 
90
          case "PRIO:ASSIGNMENT":
 
91
            return ASSIGNMENT;
 
92
          case "CONDITIONAL_OR":
 
93
          case "PRIO:CONDITIONAL_OR":
 
94
            return CONDITIONAL_OR;
 
95
          case "CONDITIONAL_AND":
 
96
          case "PRINO:CONDITIONAL_AND":
 
97
            return CONDITIONAL_AND;
 
98
          case "EQUALITY":
 
99
          case "PRINO:EQUALITY":
 
100
            return EQUALITY;
 
101
          case "MULTIPLICATIVE":
 
102
          case "PRINO:MULTIPLICATIVE":
 
103
            return MULTIPLICATIVE;
 
104
          case "UNARY":
 
105
          case "PRINO:UNARY":
 
106
            return UNARY;
 
107
          case "LABEL":
 
108
          case "PRINO:LABEL":
 
109
            return LABEL;
 
110
          case "DICE":
 
111
          case "PRINO:DICE":
 
112
            return DICE;
 
113
          case "FUNCTON":
 
114
          case "PRINO:FUNCTON":
 
115
            return FUNCTION;
 
116
          case "VALUE":
 
117
          case "PRINO:VALUE":
 
118
            return VALUE;
 
119
          default:
 
120
            assert_not_reached ();
 
121
        }
 
122
      }
 
123
    }
 
124
 
 
125
    /** Generation to use to get the root with @link parent */
59
126
    protected const int ROOT_GENERATION = int.MAX;
60
127
    
61
128
    /* ********************************************************************** */
62
129
    
63
130
    /**
64
 
     * tells weather the token is right associative or not.
 
131
     * tells whether the token is right associative or not.
65
132
     */
66
133
    public bool right_assosiative {get;set; default = false;}
67
134
    
68
 
    
69
 
    private int real_priority;
70
 
    
71
135
    /** The parent token of this token*/
72
136
    protected unowned Token? parent {protected get; protected set;}
73
137
    
74
 
    public virtual int priority {public get {
75
 
      return __get_priority ();
76
 
     } protected construct set {
77
 
       __set_priority (value);
78
 
     }}
 
138
    public virtual int priority {public get; protected construct set;}
79
139
    
80
140
    /** Starting position of the token in the expression */
81
141
    protected int position;
108
168
    /** all children of this token */
109
169
    private (unowned Token?)[] children;
110
170
    
111
 
    
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;}
 
171
    /*
 
172
     * These values should have a protected setter, but I could not get it to
 
173
     * work. So we will have to live with this.
 
174
     */
 
175
    public FastNumber result_value;
 
176
    public FastNumber result_max_value;
 
177
    public FastNumber result_min_value;
115
178
    public string result_string {public get; protected set; default = "";}
116
179
    
117
180
    construct {
118
 
      // Valgrind says there is a memory leak here... But it's actually
119
 
      // GObject's constructor that is leaking.
120
181
      children = new Token[max_num_child];
121
182
      next_child = 0;
 
183
      
 
184
      result_value = FastNumber ();
 
185
      result_max_value = FastNumber ();
 
186
      result_min_value = FastNumber ();
122
187
    }
123
188
    
124
189
    
126
191
      this.position = position;
127
192
    }
128
193
    
 
194
    
129
195
    /**
130
196
     * Reorders result_min_value and result_max_value, so that
131
 
     * result_max varue is the bigger of the two, and
 
197
     * result_max_value is the bigger of the two, and
132
198
     * result_min_value is the smaller.
133
199
     */
134
200
    protected void reorder_max_min_values () {
135
 
      if (result_max_value < result_min_value) {
136
 
        long tmp = result_max_value;
 
201
      if (result_max_value.compare (result_min_value) <= 0) {
 
202
        FastNumber tmp = result_max_value;
137
203
        result_max_value = result_min_value;
138
204
        result_min_value = tmp;
139
205
      }
216
282
     * @throws GLib.Error an error if an error has orrured in the evaluation of the tree.
217
283
     */
218
284
    protected abstract void evaluate_self (Context instance) throws GLib.Error;
219
 
    
220
 
    /* *********** IGNORE THE MESS I HAVE CREATED FRO MY SELF *************** */
221
 
    /** IGNORE ME */
222
 
    protected void __set_priority (int prio) {
223
 
      real_priority = prio;
224
 
    }
225
 
    
226
 
    /** IGNORE ME*/
227
 
    protected int __get_priority () {
228
 
      return real_priority;
229
 
    }
230
 
    
231
 
    
232
285
  }
233
286
  
234
287
}