/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/function/function_token.vala

  • Committer: Gustav Hartvigsson
  • Date: 2024-12-21 23:51:45 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20241221235145-4metak6z6u6vf6b0
[General] Major refactoring. Utils -> Vee nenaming.
Todo: Split libvee into a different library??

Also: Fixed spelling mistakes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
using Gee;
2
2
 
3
3
using VQDR.Expression;
 
4
using Vee;
4
5
 
5
6
namespace VQDR.Expression {
6
7
  public abstract class FunctionToken : Token {
8
9
      base (0);
9
10
    }
10
11
    
 
12
    private struct Entry {
 
13
      public string key;
 
14
      public Type? val;
 
15
    }
 
16
    
 
17
    
11
18
    // We only store the type, as that is what is important.
12
19
    private static Gee.HashMap<string, Type?> _allowed_functions;
13
20
    
14
 
    protected const long UNDEFINED = long.MIN + 1; //+1 so that I can still use Long.MIN_VALUE.
 
21
    // +1 so that I can still use int64.MIN_VALUE.
 
22
    protected const int64 UNDEFINED = int64.MIN + 1; 
15
23
    /** Right arrow */
16
24
    protected const string CH_RARR = "\u2192";
17
25
    /** Left arrow */
65
73
    /** Truncated output: equal */
66
74
    protected const string SYM_TRUNK_PART_EQUAL = CH_EQUAL; //"="
67
75
    /** Truncated output: begin */
68
 
    protected const string SYM_TRUNK_BEGIN = SYM_BEGIN + SYM_TRUNK_PART_ELLIPSIS + SYM_TRUNK_PART_EQUAL; //"[...="
 
76
    protected const string SYM_TRUNK_BEGIN = SYM_BEGIN
 
77
                                           + SYM_TRUNK_PART_ELLIPSIS
 
78
                                           + SYM_TRUNK_PART_EQUAL; //"[...="
69
79
    /** Truncated output: end */
70
80
    protected const string SYM_TRUNK_END = SYM_END; //"]"
71
81
    
72
82
    construct {
73
 
      _allowed_functions = new Gee.HashMap<string, Type?> ();
74
 
      this.priority = PRIO_FUNCTION;
 
83
      this.priority = Prio.FUNCTION;
75
84
    }
76
85
    
 
86
    
77
87
    /**
78
 
     * Ititialise the right functon token by it's name.
 
88
     * Initialise the right function token by it's name.
79
89
     * 
80
90
     * @param token Token of the Function.
81
91
     * @param position Token position.
82
92
     * @return An instance representing the function, or @c null if not found.
83
93
     */
84
 
    public static FunctionToken? init_token (string token, int position) {
85
 
      FunctionToken? retval = null;
 
94
    public static FunctionToken? init_token (string token, int32 position) {
 
95
      
 
96
      if (_allowed_functions == null) {
 
97
        // Initialise the HashMap if it is not created.
 
98
        _allowed_functions = new Gee.HashMap<string, Type?> ();
 
99
        
 
100
        Entry[] entries = {
 
101
            {"round_up", typeof (RoundUpFunctionToken)},
 
102
            {"round_down", typeof (RoundDownFunctionToken)},
 
103
            {"roll_and_keep", typeof (RollAndKeepFunctionToken)},
 
104
            {null, null}
 
105
        };
 
106
        
 
107
        foreach (Entry e in entries) {
 
108
          _allowed_functions.@set (e.key, e.val);
 
109
        }
 
110
        
 
111
      }
86
112
      
87
113
      // We get the token type.
88
114
      Type? t = _allowed_functions.@get (token.down ());
89
115
      
90
116
      if (t != null) {
91
117
          // Construct a new instance of the token.
92
 
          retval = (FunctionToken) GLib.Object.@new (t, null, null);
93
 
          
 
118
          return (FunctionToken) GLib.Object.@new (t, null, null);
94
119
      }
95
 
      
96
 
      return retval;
 
120
      return null;
97
121
    }
98
122
    
99
123
    /**
122
146
      return ret_val;
123
147
    }
124
148
    
125
 
    protected long get_optional_child_raw_result (Context instance,
126
 
                                                  int index,
127
 
                                                  long default_result)
 
149
    protected FastNumber get_optional_child_raw_result (Context instance,
 
150
                                                  int32 index,
 
151
                                                  int64 default_result)
128
152
                                                  throws GLib.Error {
129
153
     Token? tmp_roll = get_child (index);
130
154
    
132
156
         tmp_roll.evaluate (instance);
133
157
         return tmp_roll.result_value;
134
158
     } else {
135
 
       return default_result;
 
159
       return FastNumber.raw (default_result);
136
160
     }
 
161
     
137
162
    }
138
 
    
139
 
    
140
 
    
141
163
  }
142
164
}