first documentation run

This commit is contained in:
Sebastian Hugentobler 2025-05-26 12:24:35 +02:00
parent e065108f58
commit f59e6a5fd5
Signed by: shu
SSH key fingerprint: SHA256:ppcx6MlixdNZd5EUM1nkHOKoyQYoJwzuQKXM6J/t66M
21 changed files with 64 additions and 23 deletions

View file

@ -10,7 +10,7 @@ use crate::{
use core::fmt::Debug;
use std::{cell::RefCell, fmt::Display, rc::Rc};
/// A lox function.
/// A Lox function with name, parameters, body, and closure environment.
#[derive(Debug, Clone)]
pub struct Function {
pub name: Token,
@ -62,6 +62,8 @@ impl Callable for Function {
}
impl Function {
/// Bind a method to an instance, creating a new function with "this" defined in its environment.
/// This allows methods to access the instance they belong to.
pub fn bind(&self, instance: Rc<RefCell<Instance>>) -> Self {
let mut env = Environment::with_enclosing(self.closure.clone());
env.define("this".to_string(), Value::Instance(instance));