Skip to content

Commit

Permalink
support for unary minus (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored Oct 1, 2024
1 parent ef843be commit 879ada6
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Source/Parser/Expressions/ExpressionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ private enum OperationPriority
AddSubtract,
MulDivMod,
Not,
Negate,
Parenthesis,
}

Expand Down Expand Up @@ -642,6 +643,16 @@ private static ExpressionBase ParseClauseCore(PositionalTokenizer tokenizer)
result.Location = new TextRange(tokenStart, tokenEnd);
return result;
}
else if (Char.IsLetter(tokenizer.NextChar) || tokenizer.NextChar == '(')
{
var next = ParseExpression(tokenizer, OperationPriority.Negate);
if (next.Type == ExpressionType.Error)
return next;

var result = new MathematicExpression(new IntegerConstantExpression(0), MathematicOperation.Subtract, next);
result.Location = new TextRange(tokenStart, next.Location.End);
return result;
}
return ParseError(tokenizer, "Minus without value", tokenStart.Line, tokenStart.Column);

case '{':
Expand Down
14 changes: 14 additions & 0 deletions Tests/Parser/Expressions/ExpressionBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -694,5 +694,19 @@ public void TestParseLineColumnValues()
Assert.That(expression.Location.End.Line, Is.EqualTo(7));
Assert.That(expression.Location.End.Column, Is.EqualTo(8));
}

[Test]
public void TestParseUnaryMinusVariable()
{
var tokenizer = CreateTokenizer("-a");
var expression = ExpressionBase.Parse(tokenizer);

Assert.That(expression, Is.InstanceOf<MathematicExpression>());
var mathematic = (MathematicExpression)expression;
Assert.That(mathematic.Left, Is.EqualTo(new IntegerConstantExpression(0)));
Assert.That(mathematic.Operation, Is.EqualTo(MathematicOperation.Subtract));
Assert.That(mathematic.Right, Is.InstanceOf<VariableExpression>());
Assert.That(((VariableExpression)mathematic.Right).Name, Is.EqualTo("a"));
}
}
}
78 changes: 78 additions & 0 deletions Tests/Parser/Expressions/MathematicExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,5 +425,83 @@ public void TestCombining(string input, string expected)
result.AppendString(builder);
Assert.That(builder.ToString(), Is.EqualTo(expected));
}

[Test]
public void TestUnaryMinusVariable()
{
var scope = AchievementScriptTests.Evaluate(
"a = 8\n" +
"b = -a"
);

var b = scope.GetVariable("b");
Assert.That(b, Is.InstanceOf<IntegerConstantExpression>());
Assert.That(((IntegerConstantExpression)b).Value, Is.EqualTo(-8));
}

[Test]
public void TestUnaryMinusAddition()
{
var scope = AchievementScriptTests.Evaluate(
"a = 8\n" +
"b = -a + 2"
);

var b = scope.GetVariable("b");
Assert.That(b, Is.InstanceOf<IntegerConstantExpression>());
Assert.That(((IntegerConstantExpression)b).Value, Is.EqualTo(-6));
}

[Test]
public void TestUnaryMinusMultiplication()
{
var scope = AchievementScriptTests.Evaluate(
"a = 8\n" +
"b = -a * 2"
);

var b = scope.GetVariable("b");
Assert.That(b, Is.InstanceOf<IntegerConstantExpression>());
Assert.That(((IntegerConstantExpression)b).Value, Is.EqualTo(-16));
}

[Test]
public void TestUnaryMinusParenthesis()
{
var scope = AchievementScriptTests.Evaluate(
"a = 8\n" +
"b = -(a + 2)"
);

var b = scope.GetVariable("b");
Assert.That(b, Is.InstanceOf<IntegerConstantExpression>());
Assert.That(((IntegerConstantExpression)b).Value, Is.EqualTo(-10));
}

[Test]
public void TestUnaryMinusArrayValue()
{
var scope = AchievementScriptTests.Evaluate(
"a = [8]\n" +
"b = -a[0]"
);

var b = scope.GetVariable("b");
Assert.That(b, Is.InstanceOf<IntegerConstantExpression>());
Assert.That(((IntegerConstantExpression)b).Value, Is.EqualTo(-8));
}

[Test]
public void TestUnaryMinusFunctionCall()
{
var scope = AchievementScriptTests.Evaluate(
"function a(n) => n\n" +
"b = -a(8)"
);

var b = scope.GetVariable("b");
Assert.That(b, Is.InstanceOf<IntegerConstantExpression>());
Assert.That(((IntegerConstantExpression)b).Value, Is.EqualTo(-8));
}
}
}

0 comments on commit 879ada6

Please sign in to comment.