Skip to content

Commit

Permalink
Merge pull request #248 from stevehalliwell/imp
Browse files Browse the repository at this point in the history
Merge imp into main
  • Loading branch information
stevehalliwell authored May 17, 2024
2 parents 499ec80 + 0d35e9d commit d46cad6
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 126 deletions.
104 changes: 12 additions & 92 deletions ulox/ulox.core.tests/ClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ class T
Assert.AreEqual("1230", testEngine.InterpreterResult);
}

[Test]
public void Class_MultiVarTrailingComma()
{
testEngine.Run(@"
class T
{
var a= 10, b = 20, c = 30,;
}");

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

[Test]
public void Engine_Class_BoundMethod()
{
Expand Down Expand Up @@ -1009,81 +1021,6 @@ class T
Assert.AreEqual("2", testEngine.InterpreterResult);
}

[Test]
public void Engine_Class_StaticFields()
{
testEngine.Run(@"
class T
{
static var a = 2;
}
print(T.a);");

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

[Test]
public void Engine_Class_StaticFields_Modify()
{
testEngine.Run(@"
class T
{
static var a = 2;
}
T.a = 1;
print(T.a);");

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

[Test]
public void Engine_Class_StaticFields_WhenClassModified_ShouldThrow()
{
testEngine.Run(@"
class T
{
static var a = 2;
}
T.b = 5;");

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

[Test]
public void Engine_NoThis_Method_WorksAsStatic()
{
testEngine.Run(@"
class T
{
NoMemberMethod()
{
retval = 7;
}
}
print(T.NoMemberMethod());");

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

[Test]
public void Static_Method_OnClass()
{
testEngine.Run(@"
class T
{
static StaticMethod()
{
retval = 7;
}
}
print(T.StaticMethod());");

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

//todo we'd like to do this but not currently able due to how methods are stored and looked up
// [Test]
Expand Down Expand Up @@ -1116,23 +1053,6 @@ class T

StringAssert.StartsWith("Undefined property 'foo', cannot bind method as it has no fromClass a", testEngine.InterpreterResult);
}

[Test]
public void Static_Method_OnInstance()
{
testEngine.Run(@"
class T
{
static StaticMethod()
{
retval = 7;
}
}
print(T().StaticMethod());");

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

[Test]
public void Mixin_WhenDeclared_ShouldCompileCleanly()
Expand Down
16 changes: 16 additions & 0 deletions ulox/ulox.core.tests/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,5 +467,21 @@ loop all

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

[Test]
public void EnumAll_WhenCount_ShouldBe2()
{
testEngine.Run(@"
enum Foo
{
Bar,
Baz,
}
print(Foo.All.Count());
");

Assert.AreEqual("2", testEngine.InterpreterResult);
}
}
}
14 changes: 14 additions & 0 deletions ulox/ulox.core.tests/ExpectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ public void Expect_FalsyMultiPartExpression_Aborts()
StringAssert.Contains("Expect failed, 'true and 1 == 2' at ip:", testEngine.InterpreterResult);
}

[Test]
public void Expect_MultiPartExpressionWithTrailingComma_Passes()
{
testEngine.Run(@"
expect
1 == 1,
2 == 2,
!null,
;"
);

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

[Test]
public void Expect_FalsyMultiPartExpressionWithMessage_AbortsWithMessage()
{
Expand Down
42 changes: 42 additions & 0 deletions ulox/ulox.core.tests/MatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,48 @@ match a
Assert.AreEqual("4", testEngine.InterpreterResult);
}

[Test]
public void Match_WhenEnum_ShouldPass()
{
testEngine.Run(@"
enum Foo
{
Bar,
Baz
}
var f = Foo.Bar;
match f
{
Foo.Bar: print(1);
Foo.Baz: print(2);
}");

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

[Test]
public void Match_WhenEnumAndEmptyCase_ShouldPass()
{
testEngine.Run(@"
enum Foo
{
Bar,
Baz
}
var f = Foo.Bar;
match f
{
Foo.Bar: ;
Foo.Baz: print(2);
}");

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

[Test]
public void Match_WhenIntWithBody_ShouldPass()
{
Expand Down
118 changes: 118 additions & 0 deletions ulox/ulox.core.tests/StaticClassTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine;
using NUnit.Framework;

namespace ULox.Core.Tests
{
public class StaticClassTests : EngineTestBase
{
[Test]
public void Engine_Class_StaticField()
{
testEngine.Run(@"
class T
{
static var a = 2;
}
print(T.a);");

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

[Test]
public void Engine_Class_StaticFields()
{
testEngine.Run(@"
class T
{
static var
a = 2,
b = 1,
;
}
print(T.a);");

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

[Test]
public void Engine_Class_StaticFields_Modify()
{
testEngine.Run(@"
class T
{
static var a = 2;
}
T.a = 1;
print(T.a);");

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

[Test]
public void Engine_Class_StaticFields_WhenClassModified_ShouldThrow()
{
testEngine.Run(@"
class T
{
static var a = 2;
}
T.b = 5;");

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

[Test]
public void Engine_NoThis_Method_WorksAsStatic()
{
testEngine.Run(@"
class T
{
NoMemberMethod()
{
retval = 7;
}
}
print(T.NoMemberMethod());");

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

[Test]
public void Static_Method_OnClass()
{
testEngine.Run(@"
class T
{
static StaticMethod()
{
retval = 7;
}
}
print(T.StaticMethod());");

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

[Test]
public void Static_Method_OnInstance()
{
testEngine.Run(@"
class T
{
static StaticMethod()
{
retval = 7;
}
}
print(T().StaticMethod());"
);

Assert.AreEqual("7", testEngine.InterpreterResult);
}
}
}
6 changes: 4 additions & 2 deletions ulox/ulox.core/Package/Runtime/Compiler/CompilerStatements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ public static void ExpectStatement(Compiler compiler)
compiler.EmitPacket(new ByteCodePacket(OpCode.THROW));
compiler.EmitLabel(thenjumpLabel);
compiler.EmitPop();
}
while (compiler.TokenIterator.Match(TokenType.COMMA));

//if trailing comma, eat it
compiler.TokenIterator.Match(TokenType.COMMA);
} while (!compiler.TokenIterator.Check(TokenType.END_STATEMENT));

compiler.ConsumeEndStatement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ private void StaticProperty(Compiler compiler)
compiler.EmitPacket(new ByteCodePacket(OpCode.SET_PROPERTY, nameConstant));
compiler.EmitPop();
_needsEndClosure = true;
} while (compiler.TokenIterator.Match(TokenType.COMMA));

//if trailing comma, eat it
compiler.TokenIterator.Match(TokenType.COMMA);
} while (compiler.TokenIterator.Check(TokenType.IDENTIFIER));

compiler.ConsumeEndStatement();
}
Expand Down
Loading

0 comments on commit d46cad6

Please sign in to comment.