/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: 2021-09-09 15:40:16 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210909154016-0hm69n49w2slfky5
added more testes for the parser.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
 
20
    
 
21
    
 
22
    /** Operator precedence and associativity
 
23
     * 
 
24
     * higher number is higher priority, and is to be done befroe those
 
25
     * with lower number.
 
26
     */
 
27
    enum Prio {
 
28
      /** Priority for assignment "=" operator */
 
29
      ASSIGNMENT = 0,
 
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 */
 
51
      VALUE;
 
52
      
 
53
      /** get the name of the priority */
 
54
      string to_string () {
 
55
        switch (this) {
 
56
          case ASSIGNMENT:
 
57
            return "prio: assigment";
 
58
          case CONDITIONAL_OR:
 
59
            return "prio: conditonal or";
 
60
          case CONDITIONAL_AND:
 
61
            return "prio: conidonal and";
 
62
          case EQUALITY:
 
63
            return "prio: equality";
 
64
          case MULTIPLICATIVE:
 
65
            return "prio: multiplicative";
 
66
          case UNARY:
 
67
            return "prio: unary";
 
68
          case LABEL:
 
69
            return "prio: label";
 
70
          case DICE:
 
71
            return "prio: dice";
 
72
          case FUNCTION:
 
73
            return "prio: function";
 
74
          case VALUE:
 
75
            return "prio: value";
 
76
          default:
 
77
            assert_not_reached ();
 
78
        }
 
79
      }
 
80
    }
57
81
 
58
82
    /** Generation to use to get the root with {@link #getParent} */
59
83
    protected const int ROOT_GENERATION = int.MAX;
108
132
    /** all children of this token */
109
133
    private (unowned Token?)[] children;
110
134
    
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;}
 
135
    /*
 
136
     * These values should have a protected setter, but I could not get it to
 
137
     * work. So we will have to live with this.
 
138
     */
 
139
    public FastNumber result_value;
 
140
    public FastNumber result_max_value;
 
141
    public FastNumber result_min_value;
115
142
    public string result_string {public get; protected set; default = "";}
116
143
    
117
144
    construct {
118
 
      // Valgrind says there is a memory leak here... But it's actually
119
 
      // GObject's constructor that is leaking.
120
145
      children = new Token[max_num_child];
121
146
      next_child = 0;
 
147
      
 
148
      result_value = FastNumber ();
 
149
      result_max_value = FastNumber ();
 
150
      result_min_value = FastNumber ();
122
151
    }
123
152
    
124
153
    
132
161
     * result_min_value is the smaller.
133
162
     */
134
163
    protected void reorder_max_min_values () {
135
 
      if (result_max_value < result_min_value) {
136
 
        long tmp = result_max_value;
 
164
      if (result_max_value.compare (result_min_value) <= 0) {
 
165
        FastNumber tmp = result_max_value;
137
166
        result_max_value = result_min_value;
138
167
        result_min_value = tmp;
139
168
      }
217
246
     */
218
247
    protected abstract void evaluate_self (Context instance) throws GLib.Error;
219
248
    
220
 
    /* *********** IGNORE THE MESS I HAVE CREATED FRO MY SELF *************** */
 
249
    /* *********** IGNORE THE MESS I HAVE CREATED FOR MY SELF *************** */
221
250
    /** IGNORE ME */
222
251
    protected void __set_priority (int prio) {
223
252
      real_priority = prio;