-
Notifications
You must be signed in to change notification settings - Fork 499
/
Copy pathNativeFunctionCall.cs
313 lines (248 loc) · 11.1 KB
/
NativeFunctionCall.cs
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.Collections.Generic;
namespace Ink.Runtime
{
internal class NativeFunctionCall : Runtime.Object
{
public const string Add = "+";
public const string Subtract = "-";
public const string Divide = "/";
public const string Multiply = "*";
public const string Mod = "%";
public const string Negate = "~";
public const string Equal = "==";
public const string Greater = ">";
public const string Less = "<";
public const string GreaterThanOrEquals = ">=";
public const string LessThanOrEquals = "<=";
public const string NotEquals = "!=";
public const string Not = "!";
public const string And = "&&";
public const string Or = "||";
public const string Min = "MIN";
public const string Max = "MAX";
public static NativeFunctionCall CallWithName(string functionName)
{
return new NativeFunctionCall (functionName);
}
public static bool CallExistsWithName(string functionName)
{
GenerateNativeFunctionsIfNecessary ();
return _nativeFunctions.ContainsKey (functionName);
}
public string name {
get {
return _name;
}
protected set {
_name = value;
if( !_isPrototype )
_prototype = _nativeFunctions [_name];
}
}
string _name;
public int numberOfParameters {
get {
if (_prototype) {
return _prototype.numberOfParameters;
} else {
return _numberOfParameters;
}
}
protected set {
_numberOfParameters = value;
}
}
int _numberOfParameters;
public Runtime.Object Call(List<Runtime.Object> parameters)
{
if (_prototype) {
return _prototype.Call(parameters);
}
if (numberOfParameters != parameters.Count) {
throw new System.Exception ("Unexpected number of parameters");
}
foreach (var p in parameters) {
if (p is Void)
throw new StoryException ("Attempting to perform operation on a void value. Did you forget to 'return' a value from a function you called here?");
}
var coercedParams = CoerceValuesToSingleType (parameters);
ValueType coercedType = coercedParams[0].valueType;
if (coercedType == ValueType.Int) {
return Call<int> (coercedParams);
} else if (coercedType == ValueType.Float) {
return Call<float> (coercedParams);
} else if (coercedType == ValueType.String) {
return Call<string> (coercedParams);
} else if (coercedType == ValueType.DivertTarget) {
return Call<Path> (coercedParams);
}
return null;
}
Value Call<T>(List<Value> parametersOfSingleType)
{
Value param1 = (Value) parametersOfSingleType [0];
ValueType valType = param1.valueType;
var val1 = (Value<T>)param1;
int paramCount = parametersOfSingleType.Count;
if (paramCount == 2 || paramCount == 1) {
object opForTypeObj = null;
if (!_operationFuncs.TryGetValue (valType, out opForTypeObj)) {
throw new StoryException ("Can not perform operation '"+this.name+"' on "+valType);
}
// Binary
if (paramCount == 2) {
Value param2 = (Value) parametersOfSingleType [1];
var val2 = (Value<T>)param2;
var opForType = (BinaryOp<T>)opForTypeObj;
// Return value unknown until it's evaluated
object resultVal = opForType (val1.value, val2.value);
return Value.Create (resultVal);
}
// Unary
else {
var opForType = (UnaryOp<T>)opForTypeObj;
var resultVal = opForType (val1.value);
return Value.Create (resultVal);
}
}
else {
throw new System.Exception ("Unexpected number of parameters to NativeFunctionCall: " + parametersOfSingleType.Count);
}
}
List<Value> CoerceValuesToSingleType(List<Runtime.Object> parametersIn)
{
ValueType valType = ValueType.Int;
// Find out what the output type is
// "higher level" types infect both so that binary operations
// use the same type on both sides. e.g. binary operation of
// int and float causes the int to be casted to a float.
foreach (var obj in parametersIn) {
var val = (Value)obj;
if (val.valueType > valType) {
valType = val.valueType;
}
}
// Coerce to this chosen type
var parametersOut = new List<Value> ();
foreach (Value val in parametersIn) {
var castedValue = val.Cast (valType);
parametersOut.Add (castedValue);
}
return parametersOut;
}
public NativeFunctionCall(string name)
{
GenerateNativeFunctionsIfNecessary ();
this.name = name;
}
// Require default constructor for serialisation
public NativeFunctionCall() {
GenerateNativeFunctionsIfNecessary ();
}
// Only called internally to generate prototypes
NativeFunctionCall (string name, int numberOfParamters)
{
_isPrototype = true;
this.name = name;
this.numberOfParameters = numberOfParamters;
}
static void GenerateNativeFunctionsIfNecessary()
{
if (_nativeFunctions == null) {
_nativeFunctions = new Dictionary<string, NativeFunctionCall> ();
// Int operations
AddIntBinaryOp(Add, (x, y) => x + y);
AddIntBinaryOp(Subtract, (x, y) => x - y);
AddIntBinaryOp(Multiply, (x, y) => x * y);
AddIntBinaryOp(Divide, (x, y) => x / y);
AddIntBinaryOp(Mod, (x, y) => x % y);
AddIntUnaryOp (Negate, x => -x);
AddIntBinaryOp(Equal, (x, y) => x == y ? 1 : 0);
AddIntBinaryOp(Greater, (x, y) => x > y ? 1 : 0);
AddIntBinaryOp(Less, (x, y) => x < y ? 1 : 0);
AddIntBinaryOp(GreaterThanOrEquals, (x, y) => x >= y ? 1 : 0);
AddIntBinaryOp(LessThanOrEquals, (x, y) => x <= y ? 1 : 0);
AddIntBinaryOp(NotEquals, (x, y) => x != y ? 1 : 0);
AddIntUnaryOp (Not, x => (x == 0) ? 1 : 0);
AddIntBinaryOp(And, (x, y) => x != 0 && y != 0 ? 1 : 0);
AddIntBinaryOp(Or, (x, y) => x != 0 || y != 0 ? 1 : 0);
AddIntBinaryOp(Max, (x, y) => Math.Max(x, y));
AddIntBinaryOp(Min, (x, y) => Math.Min(x, y));
// Float operations
AddFloatBinaryOp(Add, (x, y) => x + y);
AddFloatBinaryOp(Subtract, (x, y) => x - y);
AddFloatBinaryOp(Multiply, (x, y) => x * y);
AddFloatBinaryOp(Divide, (x, y) => x / y);
AddFloatBinaryOp(Mod, (x, y) => x % y); // TODO: Is this the operation we want for floats?
AddFloatUnaryOp (Negate, x => -x);
AddFloatBinaryOp(Equal, (x, y) => x == y ? (int)1 : (int)0);
AddFloatBinaryOp(Greater, (x, y) => x > y ? (int)1 : (int)0);
AddFloatBinaryOp(Less, (x, y) => x < y ? (int)1 : (int)0);
AddFloatBinaryOp(GreaterThanOrEquals, (x, y) => x >= y ? (int)1 : (int)0);
AddFloatBinaryOp(LessThanOrEquals, (x, y) => x <= y ? (int)1 : (int)0);
AddFloatBinaryOp(NotEquals, (x, y) => x != y ? (int)1 : (int)0);
AddFloatUnaryOp (Not, x => (x == 0.0f) ? (int)1 : (int)0);
AddFloatBinaryOp(And, (x, y) => x != 0.0f && y != 0.0f ? (int)1 : (int)0);
AddFloatBinaryOp(Or, (x, y) => x != 0.0f || y != 0.0f ? (int)1 : (int)0);
AddFloatBinaryOp(Max, (x, y) => Math.Max(x, y));
AddFloatBinaryOp(Min, (x, y) => Math.Min(x, y));
// String operations
AddStringBinaryOp(Add, (x, y) => x + y); // concat
AddStringBinaryOp(Equal, (x, y) => x.Equals(y) ? (int)1 : (int)0);
// Special case: The only operation you can do on divert target values
BinaryOp<Path> divertTargetsEqual = (Path d1, Path d2) => {
return d1.Equals(d2) ? 1 : 0;
};
AddOpToNativeFunc (Equal, 2, ValueType.DivertTarget, divertTargetsEqual);
}
}
void AddOpFuncForType(ValueType valType, object op)
{
if (_operationFuncs == null) {
_operationFuncs = new Dictionary<ValueType, object> ();
}
_operationFuncs [valType] = op;
}
static void AddOpToNativeFunc(string name, int args, ValueType valType, object op)
{
NativeFunctionCall nativeFunc = null;
if (!_nativeFunctions.TryGetValue (name, out nativeFunc)) {
nativeFunc = new NativeFunctionCall (name, args);
_nativeFunctions [name] = nativeFunc;
}
nativeFunc.AddOpFuncForType (valType, op);
}
static void AddIntBinaryOp(string name, BinaryOp<int> op)
{
AddOpToNativeFunc (name, 2, ValueType.Int, op);
}
static void AddIntUnaryOp(string name, UnaryOp<int> op)
{
AddOpToNativeFunc (name, 1, ValueType.Int, op);
}
static void AddFloatBinaryOp(string name, BinaryOp<float> op)
{
AddOpToNativeFunc (name, 2, ValueType.Float, op);
}
static void AddStringBinaryOp(string name, BinaryOp<string> op)
{
AddOpToNativeFunc (name, 2, ValueType.String, op);
}
static void AddFloatUnaryOp(string name, UnaryOp<float> op)
{
AddOpToNativeFunc (name, 1, ValueType.Float, op);
}
public override string ToString ()
{
return "Native '" + name + "'";
}
delegate object BinaryOp<T>(T left, T right);
delegate object UnaryOp<T>(T val);
NativeFunctionCall _prototype;
bool _isPrototype;
// Operations for each data type, for a single operation (e.g. "+")
Dictionary<ValueType, object> _operationFuncs;
static Dictionary<string, NativeFunctionCall> _nativeFunctions;
}
}