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:
Bob Nystrom
2013-11-08 01:07:32 +00:00
parent 769e604dd1
commit 266881994f
8 changed files with 140 additions and 79 deletions
+4
View File
@@ -0,0 +1,4 @@
class Foo {}
var foo = Foo.new
io.write(foo is Foo) // expect: true
+7
View File
@@ -0,0 +1,7 @@
// Empty body.
class A {}
// Newline body.
class B {
}
+16
View File
@@ -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").