mirror of
https://github.com/teo3300/rust-mal.git
synced 2026-01-12 01:05:32 +01:00
Map/Vector applicaility
- Maps and Vectors can be applied as functions, using keys and int as their arguments - Added Identity function - Test before install - added conditional mapping
This commit is contained in:
2
Makefile
2
Makefile
@ -12,6 +12,6 @@ conf:
|
||||
mkdir -p ${HOME}/.config/mal/libs
|
||||
cp -f libs/* ${HOME}/.config/mal/libs/
|
||||
|
||||
install: build-release conf
|
||||
install: build-release test conf
|
||||
sudo cp target/release/rust-mal /usr/local/bin/mal
|
||||
sudo chown ${USER} /usr/local/bin/mal
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
;; Previously in core.rs
|
||||
; Logic
|
||||
|
||||
; Identity function
|
||||
(def! I (fn* [x] x))
|
||||
|
||||
(def! not (fn* [x]
|
||||
(if x nil true)))
|
||||
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
(def! map-nil (fn* [v l]
|
||||
"Map nil values of l to the specified value"
|
||||
(map-if v nil l)))
|
||||
|
||||
(def! concat (fn* [x y] (def! concat-r (fn* [x y t]
|
||||
"Concatenate arguments, keeping their order"
|
||||
(if (car x)
|
||||
|
||||
26
src/env.rs
26
src/env.rs
@ -110,6 +110,32 @@ pub fn call_func(func: &MalType, args: &[MalType]) -> CallRet {
|
||||
_ => scream!(),
|
||||
}
|
||||
}
|
||||
M::Map(m) => {
|
||||
if args.is_empty() {
|
||||
return Err(MalErr::unrecoverable("No key provided to Map construct"));
|
||||
}
|
||||
match &args[0] {
|
||||
M::Str(s) | M::Key(s) => {
|
||||
Ok(CallFunc::Builtin(m.get(s).unwrap_or_default().clone()))
|
||||
}
|
||||
_ => Err(MalErr::unrecoverable("Map argument must be string or key")),
|
||||
}
|
||||
}
|
||||
M::Vector(v) => {
|
||||
if args.is_empty() {
|
||||
return Err(MalErr::unrecoverable("No key provided to Vector construct"));
|
||||
}
|
||||
match &args[0] {
|
||||
M::Int(i) => {
|
||||
if { 0..v.len() as isize }.contains(i) {
|
||||
Ok(CallFunc::Builtin(v[*i as usize].clone()))
|
||||
} else {
|
||||
Ok(CallFunc::Builtin(M::Nil))
|
||||
}
|
||||
}
|
||||
_ => Err(MalErr::unrecoverable("Map argument must be string or key")),
|
||||
}
|
||||
}
|
||||
_ => Err(MalErr::unrecoverable(
|
||||
format!("{:?} is not a function", prt(func)).as_str(),
|
||||
)),
|
||||
|
||||
Reference in New Issue
Block a user