From 0ef1318a35aadde4f6a139a2519afe84103439a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Mon, 2 Jan 2023 21:37:29 +0100 Subject: [PATCH] Interpreter - Function name is optional (lambdas...) --- src/interpreter/functions.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/interpreter/functions.rs b/src/interpreter/functions.rs index 00d31e9..8335283 100644 --- a/src/interpreter/functions.rs +++ b/src/interpreter/functions.rs @@ -14,19 +14,19 @@ use super::{Callable, EnvironmentRef, Value}; /// A function implemented in the Lox-ish language. #[derive(Debug)] pub(crate) struct Function { - name: Token, + name: Option, params: Vec, body: Vec, } impl Function { pub(crate) fn new( - name: &Token, + name: Option<&Token>, params: &Vec, body: &Vec, ) -> Rc> { let fun = Self { - name: name.clone(), + name: name.map(|t| t.clone()), params: params.clone(), body: body.clone(), }; @@ -66,6 +66,9 @@ impl Callable for Function { impl ToString for Function { fn to_string(&self) -> String { - format!("", self.name.lexeme) + match &self.name { + None => "".to_owned(), + Some(token) => format!("", token.lexeme), + } } }