2023-02-27 22:53:54 -08:00
|
|
|
/* relish: versatile lisp shell
|
|
|
|
|
* Copyright (C) 2021 Aidan Hahn
|
|
|
|
|
*
|
|
|
|
|
* 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 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use crate::eval::eval;
|
2023-03-01 11:38:02 -08:00
|
|
|
use crate::segment::{Ctr, Seg};
|
2023-02-27 22:53:54 -08:00
|
|
|
use crate::sym::SymTable;
|
|
|
|
|
|
2023-03-01 11:38:02 -08:00
|
|
|
pub fn if_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
|
2023-02-28 11:12:27 -08:00
|
|
|
let cond: bool;
|
|
|
|
|
match *ast.car {
|
|
|
|
|
Ctr::Seg(ref cond_form) => {
|
|
|
|
|
if let Ctr::Bool(cond_from_eval) = *eval(cond_form, syms)? {
|
|
|
|
|
cond = cond_from_eval;
|
|
|
|
|
} else {
|
2023-03-01 11:38:02 -08:00
|
|
|
return Err("first argument to if must evaluate to be a boolean".to_string());
|
2023-02-28 11:12:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ctr::Bool(cond_from_car) => cond = cond_from_car,
|
2023-03-01 11:38:02 -08:00
|
|
|
_ => return Err("first argument to if must evaluate to be a boolean".to_string()),
|
2023-02-28 11:12:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let then_form: &Seg;
|
|
|
|
|
if let Ctr::Seg(ref s) = *ast.cdr {
|
|
|
|
|
then_form = s;
|
|
|
|
|
} else {
|
2023-03-01 11:38:02 -08:00
|
|
|
return Err("impossible condition: not enough args to if".to_string());
|
2023-02-28 11:12:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cond {
|
|
|
|
|
// then
|
|
|
|
|
match *then_form.car {
|
|
|
|
|
Ctr::Seg(ref first_arg) => Ok(*eval(first_arg, syms)?),
|
|
|
|
|
_ => {
|
|
|
|
|
//Ok(*eval(&Seg::from_mono(then_form.car.clone()), syms)?)},
|
|
|
|
|
let eval_tree = &Seg::from_mono(then_form.car.clone());
|
|
|
|
|
let eval_res = *eval(eval_tree, syms)?;
|
|
|
|
|
if let Ctr::Seg(ref s) = eval_res {
|
|
|
|
|
Ok(*s.car.clone())
|
2023-02-27 22:53:54 -08:00
|
|
|
} else {
|
2023-02-28 11:12:27 -08:00
|
|
|
Err("impossible condition: list evals to non list".to_string())
|
|
|
|
|
}
|
2023-03-01 11:38:02 -08:00
|
|
|
}
|
2023-02-28 11:12:27 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// else
|
|
|
|
|
if let Ctr::Seg(ref else_form) = *then_form.cdr {
|
|
|
|
|
match *else_form.car {
|
|
|
|
|
Ctr::Seg(ref second_arg) => Ok(*eval(second_arg, syms)?),
|
|
|
|
|
_ => {
|
|
|
|
|
//Ok(*eval(&Seg::from_mono(then_form.car.clone()), syms)?)},
|
|
|
|
|
let eval_tree = &Seg::from_mono(else_form.car.clone());
|
|
|
|
|
let eval_res = *eval(eval_tree, syms)?;
|
|
|
|
|
if let Ctr::Seg(ref s) = eval_res {
|
|
|
|
|
Ok(*s.car.clone())
|
2023-02-27 22:53:54 -08:00
|
|
|
} else {
|
2023-02-28 11:12:27 -08:00
|
|
|
Err("impossible condition: list evals to non list".to_string())
|
2023-02-27 22:53:54 -08:00
|
|
|
}
|
2023-03-01 11:38:02 -08:00
|
|
|
}
|
2023-02-27 22:53:54 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-02-28 11:12:27 -08:00
|
|
|
Err("impossible condition: args not in standard form".to_string())
|
2023-02-27 22:53:54 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:38:02 -08:00
|
|
|
pub fn let_callback(_ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
2023-02-28 11:12:27 -08:00
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:38:02 -08:00
|
|
|
pub fn while_callback(_ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
2023-02-27 22:53:54 -08:00
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:38:02 -08:00
|
|
|
pub fn map_callback(_ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
2023-02-27 22:53:54 -08:00
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:38:02 -08:00
|
|
|
pub fn circuit_callback(_ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
2023-02-27 22:53:54 -08:00
|
|
|
todo!()
|
|
|
|
|
}
|