feat: add this keyword support with method detection and error handling
Introduce TOKEN_THIS token and `this` keyword parsing in the compiler, enabling the `this` expression for method contexts. Add `isMethod` flag to Compiler struct to track whether the current function is a method, and pass it through `initCompiler`. Implement `this_` parsing function that emits appropriate bytecode or errors when used outside a method. Remove the now-unnecessary `numLocals` field from ObjFn struct in vm.h. Add test cases for `this` at top level (expects error), in a method (valid usage), in a top-level function (expects error), and as a variable name (expects error).
This commit is contained in:
@@ -0,0 +1 @@
|
||||
this // expect error
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
bar { this }
|
||||
baz { "baz" }
|
||||
}
|
||||
|
||||
Foo.new.bar.baz
|
||||
@@ -0,0 +1,3 @@
|
||||
fn {
|
||||
this // expect error
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
var this = "value" // expect error
|
||||
Reference in New Issue
Block a user