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