/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk
1 by Gustav Hartvigsson
* Initial code - far from done
1
using Gee;
2
3
using VQDR.Expression;
36 by Gustav Hartvigsson
Refactoring.
4
using Utils;
1 by Gustav Hartvigsson
* Initial code - far from done
5
6
namespace VQDR.Expression {
7
  public abstract class FunctionToken : Token {
8
    protected FunctionToken () {
9
      base (0);
10
    }
11
    
26 by Gustav Hartvigsson
* Fixed the "allowed function token type" map...
12
    private struct Entry {
13
      public string key;
14
      public Type? val;
15
    }
16
    
17
    
1 by Gustav Hartvigsson
* Initial code - far from done
18
    // We only store the type, as that is what is important.
19
    private static Gee.HashMap<string, Type?> _allowed_functions;
20
    
52.1.2 by Gustav Hartvigsson
no more 's
21
    protected const int64 UNDEFINED = int64.MIN + 1; //+1 so that I can still use int64.MIN_VALUE.
1 by Gustav Hartvigsson
* Initial code - far from done
22
    /** Right arrow */
23
    protected const string CH_RARR = "\u2192";
24
    /** Left arrow */
25
    protected const string CH_LARR = "\u2190";
26
    /** Round up open bracket */
27
    protected const string CH_RUP_OP = "\u2308";
28
    /** Round up closed bracket */
29
    protected const string CH_RUP_CL = "\u2309";
30
    /** Round down open bracket */
31
    protected const string CH_RDN_OP = "\u230a";
32
    /** Round down closed bracket */
33
    protected const string CH_RDN_CL = "\u230b";
34
    /** Absolute open bracket */
35
    protected const string CH_ABS_OP = "|";
36
    /** Absolute closed bracket */
37
    protected const string CH_ABS_CL = "|";
38
    /** Greater than */
39
    protected const string CH_GT = ">";
40
    /** Lower than */
41
    protected const string CH_LT = "<";
42
    /** Equal */
43
    protected const string CH_EQUAL = "=";
44
45
    /** Begin of a complex result */
46
    protected const string SYM_BEGIN = "[";
47
    /** End of a complex result */
48
    protected const string SYM_END = "]";
49
    /** Begin of a complex result, alternative */
50
    protected const string SYM_BEGIN_ALT = "{";
51
    /** End of a complex result, alternative */
52
    protected const string SYM_END_ALT = "}";
53
    /** Separator for different roll result */
54
    protected const string SYM_SEP = ",";
55
    /** Separator for different value of same roll */
56
    protected const string SYM_SEP_SAME = ":";
57
    /** Separator for  overall (fumble, critical, botch, glitch) */
58
    protected const string SYM_SEP_ = "\u2261"; //"=" (with 3 lines)
59
    /** Denotes a success */
60
    protected const string SYM_SUCCESS = "!";
61
    /** Denotes a failure */
62
    protected const string SYM_FAILURE = "*";
63
    /** Denotes an extra result */
64
    protected const string SYM_EXTRA = "!";
65
    /** Denotes a selected result */
66
    protected const string SYM_SELECTED = "!";
67
    /** Separator for exploding rolls */
68
    protected const string SYM_EXPLODE = "\u00bb"; //">>"
69
70
    /** Truncated output: ellipsis */
71
    protected const string SYM_TRUNK_PART_ELLIPSIS = "\u2026"; //"..."
72
    /** Truncated output: equal */
73
    protected const string SYM_TRUNK_PART_EQUAL = CH_EQUAL; //"="
74
    /** Truncated output: begin */
75
    protected const string SYM_TRUNK_BEGIN = SYM_BEGIN + SYM_TRUNK_PART_ELLIPSIS + SYM_TRUNK_PART_EQUAL; //"[...="
76
    /** Truncated output: end */
77
    protected const string SYM_TRUNK_END = SYM_END; //"]"
78
    
79
    construct {
14 by Gustav Hartvigsson
* Use enum instead of static values for the priority of operations.
80
      this.priority = Prio.FUNCTION;
1 by Gustav Hartvigsson
* Initial code - far from done
81
    }
82
    
26 by Gustav Hartvigsson
* Fixed the "allowed function token type" map...
83
    
1 by Gustav Hartvigsson
* Initial code - far from done
84
    /**
85
     * Ititialise the right functon token by it's name.
86
     * 
87
     * @param token Token of the Function.
88
     * @param position Token position.
89
     * @return An instance representing the function, or @c null if not found.
90
     */
52.1.3 by Gustav Hartvigsson
int -> int32
91
    public static FunctionToken? init_token (string token, int32 position) {
26 by Gustav Hartvigsson
* Fixed the "allowed function token type" map...
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)},
35 by Gustav Hartvigsson
a few things: FastNumbers, simplifications and AbstractPoolToken.
100
            {"roll_and_keep", typeof (RollAndKeepFunctionToken)},
26 by Gustav Hartvigsson
* Fixed the "allowed function token type" map...
101
            {null, null}
102
        };
103
        
104
        foreach (Entry e in entries) {
105
          _allowed_functions.@set (e.key, e.val);
106
        }
107
        
108
      }
109
      
1 by Gustav Hartvigsson
* Initial code - far from done
110
      // We get the token type.
111
      Type? t = _allowed_functions.@get (token.down ());
112
      
113
      if (t != null) {
114
          // Construct a new instance of the token.
45 by Gustav Hartvigsson
* woopes.
115
          return (FunctionToken) GLib.Object.@new (t, null, null);
1 by Gustav Hartvigsson
* Initial code - far from done
116
      }
45 by Gustav Hartvigsson
* woopes.
117
      return null;
1 by Gustav Hartvigsson
* Initial code - far from done
118
    }
119
    
120
    /**
121
     * Add a FunctionToken Type to the list of allowed functions.
122
     * 
123
     * Note: The Type must be derived from FunctionToken, as there is an
124
     *        assert that chkecks.
125
     */
126
    public static void add_function (string token, Type t) {
127
      assert (t.is_a (typeof (FunctionToken)) == true);
128
      _allowed_functions.@set (token, t);
129
    }
130
    
131
    public static string get_function_name (Type t) {
132
      assert (t.is_a (typeof (FunctionToken)) == true);
133
      string ret_val = null;
134
      
135
      _allowed_functions.map_iterator ().@foreach ((k,v) => {
136
        if (v == t) {
137
          ret_val = k;
138
          return false;
139
        }
140
        return true;
141
      });
142
      
143
      return ret_val;
144
    }
145
    
12.1.1 by Gustav Hartvigsson
* Switch to using FastNumber instead of something else.
146
    protected FastNumber get_optional_child_raw_result (Context instance,
52.1.3 by Gustav Hartvigsson
int -> int32
147
                                                  int32 index,
52.1.2 by Gustav Hartvigsson
no more 's
148
                                                  int64 default_result)
1 by Gustav Hartvigsson
* Initial code - far from done
149
                                                  throws GLib.Error {
150
     Token? tmp_roll = get_child (index);
151
    
152
     if (tmp_roll != null) {
153
         tmp_roll.evaluate (instance);
154
         return tmp_roll.result_value;
155
     } else {
12.1.2 by Gustav Hartvigsson
* Fastnumber is now a struct,
156
       return FastNumber.raw (default_result);
1 by Gustav Hartvigsson
* Initial code - far from done
157
     }
26 by Gustav Hartvigsson
* Fixed the "allowed function token type" map...
158
     
1 by Gustav Hartvigsson
* Initial code - far from done
159
    }
160
  }
161
}