2023-02-17 21:00:07 -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, Type};
|
|
|
|
|
use crate::sym::{Args, SymTable, Symbol, UserFn, ValueType};
|
2023-02-17 21:00:07 -08:00
|
|
|
use std::env;
|
2023-02-27 17:30:49 -08:00
|
|
|
use std::rc::Rc;
|
2023-02-17 21:00:07 -08:00
|
|
|
|
2023-02-27 22:53:54 -08:00
|
|
|
pub mod append;
|
2023-03-02 09:43:12 -08:00
|
|
|
pub mod boolean;
|
2023-03-03 14:29:53 -08:00
|
|
|
pub mod control;
|
2023-02-27 22:53:54 -08:00
|
|
|
//pub mod str;
|
2023-02-17 21:00:07 -08:00
|
|
|
|
2023-02-27 22:53:54 -08:00
|
|
|
/// 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> {
|
2023-03-01 11:38:02 -08:00
|
|
|
syms.insert(
|
|
|
|
|
"append".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("append"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: false,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: append::APPEND_DOCSTRING.to_string(),
|
2023-03-01 11:38:02 -08:00
|
|
|
value: ValueType::Internal(Rc::new(append::append_callback)),
|
|
|
|
|
},
|
|
|
|
|
);
|
2023-02-27 22:53:54 -08:00
|
|
|
|
2023-03-01 12:27:45 -08:00
|
|
|
syms.insert(
|
|
|
|
|
"echo".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("echo"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: false,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: ECHO_DOCSTRING.to_string(),
|
2023-03-01 12:27:45 -08:00
|
|
|
value: ValueType::Internal(Rc::new(_echo_callback)),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2023-03-01 11:38:02 -08:00
|
|
|
syms.insert(
|
|
|
|
|
"if".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("if"),
|
|
|
|
|
args: Args::Lazy(3),
|
|
|
|
|
conditional_branches: true,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: control::IF_DOCSTRING.to_string(),
|
2023-03-01 11:38:02 -08:00
|
|
|
value: ValueType::Internal(Rc::new(control::if_callback)),
|
|
|
|
|
},
|
|
|
|
|
);
|
2023-02-27 17:30:49 -08:00
|
|
|
|
2023-03-01 15:17:50 -08:00
|
|
|
syms.insert(
|
|
|
|
|
"let".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("let"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: true,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: control::LET_DOCSTRING.to_string(),
|
2023-03-01 15:17:50 -08:00
|
|
|
value: ValueType::Internal(Rc::new(control::let_callback)),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2023-03-02 15:29:50 -08:00
|
|
|
syms.insert(
|
|
|
|
|
"while".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("while"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: true,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: control::WHILE_DOCSTRING.to_string(),
|
2023-03-02 15:29:50 -08:00
|
|
|
value: ValueType::Internal(Rc::new(control::while_callback)),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"circuit".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("circuit"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: true,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: control::CIRCUIT_DOCSTRING.to_string(),
|
2023-03-02 15:29:50 -08:00
|
|
|
value: ValueType::Internal(Rc::new(control::circuit_callback)),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2023-03-02 09:43:12 -08:00
|
|
|
syms.insert(
|
|
|
|
|
"and".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("and"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: false,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: boolean::AND_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(boolean::and_callback)),
|
2023-03-03 14:29:53 -08:00
|
|
|
},
|
2023-03-02 09:43:12 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"or".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("or"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: false,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: boolean::OR_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(boolean::or_callback)),
|
2023-03-03 14:29:53 -08:00
|
|
|
},
|
2023-03-02 09:43:12 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"not".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("not"),
|
|
|
|
|
args: Args::Strict(vec![Type::Bool]),
|
|
|
|
|
conditional_branches: false,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: boolean::NOT_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(boolean::not_callback)),
|
2023-03-03 14:29:53 -08:00
|
|
|
},
|
2023-03-02 09:43:12 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"eq?".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("eq?"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: false,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: boolean::ISEQ_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(boolean::iseq_callback)),
|
2023-03-03 14:29:53 -08:00
|
|
|
},
|
2023-03-02 09:43:12 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"toggle".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("toggle"),
|
|
|
|
|
args: Args::Lazy(1),
|
|
|
|
|
conditional_branches: true,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: boolean::TOGGLE_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(boolean::toggle_callback)),
|
2023-03-03 14:29:53 -08:00
|
|
|
},
|
2023-03-02 09:43:12 -08:00
|
|
|
);
|
|
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
syms.insert(
|
|
|
|
|
"help".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("help"),
|
|
|
|
|
args: Args::Strict(vec![Type::Symbol]),
|
|
|
|
|
conditional_branches: true,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: HELP_DOCSTRING.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
value: ValueType::Internal(Rc::new(_help_callback)),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2023-03-06 15:25:23 -08:00
|
|
|
syms.insert(
|
|
|
|
|
"set?".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("set?"),
|
|
|
|
|
args: Args::Strict(vec![Type::Symbol]),
|
|
|
|
|
conditional_branches: true,
|
|
|
|
|
docs: ISSET_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(_isset_callback)),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2023-02-27 17:30:49 -08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 22:53:54 -08:00
|
|
|
/// dynamic_stdlib
|
|
|
|
|
/// takes configuration data and uses it to insert dynamic
|
|
|
|
|
/// callbacks with configuration into a symtable
|
2023-03-01 11:14:42 -08:00
|
|
|
pub fn dynamic_stdlib(syms: &mut SymTable) -> Result<(), String> {
|
|
|
|
|
//get CFG_RELISH_ENV from syms
|
2023-03-01 11:38:02 -08:00
|
|
|
let env_cfg_user_form = syms
|
|
|
|
|
.call_symbol(&"CFG_RELISH_ENV".to_string(), &Seg::new(), true)
|
2023-03-01 11:14:42 -08:00
|
|
|
.unwrap_or_else(|_: String| Box::new(Ctr::None))
|
|
|
|
|
.to_string()
|
|
|
|
|
.ne("");
|
|
|
|
|
|
2023-03-01 11:38:02 -08:00
|
|
|
syms.insert(
|
|
|
|
|
"def".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("define"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: true,
|
2023-03-06 15:12:59 -08:00
|
|
|
docs: STORE_DOCSTRING.to_string(),
|
2023-03-01 11:38:02 -08:00
|
|
|
value: ValueType::Internal(Rc::new(
|
|
|
|
|
move |ast: &Seg, syms: &mut SymTable| -> Result<Ctr, String> {
|
|
|
|
|
_store_callback(ast, syms, env_cfg_user_form)
|
|
|
|
|
},
|
|
|
|
|
)),
|
2023-02-27 22:53:54 -08:00
|
|
|
},
|
2023-03-01 11:38:02 -08:00
|
|
|
);
|
2023-02-27 17:30:49 -08:00
|
|
|
|
2023-02-27 22:53:54 -08:00
|
|
|
Ok(())
|
2023-02-27 17:30:49 -08:00
|
|
|
}
|
2023-02-17 21:00:07 -08:00
|
|
|
|
2023-03-06 15:12:59 -08:00
|
|
|
pub const ECHO_DOCSTRING: &str = "traverses any number of arguments. Prints their evaluated values on a new line for each.";
|
|
|
|
|
|
2023-03-01 12:27:45 -08:00
|
|
|
fn _echo_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
|
|
|
|
if ast.len() == 1 {
|
|
|
|
|
println!("{}", ast.car);
|
|
|
|
|
} else {
|
|
|
|
|
ast.circuit(&mut |arg: &Ctr| print!("{}", arg) == ());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Ctr::None)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 15:12:59 -08:00
|
|
|
pub const HELP_DOCSTRING: &str = "prints help text for a given symbol. Expects only one argument.";
|
|
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
fn _help_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
|
|
|
|
|
if ast.len() != 1 {
|
2023-03-05 22:21:18 -08:00
|
|
|
return Err("help only takes a single argument".to_string());
|
2023-03-05 22:18:49 -08:00
|
|
|
}
|
|
|
|
|
if let Ctr::Symbol(ref symbol) = *ast.car {
|
|
|
|
|
if let Some(ref sym) = syms.get(symbol) {
|
|
|
|
|
let args_str: String;
|
|
|
|
|
if let ValueType::VarForm(_) = sym.value {
|
|
|
|
|
args_str = "(its a variable)".to_string();
|
|
|
|
|
} else {
|
|
|
|
|
args_str = sym.args.to_string();
|
|
|
|
|
}
|
2023-03-05 22:21:18 -08:00
|
|
|
println!(
|
|
|
|
|
"NAME: {0}\n
|
2023-03-05 22:18:49 -08:00
|
|
|
ARGS: {1}\n
|
|
|
|
|
DOCUMENTATION:\n
|
|
|
|
|
{2}\n
|
|
|
|
|
CURRENT VALUE AND/OR BODY:
|
2023-03-06 15:12:59 -08:00
|
|
|
{3}",
|
|
|
|
|
sym.name, args_str, sym.docs, sym.value
|
2023-03-05 22:21:18 -08:00
|
|
|
);
|
2023-03-05 22:18:49 -08:00
|
|
|
} else {
|
2023-03-05 22:21:18 -08:00
|
|
|
return Err("undefined symbol".to_string());
|
2023-03-05 22:18:49 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-03-05 22:21:18 -08:00
|
|
|
return Err("help should only be called on a symbol".to_string());
|
2023-03-05 22:18:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Ctr::None)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 15:12:59 -08:00
|
|
|
pub const STORE_DOCSTRING: &str = "allows user to define functions and variables.
|
|
|
|
|
A call may take one of three forms:
|
|
|
|
|
1. variable declaration:
|
|
|
|
|
Takes a name, doc string, and a value.
|
|
|
|
|
(def myvar 'my special variable' 'my var value')
|
|
|
|
|
2. function declaration:
|
|
|
|
|
Takes a name, doc string, list of arguments, and one or more bodies to evaluate.
|
|
|
|
|
Result of evaluating the final body is returned.
|
|
|
|
|
(def myfunc 'does a thing' (myarg1 myarg2) (dothing myarg1 myarg2) (add myarg1 myarg2))
|
|
|
|
|
3. symbol un-definition:
|
|
|
|
|
Takes just a name. Removes variable from table.
|
|
|
|
|
(def useless-var)";
|
|
|
|
|
|
2023-03-01 11:38:02 -08:00
|
|
|
fn _store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr, String> {
|
2023-03-05 22:18:49 -08:00
|
|
|
let is_var = ast.len() == 3;
|
2023-02-17 21:00:07 -08:00
|
|
|
if let Ctr::Symbol(ref identifier) = *ast.car {
|
|
|
|
|
match &*ast.cdr {
|
2023-03-05 22:18:49 -08:00
|
|
|
// define a symbol
|
|
|
|
|
Ctr::Seg(doc_tree) => {
|
2023-03-05 22:21:18 -08:00
|
|
|
if let Ctr::String(ref doc) = *doc_tree.car {
|
2023-03-05 22:18:49 -08:00
|
|
|
match &*doc_tree.cdr {
|
|
|
|
|
// define a variable
|
|
|
|
|
Ctr::Seg(data_tree) if is_var => match eval(&Box::new(data_tree), syms) {
|
|
|
|
|
Ok(seg) => {
|
|
|
|
|
if let Ctr::Seg(ref val) = *seg {
|
|
|
|
|
syms.insert(
|
|
|
|
|
identifier.clone(),
|
|
|
|
|
Symbol {
|
|
|
|
|
value: ValueType::VarForm(val.car.clone()),
|
|
|
|
|
name: identifier.clone(),
|
|
|
|
|
args: Args::None,
|
|
|
|
|
docs: doc.to_owned(),
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
if env_cfg {
|
|
|
|
|
env::set_var(identifier.clone(), val.car.to_string());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return Err("impossible args to export".to_string());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(e) => return Err(format!("couldnt eval symbol: {}", e)),
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// define a function
|
|
|
|
|
Ctr::Seg(data_tree) if !is_var => {
|
|
|
|
|
if let Ctr::Seg(ref args) = *data_tree.car {
|
|
|
|
|
let mut arg_list = vec![];
|
|
|
|
|
if !args.circuit(&mut |c: &Ctr| -> bool {
|
|
|
|
|
if let Ctr::Symbol(ref arg) = c {
|
|
|
|
|
arg_list.push(arg.clone());
|
|
|
|
|
true
|
|
|
|
|
} else if let Ctr::None = c {
|
|
|
|
|
// a user cannot type a None
|
|
|
|
|
// this case represents no args
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}) {
|
|
|
|
|
return Err(
|
2023-03-05 22:21:18 -08:00
|
|
|
"all arguments defined for function must be of type symbol"
|
|
|
|
|
.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Ctr::Seg(ref bodies) = *data_tree.cdr {
|
|
|
|
|
syms.insert(
|
|
|
|
|
identifier.clone(),
|
|
|
|
|
Symbol {
|
|
|
|
|
value: ValueType::FuncForm(UserFn {
|
|
|
|
|
ast: Box::new(bodies.clone()),
|
|
|
|
|
arg_syms: arg_list.clone(),
|
|
|
|
|
}),
|
|
|
|
|
name: identifier.clone(),
|
|
|
|
|
args: Args::Lazy(arg_list.len() as u128),
|
|
|
|
|
docs: doc.to_owned(),
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return Err(
|
|
|
|
|
"expected one or more function bodies in function definition"
|
|
|
|
|
.to_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-03-05 22:21:18 -08:00
|
|
|
return Err(
|
|
|
|
|
"expected list of arguments in function definition".to_string()
|
|
|
|
|
);
|
2023-03-05 22:18:49 -08:00
|
|
|
}
|
2023-03-05 22:21:18 -08:00
|
|
|
}
|
2023-03-05 22:18:49 -08:00
|
|
|
|
|
|
|
|
// theres a name and a doc string but nothing else
|
2023-03-05 22:21:18 -08:00
|
|
|
_ => return Err("have name and doc string, but no body.".to_string()),
|
2023-02-17 21:00:07 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-03-05 22:21:18 -08:00
|
|
|
return Err("doc string is a required argument".to_string());
|
2023-02-17 21:00:07 -08:00
|
|
|
}
|
2023-03-05 22:21:18 -08:00
|
|
|
}
|
2023-03-05 22:18:49 -08:00
|
|
|
|
|
|
|
|
// undefine a symbol
|
2023-02-17 21:00:07 -08:00
|
|
|
Ctr::None => {
|
2023-02-23 23:01:47 -08:00
|
|
|
syms.remove(&identifier.to_string());
|
2023-02-17 21:00:07 -08:00
|
|
|
if env_cfg {
|
|
|
|
|
env::remove_var(identifier);
|
|
|
|
|
}
|
2023-03-05 22:21:18 -08:00
|
|
|
}
|
2023-03-05 22:18:49 -08:00
|
|
|
|
|
|
|
|
_ => return Err("arguments not in standard form".to_string()),
|
2023-02-17 21:00:07 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-03-05 22:21:18 -08:00
|
|
|
return Err("first argument to export must be a symbol".to_string());
|
2023-02-17 21:00:07 -08:00
|
|
|
}
|
2023-02-27 22:53:54 -08:00
|
|
|
Ok(Ctr::None)
|
2023-02-17 21:00:07 -08:00
|
|
|
}
|
2023-03-06 15:25:23 -08:00
|
|
|
|
|
|
|
|
pub const ISSET_DOCSTRING: &str = "accepts a single argument: a symbol.
|
|
|
|
|
returns true or false according to whether or not the symbol is found in the symbol table.";
|
|
|
|
|
|
|
|
|
|
fn _isset_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
|
|
|
|
|
if ast.len() != 1 {
|
|
|
|
|
Err("help only takes a single argument".to_string())
|
|
|
|
|
} else {
|
|
|
|
|
if let Ctr::Symbol(ref symbol) = *ast.car {
|
|
|
|
|
if let Some(_) = syms.get(symbol) {
|
|
|
|
|
Ok(Ctr::Bool(true))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Ctr::Bool(false))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Err("help should only be called on a symbol".to_string())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|