/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: 2020-06-07 18:48:24 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20200607184824-jf14f7a1b1di2i2q
* Initial code - far from done

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;
5
4
 
6
5
namespace VQDR.Expression {
7
6
  public abstract class FunctionToken : Token {
9
8
      base (0);
10
9
    }
11
10
    
12
 
    private struct Entry {
13
 
      public string key;
14
 
      public Type? val;
15
 
    }
16
 
    
17
 
    
18
11
    // We only store the type, as that is what is important.
19
12
    private static Gee.HashMap<string, Type?> _allowed_functions;
20
13
    
77
70
    protected const string SYM_TRUNK_END = SYM_END; //"]"
78
71
    
79
72
    construct {
80
 
      this.priority = Prio.FUNCTION;
 
73
      _allowed_functions = new Gee.HashMap<string, Type?> ();
 
74
      this.priority = PRIO_FUNCTION;
81
75
    }
82
76
    
83
 
    
84
77
    /**
85
78
     * Ititialise the right functon token by it's name.
86
79
     * 
89
82
     * @return An instance representing the function, or @c null if not found.
90
83
     */
91
84
    public static FunctionToken? init_token (string token, int 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
 
      }
 
85
      FunctionToken? retval = null;
109
86
      
110
87
      // We get the token type.
111
88
      Type? t = _allowed_functions.@get (token.down ());
112
89
      
113
90
      if (t != null) {
114
91
          // Construct a new instance of the token.
115
 
          return (FunctionToken) GLib.Object.@new (t, null, null);
 
92
          retval = (FunctionToken) GLib.Object.@new (t, null, null);
 
93
          
116
94
      }
117
 
      return null;
 
95
      
 
96
      return retval;
118
97
    }
119
98
    
120
99
    /**
143
122
      return ret_val;
144
123
    }
145
124
    
146
 
    protected FastNumber get_optional_child_raw_result (Context instance,
 
125
    protected long get_optional_child_raw_result (Context instance,
147
126
                                                  int index,
148
127
                                                  long default_result)
149
128
                                                  throws GLib.Error {
153
132
         tmp_roll.evaluate (instance);
154
133
         return tmp_roll.result_value;
155
134
     } else {
156
 
       return FastNumber.raw (default_result);
 
135
       return default_result;
157
136
     }
158
 
     
159
137
    }
 
138
    
 
139
    
 
140
    
160
141
  }
161
142
}