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:
teo3300
2024-06-22 20:23:24 +09:00
parent 5db1cf1910
commit 5996af1aea
4 changed files with 35 additions and 1 deletions

View File

@ -12,6 +12,6 @@ conf:
mkdir -p ${HOME}/.config/mal/libs mkdir -p ${HOME}/.config/mal/libs
cp -f libs/* ${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 cp target/release/rust-mal /usr/local/bin/mal
sudo chown ${USER} /usr/local/bin/mal sudo chown ${USER} /usr/local/bin/mal

View File

@ -1,5 +1,9 @@
;; Previously in core.rs ;; Previously in core.rs
; Logic ; Logic
; Identity function
(def! I (fn* [x] x))
(def! not (fn* [x] (def! not (fn* [x]
(if x nil true))) (if x nil true)))

View File

@ -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] (def! concat (fn* [x y] (def! concat-r (fn* [x y t]
"Concatenate arguments, keeping their order" "Concatenate arguments, keeping their order"
(if (car x) (if (car x)

View File

@ -110,6 +110,32 @@ pub fn call_func(func: &MalType, args: &[MalType]) -> CallRet {
_ => scream!(), _ => 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( _ => Err(MalErr::unrecoverable(
format!("{:?} is not a function", prt(func)).as_str(), format!("{:?} is not a function", prt(func)).as_str(),
)), )),