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:
Bob Nystrom
2015-01-13 21:36:42 -08:00
parent 9cb414d63b
commit a8e2ba233c
8 changed files with 124 additions and 61 deletions
+18
View File
@@ -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
+6
View File
@@ -0,0 +1,6 @@
class Foo {
bar {
var A = "value"
var A = "other" // expect error
}
}
+8
View File
@@ -0,0 +1,8 @@
var Nonlocal = "outer"
{
var Nonlocal = "inner"
IO.print(Nonlocal) // expect: inner
}
IO.print(Nonlocal) // expect: outer
@@ -0,0 +1,6 @@
var Global = "global"
// TODO: Forward reference to global declared after use.
new Fn {
IO.print(Global) // expect: global
}.call
+15
View File
@@ -0,0 +1,15 @@
var Global = "global"
// TODO: Forward reference to global declared after use.
class Foo {
method {
IO.print(Global)
}
static classMethod {
IO.print(Global)
}
}
(new Foo).method // expect: global
Foo.classMethod // expect: global