/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;
4
5
namespace VQDR.Expression {
6
  public abstract class FunctionToken : Token {
7
    protected FunctionToken () {
8
      base (0);
9
    }
10
    
11
    // We only store the type, as that is what is important.
12
    private static Gee.HashMap<string, Type?> _allowed_functions;
13
    
14
    protected const long UNDEFINED = long.MIN + 1; //+1 so that I can still use Long.MIN_VALUE.
15
    /** Right arrow */
16
    protected const string CH_RARR = "\u2192";
17
    /** Left arrow */
18
    protected const string CH_LARR = "\u2190";
19
    /** Round up open bracket */
20
    protected const string CH_RUP_OP = "\u2308";
21
    /** Round up closed bracket */
22
    protected const string CH_RUP_CL = "\u2309";
23
    /** Round down open bracket */
24
    protected const string CH_RDN_OP = "\u230a";
25
    /** Round down closed bracket */
26
    protected const string CH_RDN_CL = "\u230b";
27
    /** Absolute open bracket */
28
    protected const string CH_ABS_OP = "|";
29
    /** Absolute closed bracket */
30
    protected const string CH_ABS_CL = "|";
31
    /** Greater than */
32
    protected const string CH_GT = ">";
33
    /** Lower than */
34
    protected const string CH_LT = "<";
35
    /** Equal */
36
    protected const string CH_EQUAL = "=";
37
38
    /** Begin of a complex result */
39
    protected const string SYM_BEGIN = "[";
40
    /** End of a complex result */
41
    protected const string SYM_END = "]";
42
    /** Begin of a complex result, alternative */
43
    protected const string SYM_BEGIN_ALT = "{";
44
    /** End of a complex result, alternative */
45
    protected const string SYM_END_ALT = "}";
46
    /** Separator for different roll result */
47
    protected const string SYM_SEP = ",";
48
    /** Separator for different value of same roll */
49
    protected const string SYM_SEP_SAME = ":";
50
    /** Separator for  overall (fumble, critical, botch, glitch) */
51
    protected const string SYM_SEP_ = "\u2261"; //"=" (with 3 lines)
52
    /** Denotes a success */
53
    protected const string SYM_SUCCESS = "!";
54
    /** Denotes a failure */
55
    protected const string SYM_FAILURE = "*";
56
    /** Denotes an extra result */
57
    protected const string SYM_EXTRA = "!";
58
    /** Denotes a selected result */
59
    protected const string SYM_SELECTED = "!";
60
    /** Separator for exploding rolls */
61
    protected const string SYM_EXPLODE = "\u00bb"; //">>"
62
63
    /** Truncated output: ellipsis */
64
    protected const string SYM_TRUNK_PART_ELLIPSIS = "\u2026"; //"..."
65
    /** Truncated output: equal */
66
    protected const string SYM_TRUNK_PART_EQUAL = CH_EQUAL; //"="
67
    /** Truncated output: begin */
68
    protected const string SYM_TRUNK_BEGIN = SYM_BEGIN + SYM_TRUNK_PART_ELLIPSIS + SYM_TRUNK_PART_EQUAL; //"[...="
69
    /** Truncated output: end */
70
    protected const string SYM_TRUNK_END = SYM_END; //"]"
71
    
72
    construct {
73
      _allowed_functions = new Gee.HashMap<string, Type?> ();
74
      this.priority = PRIO_FUNCTION;
75
    }
76
    
77
    /**
78
     * Ititialise the right functon token by it's name.
79
     * 
80
     * @param token Token of the Function.
81
     * @param position Token position.
82
     * @return An instance representing the function, or @c null if not found.
83
     */
84
    public static FunctionToken? init_token (string token, int position) {
85
      FunctionToken? retval = null;
86
      
87
      // We get the token type.
88
      Type? t = _allowed_functions.@get (token.down ());
89
      
90
      if (t != null) {
91
          // Construct a new instance of the token.
92
          retval = (FunctionToken) GLib.Object.@new (t, null, null);
93
          
94
      }
95
      
96
      return retval;
97
    }
98
    
99
    /**
100
     * Add a FunctionToken Type to the list of allowed functions.
101
     * 
102
     * Note: The Type must be derived from FunctionToken, as there is an
103
     *        assert that chkecks.
104
     */
105
    public static void add_function (string token, Type t) {
106
      assert (t.is_a (typeof (FunctionToken)) == true);
107
      _allowed_functions.@set (token, t);
108
    }
109
    
110
    public static string get_function_name (Type t) {
111
      assert (t.is_a (typeof (FunctionToken)) == true);
112
      string ret_val = null;
113
      
114
      _allowed_functions.map_iterator ().@foreach ((k,v) => {
115
        if (v == t) {
116
          ret_val = k;
117
          return false;
118
        }
119
        return true;
120
      });
121
      
122
      return ret_val;
123
    }
124
    
125
    protected long get_optional_child_raw_result (Context instance,
126
                                                  int index,
127
                                                  long default_result)
128
                                                  throws GLib.Error {
129
     Token? tmp_roll = get_child (index);
130
    
131
     if (tmp_roll != null) {
132
         tmp_roll.evaluate (instance);
133
         return tmp_roll.result_value;
134
     } else {
135
       return default_result;
136
     }
137
    }
138
    
139
    
140
    
141
  }
142
}