From 11c1197f4f10bb84b93e5451724752992b909b5d Mon Sep 17 00:00:00 2001 From: Gabriel Bento Date: Thu, 14 Nov 2024 22:33:49 -0400 Subject: [PATCH] fix: class error when passing arguments This error only happens when arguments are passed to the class init method. --- src/interpreter.rs | 5 ++++- test_files/class.lox | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test_files/class.lox diff --git a/src/interpreter.rs b/src/interpreter.rs index 4c0c6f0..d532073 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -318,7 +318,10 @@ impl Interpreter { callee.check_arity(evaluated_args.len(), paren)?; callee.call(self, evaluated_args) } - Value::Class(callee) => callee.call(self, vec![]), + Value::Class(callee) => { + callee.check_arity(evaluated_args.len(), paren)?; + callee.call(self, evaluated_args) + }, _ => Exception::runtime_error( paren.clone(), String::from("Can only call functions and classes."), diff --git a/test_files/class.lox b/test_files/class.lox new file mode 100644 index 0000000..aac4d47 --- /dev/null +++ b/test_files/class.lox @@ -0,0 +1,12 @@ +class Player { + init(name) { + this.name = name; + } + + run() { + print("Player " + this.name + " is running..."); + } +} + +var p = Player("Foo"); +p.run(); \ No newline at end of file