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