Skip to content

Commit

Permalink
Merge pull request #257 from stevehalliwell/remove-overload
Browse files Browse the repository at this point in the history
Merge remove-overload into main
  • Loading branch information
stevehalliwell committed Jun 19, 2024
2 parents 4286728 + 54e274f commit c569f1a
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 1,038 deletions.
68 changes: 49 additions & 19 deletions ulox/ulox.core.tests/ClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -703,25 +703,6 @@ class A{MethA(){print (1);}}");
Assert.AreEqual("1", testEngine.InterpreterResult);
}

[Test]
public void Engine_OperatorOverload_ClassAdd()
{
testEngine.Run(@"
class V
{
var a;
init(a){}
_add(lhs,rhs){retval = V(lhs.a + rhs.a);}
}
var v1 = V(1);
var v2 = V(2);
var res = v1 + v2;
print(res.a);");

Assert.AreEqual("3", testEngine.InterpreterResult);
}

[Test]
public void Init_AttempCreateField_ShouldFail()
{
Expand Down Expand Up @@ -1639,5 +1620,54 @@ class Foo

StringAssert.StartsWith("Attempted to create a new ", testEngine.InterpreterResult);
}

[Test]
public void CreateClosure_MethodDoesNotExist_Error()
{
testEngine.Run(@"
class A{MethA(){print (1);}}
var a = A();
var closure = a.Meth;");
StringAssert.StartsWith("Undefined method 'Meth' at", testEngine.InterpreterResult);
}

[Test]
public void Invoke_MethodDoesNotExist_Error()
{
testEngine.Run(@"
class A{MethA(){print (1);}}
var a = A();
a.Meth();");
StringAssert.StartsWith("No method of name", testEngine.InterpreterResult);
}

[Test]
public void Invoke_Dynamic_Error()
{
testEngine.Run(@"
var a = {=};
a.Meth();");
StringAssert.StartsWith("No method of name", testEngine.InterpreterResult);
}

[Test]
public void Invoke_Lib_Error()
{
testEngine.Run(@"
var a = Math;
a.Meth();");
StringAssert.StartsWith("Cannot invoke 'Meth' on '<inst >' with no class", testEngine.InterpreterResult);
}

[Test]
public void Invoke_WrongType_Error()
{
testEngine.Run(@"
var a = 7;
var closure = a.Meth();");
StringAssert.StartsWith("Cannot invoke 'Meth' on '7' at ", testEngine.InterpreterResult);
}
}
}
12 changes: 12 additions & 0 deletions ulox/ulox.core.tests/FunctionAndDelegateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,17 @@ fun T(a,b) (c,d,e,f,g)

Assert.AreEqual("3-120.51", testEngine.InterpreterResult);
}

[Test]
public void Delegate_WhenNoArgsAndCalledWith1_ShouldError()
{
testEngine.Run(@"
fun Del() {print(""Hello"");}
var del = Del;
del(5);
");

StringAssert.StartsWith("Wrong number of params given to", testEngine.InterpreterResult);
}
}
}
239 changes: 0 additions & 239 deletions ulox/ulox.core.tests/LoopingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,56 +393,6 @@ loop arr
StringAssert.StartsWith("Cannot perform countof on '<inst Stub>' at ip:", testEngine.InterpreterResult);
}

[Test]
public void CountOf_WhenGivenFakeList_ShouldMatch()
{
testEngine.Run(@"
class FakeList { _co(self) { retval = 1; } }
var arr = FakeList();
print(countof arr);
");

Assert.AreEqual("1", testEngine.InterpreterResult);
}

[Test]
public void Loop_WhenGivenFakeList_ShouldFailToInvoke()
{
testEngine.Run(@"
class FakeList { _co(self) { retval = 1; } }
var arr = FakeList();
print(countof arr);
loop arr
{
}
");

StringAssert.StartsWith("1Cannot perform get index on type 'Instance' at ip:", testEngine.InterpreterResult);
}

[Test]
public void Loop_WhenGivenCustomList_ShouldPass()
{
testEngine.Run(@"
class FakeList
{
_co(self) { retval = 1; }
_gi(self, i) { retval = ""Hello""; }
}
var arr = FakeList();
loop arr
{
print(item);
}
");

Assert.AreEqual("Hello", testEngine.InterpreterResult);
}

[Test]
public void Remove_WhenWithinLoop_ShouldRemoveItem()
{
Expand Down Expand Up @@ -910,194 +860,5 @@ loop arr

Assert.AreEqual("", testEngine.InterpreterResult);
}

[Test]
public void Loop_WhenComplexNestedLocalsAndContinued_ShouldMoveToIterationStep()
{
testEngine.Run(@"
class Vec2
{
static Create(x,y)
{
var ret = Vec2();
ret.x = x;
ret.y = y;
retval = ret;
}
static Scale(v2, scalar)
{
retval = Vec2.Create(v2.x* scalar, v2.y*scalar);
}
static Rotate(v2, rad)
{
var cos = Math.Cos(rad);
var sin = Math.Sin(rad);
var x = v2.x * cos - v2.y * sin;
var y = v2.x * sin + v2.y * cos;
retval = Vec2.Create(x,y);
}
static Lerp(a, b, p)
{
var x = a.x + (b.x - a.x) * p;
var y = a.y + (b.y - a.y) * p;
retval = Vec2.Create(x,y);
}
static Length(v2)
{
retval = Math.Sqrt(v2.x*v2.x + v2.y*v2.y);
}
//TODO needs tests
static Normalise(v2)
{
var len = Math.Sqrt(v2.x*v2.x + v2.y*v2.y);
retval = Vec2.Create(v2.x/len, v2.y/len);
}
static Dot(lhs, rhs)
{
retval = lhs.x * rhs.x + lhs.y * rhs.y;
}
static Angle(v)
{
var rads = Math.Atan2(v.y, v.x);
retval = Math.Rad2Deg(rads);
}
var x = 0, y = 0;
_add(lhs, rhs)
{
retval = Vec2.Create(lhs.x + rhs.x, lhs.y + rhs.y);
}
_sub(lhs, rhs)
{
retval = Vec2.Create(lhs.x - rhs.x, lhs.y - rhs.y);
}
_mul(lhs, rhs)
{
retval = Vec2.Create(lhs.x * rhs.x, lhs.y * rhs.y);
}
_eq(lhs, rhs)
{
retval = lhs.x == rhs.x and lhs.y == rhs.y;
}
}
class ShipControls
{
var throttle = 0;
var rudder = 0;
}
class Ship
{
var pos = Vec2();
var vel = Vec2();
var heading = 0;
var controls = ShipControls();
}
class EnemyShipAIBasic
{
Tick(enemyShips, targetShips, dt)
{
loop enemyShips
{
var ship = item;
var pos = ship.pos;
var bestTarget = EnemyTargetSelection.Select(ship, targetShips);
if (bestTarget)
{
var distanceAheadScale = 0.1;
var headingAlignForFar = 35;
var farDist = 15;
var posDiff = bestTarget.pos - ship.pos;
var velDiff = bestTarget.vel - ship.vel;
var headingDiff = ship.heading - bestTarget.heading;
var headingDiffAbs = Math.Abs(headingDiff);
var approachingDot = Vec2.Dot(Vec2.Normalise(posDiff), Vec2.Normalise(ship.vel));
var isMovingTowards = approachingDot > 0;
var distance = Vec2.Length(posDiff);
var isFar = distance > farDist;
var isNear = !isFar;
var distNorm = distance / farDist;
var aheadTargetRudder = EnemyShipAIBasic.RudderFromHeadingDiff(headingDiff, headingDiffAbs);
var prevThrottle = ship.controls.throttle;
var prevRudder = ship.controls.rudder;
if(isMovingTowards)
{
ship.controls.throttle = 1;
ship.controls.rudder = aheadTargetRudder;
continue;
}
if(isFar)
{
ship.controls.throttle = 1;
if(headingDiffAbs < 45)
ship.controls.throttle = 0;
continue;
}
if(isNear)
{
ship.controls.throttle = prevThrottle;
ship.controls.rudder = aheadTargetRudder * distNorm;
continue;
}
}
else
{
ship.controls.throttle = 1;
ship.controls.rudder = 1;
}
}
}
RudderFromHeadingDiff(headingDiff, absHeadingDiff)
{
var amt = absHeadingDiff / 180;
if(headingDiff > 0)
retval = amt;
else
retval = -amt;
}
}
class EnemyTargetSelection
{
Select(enemyShip, availTargets)
{
retval = availTargets[0];
}
}
var enemyShips = [];
for(var i = 0; i < 10; i +=1 )
{
var ship = Ship();
ship.pos = Vec2.Create(i*2, 0);
enemyShips.Add(ship);
}
var targetShips = [Ship()];
EnemyShipAIBasic.Tick(enemyShips, targetShips, 0.1);
");

Assert.AreEqual("", testEngine.InterpreterResult);
}
}
}
14 changes: 14 additions & 0 deletions ulox/ulox.core.tests/OperatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,19 @@ public void Mod_WhenNeg2and3_ShouldPrint1()
print(-2%3);");
Assert.AreEqual("1", testEngine.InterpreterResult);
}

[Test]
[TestCase("+")]
[TestCase("-")]
[TestCase("*")]
[TestCase("/")]
[TestCase("%")]
[TestCase(">")]
[TestCase("<")]
public void Op_WhenIncompatTypes_ShouldError(string op)
{
testEngine.Run($"var res = 1 {op} [];");
StringAssert.StartsWith("Cannot perform op", testEngine.InterpreterResult);
}
}
}
Loading

0 comments on commit c569f1a

Please sign in to comment.