/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: 2022-06-01 12:44:04 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20220601124404-mpv4761nr16f0duq
run_gdb.sh +x

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 Utils;
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
    protected const int64 UNDEFINED = int64.MIN + 1; //+1 so that I can still use int64.MIN_VALUE.
15
22
    /** Right arrow */
16
23
    protected const string CH_RARR = "\u2192";
17
24
    /** Left arrow */
70
77
    protected const string SYM_TRUNK_END = SYM_END; //"]"
71
78
    
72
79
    construct {
73
 
      _allowed_functions = new Gee.HashMap<string, Type?> ();
74
 
      this.priority = PRIO_FUNCTION;
 
80
      this.priority = Prio.FUNCTION;
75
81
    }
76
82
    
 
83
    
77
84
    /**
78
85
     * Ititialise the right functon token by it's name.
79
86
     * 
81
88
     * @param position Token position.
82
89
     * @return An instance representing the function, or @c null if not found.
83
90
     */
84
 
    public static FunctionToken? init_token (string token, int position) {
85
 
      FunctionToken? retval = null;
 
91
    public static FunctionToken? init_token (string token, int32 position) {
 
92
      
 
93
      if (_allowed_functions == null) {
 
94
        // Intialise the HashMap if it is not created.
 
95
        _allowed_functions = new Gee.HashMap<string, Type?> ();
 
96
        
 
97
        Entry[] entries = {
 
98
            {"round_up", typeof (RoundUpFunctionToken)},
 
99
            {"round_down", typeof (RoundDownFunctionToken)},
 
100
            {"roll_and_keep", typeof (RollAndKeepFunctionToken)},
 
101
            {null, null}
 
102
        };
 
103
        
 
104
        foreach (Entry e in entries) {
 
105
          _allowed_functions.@set (e.key, e.val);
 
106
        }
 
107
        
 
108
      }
86
109
      
87
110
      // We get the token type.
88
111
      Type? t = _allowed_functions.@get (token.down ());
89
112
      
90
113
      if (t != null) {
91
114
          // Construct a new instance of the token.
92
 
          retval = (FunctionToken) GLib.Object.@new (t, null, null);
93
 
          
 
115
          return (FunctionToken) GLib.Object.@new (t, null, null);
94
116
      }
95
 
      
96
 
      return retval;
 
117
      return null;
97
118
    }
98
119
    
99
120
    /**
122
143
      return ret_val;
123
144
    }
124
145
    
125
 
    protected long get_optional_child_raw_result (Context instance,
126
 
                                                  int index,
127
 
                                                  long default_result)
 
146
    protected FastNumber get_optional_child_raw_result (Context instance,
 
147
                                                  int32 index,
 
148
                                                  int64 default_result)
128
149
                                                  throws GLib.Error {
129
150
     Token? tmp_roll = get_child (index);
130
151
    
132
153
         tmp_roll.evaluate (instance);
133
154
         return tmp_roll.result_value;
134
155
     } else {
135
 
       return default_result;
 
156
       return FastNumber.raw (default_result);
136
157
     }
 
158
     
137
159
    }
138
 
    
139
 
    
140
 
    
141
160
  }
142
161
}