Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions Source/Parser/Expressions/ClassMemberReferenceExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,9 @@ void INestedExpressions.GetDependencies(HashSet<string> dependencies)
if (nested != null)
nested.GetDependencies(dependencies);

if (Variable != null && Variable.Name != "this")
{
nested = Variable as INestedExpressions;
if (nested != null)
nested.GetDependencies(dependencies);
}
nested = Variable as INestedExpressions;
if (nested != null)
nested.GetDependencies(dependencies);

// we can't know the type of the source object, so just
// declare dependance on all fields matching the provided name
Expand Down
18 changes: 16 additions & 2 deletions Tests/Parser/Expressions/ClassDefinitionExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ public void TestParseFunction()
Assert.That(expr.ToString(), Is.EqualTo("class Entity(1 field, 1 function)"));
}

[Test]
public void TestParseLambdaReference()
{
var expr = Parse(
"class Entity {\n" +
" addr = 4\n" +
" function hp() => sum_of(range(0, 5), (i) => i < this.addr)\n" +
"}");
Assert.That(expr.Name.Name, Is.EqualTo("Entity"));
Assert.That(expr.ToString(), Is.EqualTo("class Entity(1 field, 1 function)"));
}

[Test]
public void TestParseErrorInsideField()
{
Expand Down Expand Up @@ -263,8 +275,9 @@ public void TestGetDependencies()
var dependencies = new HashSet<string>();
((INestedExpressions)expr).GetDependencies(dependencies);

Assert.That(dependencies.Count, Is.EqualTo(3));
Assert.That(dependencies.Count, Is.EqualTo(4));
Assert.That(dependencies.Contains("word"));
Assert.That(dependencies.Contains("this"));
Assert.That(dependencies.Contains(".addr"));
Assert.That(dependencies.Contains("global_var"));
}
Expand All @@ -281,8 +294,9 @@ public void TestGetDependenciesNested()
var dependencies = new HashSet<string>();
((INestedExpressions)expr).GetDependencies(dependencies);

Assert.That(dependencies.Count, Is.EqualTo(5));
Assert.That(dependencies.Count, Is.EqualTo(6));
Assert.That(dependencies.Contains("word"));
Assert.That(dependencies.Contains("this"));
Assert.That(dependencies.Contains(".addr"));
Assert.That(dependencies.Contains(".x"));
Assert.That(dependencies.Contains("global"));
Expand Down
28 changes: 28 additions & 0 deletions Tests/Parser/Expressions/ClassMemberReferenceExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,5 +410,33 @@ public void TestReadFromNestedIndexNonArray()
var error = assign.Execute(scope);
ExpressionTests.AssertError(error, "Cannot index integer: this.points");
}

[Test]
public void TestLambdaReference()
{
var entityClass = ExpressionTests.Parse<ClassDefinitionExpression>(
"class Entity {\n" +
" addr = 2\n" +
" function multiplyBy6() => sum_of(range(0, 5), (i) => this.addr)\n" +
"}");

var scope = new InterpreterScope(AchievementScriptInterpreter.GetGlobalScope());
scope.Context = new AchievementScriptContext();
entityClass.Execute(scope);

var constructor = new FunctionCallExpression("Entity", new ExpressionBase[] { new IntegerConstantExpression(4) });
var entity = constructor.Evaluate(scope);
Assert.That(entity, Is.InstanceOf<ClassInstanceExpression>());

scope.AssignVariable(new VariableExpression("e"), entity);

var assign = Parse("n = e.multiplyBy6()");
var error = assign.Execute(scope);
Assert.That(error, Is.Null);

var n = scope.GetVariable("n");
Assert.That(n, Is.InstanceOf<IntegerConstantExpression>());
Assert.That(((IntegerConstantExpression)n).Value, Is.EqualTo(24)); // 4+4+4+4+4+4
}
}
}
Loading