/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-11-11 19:49:21 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20211111194921-ayb9vuhi7wzfucx0
a few things: FastNumbers, simplifications and AbstractPoolToken.

* Swinched Context over to FastNumber

* simmplified logic in Variabel.compare () Variabel.equals ()
  and Variabel.equals_values.

* Tests: Don't use 'new' with structs.

* The Dice class now has a .singel constructor.

* Put RollAndKeepFunctionToken in the corret namespace.

* Added test function for constant value token to the
  token test program.

* Fixed error with test path for GLib.Test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using Vee;
 
1
using VQDR.Common;
2
2
using GLib;
3
3
 
4
4
namespace VQDR.Expression {
5
5
  
6
6
  public abstract class Token : GLib.Object {
7
7
    
 
8
    //public static int VALUES_OUTPUT_PRECISION_FACTOR = (int)Math.pow(10, VALUES_OUTPUT_PRECISION_DIGITS);
 
9
    
 
10
    
8
11
    /** Max size of a single token */
9
 
    public const int32 MAX_TOKEN_STRING_LENGTH = 64;
 
12
    public const int MAX_TOKEN_STRING_LENGTH = 64;
10
13
    /** Max size of an expression */
11
 
    public const int32 MAX_TOTAL_STRING_LENGTH = 200;
 
14
    public const int MAX_TOTAL_STRING_LENGTH = 200;
12
15
 
13
16
    /** Max iteration number for a token (function) */
14
 
    public const int32 MAX_TOKEN_ITERATIONS = 500;
 
17
    public const int MAX_TOKEN_ITERATIONS = 500;
15
18
    /** Max iteration number for the expression */
16
 
    public const int32 MAX_TOTAL_ITERATIONS = 5000;
 
19
    public const int MAX_TOTAL_ITERATIONS = 5000;
17
20
    
18
21
    
19
22
    /**
23
26
     * with lower number.
24
27
     */
25
28
    public enum Prio {
26
 
      /** invalid case. Prevents null-trap. */
27
 
      INVALID = 0,
28
29
      /** Priority for assignment "=" operator */
29
 
      ASSIGNMENT = 1,
 
30
      ASSIGNMENT = 0,
30
31
      /** Priority for conditional OR "||" operator */
31
32
      CONDITIONAL_OR,
32
33
      /** Priority for conditional AND "&&" operator */
48
49
      /** Priority for functions */
49
50
      FUNCTION,
50
51
      /** Priority for values */
51
 
      VALUE,
52
 
      /** The number of values. */
53
 
      _NUM_VAL;
 
52
      VALUE;
54
53
      
55
54
      /** get the name of the priority */
56
55
      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);
59
56
        switch (this) {
60
57
          case ASSIGNMENT:
61
 
            return "ASSIGNMENT";
 
58
            return "prio: assigment";
62
59
          case CONDITIONAL_OR:
63
 
            return "CONDITIONAL_OR";
 
60
            return "prio: conditonal OR";
64
61
          case CONDITIONAL_AND:
65
 
            return "CONDITIONAL_AND";
 
62
            return "prio: conidonal AND";
66
63
          case EQUALITY:
67
 
            return "EQUALITY";
68
 
          case COMPARISON:
69
 
            return "COMPARISON";
70
 
          case ADDICTIVE:
71
 
            return "ADDICTIVE";
 
64
            return "prio: equality";
72
65
          case MULTIPLICATIVE:
73
 
            return "MULTIPLICATIVE";
 
66
            return "prio: multiplicative";
74
67
          case UNARY:
75
 
            return "UNARY";
 
68
            return "prio: unary";
76
69
          case LABEL:
77
 
            return "LABEL";
 
70
            return "prio: label";
78
71
          case DICE:
79
 
            return "DICE";
 
72
            return "prio: dice";
80
73
          case FUNCTION:
81
 
            return "FUNCTION";
 
74
            return "prio: function";
82
75
          case VALUE:
83
 
            return "VALUE";
84
 
          default:
85
 
            assert_not_reached ();
86
 
        }
87
 
      }
88
 
 
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 ();
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;
102
 
          case "COMPARISON":
103
 
            return COMPARISON;
104
 
          case "ADDICTIVE":
105
 
            return ADDICTIVE;
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;
 
76
            return "prio: value";
118
77
          default:
119
78
            assert_not_reached ();
120
79
        }
122
81
    }
123
82
 
124
83
    /** Generation to use to get the root with @link parent */
125
 
    protected const int32 ROOT_GENERATION = int.MAX;
 
84
    protected const int ROOT_GENERATION = int.MAX;
126
85
    
127
86
    /* ********************************************************************** */
128
87
    
134
93
    /** The parent token of this token*/
135
94
    protected unowned Token? parent {protected get; protected set;}
136
95
    
137
 
    public virtual Prio priority {public get; protected construct set;}
 
96
    public virtual int priority {public get; protected construct set;}
138
97
    
139
98
    /** Starting position of the token in the expression */
140
 
    public int32 position {public get; protected set;}
 
99
    protected int position;
141
100
    
142
 
    private int internal_next_child_num;
143
101
    /** 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;}}
 
102
    private int next_child;
146
103
    
147
104
    /** 
148
105
     * The optional that this child represents.
149
106
     * 
150
107
     * 
151
108
     */
152
 
    public int32 optional_num_child {public get; protected construct set;}
 
109
    public int optional_num_child {public get; protected construct set;}
153
110
    
154
111
    /** The mandatory number of child this token can have */
155
 
    public int32 mandatory_num_child {public get; protected construct set;}
 
112
    public int mandatory_num_child {public get; protected construct set;}
156
113
    
157
114
    /** 
158
115
     * The maximum number of chidren 
162
119
     * 
163
120
     * Can not be set.
164
121
     */
165
 
    public int32 max_num_child {get {
 
122
    public int max_num_child {get {
166
123
      return optional_num_child + mandatory_num_child;
167
124
     }}
168
125
     
180
137
    
181
138
    construct {
182
139
      children = new Token[max_num_child];
183
 
      next_child_num = 0;
 
140
      next_child = 0;
184
141
      
185
142
      result_value = FastNumber ();
186
143
      result_max_value = FastNumber ();
188
145
    }
189
146
    
190
147
    
191
 
    protected Token (int32 position) {
 
148
    protected Token (int position) {
192
149
      this.position = position;
193
150
    }
194
151
    
213
170
     * 
214
171
     * and index of 0 is illegal.
215
172
     */
216
 
    public unowned Token? get_child (int32 index)
217
 
                          requires (index > 0 && index <= max_num_child) {
 
173
    public unowned Token? get_child (int index) requires (index > 0 && index <= max_num_child) {
218
174
      
219
175
      return children[index -1 ];
220
176
    }
221
177
    
222
 
    public void set_next_child (Token child) throws ArgError {
223
 
      next_child_num++;
224
 
      set_child (next_child_num, child);
225
 
    }
226
178
    
227
179
    /**
228
180
     * Set a child token to this this token.
231
183
     * 
232
184
     * and index of 0 is illegal.
233
185
     */
234
 
    public void set_child (int32 index, Token? child)
235
 
                requires (index > 0 && index <= max_num_child) {
 
186
    public void set_child (int index, Token? child) requires (index > 0 && index <= max_num_child) {
236
187
      
237
188
      children[index - 1] = child;
238
189