/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;
12.1.1 by Gustav Hartvigsson
* Switch to using FastNumber instead of something else.
4
using VQDR.Common;
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
    
21
    protected const long UNDEFINED = long.MIN + 1; //+1 so that I can still use Long.MIN_VALUE.
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
     */
91
    public static FunctionToken? init_token (string token, int 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)},
100
            {null, null}
101
        };
102
        
103
        foreach (Entry e in entries) {
104
          _allowed_functions.@set (e.key, e.val);
105
        }
106
        
107
      }
108
      
1 by Gustav Hartvigsson
* Initial code - far from done
109
      FunctionToken? retval = null;
110
      
111
      // We get the token type.
112
      Type? t = _allowed_functions.@get (token.down ());
113
      
114
      if (t != null) {
115
          // Construct a new instance of the token.
116
          retval = (FunctionToken) GLib.Object.@new (t, null, null);
117
          
118
      }
119
      
120
      return retval;
121
    }
122
    
123
    /**
124
     * Add a FunctionToken Type to the list of allowed functions.
125
     * 
126
     * Note: The Type must be derived from FunctionToken, as there is an
127
     *        assert that chkecks.
128
     */
129
    public static void add_function (string token, Type t) {
130
      assert (t.is_a (typeof (FunctionToken)) == true);
131
      _allowed_functions.@set (token, t);
132
    }
133
    
134
    public static string get_function_name (Type t) {
135
      assert (t.is_a (typeof (FunctionToken)) == true);
136
      string ret_val = null;
137
      
138
      _allowed_functions.map_iterator ().@foreach ((k,v) => {
139
        if (v == t) {
140
          ret_val = k;
141
          return false;
142
        }
143
        return true;
144
      });
145
      
146
      return ret_val;
147
    }
148
    
12.1.1 by Gustav Hartvigsson
* Switch to using FastNumber instead of something else.
149
    protected FastNumber get_optional_child_raw_result (Context instance,
1 by Gustav Hartvigsson
* Initial code - far from done
150
                                                  int index,
151
                                                  long default_result)
152
                                                  throws GLib.Error {
153
     Token? tmp_roll = get_child (index);
154
    
155
     if (tmp_roll != null) {
156
         tmp_roll.evaluate (instance);
157
         return tmp_roll.result_value;
158
     } else {
12.1.2 by Gustav Hartvigsson
* Fastnumber is now a struct,
159
       return FastNumber.raw (default_result);
1 by Gustav Hartvigsson
* Initial code - far from done
160
     }
26 by Gustav Hartvigsson
* Fixed the "allowed function token type" map...
161
     
1 by Gustav Hartvigsson
* Initial code - far from done
162
    }
163
  }
164
}