From 89e9cbc6129fe21cc4a2164350292de5bee4776b Mon Sep 17 00:00:00 2001 From: Ricci Adams Date: Mon, 6 Mar 2023 22:34:33 -0800 Subject: [PATCH] Remove @each (#169) --- src/Generator.js | 70 ----------------- test/issues/TestIssue41.ns | 32 -------- test/issues/TestIssue59.ns | 60 --------------- test/issues/TestIssue89.ns | 13 ---- test/single/Each.ns | 153 ------------------------------------- 5 files changed, 328 deletions(-) delete mode 100644 test/issues/TestIssue41.ns delete mode 100644 test/issues/TestIssue59.ns delete mode 100644 test/issues/TestIssue89.ns delete mode 100644 test/single/Each.ns diff --git a/src/Generator.js b/src/Generator.js index 9758307..b4b9f96 100644 --- a/src/Generator.js +++ b/src/Generator.js @@ -922,73 +922,6 @@ generate() } } - function handleNSEachStatement(node) - { - if (language === LanguageTypechecker) { - let object = ""; - - if (node.left.type == Syntax.VariableDeclaration) { - object = node.left.kind + " " + node.left.declarations[0].id.name; - } else { - object = node.left.name; - } - - modifier.from(node).to(node.right).replace("for (" + object + " = N$_atEachGetMember("); - modifier.from(node.right).to(node.body).replace(") ; N$_atEachTest() ; ) "); - - } else { - let i = makeTemporaryVariable(false); - let length = makeTemporaryVariable(false); - - let object, array; - let initLeft = "var "; - let initRight = ""; - let expr = false; - - // The left side is "var foo", "let foo", etc - if (node.left.type == Syntax.VariableDeclaration) { - object = node.left.declarations[0].id.name; - initLeft = node.left.kind + " " + object + ", "; - - // The left side is just an identifier - } else if (node.left.type == Syntax.Identifier) { - if (currentClass && currentClass.isIvar(node.left.name, true)) { - Utils.throwError(NSError.RestrictedUsage, `Cannot use ivar "${node.left.name}" on left-hand side of @each`, node); - } - - object = node.left.name; - } - - // The right side is a simple identifier - if (node.right.type == Syntax.Identifier && currentClass && !currentClass.isIvar(node.right.name, true)) { - array = node.right.name; - - // The right side is an expression, we need an additional variable - } else { - array = makeTemporaryVariable(false); - initLeft += array + " = ("; - initRight = initRight + "), "; - expr = true; - } - - initRight += i + " = 0, " + length + " = (" + array + " ? " + array + ".length : 0)"; - - let test = i + " < " + length; - let increment = i + "++"; - - if (expr) { - modifier.from(node).to(node.right).replace("for (" + initLeft); - modifier.from(node.right).to(node.body).replace(initRight + "; " + test + "; " + increment + ") "); - } else { - modifier.from(node).to(node.body).replace("for (" + initLeft + initRight + "; " + test + "; " + increment + ") "); - } - - if (node.body.body.length) { - modifier.from(node.body).to(node.body.body[0]).insert("{" + object + " = " + array + "[" + i + "];"); - } - } - } - function handleNSGlobalDeclaration(node) { let declaration = node.declaration; @@ -1177,9 +1110,6 @@ generate() } else if (type === Syntax.NSTypeAnnotation) { handleNSTypeAnnotation(node, parent); - } else if (type === Syntax.NSEachStatement) { - handleNSEachStatement(node); - } else if (type === Syntax.NSGlobalDeclaration) { handleNSGlobalDeclaration(node); diff --git a/test/issues/TestIssue41.ns b/test/issues/TestIssue41.ns deleted file mode 100644 index d04ffd3..0000000 --- a/test/issues/TestIssue41.ns +++ /dev/null @@ -1,32 +0,0 @@ - -(function() { "use strict"; - -@class Issue41Class - -@property (private) array: Array; - -- (Number) check -{ - _array = [ 1, 2, 3 ]; - - var sum = 0; - @each (var i in _array) { - sum += i; - } - - return sum; -} - -@end - -}()); - - - -function runTests() -{ - var instance = [[Issue41Class alloc] init]; - return [instance check] == 6; -} - -runTests(); diff --git a/test/issues/TestIssue59.ns b/test/issues/TestIssue59.ns deleted file mode 100644 index 0d81917..0000000 --- a/test/issues/TestIssue59.ns +++ /dev/null @@ -1,60 +0,0 @@ - -(function() { "use strict"; - -@class Issue59Class - -@property (private) array: Array; - -- (Number) check -{ - _array = null; - - var sum = 0; - @each (var i in _array) { - sum += i; - } - - return sum; -} - - -- (Number) check2 -{ - function getNull() { - return null; - } - - var sum = 0; - @each (var i in getNull()) { - sum += i; - } - - return sum; -} - - -- (Number) check3 -{ - var nullVar = null; - - var sum = 0; - @each (var i in nullVar) { - sum += i; - } - - return sum; -} - -@end - -}()); - - - -function runTests() -{ - var instance = [[Issue59Class alloc] init]; - return [instance check] == 0 && [instance check2] == 0 && [instance check3] == 0; -} - -runTests(); diff --git a/test/issues/TestIssue89.ns b/test/issues/TestIssue89.ns deleted file mode 100644 index b04bf9a..0000000 --- a/test/issues/TestIssue89.ns +++ /dev/null @@ -1,13 +0,0 @@ -require("assert"); - -var sum = 0; -@each (var i in [ 1, 2, 3, 0, 4, 5, 6 ]) { - sum += i; -} - -@each (var i in [ ]) { } - - -assert.equal(sum, 21); - -true; \ No newline at end of file diff --git a/test/single/Each.ns b/test/single/Each.ns deleted file mode 100644 index 7b29d98..0000000 --- a/test/single/Each.ns +++ /dev/null @@ -1,153 +0,0 @@ -//@opts = { } - -var assert = require("assert"); - - -@class EachIteratorTests - -@property (private) arrayAccessCount: Number; - -- (Array) testArray -{ - _arrayAccessCount++; - - return [ - [ "A", "B", "C" ], - [ "D", "E", "F" ], - [ "G", "H", "I" ], - ]; -} - - -- (BOOL) runTests -{ - function runTest(test) { - var accessCount = _arrayAccessCount; - test(); - assert(_arrayAccessCount, accessCount + 1); - } - - function test1() { - var grid = [self testArray]; - var output = ""; - - @each (var row in grid) { - @each (var col in row) { - output += col; - } - } - - assert.equal(output, "ABCDEFGHI"); - } - - function test2() { - var grid = [self testArray]; - var output = ""; - - @each (var col in grid[1]) { - output += col; - } - - assert.equal(output, "DEF"); - } - - function test3() { - var grid = [self testArray]; - var output = ""; - - var col; - @each (col in grid[2]) { - output += col; - } - - assert.equal(output, "GHI"); - } - - function test4() { - var output = ""; - - @each (var row in [self testArray]) { - @each (var col in row) { - output += col; - } - } - - assert.equal(output, "ABCDEFGHI"); - } - - function test5() { - var output = ""; - - @each (var row in [self testArray]) { - @each (var col in row) { - if (col == "E") { - break; - } - - if (col == "H") { - continue; - } - - output += col; - } - } - - assert.equal(output, "ABCDGI"); - } - - function test6() { - var grid = [self testArray]; - var output = ""; - - @each (var row in grid) { - @each (var col in row) { - output += col; - } - } - - @each (var row2 in grid) { - @each (var col2 in row2) { - output += col2; - } - } - - assert.equal(output, "ABCDEFGHIABCDEFGHI"); - } - - - // Same as test6 but with 'let' - function test7() { - let grid = [self testArray]; - let output = ""; - - @each (let row in grid) { - @each (let col in row) { - output += col; - } - } - - @each (let row in grid) { - @each (let col in row) { - output += col; - } - } - - assert.equal(output, "ABCDEFGHIABCDEFGHI"); - } - - runTest(test1); - runTest(test2); - runTest(test3); - runTest(test4); - runTest(test5); - runTest(test6); - runTest(test7); - - return true; -} - -@end - - -var instance = [[EachIteratorTests alloc] init]; -[instance runTests]