feat: implement class inheritance with 'is' keyword and superclass method copying
Add support for class inheritance in the compiler by parsing the 'is' keyword to load a superclass and emit a CODE_SUBCLASS instruction. In the VM, extend newClass to accept a superclass parameter, copy inherited methods from the superclass into the new class, and store the superclass pointer in the ObjClass struct. Introduce a root Object class in the core library and set it as the superclass for all built-in types. Add a test file verifying method inheritance and arity-based dispatch.
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
class Foo {
|
||||
methodOnFoo { io.write("foo") }
|
||||
method(a) { io.write("foo") }
|
||||
method(a, b, c) { io.write("foo") }
|
||||
}
|
||||
|
||||
class Bar is Foo {
|
||||
methodOnBar { io.write("bar") }
|
||||
method(a, b) { io.write("bar") }
|
||||
method(a, b, c, d) { io.write("bar") }
|
||||
}
|
||||
|
||||
var bar = Bar.new
|
||||
bar.methodOnFoo // expect: foo
|
||||
bar.methodOnBar // expect: bar
|
||||
|
||||
// Methods with different arity do not shadow each other.
|
||||
bar.method(1) // expect: foo
|
||||
bar.method(1, 2) // expect: bar
|
||||
bar.method(1, 2, 3) // expect: foo
|
||||
bar.method(1, 2, 3, 4) // expect: bar
|
||||
|
||||
// TODO(bob): Overriding (or BETA-style refining?).
|
||||
// TODO(bob): Private fields.
|
||||
// TODO(bob): Super (or inner) calls.
|
||||
// TODO(bob): Grammar for what expressions can follow "is".
|
||||
Reference in New Issue
Block a user