Commit the old list implementation

This commit is contained in:
Sameer Rahmani 2020-09-18 21:52:45 +01:00
parent fa9c51e71c
commit bb83142ffd
8 changed files with 117 additions and 13 deletions

View File

@ -1,2 +1,3 @@
(defn hello! (name)
(println "Hello %s" name))
(def a 4)
;; (defn hello! (name)
;; (println "Hello %s" name))

View File

@ -15,7 +15,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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>),
List(Box<collections::List>),
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()),
}
}

View File

@ -15,8 +15,25 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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())
}

View File

@ -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)
}

View File

@ -14,12 +14,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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;

21
src/types/collections.rs Normal file
View File

@ -0,0 +1,21 @@
/** Serene --- Yet an other Lisp
*
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
pub mod core;
pub mod list;
pub use self::core::Seq;
pub use self::list::List;

View File

@ -0,0 +1,40 @@
/** Serene --- Yet an other Lisp
*
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
pub trait Seq<T> {
fn first(&self) -> &T;
fn rest(&self) -> Option<&Self>;
}
pub fn first<'a, T, S: Seq<T>>(coll: impl Into<Option<&'a S>>) -> 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<T>>(coll: impl Into<Option<&'a S>>) -> Option<&'a S> {
coll.into().and_then(rest)
// match coll.into() {
// Some(v) => v.rest(),
// None => None,
// }
}

View File

@ -15,15 +15,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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<T> { Nil, Cons(T, Box<List<T>>) }
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<Expr> 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!"),
}
}
}