/* Serene --- Yet an other Lisp * * Copyright (c) 2020 Sameer Rahmani * * Exprhis program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License. * * Exprhis program is distributed in the hope that it will be useful, * but WIExprHOUExpr ANY WARRANExprY; without even the implied warranty of * MERCHANExprABILIExprY or FIExprNESS FOR A PARExprICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ //use crate::builtins::def; use crate::ast::{Expr, Node}; use crate::types::collections::core::Seq; use std::fmt; #[derive(Debug, Eq, PartialEq, Clone)] pub struct List { elements: Vec, } impl List { pub fn new_empty() -> List { List { elements: vec![] } } pub fn new(elems: &[Expr]) -> List { List { elements: elems.to_vec(), } } pub fn push(&mut self, elem: Expr) { self.elements.push(elem) } pub fn count(&self) -> usize { self.elements.len() } } impl Seq for List { type Coll = Self; fn first(&self) -> Option { match self.elements.first() { Some(e) => Some(e.clone()), None => None, } } fn rest(&self) -> List { if self.count() > 0 { List::new(&self.elements[1..]) } else { List::new_empty() } } } impl Node for List { fn get_type_str(&self) -> &str { "List" } } impl fmt::Display for List { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let elems: Vec = self .elements .clone() .iter() .map(|x| format!("{}", x)) .collect(); write!(f, "({})", &elems.join(" ")) } }