/* 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 .
*/
use crate::segment::{Ctr, Seg, Type};
use crate::sym::{Args, SymTable, Symbol, ValueType};
use std::rc::Rc;
pub mod append;
pub mod boolean;
pub mod control;
pub mod decl;
pub mod math;
//pub mod str;
/// static_stdlib
/// inserts all stdlib functions that can be inserted without
/// any kind of further configuration data into a symtable
pub fn static_stdlib(syms: &mut SymTable) -> Result<(), String> {
syms.insert(
"append".to_string(),
Symbol {
name: String::from("append"),
args: Args::Infinite,
conditional_branches: false,
docs: append::APPEND_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(append::append_callback)),
},
);
syms.insert(
"echo".to_string(),
Symbol {
name: String::from("echo"),
args: Args::Infinite,
conditional_branches: false,
docs: ECHO_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(_echo_callback)),
},
);
syms.insert(
"if".to_string(),
Symbol {
name: String::from("if"),
args: Args::Lazy(3),
conditional_branches: true,
docs: control::IF_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(control::if_callback)),
},
);
syms.insert(
"let".to_string(),
Symbol {
name: String::from("let"),
args: Args::Infinite,
conditional_branches: true,
docs: control::LET_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(control::let_callback)),
},
);
syms.insert(
"while".to_string(),
Symbol {
name: String::from("while"),
args: Args::Infinite,
conditional_branches: true,
docs: control::WHILE_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(control::while_callback)),
},
);
syms.insert(
"circuit".to_string(),
Symbol {
name: String::from("circuit"),
args: Args::Infinite,
conditional_branches: true,
docs: control::CIRCUIT_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(control::circuit_callback)),
},
);
syms.insert(
"and".to_string(),
Symbol {
name: String::from("and"),
args: Args::Infinite,
conditional_branches: false,
docs: boolean::AND_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(boolean::and_callback)),
},
);
syms.insert(
"or".to_string(),
Symbol {
name: String::from("or"),
args: Args::Infinite,
conditional_branches: false,
docs: boolean::OR_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(boolean::or_callback)),
},
);
syms.insert(
"not".to_string(),
Symbol {
name: String::from("not"),
args: Args::Strict(vec![Type::Bool]),
conditional_branches: false,
docs: boolean::NOT_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(boolean::not_callback)),
},
);
syms.insert(
"eq?".to_string(),
Symbol {
name: String::from("eq?"),
args: Args::Infinite,
conditional_branches: false,
docs: boolean::ISEQ_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(boolean::iseq_callback)),
},
);
syms.insert(
"toggle".to_string(),
Symbol {
name: String::from("toggle"),
args: Args::Lazy(1),
conditional_branches: true,
docs: boolean::TOGGLE_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(boolean::toggle_callback)),
},
);
syms.insert(
"help".to_string(),
Symbol {
name: String::from("help"),
args: Args::Strict(vec![Type::Symbol]),
conditional_branches: true,
docs: decl::HELP_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(decl::help_callback)),
},
);
syms.insert(
"set?".to_string(),
Symbol {
name: String::from("set?"),
args: Args::Strict(vec![Type::Symbol]),
conditional_branches: true,
docs: decl::ISSET_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(decl::isset_callback)),
},
);
syms.insert(
"env".to_string(),
Symbol {
name: String::from("env"),
args: Args::None,
conditional_branches: false,
docs: decl::ENV_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(decl::env_callback)),
},
);
syms.insert(
"add".to_string(),
Symbol {
name: String::from("add"),
args: Args::Infinite,
conditional_branches: false,
docs: math::ADD_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::add_callback)),
},
);
syms.insert(
"sub".to_string(),
Symbol {
name: String::from("sub"),
args: Args::Infinite,
conditional_branches: false,
docs: math::SUB_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::sub_callback)),
},
);
syms.insert(
"div".to_string(),
Symbol {
name: String::from("div"),
args: Args::Lazy(2),
conditional_branches: false,
docs: math::DIV_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::div_callback)),
},
);
syms.insert(
"mul".to_string(),
Symbol {
name: String::from("mul"),
args: Args::Infinite,
conditional_branches: false,
docs: math::MUL_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::mul_callback)),
},
);
syms.insert(
"int".to_string(),
Symbol {
name: String::from("int"),
args: Args::Lazy(1),
conditional_branches: false,
docs: math::INTCAST_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::intcast_callback)),
},
);
syms.insert(
"float".to_string(),
Symbol {
name: String::from("float"),
args: Args::Lazy(1),
conditional_branches: false,
docs: math::FLOATCAST_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::floatcast_callback)),
},
);
syms.insert(
"len".to_string(),
Symbol {
name: String::from("len"),
args: Args::Strict(vec![Type::Seg]),
conditional_branches: false,
docs: append::LEN_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(append::len_callback)),
},
);
syms.insert(
"car".to_string(),
Symbol {
name: String::from("car"),
args: Args::Strict(vec![Type::Seg]),
conditional_branches: false,
docs: append::CAR_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(append::car_callback)),
},
);
syms.insert(
"cdr".to_string(),
Symbol {
name: String::from("cdr"),
args: Args::Strict(vec![Type::Seg]),
conditional_branches: false,
docs: append::CDR_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(append::cdr_callback)),
},
);
syms.insert(
"exp".to_string(),
Symbol {
name: String::from("exp"),
args: Args::Lazy(2),
conditional_branches: false,
docs: math::EXP_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::exp_callback)),
},
);
syms.insert(
"mod".to_string(),
Symbol {
name: String::from("mod"),
args: Args::Lazy(2),
conditional_branches: false,
docs: math::MOD_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::mod_callback)),
},
);
syms.insert(
"gt?".to_string(),
Symbol {
name: String::from("gt?"),
args: Args::Lazy(2),
conditional_branches: false,
docs: math::ISGT_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::isgt_callback)),
},
);
syms.insert(
"lt?".to_string(),
Symbol {
name: String::from("lt?"),
args: Args::Lazy(2),
conditional_branches: false,
docs: math::ISLT_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::islt_callback)),
},
);
syms.insert(
"gte?".to_string(),
Symbol {
name: String::from("gt?"),
args: Args::Lazy(2),
conditional_branches: false,
docs: math::ISGTE_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::isgte_callback)),
},
);
syms.insert(
"lte?".to_string(),
Symbol {
name: String::from("lt?"),
args: Args::Lazy(2),
conditional_branches: false,
docs: math::ISLTE_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(math::islte_callback)),
},
);
Ok(())
}
/// dynamic_stdlib
/// takes configuration data and uses it to insert dynamic
/// callbacks with configuration into a symtable
pub fn dynamic_stdlib(syms: &mut SymTable) -> Result<(), String> {
//get CFG_RELISH_ENV from syms
let env_cfg_user_form = syms
.call_symbol(&"CFG_RELISH_ENV".to_string(), &Seg::new(), true)
.unwrap_or_else(|_: String| Box::new(Ctr::None))
.to_string()
.ne("");
syms.insert(
"def".to_string(),
Symbol {
name: String::from("define"),
args: Args::Infinite,
conditional_branches: true,
docs: decl::STORE_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(
move |ast: &Seg, syms: &mut SymTable| -> Result {
decl::store_callback(ast, syms, env_cfg_user_form)
},
)),
},
);
Ok(())
}
pub const ECHO_DOCSTRING: &str =
"traverses any number of arguments. Prints their evaluated values on a new line for each.";
fn _echo_callback(ast: &Seg, _syms: &mut SymTable) -> Result {
if ast.len() == 1 {
println!("{}", ast.car);
} else {
ast.circuit(&mut |arg: &Ctr| print!("{}", arg) == ());
}
Ok(Ctr::None)
}