feat: implement static methods as local variables in implicit class scope

Refactor the compiler to treat static methods as local variables defined in an implicit scope surrounding the class, eliminating the need for VM-level static method support. This changes `declareVariable` to use the previously consumed token, adds `declareNamedVariable` for explicit token consumption, and updates `parameterList` to use the new function. Adjusts VM bytecode ordering for `CODE_METHOD_INSTANCE` and `CODE_METHOD_STATIC` to pop the class before the function body. Adds comprehensive test coverage for static fields including closures, nested classes, default null values, instance method access, and error cases for field usage outside classes or in static methods.
This commit is contained in:
Bob Nystrom
2014-01-19 21:01:51 +00:00
parent 9d71e765c8
commit 7ad96755f4
15 changed files with 307 additions and 81 deletions
+16
View File
@@ -0,0 +1,16 @@
class Foo {
static initialize { __field = "Foo field" }
static closeOverGet {
return fn { return __field }
}
static closeOverSet {
return fn { __field = "new value" }
}
}
Foo.initialize
IO.print(Foo.closeOverGet.call) // expect: Foo field
Foo.closeOverSet.call
IO.print(Foo.closeOverGet.call) // expect: new value
+5
View File
@@ -0,0 +1,5 @@
class Foo {
static write { IO.print(__field) }
}
Foo.write // expect: null
+25
View File
@@ -0,0 +1,25 @@
class Foo {
set(a, b, c, d, e) {
__a = a
__b = b
__c = c
__d = d
__e = e
}
write {
IO.print(__a)
IO.print(__b)
IO.print(__c)
IO.print(__d)
IO.print(__e)
}
}
(new Foo).set(1, 2, 3, 4, 5)
(new Foo).write
// expect: 1
// expect: 2
// expect: 3
// expect: 4
// expect: 5
+25
View File
@@ -0,0 +1,25 @@
class Foo {
static set(a, b, c, d, e) {
__a = a
__b = b
__c = c
__d = d
__e = e
}
static write {
IO.print(__a)
IO.print(__b)
IO.print(__c)
IO.print(__d)
IO.print(__e)
}
}
Foo.set(1, 2, 3, 4, 5)
Foo.write
// expect: 1
// expect: 2
// expect: 3
// expect: 4
// expect: 5
+34
View File
@@ -0,0 +1,34 @@
class Outer {
static staticMethod {
__field = "outer"
IO.print(__field) // expect: outer
class Inner {
static staticMethod {
__field = "inner"
IO.print(__field) // expect: inner
}
}
Inner.staticMethod
IO.print(__field) // expect: outer
}
instanceMethod {
__field = "outer"
IO.print(__field) // expect: outer
class Inner {
instanceMethod {
__field = "inner"
IO.print(__field) // expect: inner
}
}
(new Inner).instanceMethod
IO.print(__field) // expect: outer
}
}
Outer.staticMethod
(new Outer).instanceMethod
+1
View File
@@ -0,0 +1 @@
__field = "wat" // expect error
+8
View File
@@ -0,0 +1,8 @@
class Foo {
static write { IO.print(__field) } // Compile a use of the field...
static init { __field = "value" } // ...before an assignment to it.
}
// But invoke them in the right order.
Foo.init
Foo.write // expect: value