From d2b66db85229eb4278899d39f2e08d933927aa44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Thu, 12 Jan 2023 08:47:16 +0100 Subject: [PATCH] Interpreter - Filter non-static methods during class creation --- src/interpreter/interpretable.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/interpreter/interpretable.rs b/src/interpreter/interpretable.rs index a6016b3..0929386 100644 --- a/src/interpreter/interpretable.rs +++ b/src/interpreter/interpretable.rs @@ -1,13 +1,16 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc}; use crate::{ - ast::{ClassDecl, ExprNode, FunDecl, GetExpr, ProgramNode, SetExpr, StmtNode, VariableExpr}, + ast::{ + ClassDecl, ClassMemberDecl, ExprNode, FunDecl, GetExpr, ProgramNode, SetExpr, StmtNode, + VariableExpr, + }, errors::{ErrorKind, SloxError, SloxResult}, resolver::ResolvedVariables, tokens::{Token, TokenType}, }; -use super::{classes::{Class, PropertyCarrier}, functions::Function, Environment, EnvironmentRef, Value}; +use super::{classes::Class, functions::Function, Environment, EnvironmentRef, Value}; /// Evaluate an interpretable, returning its value. pub fn evaluate(ast: &ProgramNode, vars: ResolvedVariables) -> SloxResult { @@ -198,10 +201,10 @@ impl StmtNode { fn on_class_decl(&self, es: &mut InterpreterState, decl: &ClassDecl) -> InterpreterResult { es.environment.borrow_mut().define(&decl.name, None)?; let methods = decl - .methods + .members .iter() - .map(|method| { - ( + .filter_map(|member| match member { + ClassMemberDecl::Method(method) => Some(( method.name.lexeme.clone(), Function::new( Some(&method.name), @@ -210,7 +213,8 @@ impl StmtNode { es.environment.clone(), method.name.lexeme == "init", ), - ) + )), + _ => None, }) .collect::>(); let class = Class::new(decl.name.lexeme.clone(), methods);