2023-03-06 15:29:01 -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-05-23 22:06:11 +00:00
|
|
|
use crate::error::{Traceback, start_trace};
|
2023-05-21 23:53:00 +00:00
|
|
|
use crate::segment::{Ctr, Seg, Type};
|
|
|
|
|
use crate::stdlib::{CONSOLE_XDIM_VNAME, RELISH_DEFAULT_CONS_WIDTH};
|
2023-05-25 23:08:44 +00:00
|
|
|
use crate::sym::{SymTable, Symbol, UserFn, ValueType, Args};
|
2023-03-06 15:29:01 -08:00
|
|
|
use std::env;
|
2023-05-25 23:08:44 +00:00
|
|
|
use std::rc::Rc;
|
2023-03-06 15:29:01 -08:00
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const QUOTE_DOCSTRING: &str = "takes a single unevaluated tree and returns it as it is: unevaluated.";
|
|
|
|
|
fn quote_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
2023-03-10 16:54:22 -08:00
|
|
|
if ast.len() > 1 {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(("quote", "do not quote more than one thing at a time").into()))
|
2023-03-10 16:54:22 -08:00
|
|
|
} else {
|
2023-03-11 22:04:46 -08:00
|
|
|
Ok(*ast.car.clone())
|
2023-03-10 16:54:22 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const EVAL_DOCSTRING: &str = "takes an unevaluated argument and evaluates it.
|
2023-03-11 22:04:46 -08:00
|
|
|
Specifically, does one pass of the tree simplification algorithm.
|
|
|
|
|
If you have a variable referencing another variable you will get the
|
|
|
|
|
referenced variable.";
|
2023-05-25 23:08:44 +00:00
|
|
|
fn eval_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
2023-03-10 16:54:22 -08:00
|
|
|
if ast.len() > 1 {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(
|
|
|
|
|
("eval", "do not eval more than one thing at a time")
|
|
|
|
|
.into()))
|
2023-03-10 16:54:22 -08:00
|
|
|
} else {
|
2023-03-11 22:04:46 -08:00
|
|
|
match *ast.car {
|
2023-05-23 22:06:11 +00:00
|
|
|
Ctr::Seg(ref s) => {
|
|
|
|
|
match eval(s, syms) {
|
|
|
|
|
Err(e) => Err(e.with_trace(
|
|
|
|
|
("eval", "evaluation failure")
|
|
|
|
|
.into())),
|
|
|
|
|
Ok(s) => Ok(*s.clone()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-17 11:42:36 -07:00
|
|
|
Ctr::Symbol(ref sym) => {
|
2023-05-23 22:06:11 +00:00
|
|
|
let intermediate = syms.call_symbol(sym, &Seg::new(), true);
|
|
|
|
|
if let Err(e) = intermediate {
|
|
|
|
|
return Err(e.with_trace(
|
|
|
|
|
("eval", "evaluation failure")
|
|
|
|
|
.into()))
|
|
|
|
|
}
|
|
|
|
|
let res = *intermediate?;
|
|
|
|
|
if let Ctr::Seg(ref s) = res {
|
|
|
|
|
match eval(s, syms) {
|
|
|
|
|
Err(e) => Err(e.with_trace(
|
|
|
|
|
("eval", "evaluation failure")
|
|
|
|
|
.into())),
|
|
|
|
|
Ok(s) => Ok(*s.clone()),
|
|
|
|
|
}
|
2023-03-17 11:42:36 -07:00
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
Ok(res)
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
},
|
2023-03-17 12:21:42 -07:00
|
|
|
_ => Ok(*ast.car.clone())
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
2023-03-17 12:21:42 -07:00
|
|
|
|
|
|
|
|
/* this bit removed because it was determined eval shouldnt do things twice
|
|
|
|
|
* kept here for reference purposes since I have gone back and forth on this
|
|
|
|
|
* a bit
|
|
|
|
|
*
|
2023-05-03 15:04:54 -07:00
|
|
|
* thank you for your patience (ava)
|
2023-03-17 12:21:42 -07:00
|
|
|
|
2023-03-17 11:42:36 -07:00
|
|
|
match arguments {
|
2023-03-11 22:04:46 -08:00
|
|
|
Ctr::Seg(ref s) => Ok(*eval(s, syms)?.clone()),
|
|
|
|
|
Ctr::Symbol(ref sym) => {
|
|
|
|
|
let intermediate = syms.call_symbol(sym, &Seg::new(), true)?;
|
|
|
|
|
if let Ctr::Seg(ref s) = *intermediate {
|
|
|
|
|
Ok(*eval(s, syms)?.clone())
|
|
|
|
|
} else {
|
|
|
|
|
Ok(*intermediate)
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-03-17 11:42:36 -07:00
|
|
|
_ => Ok(arguments)
|
2023-05-03 15:04:54 -07:00
|
|
|
}*/
|
2023-03-10 16:54:22 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const HELP_DOCSTRING: &str = "prints help text for a given symbol. Expects only one argument.";
|
|
|
|
|
fn help_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
2023-03-06 15:29:01 -08:00
|
|
|
if ast.len() != 1 {
|
2023-05-23 22:06:11 +00:00
|
|
|
return Err(start_trace(("help", "expected one input").into()));
|
2023-03-06 15:29:01 -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();
|
|
|
|
|
}
|
|
|
|
|
println!(
|
|
|
|
|
"NAME: {0}\n
|
|
|
|
|
ARGS: {1}\n
|
|
|
|
|
DOCUMENTATION:\n
|
|
|
|
|
{2}\n
|
|
|
|
|
CURRENT VALUE AND/OR BODY:
|
|
|
|
|
{3}",
|
|
|
|
|
sym.name, args_str, sym.docs, sym.value
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
return Err(start_trace(("help", format!("{symbol} is undefined")).into()));
|
2023-03-06 15:29:01 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
return Err(start_trace(("help", "expected input to be a symbol").into()));
|
2023-03-06 15:29:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Ctr::None)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const ISSET_DOCSTRING: &str = "accepts a single argument: a symbol.
|
2023-03-06 15:29:01 -08:00
|
|
|
returns true or false according to whether or not the symbol is found in the symbol table.";
|
2023-05-25 23:08:44 +00:00
|
|
|
fn isset_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
2023-03-06 15:29:01 -08:00
|
|
|
if ast.len() != 1 {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(("set?", "expcted one input").into()))
|
2023-03-06 15:29:01 -08:00
|
|
|
} 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 {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(("set?", "expected argument to be a input").into()))
|
2023-03-06 15:29:01 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-06 15:50:02 -08:00
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const ENV_DOCSTRING: &str = "takes no arguments
|
2023-03-06 15:50:02 -08:00
|
|
|
prints out all available symbols and their associated values";
|
2023-05-25 23:08:44 +00:00
|
|
|
fn env_callback(_ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
2023-05-21 23:53:00 +00:00
|
|
|
// get width of current output
|
|
|
|
|
let xdim: i128;
|
|
|
|
|
if let Ctr::Integer(dim) = *syms
|
|
|
|
|
.call_symbol(&CONSOLE_XDIM_VNAME.to_string(), &Seg::new(), true)
|
2023-05-23 22:06:11 +00:00
|
|
|
.unwrap_or_else(|_: Traceback| Box::new(Ctr::None)) {
|
2023-05-21 23:53:00 +00:00
|
|
|
xdim = dim;
|
|
|
|
|
} else {
|
|
|
|
|
println!("{} contains non integer value, defaulting to {}",
|
|
|
|
|
CONSOLE_XDIM_VNAME, RELISH_DEFAULT_CONS_WIDTH);
|
|
|
|
|
xdim = RELISH_DEFAULT_CONS_WIDTH as i128;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut v_col_len = 0;
|
|
|
|
|
let mut f_col_len = 0;
|
2023-03-06 15:50:02 -08:00
|
|
|
let mut functions = vec![];
|
2023-05-21 23:53:00 +00:00
|
|
|
let mut variables = vec![];
|
2023-03-06 15:50:02 -08:00
|
|
|
for (name, val) in syms.iter() {
|
2023-05-21 23:53:00 +00:00
|
|
|
if let ValueType::VarForm(l) = &val.value {
|
|
|
|
|
let token: String;
|
|
|
|
|
match l.to_type() {
|
|
|
|
|
Type::Lambda => token = format!("{}: <lambda>", name),
|
|
|
|
|
Type::Seg => token = format!("{}: <form>", name),
|
|
|
|
|
_ => token = format!("{}: {}", name, val.value.to_string()),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if token.len() > v_col_len && token.len() < xdim as usize {
|
|
|
|
|
v_col_len = token.len();
|
2023-03-06 15:52:09 -08:00
|
|
|
}
|
2023-05-21 23:53:00 +00:00
|
|
|
|
|
|
|
|
variables.push(token);
|
|
|
|
|
} else {
|
|
|
|
|
if f_col_len < name.len() && name.len() < xdim as usize {
|
|
|
|
|
f_col_len = name.len();
|
|
|
|
|
}
|
|
|
|
|
functions.push(name.clone());
|
2023-03-06 15:50:02 -08:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-21 23:53:00 +00:00
|
|
|
|
|
|
|
|
let mut n_v_cols = xdim / v_col_len as i128;
|
|
|
|
|
// now decrement to make sure theres room for two spaces of padding
|
|
|
|
|
while n_v_cols > 1 && xdim % (v_col_len as i128) < (2 * n_v_cols) {
|
|
|
|
|
n_v_cols -= 1;
|
|
|
|
|
}
|
|
|
|
|
// again for functions
|
|
|
|
|
let mut n_f_cols = xdim / f_col_len as i128;
|
|
|
|
|
while n_f_cols > 1 && xdim & (f_col_len as i128) < (2 * n_f_cols) {
|
|
|
|
|
n_f_cols -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut col_iter = 0;
|
|
|
|
|
println!("VARIABLES:");
|
|
|
|
|
for var in variables {
|
|
|
|
|
print!("{:v_col_len$}", var);
|
|
|
|
|
col_iter += 1;
|
|
|
|
|
if col_iter % n_v_cols == 0 {
|
|
|
|
|
print!("\n");
|
|
|
|
|
} else {
|
|
|
|
|
print!(" ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
println!("\nFUNCTIONS:");
|
|
|
|
|
col_iter = 0;
|
2023-03-06 15:50:02 -08:00
|
|
|
for func in functions {
|
2023-05-21 23:53:00 +00:00
|
|
|
print!("{:f_col_len$}", func);
|
|
|
|
|
col_iter += 1;
|
|
|
|
|
if col_iter % n_f_cols == 0 {
|
|
|
|
|
print!("\n");
|
|
|
|
|
} else {
|
|
|
|
|
print!(" ");
|
|
|
|
|
}
|
2023-03-06 15:50:02 -08:00
|
|
|
}
|
|
|
|
|
Ok(Ctr::None)
|
|
|
|
|
}
|
2023-03-12 20:29:39 -07:00
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const LAMBDA_DOCSTRING: &str = "Takes two arguments of any type.
|
2023-03-12 20:29:39 -07:00
|
|
|
No args are evaluated when lambda is called.
|
|
|
|
|
Lambda makes sure the first argument is a list of symbols (or 'arguments') to the lambda function.
|
|
|
|
|
The next arg is stored in a tree to evaluate on demand.
|
|
|
|
|
|
|
|
|
|
Example: (lambda (x y) (add x y))
|
|
|
|
|
This can then be evaluated like so:
|
|
|
|
|
((lambda (x y) (add x y)) 1 2)
|
|
|
|
|
which is functionally equivalent to:
|
|
|
|
|
(add 1 2)";
|
2023-05-25 23:08:44 +00:00
|
|
|
fn lambda_callback(
|
2023-03-12 20:29:39 -07:00
|
|
|
ast: &Seg,
|
|
|
|
|
_syms: &mut SymTable
|
2023-05-23 22:06:11 +00:00
|
|
|
) -> Result<Ctr, Traceback> {
|
2023-03-12 20:29:39 -07:00
|
|
|
let mut args = vec![];
|
|
|
|
|
if let Ctr::Seg(ref arg_head) = *ast.car {
|
|
|
|
|
if !arg_head.circuit(&mut |arg: &Ctr| -> bool {
|
|
|
|
|
if let Ctr::Symbol(ref s) = *arg {
|
|
|
|
|
args.push(s.clone());
|
|
|
|
|
true
|
2023-03-13 15:02:19 -07:00
|
|
|
} else if let Ctr::None = *arg {
|
|
|
|
|
// no args case
|
|
|
|
|
true
|
2023-03-12 20:29:39 -07:00
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}) {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(("lambda", "lambda inputs should all be symbols").into()))
|
2023-03-12 20:29:39 -07:00
|
|
|
} else {
|
|
|
|
|
if let Ctr::Seg(ref eval_head) = *ast.cdr {
|
2023-03-13 15:02:19 -07:00
|
|
|
if let Ctr::Seg(_) = *eval_head.car {
|
|
|
|
|
Ok(Ctr::Lambda(UserFn{
|
|
|
|
|
ast: Box::new(eval_head.clone()),
|
|
|
|
|
arg_syms: args,
|
|
|
|
|
}))
|
|
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(("lambda", "expected list of forms for lambda body").into()))
|
2023-03-13 15:02:19 -07:00
|
|
|
}
|
2023-03-12 20:29:39 -07:00
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(("lambda", "not enough args").into()))
|
2023-03-12 20:29:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(("lambda", "expected list of lambda inputs").into()))
|
2023-03-12 20:29:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-17 11:42:36 -07:00
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const GETDOC_DOCSTRING: &str = "accepts an unevaluated symbol, returns the doc string.
|
2023-03-17 13:06:27 -07:00
|
|
|
Returns an error if symbol is undefined.
|
2023-03-17 11:42:36 -07:00
|
|
|
|
2023-03-17 13:06:27 -07:00
|
|
|
Note: make sure to quote the input like this:
|
|
|
|
|
(get-doc (quote symbol-name))";
|
2023-05-25 23:08:44 +00:00
|
|
|
fn getdoc_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
2023-03-17 13:06:27 -07:00
|
|
|
if let Ctr::Symbol(ref symbol) = *ast.car {
|
|
|
|
|
if let Some(sym) = syms.get(symbol) {
|
|
|
|
|
Ok(Ctr::String(sym.docs.clone()))
|
2023-03-17 11:42:36 -07:00
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(("get-doc", "input is undefined").into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
2023-03-17 13:06:27 -07:00
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(("get-doc", "expected input to be a symbol").into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const SETDOC_DOCSTRING: &str = "accepts a symbol and a doc string.
|
2023-03-17 13:06:27 -07:00
|
|
|
Returns an error if symbol is undefined, otherwise sets the symbols docstring to the argument.
|
|
|
|
|
|
|
|
|
|
Note: make sure to quote the input like this:
|
|
|
|
|
(set-doc (quote symbol-name) my-new-docs)";
|
2023-05-25 23:08:44 +00:00
|
|
|
fn setdoc_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
2023-03-17 11:42:36 -07:00
|
|
|
if ast.len() != 2 {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(
|
|
|
|
|
("set-doc", "expected two inputs")
|
|
|
|
|
.into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
} else {
|
|
|
|
|
if let Ctr::Symbol(ref symbol) = *ast.car {
|
|
|
|
|
if let Some(mut sym) = syms.remove(symbol) {
|
|
|
|
|
if let Ctr::Seg(ref doc_node) = *ast.cdr {
|
|
|
|
|
if let Ctr::String(ref doc) = *doc_node.car {
|
|
|
|
|
sym.docs = doc.clone();
|
|
|
|
|
syms.insert(sym.name.clone(), sym);
|
|
|
|
|
Ok(Ctr::None)
|
|
|
|
|
} else {
|
|
|
|
|
syms.insert(sym.name.clone(), sym);
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(
|
|
|
|
|
("set-doc", "expected second input to be a string")
|
|
|
|
|
.into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(
|
|
|
|
|
("set-doc", "missing second input somehow")
|
|
|
|
|
.into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(
|
|
|
|
|
("set-doc", format!("{symbol} is undefined"))
|
|
|
|
|
.into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(
|
|
|
|
|
("set-doc", "first input must be a symbol")
|
|
|
|
|
.into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const STORE_DOCSTRING: &str = "allows user to define functions and variables.
|
2023-03-17 11:42:36 -07:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
Additionally, passing a tree as a name will trigger def to evaluate the tree and try to derive
|
|
|
|
|
a value from it. If it does not return a ";
|
2023-05-25 23:08:44 +00:00
|
|
|
fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr, Traceback> {
|
2023-03-17 11:42:36 -07:00
|
|
|
let is_var = ast.len() == 3;
|
|
|
|
|
let name: String;
|
|
|
|
|
let docs: String;
|
|
|
|
|
|
|
|
|
|
match *ast.car {
|
|
|
|
|
Ctr::String(ref s) => name = s.clone(),
|
|
|
|
|
Ctr::Symbol(ref s) => name = s.clone(),
|
2023-05-23 22:06:11 +00:00
|
|
|
Ctr::Seg(ref s) => match eval(s, syms) {
|
|
|
|
|
Err(e) => return Err(e.with_trace(("def", "failed to evaluate symbol name").into())),
|
|
|
|
|
Ok(s) => match *s {
|
|
|
|
|
Ctr::String(ref s) => name = s.clone(),
|
|
|
|
|
Ctr::Symbol(ref s) => name = s.clone(),
|
|
|
|
|
_ => {
|
|
|
|
|
return Err(start_trace(
|
|
|
|
|
("def", "expected symbol name input to evaluate to a symbol or a string")
|
|
|
|
|
.into()));
|
|
|
|
|
},
|
|
|
|
|
}
|
2023-03-17 11:42:36 -07:00
|
|
|
},
|
2023-05-23 22:06:11 +00:00
|
|
|
_ => return Err(start_trace(
|
|
|
|
|
("def", "expected a string or a symbol as input for symbol name")
|
|
|
|
|
.into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// remove var case
|
|
|
|
|
if ast.len() == 1 {
|
|
|
|
|
syms.remove(&name);
|
|
|
|
|
if env_cfg {
|
|
|
|
|
env::remove_var(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok(Ctr::None)
|
|
|
|
|
} else {
|
|
|
|
|
if ast.len() < 3 || ast.len() > 4 {
|
2023-05-23 22:06:11 +00:00
|
|
|
return Err(start_trace(("def", "expected 3 or 4 inputs").into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut iter: &Seg;
|
|
|
|
|
if let Ctr::Seg(ref s) = *ast.cdr {
|
|
|
|
|
iter = s;
|
|
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
return Err(start_trace(("def", "not enough inputs").into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match *iter.car {
|
|
|
|
|
Ctr::String(ref s) => docs = s.clone(),
|
2023-05-23 22:06:11 +00:00
|
|
|
Ctr::Symbol(ref s) => match syms.call_symbol(s, &Seg::new(), true) {
|
|
|
|
|
Ok(d) => if let Ctr::String(doc) = *d {
|
2023-03-17 11:42:36 -07:00
|
|
|
docs = doc.clone();
|
|
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
return Err(start_trace(("def", "expected docs input to evaluate to a string").into()))
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
Err(e) => return Err(e.with_trace(
|
|
|
|
|
("def", "couldnt evaluate docs form")
|
|
|
|
|
.into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
},
|
2023-05-23 22:06:11 +00:00
|
|
|
_ => return Err(start_trace(
|
|
|
|
|
("def", "expected docs input to at least evaluate to a string if not be one")
|
|
|
|
|
.into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Ctr::Seg(ref s) = *iter.cdr {
|
|
|
|
|
iter = s;
|
|
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
return Err(start_trace(("def", "not enough inputs").into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut outer_scope_val: Seg = Seg::new();
|
|
|
|
|
let noseg = Seg::new(); // similarly, rust shouldnt need this either
|
|
|
|
|
let mut args = &noseg;
|
|
|
|
|
let mut var_val_form: &Seg = &outer_scope_val;
|
|
|
|
|
let mut expand = false;
|
|
|
|
|
match *iter.car {
|
|
|
|
|
Ctr::Seg(ref s) if !is_var => args = s,
|
|
|
|
|
Ctr::Seg(ref s) if is_var => var_val_form = s,
|
|
|
|
|
_ if is_var => {
|
|
|
|
|
expand = true;
|
|
|
|
|
outer_scope_val = Seg::from_mono(Box::new(*iter.car.clone()));
|
|
|
|
|
var_val_form = &outer_scope_val;
|
|
|
|
|
},
|
2023-05-23 22:06:11 +00:00
|
|
|
_ if !is_var => return Err(start_trace(("def", "expected a list of inputs").into())),
|
2023-05-21 23:53:00 +00:00
|
|
|
_ => unimplemented!(), // rustc is haunted and cursed
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if is_var {
|
|
|
|
|
let var_val: Ctr;
|
2023-05-23 22:06:11 +00:00
|
|
|
let var_eval_result = eval(var_val_form, syms);
|
|
|
|
|
if let Err(e) = var_eval_result {
|
|
|
|
|
return Err(e.with_trace(
|
|
|
|
|
("def", format!("couldnt evaluate {var_val_form}"))
|
|
|
|
|
.into()))
|
|
|
|
|
}
|
|
|
|
|
let var_eval_final = *var_eval_result?;
|
|
|
|
|
match var_eval_final {
|
2023-03-17 11:42:36 -07:00
|
|
|
Ctr::Seg(ref s) if expand => var_val = *s.car.clone(),
|
|
|
|
|
Ctr::Seg(ref s) if !expand => var_val = Ctr::Seg(s.clone()),
|
2023-05-23 22:06:11 +00:00
|
|
|
_ => var_val = var_eval_final,
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let outer_seg = Seg::from_mono(Box::new(var_val.clone()));
|
|
|
|
|
syms.insert(
|
|
|
|
|
name.clone(),
|
|
|
|
|
Symbol::from_ast(&name, &docs, &outer_seg, None),
|
|
|
|
|
);
|
|
|
|
|
if env_cfg {
|
2023-05-21 23:53:00 +00:00
|
|
|
match var_val.to_type() {
|
|
|
|
|
Type::Lambda => {},
|
|
|
|
|
Type::Seg => {},
|
|
|
|
|
_ => {
|
|
|
|
|
let mut s = var_val.to_string();
|
|
|
|
|
if let Ctr::String(tok) = var_val {
|
|
|
|
|
s = tok;
|
|
|
|
|
}
|
|
|
|
|
env::set_var(name.clone(), s);
|
|
|
|
|
}
|
2023-05-03 15:04:54 -07:00
|
|
|
}
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
return Ok(Ctr::None)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut arg_list = vec![];
|
|
|
|
|
if !args.circuit(&mut |c: &Ctr| -> bool {
|
|
|
|
|
if let Ctr::Symbol(s) = c {
|
|
|
|
|
arg_list.push(s.clone());
|
|
|
|
|
true
|
|
|
|
|
} else if let Ctr::None = c {
|
|
|
|
|
// no args case
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}) {
|
2023-05-23 22:06:11 +00:00
|
|
|
return Err(start_trace(
|
|
|
|
|
("def", "all inputs to function must be of type symbol")
|
|
|
|
|
.into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Ctr::Seg(ref eval_bodies) = *iter.cdr {
|
|
|
|
|
syms.insert(
|
|
|
|
|
name.clone(),
|
|
|
|
|
Symbol::from_ast(
|
|
|
|
|
&name, &docs,
|
|
|
|
|
eval_bodies,
|
|
|
|
|
Some(arg_list),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
Ok(Ctr::None)
|
|
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(
|
|
|
|
|
("def", "expected one or more forms to evaluate in function body")
|
|
|
|
|
.into()))
|
2023-03-17 11:42:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-25 23:08:44 +00:00
|
|
|
|
|
|
|
|
pub fn add_decl_lib_static(syms: &mut SymTable) {
|
|
|
|
|
syms.insert(
|
|
|
|
|
"help".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("help"),
|
|
|
|
|
args: Args::Strict(vec![Type::Symbol]),
|
|
|
|
|
conditional_branches: true,
|
|
|
|
|
docs: HELP_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(help_callback)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"env".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("env"),
|
|
|
|
|
args: Args::None,
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
docs: ENV_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(env_callback)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"quote".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("quote"),
|
|
|
|
|
args: Args::Lazy(1),
|
|
|
|
|
conditional_branches: true,
|
|
|
|
|
docs: QUOTE_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(quote_callback)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"q".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("quote"),
|
|
|
|
|
args: Args::Lazy(1),
|
|
|
|
|
conditional_branches: true,
|
|
|
|
|
docs: QUOTE_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(quote_callback)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"eval".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("eval"),
|
|
|
|
|
args: Args::Lazy(1),
|
|
|
|
|
conditional_branches: true,
|
|
|
|
|
docs: EVAL_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(eval_callback)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"lambda".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("lambda"),
|
|
|
|
|
args: Args::Lazy(2),
|
|
|
|
|
conditional_branches: true,
|
|
|
|
|
docs: LAMBDA_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(lambda_callback)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"get-doc".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("get-doc"),
|
|
|
|
|
args: Args::Strict(vec![Type::Symbol]),
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
docs: GETDOC_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(getdoc_callback)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"set-doc".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("get-doc"),
|
|
|
|
|
args: Args::Strict(vec![Type::Symbol, Type::String]),
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
docs: SETDOC_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(setdoc_callback)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn add_decl_lib_dynamic(syms: &mut SymTable, env: bool) {
|
|
|
|
|
syms.insert(
|
|
|
|
|
"def".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("define"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: true,
|
|
|
|
|
docs: STORE_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(
|
|
|
|
|
move |ast: &Seg, syms: &mut SymTable| -> Result<Ctr, Traceback> {
|
|
|
|
|
store_callback(ast, syms, env)
|
|
|
|
|
},
|
|
|
|
|
)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|