/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using Gee;

using VQDR.Expression;
using Vee;

namespace VQDR.Expression {
  public abstract class FunctionToken : Token {
    protected FunctionToken () {
      base (0);
    }
    
    private struct Entry {
      public string key;
      public Type? val;
    }
    
    
    // We only store the type, as that is what is important.
    private static Gee.HashMap<string, Type?> _allowed_functions;
    
    // +1 so that I can still use int64.MIN_VALUE.
    protected const int64 UNDEFINED = int64.MIN + 1; 
    /** Right arrow */
    protected const string CH_RARR = "\u2192";
    /** Left arrow */
    protected const string CH_LARR = "\u2190";
    /** Round up open bracket */
    protected const string CH_RUP_OP = "\u2308";
    /** Round up closed bracket */
    protected const string CH_RUP_CL = "\u2309";
    /** Round down open bracket */
    protected const string CH_RDN_OP = "\u230a";
    /** Round down closed bracket */
    protected const string CH_RDN_CL = "\u230b";
    /** Absolute open bracket */
    protected const string CH_ABS_OP = "|";
    /** Absolute closed bracket */
    protected const string CH_ABS_CL = "|";
    /** Greater than */
    protected const string CH_GT = ">";
    /** Lower than */
    protected const string CH_LT = "<";
    /** Equal */
    protected const string CH_EQUAL = "=";

    /** Begin of a complex result */
    protected const string SYM_BEGIN = "[";
    /** End of a complex result */
    protected const string SYM_END = "]";
    /** Begin of a complex result, alternative */
    protected const string SYM_BEGIN_ALT = "{";
    /** End of a complex result, alternative */
    protected const string SYM_END_ALT = "}";
    /** Separator for different roll result */
    protected const string SYM_SEP = ",";
    /** Separator for different value of same roll */
    protected const string SYM_SEP_SAME = ":";
    /** Separator for  overall (fumble, critical, botch, glitch) */
    protected const string SYM_SEP_ = "\u2261"; //"=" (with 3 lines)
    /** Denotes a success */
    protected const string SYM_SUCCESS = "!";
    /** Denotes a failure */
    protected const string SYM_FAILURE = "*";
    /** Denotes an extra result */
    protected const string SYM_EXTRA = "!";
    /** Denotes a selected result */
    protected const string SYM_SELECTED = "!";
    /** Separator for exploding rolls */
    protected const string SYM_EXPLODE = "\u00bb"; //">>"

    /** Truncated output: ellipsis */
    protected const string SYM_TRUNK_PART_ELLIPSIS = "\u2026"; //"..."
    /** Truncated output: equal */
    protected const string SYM_TRUNK_PART_EQUAL = CH_EQUAL; //"="
    /** Truncated output: begin */
    protected const string SYM_TRUNK_BEGIN = SYM_BEGIN
                                           + SYM_TRUNK_PART_ELLIPSIS
                                           + SYM_TRUNK_PART_EQUAL; //"[...="
    /** Truncated output: end */
    protected const string SYM_TRUNK_END = SYM_END; //"]"
    
    construct {
      this.priority = Prio.FUNCTION;
    }
    
    
    /**
     * Initialise the right function token by it's name.
     * 
     * @param token Token of the Function.
     * @param position Token position.
     * @return An instance representing the function, or @c null if not found.
     */
    public static FunctionToken? init_token (string token, int32 position) {
      
      if (_allowed_functions == null) {
        // Initialise the HashMap if it is not created.
        _allowed_functions = new Gee.HashMap<string, Type?> ();
        
        Entry[] entries = {
            {"round_up", typeof (RoundUpFunctionToken)},
            {"round_down", typeof (RoundDownFunctionToken)},
            {"roll_and_keep", typeof (RollAndKeepFunctionToken)},
            {null, null}
        };
        
        foreach (Entry e in entries) {
          _allowed_functions.@set (e.key, e.val);
        }
        
      }
      
      // We get the token type.
      Type? t = _allowed_functions.@get (token.down ());
      
      if (t != null) {
          // Construct a new instance of the token.
          return (FunctionToken) GLib.Object.@new (t, null, null);
      }
      return null;
    }
    
    /**
     * Add a FunctionToken Type to the list of allowed functions.
     * 
     * Note: The Type must be derived from FunctionToken, as there is an
     *        assert that chkecks.
     */
    public static void add_function (string token, Type t) {
      assert (t.is_a (typeof (FunctionToken)) == true);
      _allowed_functions.@set (token, t);
    }
    
    public static string get_function_name (Type t) {
      assert (t.is_a (typeof (FunctionToken)) == true);
      string ret_val = null;
      
      _allowed_functions.map_iterator ().@foreach ((k,v) => {
        if (v == t) {
          ret_val = k;
          return false;
        }
        return true;
      });
      
      return ret_val;
    }
    
    protected FastNumber get_optional_child_raw_result (Context instance,
                                                  int32 index,
                                                  int64 default_result)
                                                  throws GLib.Error {
     Token? tmp_roll = get_child (index);
    
     if (tmp_roll != null) {
         tmp_roll.evaluate (instance);
         return tmp_roll.result_value;
     } else {
       return FastNumber.raw (default_result);
     }
     
    }
  }
}