/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: 2024-12-22 00:30:29 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20241222003029-k74ogrm32zobz325
[General] Split libvee into it's own library.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using Utils;
 
1
using Vee;
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
 
    
11
8
    /** Max size of a single token */
12
 
    public const int MAX_TOKEN_STRING_LENGTH = 64;
 
9
    public const int32 MAX_TOKEN_STRING_LENGTH = 64;
13
10
    /** Max size of an expression */
14
 
    public const int MAX_TOTAL_STRING_LENGTH = 200;
 
11
    public const int32 MAX_TOTAL_STRING_LENGTH = 200;
15
12
 
16
13
    /** Max iteration number for a token (function) */
17
 
    public const int MAX_TOKEN_ITERATIONS = 500;
 
14
    public const int32 MAX_TOKEN_ITERATIONS = 500;
18
15
    /** Max iteration number for the expression */
19
 
    public const int MAX_TOTAL_ITERATIONS = 5000;
 
16
    public const int32 MAX_TOTAL_ITERATIONS = 5000;
20
17
    
21
18
    
22
19
    /**
57
54
      
58
55
      /** get the name of the priority */
59
56
      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);
60
59
        switch (this) {
61
60
          case ASSIGNMENT:
62
 
            return "prio: assigment";
 
61
            return "ASSIGNMENT";
63
62
          case CONDITIONAL_OR:
64
 
            return "prio: conditonal OR";
 
63
            return "CONDITIONAL_OR";
65
64
          case CONDITIONAL_AND:
66
 
            return "prio: conidonal AND";
 
65
            return "CONDITIONAL_AND";
67
66
          case EQUALITY:
68
 
            return "prio: equality";
 
67
            return "EQUALITY";
 
68
          case COMPARISON:
 
69
            return "COMPARISON";
 
70
          case ADDICTIVE:
 
71
            return "ADDICTIVE";
69
72
          case MULTIPLICATIVE:
70
 
            return "prio: multiplicative";
 
73
            return "MULTIPLICATIVE";
71
74
          case UNARY:
72
 
            return "prio: unary";
 
75
            return "UNARY";
73
76
          case LABEL:
74
 
            return "prio: label";
 
77
            return "LABEL";
75
78
          case DICE:
76
 
            return "prio: dice";
 
79
            return "DICE";
77
80
          case FUNCTION:
78
 
            return "prio: function";
 
81
            return "FUNCTION";
79
82
          case VALUE:
80
 
            return "prio: value";
 
83
            return "VALUE";
81
84
          default:
82
85
            assert_not_reached ();
83
86
        }
84
87
      }
85
88
 
86
89
      public Prio from_string (string name) {
87
 
        var collected = collect_string (name.split (" ")).up ();
 
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 ();
88
93
        switch (collected) {
89
94
          case "ASSIGMENT":
90
 
          case "PRIO:ASSIGNMENT":
91
95
            return ASSIGNMENT;
92
96
          case "CONDITIONAL_OR":
93
 
          case "PRIO:CONDITIONAL_OR":
94
97
            return CONDITIONAL_OR;
95
98
          case "CONDITIONAL_AND":
96
 
          case "PRINO:CONDITIONAL_AND":
97
99
            return CONDITIONAL_AND;
98
100
          case "EQUALITY":
99
 
          case "PRINO:EQUALITY":
100
101
            return EQUALITY;
 
102
          case "COMPARISON":
 
103
            return COMPARISON;
 
104
          case "ADDICTIVE":
 
105
            return ADDICTIVE;
101
106
          case "MULTIPLICATIVE":
102
 
          case "PRINO:MULTIPLICATIVE":
103
107
            return MULTIPLICATIVE;
104
108
          case "UNARY":
105
 
          case "PRINO:UNARY":
106
109
            return UNARY;
107
110
          case "LABEL":
108
 
          case "PRINO:LABEL":
109
111
            return LABEL;
110
112
          case "DICE":
111
 
          case "PRINO:DICE":
112
113
            return DICE;
113
114
          case "FUNCTON":
114
 
          case "PRINO:FUNCTON":
115
115
            return FUNCTION;
116
116
          case "VALUE":
117
 
          case "PRINO:VALUE":
118
117
            return VALUE;
119
118
          default:
120
119
            assert_not_reached ();
123
122
    }
124
123
 
125
124
    /** Generation to use to get the root with @link parent */
126
 
    protected const int ROOT_GENERATION = int.MAX;
 
125
    protected const int32 ROOT_GENERATION = int.MAX;
127
126
    
128
127
    /* ********************************************************************** */
129
128
    
135
134
    /** The parent token of this token*/
136
135
    protected unowned Token? parent {protected get; protected set;}
137
136
    
138
 
    public virtual int priority {public get; protected construct set;}
 
137
    public virtual Prio priority {public get; protected construct set;}
139
138
    
140
139
    /** Starting position of the token in the expression */
141
 
    protected int position;
 
140
    public int32 position {public get; protected set;}
142
141
    
 
142
    private int internal_next_child_num;
143
143
    /** The index of the next child */
144
 
    private int next_child;
 
144
    public int next_child_num {get {return internal_next_child_num;}
 
145
                 protected set{internal_next_child_num = value;}}
145
146
    
146
147
    /** 
147
148
     * The optional that this child represents.
148
149
     * 
149
150
     * 
150
151
     */
151
 
    public int optional_num_child {public get; protected construct set;}
 
152
    public int32 optional_num_child {public get; protected construct set;}
152
153
    
153
154
    /** The mandatory number of child this token can have */
154
 
    public int mandatory_num_child {public get; protected construct set;}
 
155
    public int32 mandatory_num_child {public get; protected construct set;}
155
156
    
156
157
    /** 
157
158
     * The maximum number of chidren 
161
162
     * 
162
163
     * Can not be set.
163
164
     */
164
 
    public int max_num_child {get {
 
165
    public int32 max_num_child {get {
165
166
      return optional_num_child + mandatory_num_child;
166
167
     }}
167
168
     
179
180
    
180
181
    construct {
181
182
      children = new Token[max_num_child];
182
 
      next_child = 0;
 
183
      next_child_num = 0;
183
184
      
184
185
      result_value = FastNumber ();
185
186
      result_max_value = FastNumber ();
187
188
    }
188
189
    
189
190
    
190
 
    protected Token (int position) {
 
191
    protected Token (int32 position) {
191
192
      this.position = position;
192
193
    }
193
194
    
212
213
     * 
213
214
     * and index of 0 is illegal.
214
215
     */
215
 
    public unowned Token? get_child (int index) requires (index > 0 && index <= max_num_child) {
 
216
    public unowned Token? get_child (int32 index)
 
217
                          requires (index > 0 && index <= max_num_child) {
216
218
      
217
219
      return children[index -1 ];
218
220
    }
219
221
    
 
222
    public void set_next_child (Token child) throws ArgError {
 
223
      next_child_num++;
 
224
      set_child (next_child_num, child);
 
225
    }
220
226
    
221
227
    /**
222
228
     * Set a child token to this this token.
225
231
     * 
226
232
     * and index of 0 is illegal.
227
233
     */
228
 
    public void set_child (int index, Token? child) requires (index > 0 && index <= max_num_child) {
 
234
    public void set_child (int32 index, Token? child)
 
235
                requires (index > 0 && index <= max_num_child) {
229
236
      
230
237
      children[index - 1] = child;
231
238