Add support for "nonlocal" (capitalized) names, and change how variable lookup works.
Inside a method, all local variable lookup stops at the method boundary. In other words, methods, do not close over outer local variables. If a name is not found inside a method and is lowercase, it's a method on this. If it's capitalized, it's a global variable.
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
var f = null
|
||||
var F = null
|
||||
|
||||
class Foo {
|
||||
method(param) {
|
||||
f = new Fn {
|
||||
F = new Fn {
|
||||
IO.print(param)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(new Foo).method("param")
|
||||
f.call // expect: param
|
||||
F.call // expect: param
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
var Nonlocal = "before"
|
||||
IO.print(Nonlocal) // expect: before
|
||||
Nonlocal = "after"
|
||||
IO.print(Nonlocal) // expect: after
|
||||
|
||||
class Foo {
|
||||
static method {
|
||||
Nonlocal = "method"
|
||||
}
|
||||
}
|
||||
|
||||
Foo.method
|
||||
IO.print(Nonlocal) // expect: method
|
||||
|
||||
new Fn {
|
||||
Nonlocal = "fn"
|
||||
}.call
|
||||
IO.print(Nonlocal) // expect: fn
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
bar {
|
||||
var A = "value"
|
||||
var A = "other" // expect error
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
var Nonlocal = "outer"
|
||||
|
||||
{
|
||||
var Nonlocal = "inner"
|
||||
IO.print(Nonlocal) // expect: inner
|
||||
}
|
||||
|
||||
IO.print(Nonlocal) // expect: outer
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
var global = "global"
|
||||
var Global = "global"
|
||||
// TODO: Forward reference to global declared after use.
|
||||
|
||||
new Fn {
|
||||
IO.print(global) // expect: global
|
||||
IO.print(Global) // expect: global
|
||||
}.call
|
||||
@@ -1,13 +1,13 @@
|
||||
var global = "global"
|
||||
var Global = "global"
|
||||
// TODO: Forward reference to global declared after use.
|
||||
|
||||
class Foo {
|
||||
method {
|
||||
IO.print(global)
|
||||
IO.print(Global)
|
||||
}
|
||||
|
||||
static classMethod {
|
||||
IO.print(global)
|
||||
IO.print(Global)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
var foo = "variable"
|
||||
|
||||
class Foo {
|
||||
foo { "method" }
|
||||
|
||||
method {
|
||||
IO.print(foo)
|
||||
}
|
||||
|
||||
static foo { "class method" }
|
||||
|
||||
static classMethod {
|
||||
IO.print(foo)
|
||||
}
|
||||
}
|
||||
|
||||
(new Foo).method // expect: method
|
||||
Foo.classMethod // expect: class method
|
||||
Reference in New Issue
Block a user