From b56e260d1ca9296ef008544363efefbf95a599b1 Mon Sep 17 00:00:00 2001 From: Steve Halliwell Date: Sat, 18 May 2024 07:49:40 +1000 Subject: [PATCH 1/2] Change loop vars. array and i only --- ulox/ulox.core.tests/LoopingTests.cs | 35 ++++++++++--------- ulox/ulox.core.tests/OptimiserTests.cs | 4 +-- .../uloxs/Samples/10-List.ulox | 12 +++---- .../Runtime/Compiler/Desugar/LoopDesugar.cs | 22 ++++-------- 4 files changed, 32 insertions(+), 41 deletions(-) diff --git a/ulox/ulox.core.tests/LoopingTests.cs b/ulox/ulox.core.tests/LoopingTests.cs index c2bbb771..4969003d 100644 --- a/ulox/ulox.core.tests/LoopingTests.cs +++ b/ulox/ulox.core.tests/LoopingTests.cs @@ -235,7 +235,7 @@ public void Loop_WhenGivenNumberArrayAndCustomNames_ShouldPrintIndicies() arr.Add(""b""); arr.Add(""c""); -loop arr, jtem, j +loop arr, j { print(j); } @@ -253,7 +253,7 @@ public void Loop_WhenGivenNumberArrayAndItemName_ShouldPrintItems() arr.Add(""b""); arr.Add(""c""); -loop arr,jtem +loop arr,j { print(jtem); } @@ -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 @@ -457,7 +457,7 @@ loop arr { arr.Remove(item); i -= 1; - count -= 1; + icount -= 1; } else { @@ -572,7 +572,7 @@ public void Loop_WhenCustomIndexAlreadyInScope_ShouldThrow() arr.Add(1); var j = 7; -loop arr, jtem, j +loop arr, j { } }"); @@ -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); } @@ -634,7 +634,7 @@ public void Loop_WhenGivenNumberArrayAndItemNameAndPrintCount_ShouldPrintItems() loop arr { print(item); - print(count); + print(icount); } "); @@ -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); } "); @@ -669,8 +669,8 @@ public void Loop_WhenDecreaseCount_ShouldPrintItems() loop arr { print(item); - count -= 1; - print(count); + icount -= 1; + print(icount); } "); @@ -688,7 +688,7 @@ public void Loop_WhenNestedWithExstingLocals_ShouldPrintItems() loop arr { - loop arr, jtem, j, jount + loop arr, j { print(i+jtem); } @@ -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); @@ -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} - ""); }" ); @@ -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); diff --git a/ulox/ulox.core.tests/OptimiserTests.cs b/ulox/ulox.core.tests/OptimiserTests.cs index c53aba3e..396a0352 100644 --- a/ulox/ulox.core.tests/OptimiserTests.cs +++ b/ulox/ulox.core.tests/OptimiserTests.cs @@ -385,7 +385,7 @@ public void Optimiser_Reorder_Loop_WhenGivenNumberArrayAndItemName_ShouldPrintIt testEngine.Run(@" var arr = [1,2,3,]; - loop arr,jtem + loop arr,j { print(jtem); } @@ -833,7 +833,7 @@ loop arr { arr.Remove(item); i -= 1; - count -= 1; + icount -= 1; } else { diff --git a/ulox/ulox.core.tests/uloxs/Samples/10-List.ulox b/ulox/ulox.core.tests/uloxs/Samples/10-List.ulox index 462688e1..8fb55ecd 100644 --- a/ulox/ulox.core.tests/uloxs/Samples/10-List.ulox +++ b/ulox/ulox.core.tests/uloxs/Samples/10-List.ulox @@ -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; } } diff --git a/ulox/ulox.core/Package/Runtime/Compiler/Desugar/LoopDesugar.cs b/ulox/ulox.core/Package/Runtime/Compiler/Desugar/LoopDesugar.cs index 3b913905..b4f49afd 100644 --- a/ulox/ulox.core/Package/Runtime/Compiler/Desugar/LoopDesugar.cs +++ b/ulox/ulox.core/Package/Runtime/Compiler/Desugar/LoopDesugar.cs @@ -15,8 +15,8 @@ public void ProcessDesugar(int currentTokenIndex, List 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(); @@ -24,7 +24,7 @@ public void ProcessDesugar(int currentTokenIndex, List tokens, ICompilerD // { // var i = 0; // var item = arr[i]; - // for (; i < count; i += 1) + // for (; i < icount; i += 1) // { // item = arr[i]; // print(item); @@ -33,9 +33,7 @@ public void ProcessDesugar(int currentTokenIndex, List 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"); @@ -54,19 +52,11 @@ public void ProcessDesugar(int currentTokenIndex, List 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); From b382211be17fa980e2c27a14a56e138778817cb9 Mon Sep 17 00:00:00 2001 From: Steve Halliwell Date: Sat, 18 May 2024 07:53:19 +1000 Subject: [PATCH 2/2] Update README.md --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6971d861..10109c1c 100644 --- a/README.md +++ b/README.md @@ -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); } @@ -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,