feat: add is operator for type checking and refactor primitives registration
Add TOKEN_IS token and CODE_IS bytecode to support runtime type checking via the `is` keyword. Refactor primitive registration by replacing `registerPrimitives` with `loadCore`, which compiles a core library string to define built-in classes (Bool, Class, Function, Num, Null, String, IO) and then attaches primitive methods to them. Update Primitive typedef to remove unused numArgs parameter, and add findGlobal helper to retrieve globals by name. Include test files for class syntax, class instantiation, and the new `is` operator.
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
class Foo {}
|
||||
|
||||
var foo = Foo.new
|
||||
io.write(foo is Foo) // expect: true
|
||||
@@ -0,0 +1,7 @@
|
||||
// Empty body.
|
||||
class A {}
|
||||
|
||||
// Newline body.
|
||||
class B {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
io.write(true is Bool) // expect: true
|
||||
io.write((fn 1) is Function) // expect: true
|
||||
io.write(123 is Num) // expect: true
|
||||
io.write(null is Null) // expect: true
|
||||
io.write("s" is String) // expect: true
|
||||
|
||||
io.write(Num is Bool) // expect: false
|
||||
io.write(null is Class) // expect: false
|
||||
io.write(true is Function) // expect: false
|
||||
io.write((fn 1) is Num) // expect: false
|
||||
io.write("s" is Null) // expect: false
|
||||
io.write(123 is String) // expect: false
|
||||
|
||||
// TODO(bob): Non-class on RHS.
|
||||
// TODO(bob): Precedence and associativity.
|
||||
// TODO(bob): Metaclasses ("Num is Class").
|
||||
Reference in New Issue
Block a user