mirror of
https://github.com/kanaka/mal.git
synced 2024-09-11 13:55:55 +03:00
rust: add conj, stepA. Self-hosting!
This commit is contained in:
parent
bd3067230d
commit
77b2da6cf3
@ -39,3 +39,5 @@ name = "step7_quote"
|
||||
name = "step8_macros"
|
||||
[[bin]]
|
||||
name = "step9_try"
|
||||
[[bin]]
|
||||
name = "stepA_interop"
|
||||
|
@ -3,14 +3,14 @@
|
||||
SOURCES_BASE = src/types.rs src/readline.rs \
|
||||
src/reader.rs src/printer.rs \
|
||||
src/env.rs src/core.rs
|
||||
SOURCES_LISP = src/env.rs src/core.rs src/step9_try.rs
|
||||
SOURCES_LISP = src/env.rs src/core.rs src/stepA_interop.rs
|
||||
SOURCES = $(SOURCES_BASE) $(SOURCES_LISP)
|
||||
|
||||
#####################
|
||||
|
||||
SRCS = step0_repl.rs step1_read_print.rs step2_eval.rs step3_env.rs \
|
||||
step4_if_fn_do.rs step5_tco.rs step6_file.rs step7_quote.rs \
|
||||
step8_macros.rs step9_try.rs
|
||||
step8_macros.rs step9_try.rs stepA_interop.rs
|
||||
BINS = $(SRCS:%.rs=target/%)
|
||||
|
||||
#####################
|
||||
|
@ -7,7 +7,7 @@ use std::io::File;
|
||||
use types::{MalVal,MalRet,err_val,err_str,err_string,
|
||||
Nil,Int,Strn,List,Vector,Hash_Map,Func,MalFunc,Atom,
|
||||
_nil,_true,_false,_int,string,
|
||||
list,listm,vectorm,hash_mapm,func,funcm,malfuncd};
|
||||
list,vector,listm,vectorm,hash_mapm,func,funcm,malfuncd};
|
||||
use types;
|
||||
use readline;
|
||||
use reader;
|
||||
@ -209,7 +209,7 @@ pub fn keys(a:Vec<MalVal>) -> MalRet {
|
||||
Nil => return Ok(_nil()),
|
||||
_ => return err_str("contains? on non-hash map"),
|
||||
};
|
||||
if hm.len() == 0 { return Ok(_nil()); }
|
||||
//if hm.len() == 0 { return Ok(_nil()); }
|
||||
let mut keys = vec![];
|
||||
for k in hm.keys() {
|
||||
keys.push(string(k.to_string()));
|
||||
@ -227,7 +227,7 @@ pub fn vals(a:Vec<MalVal>) -> MalRet {
|
||||
Nil => return Ok(_nil()),
|
||||
_ => return err_str("contains? on non-hash map"),
|
||||
};
|
||||
if hm.len() == 0 { return Ok(_nil()); }
|
||||
//if hm.len() == 0 { return Ok(_nil()); }
|
||||
let mut vals = vec![];
|
||||
for k in hm.values() {
|
||||
vals.push(k.clone());
|
||||
@ -382,6 +382,30 @@ pub fn map(a:Vec<MalVal>) -> MalRet {
|
||||
Ok(list(results))
|
||||
}
|
||||
|
||||
pub fn conj(a:Vec<MalVal>) -> MalRet {
|
||||
if a.len() < 2 {
|
||||
return err_str("Wrong arity to conj call");
|
||||
}
|
||||
let mut new_v:Vec<MalVal> = vec![];
|
||||
match *a[0].clone() {
|
||||
List(ref l,_) => {
|
||||
new_v.push_all(l.as_slice());
|
||||
for mv in a.iter().skip(1) {
|
||||
new_v.insert(0,mv.clone());
|
||||
}
|
||||
Ok(list(new_v))
|
||||
},
|
||||
Vector(ref l,_) => {
|
||||
new_v.push_all(l.as_slice());
|
||||
for mv in a.iter().skip(1) {
|
||||
new_v.push(mv.clone());
|
||||
}
|
||||
Ok(vector(new_v))
|
||||
},
|
||||
_ => return err_str("conj called with non-sequence"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Metadata functions
|
||||
fn with_meta(a:Vec<MalVal>) -> MalRet {
|
||||
@ -519,10 +543,12 @@ pub fn ns() -> HashMap<String,MalVal> {
|
||||
ns.insert("count".to_string(), func(count));
|
||||
ns.insert("apply".to_string(), func(apply));
|
||||
ns.insert("map".to_string(), func(map));
|
||||
ns.insert("conj".to_string(), func(conj));
|
||||
|
||||
ns.insert("with-meta".to_string(), func(with_meta));
|
||||
ns.insert("meta".to_string(), func(meta));
|
||||
ns.insert("atom".to_string(), func(types::atom));
|
||||
ns.insert("atom?".to_string(), func(types::atom_q));
|
||||
ns.insert("deref".to_string(), func(deref));
|
||||
ns.insert("reset!".to_string(), func(reset_bang));
|
||||
ns.insert("swap!".to_string(), func(swap_bang));
|
||||
|
@ -38,7 +38,7 @@ fn eval_ast(ast: MalVal, env: &HashMap<String,MalVal>) -> MalRet {
|
||||
}
|
||||
}
|
||||
Ok(match *ast { List(_,_) => list(ast_vec),
|
||||
_ => vector(ast_vec) })
|
||||
_ => vector(ast_vec) })
|
||||
},
|
||||
Hash_Map(ref hm,_) => {
|
||||
let mut new_hm: HashMap<String,MalVal> = HashMap::new();
|
||||
|
@ -39,7 +39,7 @@ fn eval_ast(ast: MalVal, env: Env) -> MalRet {
|
||||
}
|
||||
}
|
||||
Ok(match *ast { List(_,_) => list(ast_vec),
|
||||
_ => vector(ast_vec) })
|
||||
_ => vector(ast_vec) })
|
||||
},
|
||||
Hash_Map(ref hm,_) => {
|
||||
let mut new_hm: HashMap<String,MalVal> = HashMap::new();
|
||||
|
@ -40,7 +40,7 @@ fn eval_ast(ast: MalVal, env: Env) -> MalRet {
|
||||
}
|
||||
}
|
||||
Ok(match *ast { List(_,_) => list(ast_vec),
|
||||
_ => vector(ast_vec) })
|
||||
_ => vector(ast_vec) })
|
||||
},
|
||||
Hash_Map(ref hm,_) => {
|
||||
let mut new_hm: HashMap<String,MalVal> = HashMap::new();
|
||||
|
@ -40,7 +40,7 @@ fn eval_ast(ast: MalVal, env: Env) -> MalRet {
|
||||
}
|
||||
}
|
||||
Ok(match *ast { List(_,_) => list(ast_vec),
|
||||
_ => vector(ast_vec) })
|
||||
_ => vector(ast_vec) })
|
||||
},
|
||||
Hash_Map(ref hm,_) => {
|
||||
let mut new_hm: HashMap<String,MalVal> = HashMap::new();
|
||||
|
@ -41,7 +41,7 @@ fn eval_ast(ast: MalVal, env: Env) -> MalRet {
|
||||
}
|
||||
}
|
||||
Ok(match *ast { List(_,_) => list(ast_vec),
|
||||
_ => vector(ast_vec) })
|
||||
_ => vector(ast_vec) })
|
||||
},
|
||||
Hash_Map(ref hm,_) => {
|
||||
let mut new_hm: HashMap<String,MalVal> = HashMap::new();
|
||||
|
@ -92,7 +92,7 @@ fn eval_ast(ast: MalVal, env: Env) -> MalRet {
|
||||
}
|
||||
}
|
||||
Ok(match *ast { List(_,_) => list(ast_vec),
|
||||
_ => vector(ast_vec) })
|
||||
_ => vector(ast_vec) })
|
||||
},
|
||||
Hash_Map(ref hm,_) => {
|
||||
let mut new_hm: HashMap<String,MalVal> = HashMap::new();
|
||||
|
@ -151,7 +151,7 @@ fn eval_ast(ast: MalVal, env: Env) -> MalRet {
|
||||
}
|
||||
}
|
||||
Ok(match *ast { List(_,_) => list(ast_vec),
|
||||
_ => vector(ast_vec) })
|
||||
_ => vector(ast_vec) })
|
||||
},
|
||||
Hash_Map(ref hm,_) => {
|
||||
let mut new_hm: HashMap<String,MalVal> = HashMap::new();
|
||||
@ -409,6 +409,8 @@ fn main() {
|
||||
// core.mal: defined using the language itself
|
||||
let _ = rep("(def! not (fn* (a) (if a false true)))", repl_env.clone());
|
||||
let _ = rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", repl_env.clone());
|
||||
let _ = rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))", repl_env.clone());
|
||||
let _ = rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))", repl_env.clone());
|
||||
|
||||
// Invoked with command line arguments
|
||||
let args = os::args();
|
||||
|
@ -151,7 +151,7 @@ fn eval_ast(ast: MalVal, env: Env) -> MalRet {
|
||||
}
|
||||
}
|
||||
Ok(match *ast { List(_,_) => list(ast_vec),
|
||||
_ => vector(ast_vec) })
|
||||
_ => vector(ast_vec) })
|
||||
},
|
||||
Hash_Map(ref hm,_) => {
|
||||
let mut new_hm: HashMap<String,MalVal> = HashMap::new();
|
||||
@ -440,6 +440,8 @@ fn main() {
|
||||
let _ = rep("(def! *host-language* \"rust\")", repl_env.clone());
|
||||
let _ = rep("(def! not (fn* (a) (if a false true)))", repl_env.clone());
|
||||
let _ = rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", repl_env.clone());
|
||||
let _ = rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))", repl_env.clone());
|
||||
let _ = rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))", repl_env.clone());
|
||||
|
||||
// Invoked with command line arguments
|
||||
let args = os::args();
|
||||
|
478
rust/src/stepA_interop.rs
Normal file
478
rust/src/stepA_interop.rs
Normal file
@ -0,0 +1,478 @@
|
||||
// support precompiled regexes in reader.rs
|
||||
#![feature(phase)]
|
||||
#[phase(plugin)]
|
||||
extern crate regex_macros;
|
||||
extern crate regex;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::os;
|
||||
|
||||
use types::{MalVal,MalRet,MalError,ErrString,ErrMalVal,err_str,
|
||||
Nil,False,Sym,List,Vector,Hash_Map,Func,MalFunc,
|
||||
_nil,symbol,string,list,vector,hash_map,malfunc,malfuncd};
|
||||
use env::{Env,env_new,env_bind,env_root,env_find,env_set,env_get};
|
||||
mod readline;
|
||||
mod types;
|
||||
mod reader;
|
||||
mod printer;
|
||||
mod env;
|
||||
mod core;
|
||||
|
||||
// read
|
||||
fn read(str: String) -> MalRet {
|
||||
reader::read_str(str)
|
||||
}
|
||||
|
||||
// eval
|
||||
fn is_pair(x: MalVal) -> bool {
|
||||
match *x {
|
||||
List(ref lst,_) => lst.len() > 0,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn quasiquote(ast: MalVal) -> MalVal {
|
||||
if !is_pair(ast.clone()) {
|
||||
return list(vec![symbol("quote"), ast])
|
||||
}
|
||||
|
||||
match *ast.clone() {
|
||||
List(ref args,_) => {
|
||||
let ref a0 = args[0];
|
||||
match **a0 {
|
||||
Sym(ref s) => {
|
||||
if s.to_string() == "unquote".to_string() {
|
||||
let ref a1 = args[1];
|
||||
return a1.clone();
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
if is_pair(a0.clone()) {
|
||||
match **a0 {
|
||||
List(ref a0args,_) => {
|
||||
let a00 = a0args[0].clone();
|
||||
match *a00 {
|
||||
Sym(ref s) => {
|
||||
if s.to_string() == "splice-unquote".to_string() {
|
||||
return list(vec![symbol("concat"),
|
||||
a0args[1].clone(),
|
||||
quasiquote(list(args.slice(1,args.len()).to_vec()))])
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
let rest = list(args.slice(1,args.len()).to_vec());
|
||||
return list(vec![symbol("cons"),
|
||||
quasiquote(a0.clone()),
|
||||
quasiquote(rest)])
|
||||
},
|
||||
_ => _nil(), // should never reach
|
||||
}
|
||||
}
|
||||
|
||||
fn is_macro_call(ast: MalVal, env: Env) -> bool {
|
||||
match *ast {
|
||||
List(ref lst,_) => {
|
||||
let ref a0 = *lst[0];
|
||||
match *a0 {
|
||||
Sym(ref a0sym) => {
|
||||
if env_find(env.clone(), a0sym.to_string()).is_some() {
|
||||
match env_get(env, a0sym.to_string()) {
|
||||
Ok(f) => {
|
||||
match *f {
|
||||
MalFunc(ref mfd,_) => {
|
||||
mfd.is_macro
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn macroexpand(mut ast: MalVal, env: Env) -> MalRet {
|
||||
while is_macro_call(ast.clone(), env.clone()) {
|
||||
let ast2 = ast.clone();
|
||||
let args = match *ast2 {
|
||||
List(ref args,_) => args,
|
||||
_ => break,
|
||||
};
|
||||
let ref a0 = args[0];
|
||||
let mf = match **a0 {
|
||||
Sym(ref s) => {
|
||||
match env_get(env.clone(), s.to_string()) {
|
||||
Ok(mf) => mf,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
},
|
||||
_ => break,
|
||||
};
|
||||
match *mf {
|
||||
MalFunc(_,_) => {
|
||||
match mf.apply(args.slice(1,args.len()).to_vec()) {
|
||||
Ok(r) => ast = r,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
},
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
Ok(ast)
|
||||
}
|
||||
|
||||
fn eval_ast(ast: MalVal, env: Env) -> MalRet {
|
||||
let ast2 = ast.clone();
|
||||
match *ast2 {
|
||||
//match *ast {
|
||||
Sym(ref sym) => {
|
||||
env_get(env.clone(), sym.clone())
|
||||
},
|
||||
List(ref a,_) | Vector(ref a,_) => {
|
||||
let mut ast_vec : Vec<MalVal> = vec![];
|
||||
for mv in a.iter() {
|
||||
let mv2 = mv.clone();
|
||||
match eval(mv2, env.clone()) {
|
||||
Ok(mv) => { ast_vec.push(mv); },
|
||||
Err(e) => { return Err(e); },
|
||||
}
|
||||
}
|
||||
Ok(match *ast { List(_,_) => list(ast_vec),
|
||||
_ => vector(ast_vec) })
|
||||
},
|
||||
Hash_Map(ref hm,_) => {
|
||||
let mut new_hm: HashMap<String,MalVal> = HashMap::new();
|
||||
for (key, value) in hm.iter() {
|
||||
match eval(value.clone(), env.clone()) {
|
||||
Ok(mv) => { new_hm.insert(key.to_string(), mv); },
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
Ok(hash_map(new_hm))
|
||||
},
|
||||
_ => {
|
||||
Ok(ast)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
|
||||
'tco: loop {
|
||||
|
||||
//println!("eval: {}, {}", ast, env.borrow());
|
||||
//println!("eval: {}", ast);
|
||||
let mut ast2 = ast.clone();
|
||||
match *ast2 {
|
||||
List(_,_) => (), // continue
|
||||
_ => return eval_ast(ast2, env),
|
||||
}
|
||||
|
||||
// apply list
|
||||
match macroexpand(ast2, env.clone()) {
|
||||
Ok(a) => {
|
||||
ast2 = a;
|
||||
},
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
match *ast2 {
|
||||
List(_,_) => (), // continue
|
||||
_ => return Ok(ast2),
|
||||
}
|
||||
let ast3 = ast2.clone();
|
||||
|
||||
let (args, a0sym) = match *ast2 {
|
||||
List(ref args,_) => {
|
||||
if args.len() == 0 {
|
||||
return Ok(ast3);
|
||||
}
|
||||
let ref a0 = *args[0];
|
||||
match *a0 {
|
||||
Sym(ref a0sym) => (args, a0sym.as_slice()),
|
||||
_ => (args, "__<fn*>__"),
|
||||
}
|
||||
},
|
||||
_ => return err_str("Expected list"),
|
||||
};
|
||||
|
||||
match a0sym {
|
||||
"def!" => {
|
||||
let a1 = (*args)[1].clone();
|
||||
let a2 = (*args)[2].clone();
|
||||
let res = eval(a2, env.clone());
|
||||
match res {
|
||||
Ok(r) => {
|
||||
match *a1 {
|
||||
Sym(ref s) => {
|
||||
env_set(&env.clone(), s.clone(), r.clone());
|
||||
return Ok(r);
|
||||
},
|
||||
_ => {
|
||||
return err_str("def! of non-symbol")
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
},
|
||||
"let*" => {
|
||||
let let_env = env_new(Some(env.clone()));
|
||||
let a1 = (*args)[1].clone();
|
||||
let a2 = (*args)[2].clone();
|
||||
match *a1 {
|
||||
List(ref binds,_) | Vector(ref binds,_) => {
|
||||
let mut it = binds.iter();
|
||||
while it.len() >= 2 {
|
||||
let b = it.next().unwrap();
|
||||
let exp = it.next().unwrap();
|
||||
match **b {
|
||||
Sym(ref bstr) => {
|
||||
match eval(exp.clone(), let_env.clone()) {
|
||||
Ok(r) => {
|
||||
env_set(&let_env, bstr.clone(), r);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
return err_str("let* with non-symbol binding");
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => return err_str("let* with non-list bindings"),
|
||||
}
|
||||
ast = a2;
|
||||
env = let_env.clone();
|
||||
continue 'tco;
|
||||
},
|
||||
"quote" => {
|
||||
return Ok((*args)[1].clone());
|
||||
},
|
||||
"quasiquote" => {
|
||||
let a1 = (*args)[1].clone();
|
||||
ast = quasiquote(a1);
|
||||
continue 'tco;
|
||||
},
|
||||
"defmacro!" => {
|
||||
let a1 = (*args)[1].clone();
|
||||
let a2 = (*args)[2].clone();
|
||||
match eval(a2, env.clone()) {
|
||||
Ok(r) => {
|
||||
match *r {
|
||||
MalFunc(ref mfd,_) => {
|
||||
match *a1 {
|
||||
Sym(ref s) => {
|
||||
let mut new_mfd = mfd.clone();
|
||||
new_mfd.is_macro = true;
|
||||
let mf = malfuncd(new_mfd,_nil());
|
||||
env_set(&env.clone(), s.clone(), mf.clone());
|
||||
return Ok(mf);
|
||||
},
|
||||
_ => return err_str("def! of non-symbol"),
|
||||
}
|
||||
},
|
||||
_ => return err_str("def! of non-symbol"),
|
||||
}
|
||||
},
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
},
|
||||
"macroexpand" => {
|
||||
let a1 = (*args)[1].clone();
|
||||
return macroexpand(a1, env.clone())
|
||||
},
|
||||
"try*" => {
|
||||
let a1 = (*args)[1].clone();
|
||||
match eval(a1, env.clone()) {
|
||||
Ok(res) => return Ok(res),
|
||||
Err(err) => {
|
||||
if args.len() < 3 { return Err(err); }
|
||||
let a2 = (*args)[2].clone();
|
||||
let cat = match *a2 {
|
||||
List(ref cat,_) => cat,
|
||||
_ => return err_str("invalid catch* clause"),
|
||||
};
|
||||
if cat.len() != 3 {
|
||||
return err_str("wrong arity to catch* clause");
|
||||
}
|
||||
let c1 = (*cat)[1].clone();
|
||||
let bstr = match *c1 {
|
||||
Sym(ref s) => s,
|
||||
_ => return err_str("invalid catch* binding"),
|
||||
};
|
||||
let exc = match err {
|
||||
ErrMalVal(mv) => mv,
|
||||
ErrString(s) => string(s),
|
||||
};
|
||||
let bind_env = env_new(Some(env.clone()));
|
||||
env_set(&bind_env, bstr.to_string(), exc);
|
||||
let c2 = (*cat)[2].clone();
|
||||
return eval(c2, bind_env);
|
||||
},
|
||||
};
|
||||
}
|
||||
"do" => {
|
||||
let el = list(args.slice(1,args.len()-1).to_vec());
|
||||
match eval_ast(el, env.clone()) {
|
||||
Err(e) => return Err(e),
|
||||
Ok(_) => {
|
||||
let ref last = args[args.len()-1];
|
||||
ast = last.clone();
|
||||
continue 'tco;
|
||||
},
|
||||
}
|
||||
},
|
||||
"if" => {
|
||||
let a1 = (*args)[1].clone();
|
||||
let cond = eval(a1, env.clone());
|
||||
match cond {
|
||||
Err(e) => return Err(e),
|
||||
Ok(c) => match *c {
|
||||
False | Nil => {
|
||||
if args.len() >= 4 {
|
||||
let a3 = (*args)[3].clone();
|
||||
ast = a3;
|
||||
env = env.clone();
|
||||
continue 'tco;
|
||||
} else {
|
||||
return Ok(_nil());
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
let a2 = (*args)[2].clone();
|
||||
ast = a2;
|
||||
env = env.clone();
|
||||
continue 'tco;
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
"fn*" => {
|
||||
let a1 = (*args)[1].clone();
|
||||
let a2 = (*args)[2].clone();
|
||||
return Ok(malfunc(eval, a2, env.clone(), a1, _nil()));
|
||||
},
|
||||
"eval" => {
|
||||
let a1 = (*args)[1].clone();
|
||||
match eval(a1, env.clone()) {
|
||||
Ok(exp) => {
|
||||
ast = exp;
|
||||
env = env_root(&env);
|
||||
continue 'tco;
|
||||
},
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
},
|
||||
_ => { // function call
|
||||
return match eval_ast(ast3, env.clone()) {
|
||||
Err(e) => Err(e),
|
||||
Ok(el) => {
|
||||
let args = match *el {
|
||||
List(ref args,_) => args,
|
||||
_ => return err_str("Invalid apply"),
|
||||
};
|
||||
match *args.clone()[0] {
|
||||
Func(f,_) => f(args.slice(1,args.len()).to_vec()),
|
||||
MalFunc(ref mf,_) => {
|
||||
let mfc = mf.clone();
|
||||
let alst = list(args.slice(1,args.len()).to_vec());
|
||||
let new_env = env_new(Some(mfc.env.clone()));
|
||||
match env_bind(&new_env, mfc.params, alst) {
|
||||
Ok(_) => {
|
||||
ast = mfc.exp;
|
||||
env = new_env;
|
||||
continue 'tco;
|
||||
},
|
||||
Err(e) => err_str(e.as_slice()),
|
||||
}
|
||||
},
|
||||
_ => err_str("attempt to call non-function"),
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// print
|
||||
fn print(exp: MalVal) -> String {
|
||||
exp.pr_str(true)
|
||||
}
|
||||
|
||||
fn rep(str: &str, env: Env) -> Result<String,MalError> {
|
||||
match read(str.to_string()) {
|
||||
Err(e) => Err(e),
|
||||
Ok(ast) => {
|
||||
//println!("read: {}", ast);
|
||||
match eval(ast, env) {
|
||||
Err(e) => Err(e),
|
||||
Ok(exp) => Ok(print(exp)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// core.rs: defined using rust
|
||||
let repl_env = env_new(None);
|
||||
for (k, v) in core::ns().into_iter() { env_set(&repl_env, k, v); }
|
||||
// see eval() for definition of "eval"
|
||||
env_set(&repl_env, "*ARGV*".to_string(), list(vec![]));
|
||||
|
||||
// core.mal: defined using the language itself
|
||||
let _ = rep("(def! *host-language* \"rust\")", repl_env.clone());
|
||||
let _ = rep("(def! not (fn* (a) (if a false true)))", repl_env.clone());
|
||||
let _ = rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", repl_env.clone());
|
||||
let _ = rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))", repl_env.clone());
|
||||
let _ = rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))", repl_env.clone());
|
||||
|
||||
// Invoked with command line arguments
|
||||
let args = os::args();
|
||||
if args.len() > 1 {
|
||||
let mv_args = args.slice(2,args.len()).iter()
|
||||
.map(|a| string(a.to_string()))
|
||||
.collect::<Vec<MalVal>>();
|
||||
env_set(&repl_env, "*ARGV*".to_string(), list(mv_args));
|
||||
let lf = "(load-file \"".to_string() + args[1] + "\")".to_string();
|
||||
match rep(lf.as_slice(), repl_env.clone()) {
|
||||
Ok(_) => {
|
||||
os::set_exit_status(0);
|
||||
return;
|
||||
},
|
||||
Err(str) => {
|
||||
println!("Error: {}", str);
|
||||
os::set_exit_status(1);
|
||||
return;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// repl loop
|
||||
let _ = rep("(println (str \"Mal [\" *host-language* \"]\"))", repl_env.clone());
|
||||
loop {
|
||||
let line = readline::mal_readline("user> ");
|
||||
match line { None => break, _ => () }
|
||||
match rep(line.unwrap().as_slice(), repl_env.clone()) {
|
||||
Ok(str) => println!("{}", str),
|
||||
Err(ErrMalVal(_)) => (), // Blank line
|
||||
Err(ErrString(s)) => println!("Error: {}", s),
|
||||
}
|
||||
}
|
||||
}
|
@ -247,7 +247,7 @@ pub fn vector_q(a:Vec<MalVal>) -> MalRet {
|
||||
}
|
||||
match *a[0].clone() {
|
||||
Vector(_,_) => Ok(_true()),
|
||||
_ => Ok(_false()),
|
||||
_ => Ok(_false()),
|
||||
}
|
||||
}
|
||||
|
||||
@ -330,6 +330,15 @@ pub fn malfuncd(mfd: MalFuncData, meta: MalVal) -> MalVal {
|
||||
|
||||
|
||||
// Atoms
|
||||
pub fn atom_q(a:Vec<MalVal>) -> MalRet {
|
||||
if a.len() != 1 {
|
||||
return err_str("Wrong arity to atom? call");
|
||||
}
|
||||
match *a[0].clone() {
|
||||
Atom(_) => Ok(_true()),
|
||||
_ => Ok(_false()),
|
||||
}
|
||||
}
|
||||
pub fn atom(a:Vec<MalVal>) -> MalRet {
|
||||
if a.len() != 1 {
|
||||
return err_str("Wrong arity to atom call");
|
||||
@ -345,6 +354,6 @@ pub fn sequential_q(a:Vec<MalVal>) -> MalRet {
|
||||
}
|
||||
match *a[0].clone() {
|
||||
List(_,_) | Vector(_,_) => Ok(_true()),
|
||||
_ => Ok(_false()),
|
||||
_ => Ok(_false()),
|
||||
}
|
||||
}
|
||||
|
@ -58,6 +58,39 @@
|
||||
(unless2 true 7 8)
|
||||
;=>8
|
||||
|
||||
;; Testing or macro
|
||||
(or)
|
||||
;=>nil
|
||||
(or 1)
|
||||
;=>1
|
||||
(or 1 2 3 4)
|
||||
;=>1
|
||||
(or false 2)
|
||||
;=>2
|
||||
(or false nil 3)
|
||||
;=>3
|
||||
(or false nil false false nil 4)
|
||||
;=>4
|
||||
(or false nil 3 false nil 4)
|
||||
;=>3
|
||||
|
||||
;; Testing cond macro
|
||||
|
||||
(cond)
|
||||
;=>nil
|
||||
(cond true 7)
|
||||
;=>7
|
||||
(cond true 7 true 8)
|
||||
;=>7
|
||||
(cond false 7 true 8)
|
||||
;=>8
|
||||
(cond false 7 false 8 "else" 9)
|
||||
;=>9
|
||||
(cond false 7 (= 2 2) 8 "else" 9)
|
||||
;=>8
|
||||
(cond false 7 false 8 false 9)
|
||||
;=>nil
|
||||
|
||||
;; Testing macroexpand
|
||||
(macroexpand (unless2 2 3 4))
|
||||
;=>(if (not 2) 3 4)
|
||||
@ -82,22 +115,6 @@
|
||||
(and 1 2 3 4 false 5)
|
||||
;=>false
|
||||
|
||||
;; Testing or macro
|
||||
(or)
|
||||
;=>nil
|
||||
(or 1)
|
||||
;=>1
|
||||
(or 1 2 3 4)
|
||||
;=>1
|
||||
(or false 2)
|
||||
;=>2
|
||||
(or false nil 3)
|
||||
;=>3
|
||||
(or false nil false false nil 4)
|
||||
;=>4
|
||||
(or false nil 3 false nil 4)
|
||||
;=>3
|
||||
|
||||
;; Testing -> macro
|
||||
|
||||
(-> 7)
|
||||
@ -111,23 +128,6 @@
|
||||
(-> (list 7 8 9) rest (rest) first (+ 7))
|
||||
;=>16
|
||||
|
||||
;; Testing cond macro
|
||||
|
||||
(cond)
|
||||
;=>nil
|
||||
(cond true 7)
|
||||
;=>7
|
||||
(cond true 7 true 8)
|
||||
;=>7
|
||||
(cond false 7 true 8)
|
||||
;=>8
|
||||
(cond false 7 false 8 "else" 9)
|
||||
;=>9
|
||||
(cond false 7 (= 2 2) 8 "else" 9)
|
||||
;=>8
|
||||
(cond false 7 false 8 false 9)
|
||||
;=>nil
|
||||
|
||||
;; Testing EVAL in let*
|
||||
|
||||
(let* (x (or nil "yes")) x)
|
||||
|
@ -156,9 +156,18 @@
|
||||
(contains? hm2 "a")
|
||||
;=>true
|
||||
|
||||
|
||||
;;; TODO: fix. Clojure returns nil but this breaks mal impl
|
||||
(keys hm1)
|
||||
;=>()
|
||||
|
||||
(keys hm2)
|
||||
;=>("a")
|
||||
|
||||
;;; TODO: fix. Clojure returns nil but this breaks mal impl
|
||||
(vals hm1)
|
||||
;=>()
|
||||
|
||||
(vals hm2)
|
||||
;=>(1)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user