scope test_leftArrowScript_objectScope set flag OurSyncCollections. !include once #1034831 // Gazelle 22 Function Include for Testing sclass #Class1 { S hello(S x) { ret "hello " + x; } } svoid test_leftArrowScript_objectScope(LASMultiClassLoader cl default new LASMultiClassLoader(mc())) { // Accessing an instance field S classPrefix = "scriptClasses."; embedded O runScript(S code) { var parser = enableScaffolding(new GazelleV_LeftArrowScriptParser() .classDefPrefix(classPrefix) .lasClassLoader(cl)); ret leftArrowVerbose(parser, code); } // Access instance field without "this" assertEqualsVerbose(10, runScript([[ class X { i: int <- 5 def j { i plus i } } new X, j ]])); // Access superclass field without "this" assertEqualsVerbose(10, runScript([[ class X { i: int <- 5 } class Y extends X { def j { i plus i } } new Y, j ]])); // Make sure locally declared variables shadow fields assertEqualsVerbose(14, runScript([[ class X { i: int <- 5 def j { i: int <- 7; i plus i } } new X, j ]])); // Assign instance field without "this" assertEqualsVerbose(10, runScript([[ class X { i: int def j { i <- 10; this } } new X, j, i ]])); // Assign instance field in superclass without "this" assertEqualsVerbose(10, runScript([[ class X { i: int } class Y extends X { def j { i <- 10; this } } new Y, j, i ]])); // Call instance method without "this" assertEqualsVerbose(2, runScript([[ class X { i: int <- 1 def yo { i <- i plus 1 } def j { yo; i } } new X, j ]])); // Call instance method without "this" followed by "<" assertEqualsVerbose(6, runScript([[ class X { def yo n { 1 plus n } def j { yo < 5 } } new X, j ]])); // Special case: field & method with same name // (refers to the field because it's inside an expression) assertEqualsVerbose(ll("o", "o"), runScript([[ class X { bla: S <- "o" def bla { "x" } def y { ll bla bla } } new X, y ]])); // Call superclass method without "this" assertEqualsVerbose(2, runScript([[ class X { i: int <- 1 def yo { i <- i plus 1 } } class Y extends X { def j { yo; i } } new Y, j ]])); // Same with arguments and return value assertEqualsVerbose(6, runScript([[ class X { i: int <- 1 def yo x { i <- i plus x; this } def j { yo 5, i } } new X, j ]])); // Same with "<" assertEqualsVerbose(6, runScript([[ class X { i: int <- 1 def yo x { i <- i plus x; this } def j { (yo < 5) i } } new X, j ]])); // Java superclass method with "<" assertEqualsVerbose("hello world", runScript(replaceIdentifiers([[ class X extends Class1 { def yo { hello "world" } } new X, yo ]], "Class1", className(Class1)))); // Method shadows global function (TODO) /*assertEqualsVerbose(6, runScript([[ class X { } new X, j ]]));*/ // Auto-"this" in initializer assertEqualsVerbose(6, runScript([[ class X { i: int <- 5 initializer { i <- i plus 1 } } new X, i ]])); // Declare a local variable shadowing an instance field // with "var" assertEqualsVerbose(3, runScript([[ class X { i: int <- 3 def j { var i <- 5; i <- i plus 1; this } } new X, j, i ]])); }