Skip to content

Commit ee008e5

Browse files
committed
Make ~ the set invert operator, and use _ as the negation placeholder
1 parent fa986b3 commit ee008e5

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

ink-engine-runtime/NativeFunctionCall.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal class NativeFunctionCall : Runtime.Object
1010
public const string Divide = "/";
1111
public const string Multiply = "*";
1212
public const string Mod = "%";
13-
public const string Negate = "~";
13+
public const string Negate = "_"; // distinguish from "-" for subtraction
1414

1515
public const string Equal = "==";
1616
public const string Greater = ">";
@@ -19,6 +19,7 @@ internal class NativeFunctionCall : Runtime.Object
1919
public const string LessThanOrEquals = "<=";
2020
public const string NotEquals = "!=";
2121
public const string Not = "!";
22+
public const string Invert = "~";
2223

2324
public const string And = "&&";
2425
public const string Or = "||";
@@ -87,7 +88,7 @@ public Runtime.Object Call(List<Runtime.Object> parameters)
8788
// Special case:
8889
// - Set inverse (!set) requires knowledge of origin set, not just
8990
// the raw set dictionary.
90-
if (parameters.Count == 1 && parameters [0] is SetValue && name == "!") {
91+
if (parameters.Count == 1 && parameters [0] is SetValue && name == Invert) {
9192
var setValue = (SetValue)parameters [0];
9293
var inv = setValue.inverse;
9394
if (inv == null) return new SetValue ("UNKNOWN", 0);
@@ -319,18 +320,20 @@ static void GenerateNativeFunctionsIfNecessary()
319320
//AddSetBinaryOp (Multiply, (x, y) => x * y);
320321
//AddSetBinaryOp (Divide, (x, y) => x / y);
321322
//AddSetBinaryOp (Mod, (x, y) => x % y); // TODO: Is this the operation we want for floats?
322-
//AddSetUnaryOp (Negate, x => -x);
323+
//AddSetUnaryOp (Negate, x => x.Inverse());
323324

324325
AddSetBinaryOp (Equal, (x, y) => x.Equals(y) ? (int)1 : (int)0);
325326
AddSetBinaryOp (Greater, (x, y) => x.Count > 0 && x.maxItem.Value > y.maxItem.Value ? (int)1 : (int)0);
326327
AddSetBinaryOp (Less, (x, y) => y.Count > 0 && x.maxItem.Value < y.maxItem.Value ? (int)1 : (int)0);
327328
AddSetBinaryOp (GreaterThanOrEquals, (x, y) => x.Count > 0 && x.maxItem.Value >= y.maxItem.Value ? (int)1 : (int)0);
328329
AddSetBinaryOp (LessThanOrEquals, (x, y) => y.Count > 0 && x.maxItem.Value <= y.maxItem.Value ? (int)1 : (int)0);
329330
AddSetBinaryOp (NotEquals, (x, y) => !x.Equals(y) ? (int)1 : (int)0);
330-
//AddSetUnaryOp (Not, x => !x);
331331

332-
AddSetBinaryOp (And, (x, y) => x.IntersectWith(y));
333-
AddSetBinaryOp (Or, (x, y) => x.UnionWith (y));
332+
// Placeholder to ensure that Invert gets created at all,
333+
// since this function is never actually run, and is special cased in Call
334+
AddSetUnaryOp (Invert, x => x);
335+
336+
AddSetBinaryOp (And, (x, y) => x.UnionWith(y));
334337

335338
//AddSetBinaryOp (Max, (x, y) => Math.Max (x, y));
336339
//AddSetBinaryOp (Min, (x, y) => Math.Min (x, y));
@@ -389,6 +392,11 @@ static void AddSetBinaryOp (string name, BinaryOp<SetDictionary> op)
389392
AddOpToNativeFunc (name, 2, ValueType.Set, op);
390393
}
391394

395+
static void AddSetUnaryOp (string name, UnaryOp<SetDictionary> op)
396+
{
397+
AddOpToNativeFunc (name, 1, ValueType.Set, op);
398+
}
399+
392400
static void AddFloatUnaryOp(string name, UnaryOp<float> op)
393401
{
394402
AddOpToNativeFunc (name, 1, ValueType.Float, op);

inklecate/InkParser/InkParser_Expressions.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected Expression ExpressionUnary()
168168
return divertTarget;
169169
}
170170

171-
var prefixOp = (string) OneOf (String ("-"), String ("!"));
171+
var prefixOp = (string) OneOf (String ("-"), String("~"), String ("!"));
172172

173173
// Don't parse like the string rules above, in case its actually
174174
// a variable that simply starts with "not", e.g. "notable".
@@ -427,7 +427,15 @@ protected Parsed.Subset ExpressionSubset ()
427427

428428
Whitespace ();
429429

430-
Expect (String (")"), "closing ')' for set");
430+
if (subsetList != null && subsetList.Count > 0)
431+
Expect (String (")"), "closing ')' for set");
432+
433+
// Rather than parsing nothing, the subset list may have *failed* to
434+
// parse due to the parentheses containing something else, in which
435+
// case we cope with that here.
436+
else if (ParseString (")") == null)
437+
return null;
438+
431439

432440
return new Subset (subsetList);
433441
}

inklecate/ParsedHierarchy/Expression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ public override string ToString ()
151151
string nativeNameForOp
152152
{
153153
get {
154-
// Replace "-" with "~" to make it unique
154+
// Replace "-" with "_" to make it unique (compared to subtraction)
155155
if (op == "-")
156-
return "~";
156+
return "_";
157157
if (op == "not")
158158
return "!";
159159
return op;

0 commit comments

Comments
 (0)