From bb83142ffd7f0f4b7fb96e08ea0d88c8731a9d20 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Fri, 18 Sep 2020 21:52:45 +0100 Subject: [PATCH] Commit the old list implementation --- docs/examples/hello_world.srn | 5 ++-- src/ast.rs | 8 +++--- src/builtins/def.rs | 19 +++++++++++++- src/main.rs | 1 + src/types.rs | 4 +-- src/types/collections.rs | 21 +++++++++++++++ src/types/collections/core.rs | 40 +++++++++++++++++++++++++++++ src/types/{ => collections}/list.rs | 32 +++++++++++++++++++---- 8 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 src/types/collections.rs create mode 100644 src/types/collections/core.rs rename src/types/{ => collections}/list.rs (71%) diff --git a/docs/examples/hello_world.srn b/docs/examples/hello_world.srn index ac3b597..5a75c30 100644 --- a/docs/examples/hello_world.srn +++ b/docs/examples/hello_world.srn @@ -1,2 +1,3 @@ -(defn hello! (name) - (println "Hello %s" name)) +(def a 4) +;; (defn hello! (name) +;; (println "Hello %s" name)) diff --git a/src/ast.rs b/src/ast.rs index 57eba9c..17dff97 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -15,7 +15,8 @@ * along with this program. If not, see . */ use crate::compiler::Compiler; -use crate::types::{ExprResult, Expression, List, Number, Symbol}; +use crate::types::collections; +use crate::types::{ExprResult, Expression, Number, Symbol}; #[derive(Debug, Eq, PartialEq, Clone)] pub enum Expr { @@ -24,14 +25,14 @@ pub enum Expr { Num(Number), Comment, Error(String), - Cons(Box), + List(Box), Nil, NoMatch, } impl Expr { pub fn make_list(first: Expr, rest: Expr) -> Expr { - Expr::Cons(Box::new(List::new(first, rest))) + Expr::List(Box::new(collections::List::new(first, rest))) } pub fn make_symbol(v: String) -> Expr { @@ -52,6 +53,7 @@ impl Expression for Expr { fn code_gen<'ctx>(&self, compiler: &'ctx Compiler) -> ExprResult<'ctx> { match self { Expr::Sym(s) => s.code_gen(compiler), + Expr::Cons(s) => s.code_gen(compiler), _ => Err("NotImplemented".to_string()), } } diff --git a/src/builtins/def.rs b/src/builtins/def.rs index 71daa17..570ba0f 100644 --- a/src/builtins/def.rs +++ b/src/builtins/def.rs @@ -15,8 +15,25 @@ * along with this program. If not, see . */ use crate::compiler::Compiler; +use crate::types::collections::core::{first, rest}; use crate::types::{ExprResult, List}; pub fn def<'a>(compiler: &'a Compiler, args: &'a List) -> ExprResult<'a> { - Err("Not implemented".to_string()) + // TODO: We need to support docstrings for def + if args.length != 3 { + // TODO: Raise a meaningful error by including the location + panic!(format!( + "`def` expects 2 parameters, '{}' given.", + args.length + )); + } + + //let def_ = &args.first;1 + let name = first(rest(args)); + //let value = first(rest(rest(args))); + + println!("<<<< {:?}", name); + // TODO: make sure that `def_` is a symbol and its name is "def" + + Err("Is not completed".to_string()) } diff --git a/src/main.rs b/src/main.rs index 75d3f68..fd8f107 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,6 +50,7 @@ fn main() -> io::Result<()> { match reader::read_string(&buf) { Ok(v) => { println!("AST: {:#?}", v); + let g = compiler::compile(&compiler, v); println!("GEN: {:?}", g) } diff --git a/src/types.rs b/src/types.rs index 1fc218d..7fcee87 100644 --- a/src/types.rs +++ b/src/types.rs @@ -14,12 +14,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +pub mod collections; pub mod core; -pub mod list; pub mod number; pub mod symbol; +pub use self::collections::{List, Seq}; pub use self::core::{ExprResult, Expression}; -pub use self::list::List; pub use self::number::Number; pub use self::symbol::Symbol; diff --git a/src/types/collections.rs b/src/types/collections.rs new file mode 100644 index 0000000..d7fcc88 --- /dev/null +++ b/src/types/collections.rs @@ -0,0 +1,21 @@ +/** Serene --- Yet an other Lisp +* +* Copyright (c) 2020 Sameer Rahmani +* +* This 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. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR 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 . +*/ +pub mod core; +pub mod list; + +pub use self::core::Seq; +pub use self::list::List; diff --git a/src/types/collections/core.rs b/src/types/collections/core.rs new file mode 100644 index 0000000..c67fe34 --- /dev/null +++ b/src/types/collections/core.rs @@ -0,0 +1,40 @@ +/** Serene --- Yet an other Lisp +* +* Copyright (c) 2020 Sameer Rahmani +* +* This 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. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR 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 . +*/ + +pub trait Seq { + fn first(&self) -> &T; + fn rest(&self) -> Option<&Self>; +} + +pub fn first<'a, T, S: Seq>(coll: impl Into>) -> Option<&'a T> +where + S: 'a, +{ + coll.into().and_then(first) + // match coll.into() { + // Some(v) => Some(v.first()), + // None => None, + // } +} + +pub fn rest<'a, T, S: Seq>(coll: impl Into>) -> Option<&'a S> { + coll.into().and_then(rest) + // match coll.into() { + // Some(v) => v.rest(), + // None => None, + // } +} diff --git a/src/types/list.rs b/src/types/collections/list.rs similarity index 71% rename from src/types/list.rs rename to src/types/collections/list.rs index fa0786f..526da24 100644 --- a/src/types/list.rs +++ b/src/types/collections/list.rs @@ -15,15 +15,18 @@ * along with this program. If not, see . */ use crate::ast::Expr; +use crate::builtins::def; use crate::compiler::Compiler; +use crate::types::collections::core::Seq; use crate::types::core::{ExprResult, Expression}; #[derive(Debug, Eq, PartialEq, Clone)] pub struct List { - first: Expr, - rest: Expr, - length: u64, + pub car: Expr, + pub cdr: Expr, + pub length: u64, } +//pub enum List { Nil, Cons(T, Box>) } impl List { pub fn new(first: Expr, rest: Expr) -> List { @@ -43,8 +46,8 @@ impl List { } } }, - first, - rest, + car: first, + cdr: rest, } } } @@ -52,6 +55,25 @@ impl List { impl Expression for List { fn eval() {} fn code_gen<'ctx>(&self, compiler: &'ctx Compiler) -> ExprResult<'ctx> { + // match &self.car { + // Expr::Sym(s) => def(compiler, self), + // _ => , + // } + def(compiler, self); Err("Not implemented on list".to_string()) } } + +impl Seq for List { + fn first<'a>(&'a self) -> &'a Expr { + &self.car + } + + fn rest<'a>(&'a self) -> Option<&'a List> { + match &self.cdr { + Expr::Nil => None, + Expr::Cons(v) => Some(v), + _ => panic!("'rest' should not match anything else!"), + } + } +}