2020-11-02 00:11:12 +00:00
|
|
|
/* Serene --- Yet an other Lisp
|
2020-09-09 21:40:31 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
*
|
2020-11-01 19:40:07 +00:00
|
|
|
* Exprhis program is free software; you can redistribute it and/or modify
|
2020-09-09 21:40:31 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License.
|
|
|
|
*
|
2020-11-01 19:40:07 +00:00
|
|
|
* 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
|
2020-09-09 21:40:31 +01:00
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
2020-09-14 21:46:52 +01:00
|
|
|
*/
|
2020-11-02 00:55:56 +00:00
|
|
|
|
2020-11-01 19:40:07 +00:00
|
|
|
//use crate::builtins::def;
|
2020-11-10 20:14:30 +00:00
|
|
|
use crate::ast::{Expr, Node};
|
2020-09-18 21:52:45 +01:00
|
|
|
use crate::types::collections::core::Seq;
|
2020-11-02 00:55:56 +00:00
|
|
|
use std::fmt;
|
2020-06-13 17:55:27 +01:00
|
|
|
|
2020-06-20 15:20:05 +01:00
|
|
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
2020-09-14 21:46:52 +01:00
|
|
|
pub struct List {
|
2020-09-19 00:28:40 +01:00
|
|
|
elements: Vec<Expr>,
|
2020-06-20 15:20:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-14 21:46:52 +01:00
|
|
|
impl List {
|
2020-09-19 00:28:40 +01:00
|
|
|
pub fn new_empty() -> List {
|
|
|
|
List { elements: vec![] }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(elems: &[Expr]) -> List {
|
2020-09-14 21:46:52 +01:00
|
|
|
List {
|
2020-09-19 00:28:40 +01:00
|
|
|
elements: elems.to_vec(),
|
2020-09-14 21:46:52 +01:00
|
|
|
}
|
2020-06-13 17:55:27 +01:00
|
|
|
}
|
2020-09-19 00:28:40 +01:00
|
|
|
|
|
|
|
pub fn push(&mut self, elem: Expr) {
|
|
|
|
self.elements.push(elem)
|
|
|
|
}
|
|
|
|
|
2020-11-01 19:40:07 +00:00
|
|
|
pub fn count(&self) -> usize {
|
2020-09-19 01:23:33 +01:00
|
|
|
self.elements.len()
|
|
|
|
}
|
2020-06-13 17:55:27 +01:00
|
|
|
}
|
2020-06-20 15:20:05 +01:00
|
|
|
|
2020-09-19 01:23:33 +01:00
|
|
|
impl Seq<Expr> for List {
|
2020-11-01 19:40:07 +00:00
|
|
|
type Coll = Self;
|
2020-09-18 21:52:45 +01:00
|
|
|
|
2020-09-19 01:23:33 +01:00
|
|
|
fn first(&self) -> Option<Expr> {
|
|
|
|
match self.elements.first() {
|
|
|
|
Some(e) => Some(e.clone()),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rest(&self) -> List {
|
2020-11-01 19:40:07 +00:00
|
|
|
if self.count() > 0 {
|
2020-09-19 01:23:33 +01:00
|
|
|
List::new(&self.elements[1..])
|
|
|
|
} else {
|
|
|
|
List::new_empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 19:40:07 +00:00
|
|
|
|
2020-11-10 20:14:30 +00:00
|
|
|
impl Node for List {
|
|
|
|
fn get_type_str(&self) -> &str {
|
|
|
|
"List"
|
2020-11-01 19:40:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-02 00:55:56 +00:00
|
|
|
|
|
|
|
impl fmt::Display for List {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let elems: Vec<String> = self
|
|
|
|
.elements
|
|
|
|
.clone()
|
|
|
|
.iter()
|
|
|
|
.map(|x| format!("{}", x))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
write!(f, "({})", &elems.join(" "))
|
|
|
|
}
|
|
|
|
}
|