Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change loop vars. array and i only #249

Merged
merged 2 commits into from
May 17, 2024
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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ loop
//loop with following info is for iterating over collections
var myArr = [1,2,3,4,5,];
loop myArr
// the above auto defines, item, i, count. In that order,
// you can provide custom names if desired or if nested
// the above auto defines, i, item, icount. You can optionally
// provide a name for i, e.g."j" gives, jtem, jcount.
{
print("Val " + item + " @ " + i " of " + count);
}
Expand Down Expand Up @@ -339,8 +339,10 @@ testset FooTests
// It will desugar to a throw with a message
// You can specify the message as a string
// after the expression with : "literal did not match"
expect 1 == 1,
2 == 2;
expect
1 == 1,
2 == 2, //trailing commas are allowed
;
}

// test cases can also have data provided,
Expand Down
35 changes: 18 additions & 17 deletions ulox/ulox.core.tests/LoopingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public void Loop_WhenGivenNumberArrayAndCustomNames_ShouldPrintIndicies()
arr.Add(""b"");
arr.Add(""c"");

loop arr, jtem, j
loop arr, j
{
print(j);
}
Expand All @@ -253,7 +253,7 @@ public void Loop_WhenGivenNumberArrayAndItemName_ShouldPrintItems()
arr.Add(""b"");
arr.Add(""c"");

loop arr,jtem
loop arr,j
{
print(jtem);
}
Expand All @@ -271,7 +271,7 @@ public void LoopNested_WhenGivenNumberArrayAndItemName_ShouldPrintItems()
arr.Add(""b"");
arr.Add(""c"");

loop arr,jtem, j, jount
loop arr, j
{
print(jtem);
loop arr
Expand Down Expand Up @@ -457,7 +457,7 @@ loop arr
{
arr.Remove(item);
i -= 1;
count -= 1;
icount -= 1;
}
else
{
Expand Down Expand Up @@ -572,7 +572,7 @@ public void Loop_WhenCustomIndexAlreadyInScope_ShouldThrow()
arr.Add(1);
var j = 7;

loop arr, jtem, j
loop arr, j
{
}
}");
Expand Down Expand Up @@ -612,9 +612,9 @@ public void Loop_WhenNested_ShouldPrintExpected()
arr.Add(1);
var jtem = 7;

loop arrays,ytem,y
loop arrays,y
{
loop ytem, xtem, x, xount
loop ytem, x
{
print(xtem);
}
Expand All @@ -634,7 +634,7 @@ public void Loop_WhenGivenNumberArrayAndItemNameAndPrintCount_ShouldPrintItems()
loop arr
{
print(item);
print(count);
print(icount);
}
");

Expand All @@ -650,10 +650,10 @@ public void Loop_WhenGivenNumberArrayAndItemNameAndNamedCount_ShouldPrintItems()
arr.Add(""b"");
arr.Add(""c"");

loop arr,jtem,j,jount
loop arr,j
{
print(jtem);
print(jount);
print(jcount);
}
");

Expand All @@ -669,8 +669,8 @@ public void Loop_WhenDecreaseCount_ShouldPrintItems()
loop arr
{
print(item);
count -= 1;
print(count);
icount -= 1;
print(icount);
}
");

Expand All @@ -688,7 +688,7 @@ public void Loop_WhenNestedWithExstingLocals_ShouldPrintItems()

loop arr
{
loop arr, jtem, j, jount
loop arr, j
{
print(i+jtem);
}
Expand All @@ -703,7 +703,7 @@ public void Loop_WhenInFunWithArgCollection_ShouldPrintItems()
testEngine.Run(@"
fun FromRowCol(outer)
{
loop outer, it, index, c
loop outer
{
var inner = ""hi"";
print(inner);
Expand Down Expand Up @@ -740,9 +740,9 @@ public void Loop_WhenGivenInstanceFieldIdentifierAndCustomNames_ShouldPrintItems
var thing = {=};
thing.arr = [""a"",""b"",""c"",];

loop thing.arr, jtem, j, jnt
loop thing.arr, j
{
print(""{jtem}_{j}_{jnt} - "");
print(""{jtem}_{j}_{jcount} - "");
}"
);

Expand Down Expand Up @@ -1010,8 +1010,9 @@ class EnemyShipAIBasic
{
Tick(enemyShips, targetShips, dt)
{
loop enemyShips, ship
loop enemyShips
{
var ship = item;
var pos = ship.pos;
var bestTarget = EnemyTargetSelection.Select(ship, targetShips);

Expand Down
4 changes: 2 additions & 2 deletions ulox/ulox.core.tests/OptimiserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[TestFixture]
public class OptimiserTests : EngineTestBase
{
private Optimiser _opt;

Check warning on line 8 in ulox/ulox.core.tests/OptimiserTests.cs

View workflow job for this annotation

GitHub Actions / checkout and run c# tests

Non-nullable field '_opt' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

[SetUp]
public override void Setup()
Expand Down Expand Up @@ -385,7 +385,7 @@
testEngine.Run(@"
var arr = [1,2,3,];

loop arr,jtem
loop arr,j
{
print(jtem);
}
Expand Down Expand Up @@ -833,7 +833,7 @@
{
arr.Remove(item);
i -= 1;
count -= 1;
icount -= 1;
}
else
{
Expand Down
12 changes: 6 additions & 6 deletions ulox/ulox.core.tests/uloxs/Samples/10-List.ulox
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ var length = list.Count();
print("Length is " + length);

// There's support for loop with native arrays, that can optionally declare names for item and index
loop list, item, index
loop list
{
print("Item @" + index + ", is " + item);
print("Item @" + i + ", is " + item);
}

// You can even remove items during the loop and adjust the index to match
loop list, val, j
loop list, j
{
if(val % 2 == 0)
if(jtem % 2 == 0)
{
list.Remove(val);
list.Remove(jtem);
j -= 1;
count -= 1;
jcount -= 1;
}
}

Expand Down
22 changes: 6 additions & 16 deletions ulox/ulox.core/Package/Runtime/Compiler/Desugar/LoopDesugar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ public void ProcessDesugar(int currentTokenIndex, List<Token> tokens, ICompilerD
}

//we expect
// `loop arr,i,item,count { print(item);}`
// and we are going to replace with
// `loop arr,i { print(item);}`
// and we are going, create 'i'tem, 'i'count and replace with
// ` if(arr)
// {
// var count = arr.Count();
// if (count > 0)
// {
// var i = 0;
// var item = arr[i];
// for (; i < count; i += 1)
// for (; i < icount; i += 1)
// {
// item = arr[i];
// print(item);
Expand All @@ -33,9 +33,7 @@ public void ProcessDesugar(int currentTokenIndex, List<Token> tokens, ICompilerD
// }
var currentToken = tokens[currentTokenIndex];

var itemIdent = "item";
var iIdent = "i";
var countIdent = "count";
var toRemove = 2;
var origIdentTok = tokens[currentTokenIndex + 1];
var uniqueArrName = context.UniqueLocalName("arr");
Expand All @@ -54,19 +52,11 @@ public void ProcessDesugar(int currentTokenIndex, List<Token> tokens, ICompilerD

if (tokens[currentTokenIndex + toRemove].TokenType == TokenType.COMMA)
{
itemIdent = tokens[currentTokenIndex + toRemove + 1].Lexeme;
iIdent = tokens[currentTokenIndex + toRemove + 1].Lexeme;
toRemove += 2;
if (tokens[currentTokenIndex + toRemove].TokenType == TokenType.COMMA)
{
iIdent = tokens[currentTokenIndex + toRemove + 1].Lexeme;
toRemove += 2;
if (tokens[currentTokenIndex + toRemove].TokenType == TokenType.COMMA)
{
countIdent = tokens[currentTokenIndex + toRemove + 1].Lexeme;
toRemove += 2;
}
}
}
var itemIdent = iIdent + "tem";
var countIdent = iIdent + "count";

var expToPreserve = tokens.GetRange(currentTokenIndex + 1, endArrExpAt - currentTokenIndex - 1);

Expand Down
Loading