mirror of
https://github.com/teo3300/rust-mal.git
synced 2026-01-12 01:05:32 +01:00
Making all parameters optionals
If a parameter is not provided, it's set to nil
This commit is contained in:
@ -52,6 +52,7 @@
|
||||
|
||||
(def! join (fn* [l s]
|
||||
"Join element of list l to a stiring, using s as separator"
|
||||
(def! s (or s ""))
|
||||
(def! join-r (fn* [l t]
|
||||
(if (empty? l)
|
||||
t
|
||||
@ -61,8 +62,10 @@
|
||||
(def! chsub (fn* [s c1 c2]
|
||||
(strc (map-if (fn* [x] (= x c1)) (fn* [x] c2) (boom s)))))
|
||||
|
||||
(def! parse-csv (fn* [filename]
|
||||
(map (fn* [x] (split x ",")) (filter (fn* [x] (not (= x "")))(split (slurp filename) "\n")))))
|
||||
(def! parse-csv (fn* [filename row-s col-s]
|
||||
(def! col-s (or col-s "\n"))
|
||||
(def! row-s (or row-s ","))
|
||||
(map (fn* [x] (split x row-s)) (filter (fn* [x] (not (= x "")))(split (slurp filename) col-s)))))
|
||||
|
||||
(def! _toint {"0" 0 "1" 1
|
||||
"2" 2 "3" 3
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use std::io::{self, BufRead, Write};
|
||||
use std::{cell::RefCell, env, rc::Rc};
|
||||
|
||||
use crate::env::{
|
||||
@ -67,7 +68,7 @@ pub fn ns_init() -> Env {
|
||||
">=" => Fun(|a| comparison_op( |a, b| a >= b, a), "Returns true if the first argument is greater than or equal to the second one, nil otherwise"),
|
||||
"pr-str" => Fun(|a| Ok(Str(a.iter().map(|i| pr_str(i, true)).collect::<Vec<String>>().join("").into())), "Print readably all arguments"),
|
||||
"str" => Fun(|a| Ok(Str(a.iter().map(|i| pr_str(i, false)).collect::<Vec<String>>().join("").into())), "Print non readably all arguments"),
|
||||
"prn" => Fun(|a| {a.iter().for_each(|a| print!("{}", pr_str(a, false))); Ok(Nil) }, "Print readably all the arguments"),
|
||||
"prn" => Fun(|a| {a.iter().for_each(|a| print!("{}", pr_str(a, false))); let _ = io::stdout().flush(); Ok(Nil) }, "Print readably all the arguments"),
|
||||
"println" => Fun(|a| {a.iter().for_each(|a| print!("{}", pr_str(a, false))); println!(); Ok(Nil) }, "Print readably all the arguments"),
|
||||
"list" => Fun(|a| Ok(List(a.into())), "Return the arguments as a list"),
|
||||
"type" => Fun(|a| Ok(car(a)?.label_type()), "Returns a label indicating the type of it's argument"),
|
||||
@ -78,6 +79,7 @@ pub fn ns_init() -> Env {
|
||||
// A tribute to PHP's explode (PHP, a language I never used)
|
||||
"boom" => Fun(mal_boom, "Split a string into a list of characters\n; BE CAREFUL WHEN USING"),
|
||||
"read-string" => Fun(|a| read_str(Reader::new().push(car(a)?.if_string()?)).map_err(MalErr::severe), "Tokenize and read the first argument"),
|
||||
"read-line" => Fun(|_| Ok(Str(io::stdin().lock().lines().next().unwrap().unwrap().into())), "Read a line from input and return its content"),
|
||||
"slurp" => Fun(|a| Ok(Str(read_file(car(a)?.if_string()?)?)), "Read a file and return the content as a string"),
|
||||
"atom" => Fun(|a| Ok(Atom(Rc::new(RefCell::new(car(a).unwrap_or_default().clone())))), "Return an atom pointing to the given arg"),
|
||||
"deref" => Fun(|a| if_atom!(car(a)?), "Return the content of the atom argumet"),
|
||||
|
||||
12
src/env.rs
12
src/env.rs
@ -57,15 +57,21 @@ pub fn env_get(env: &Env, sym: &str) -> MalRet {
|
||||
pub fn env_binds(outer: Env, binds: &MalType, exprs: &[MalType]) -> Result<Env, MalErr> {
|
||||
let env = env_new(Some(outer));
|
||||
let binds = binds.if_vec()?;
|
||||
if binds.len() != exprs.len() {
|
||||
let binl = binds.len();
|
||||
let expl = exprs.len();
|
||||
if binl < expl {
|
||||
return Err(MalErr::unrecoverable(
|
||||
format!("Expected {} args, got {}", binds.len(), exprs.len()).as_str(),
|
||||
format!("Expected {} args, got {}", binl, expl).as_str(),
|
||||
));
|
||||
} // TODO: May be possible to leave this be and not set additional elements at all
|
||||
}
|
||||
for (bind, expr) in binds.iter().zip(exprs.iter()) {
|
||||
let bind = bind.if_symbol()?;
|
||||
env_set(&env, bind, expr);
|
||||
}
|
||||
// All arguments are optional, if an argument is not specified, set it to nil
|
||||
for bind in binds.iter().take(binl).skip(expl) {
|
||||
env_set(&env, bind.if_symbol()?, &M::Nil);
|
||||
}
|
||||
Ok(env)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user