Error Messaging Redesign

This commit contains the following:

* New data types to support full tracebacks
* New traceback data type used across stl and ast
* Updates to tests
* fixes for error messaging in sym and some stl functions
This commit is contained in:
Ava Apples Affine 2023-05-23 22:06:11 +00:00
parent 91ad4eed12
commit 789349df48
24 changed files with 837 additions and 374 deletions

View file

@ -16,6 +16,7 @@
*/
use crate::eval::eval;
use crate::error::{Traceback, start_trace};
use crate::segment::{Ctr, Seg, Type};
use crate::stdlib::{CONSOLE_XDIM_VNAME, RELISH_DEFAULT_CONS_WIDTH};
use crate::sym::{SymTable, Symbol, UserFn, ValueType};
@ -23,9 +24,9 @@ use std::env;
pub const QUOTE_DOCSTRING: &str = "takes a single unevaluated tree and returns it as it is: unevaluated.";
pub fn quote_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
pub fn quote_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
if ast.len() > 1 {
Err("do not quote more than one thing at a time".to_string())
Err(start_trace(("quote", "do not quote more than one thing at a time").into()))
} else {
Ok(*ast.car.clone())
}
@ -36,18 +37,39 @@ Specifically, does one pass of the tree simplification algorithm.
If you have a variable referencing another variable you will get the
referenced variable.";
pub fn eval_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
pub fn eval_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
if ast.len() > 1 {
Err("do not eval more than one thing at a time".to_string())
Err(start_trace(
("eval", "do not eval more than one thing at a time")
.into()))
} else {
match *ast.car {
Ctr::Seg(ref s) => Ok(*eval(s, syms)?.clone()),
Ctr::Seg(ref s) => {
match eval(s, syms) {
Err(e) => Err(e.with_trace(
("eval", "evaluation failure")
.into())),
Ok(s) => Ok(*s.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())
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()),
}
} else {
Ok(*intermediate)
Ok(res)
}
},
_ => Ok(*ast.car.clone())
@ -76,9 +98,9 @@ pub fn eval_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
pub const HELP_DOCSTRING: &str = "prints help text for a given symbol. Expects only one argument.";
pub fn help_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
pub fn help_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
if ast.len() != 1 {
return Err("help only takes a single argument".to_string());
return Err(start_trace(("help", "expected one input").into()));
}
if let Ctr::Symbol(ref symbol) = *ast.car {
if let Some(ref sym) = syms.get(symbol) {
@ -98,10 +120,10 @@ CURRENT VALUE AND/OR BODY:
sym.name, args_str, sym.docs, sym.value
);
} else {
return Err("undefined symbol".to_string());
return Err(start_trace(("help", format!("{symbol} is undefined")).into()));
}
} else {
return Err("help should only be called on a symbol".to_string());
return Err(start_trace(("help", "expected input to be a symbol").into()));
}
Ok(Ctr::None)
@ -110,9 +132,9 @@ CURRENT VALUE AND/OR BODY:
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.";
pub fn isset_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
pub fn isset_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
if ast.len() != 1 {
Err("help only takes a single argument".to_string())
Err(start_trace(("set?", "expcted one input").into()))
} else {
if let Ctr::Symbol(ref symbol) = *ast.car {
if let Some(_) = syms.get(symbol) {
@ -121,7 +143,7 @@ pub fn isset_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
Ok(Ctr::Bool(false))
}
} else {
Err("help should only be called on a symbol".to_string())
Err(start_trace(("set?", "expected argument to be a input").into()))
}
}
}
@ -129,12 +151,12 @@ pub fn isset_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
pub const ENV_DOCSTRING: &str = "takes no arguments
prints out all available symbols and their associated values";
pub fn env_callback(_ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
pub fn env_callback(_ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
// get width of current output
let xdim: i128;
if let Ctr::Integer(dim) = *syms
.call_symbol(&CONSOLE_XDIM_VNAME.to_string(), &Seg::new(), true)
.unwrap_or_else(|_: String| Box::new(Ctr::None)) {
.unwrap_or_else(|_: Traceback| Box::new(Ctr::None)) {
xdim = dim;
} else {
println!("{} contains non integer value, defaulting to {}",
@ -218,7 +240,7 @@ which is functionally equivalent to:
pub fn lambda_callback(
ast: &Seg,
_syms: &mut SymTable
) -> Result<Ctr, String> {
) -> Result<Ctr, Traceback> {
let mut args = vec![];
if let Ctr::Seg(ref arg_head) = *ast.car {
if !arg_head.circuit(&mut |arg: &Ctr| -> bool {
@ -232,7 +254,7 @@ pub fn lambda_callback(
false
}
}) {
Err("all elements of first argumnets must be symbols".to_string())
Err(start_trace(("lambda", "lambda inputs should all be symbols").into()))
} else {
if let Ctr::Seg(ref eval_head) = *ast.cdr {
if let Ctr::Seg(_) = *eval_head.car {
@ -241,14 +263,14 @@ pub fn lambda_callback(
arg_syms: args,
}))
} else {
Err("function body must be in list form".to_string())
Err(start_trace(("lambda", "expected list of forms for lambda body").into()))
}
} else {
Err("not enough args".to_string())
Err(start_trace(("lambda", "not enough args").into()))
}
}
} else {
Err("first argument should be a list of symbols".to_string())
Err(start_trace(("lambda", "expected list of lambda inputs").into()))
}
}
@ -258,15 +280,15 @@ Returns an error if symbol is undefined.
Note: make sure to quote the input like this:
(get-doc (quote symbol-name))";
pub fn getdoc_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
pub fn getdoc_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
if let Ctr::Symbol(ref symbol) = *ast.car {
if let Some(sym) = syms.get(symbol) {
Ok(Ctr::String(sym.docs.clone()))
} else {
Err("undefined symbol".to_string())
Err(start_trace(("get-doc", "input is undefined").into()))
}
} else {
Err("get-doc should only be called on a symbol".to_string())
Err(start_trace(("get-doc", "expected input to be a symbol").into()))
}
}
@ -276,9 +298,11 @@ Returns an error if symbol is undefined, otherwise sets the symbols docstring to
Note: make sure to quote the input like this:
(set-doc (quote symbol-name) my-new-docs)";
pub fn setdoc_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
pub fn setdoc_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
if ast.len() != 2 {
Err("set-doc only takes two arguments".to_string())
Err(start_trace(
("set-doc", "expected two inputs")
.into()))
} else {
if let Ctr::Symbol(ref symbol) = *ast.car {
if let Some(mut sym) = syms.remove(symbol) {
@ -289,17 +313,25 @@ pub fn setdoc_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
Ok(Ctr::None)
} else {
syms.insert(sym.name.clone(), sym);
Err("second arg must be a string".to_string())
Err(start_trace(
("set-doc", "expected second input to be a string")
.into()))
}
} else {
Err("impossible: not a second arg".to_string())
Err(start_trace(
("set-doc", "missing second input somehow")
.into()))
}
} else {
Err("undefined symbol".to_string())
Err(start_trace(
("set-doc", format!("{symbol} is undefined"))
.into()))
}
} else {
Err("first argument must be a symbol".to_string())
Err(start_trace(
("set-doc", "first input must be a symbol")
.into()))
}
}
}
@ -321,7 +353,7 @@ Additionally, passing a tree as a name will trigger def to evaluate the tree and
a value from it. If it does not return a ";
pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr, String> {
pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr, Traceback> {
let is_var = ast.len() == 3;
let name: String;
let docs: String;
@ -329,15 +361,21 @@ pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<C
match *ast.car {
Ctr::String(ref s) => name = s.clone(),
Ctr::Symbol(ref s) => name = s.clone(),
Ctr::Seg(ref s) => match *eval(s, syms)? {
Ctr::String(ref s) => name = s.clone(),
Ctr::Symbol(ref s) => name = s.clone(),
_ => {
println!("{}", *eval(s, syms)?);
return Err("evaluated symbol name doesnt make sense".to_string());
},
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()));
},
}
},
_ => return Err("symbol name doesnt make sense".to_string()),
_ => return Err(start_trace(
("def", "expected a string or a symbol as input for symbol name")
.into()))
}
// remove var case
@ -350,7 +388,7 @@ pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<C
return Ok(Ctr::None)
} else {
if ast.len() < 3 || ast.len() > 4 {
return Err("expected 3 or 4 args".to_string())
return Err(start_trace(("def", "expected 3 or 4 inputs").into()))
}
}
@ -358,25 +396,31 @@ pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<C
if let Ctr::Seg(ref s) = *ast.cdr {
iter = s;
} else {
return Err("not enough args".to_string())
return Err(start_trace(("def", "not enough inputs").into()))
}
match *iter.car {
Ctr::String(ref s) => docs = s.clone(),
Ctr::Symbol(ref s) => {
if let Ctr::String(doc) = *syms.call_symbol(&s, &Seg::new(), true)? {
Ctr::Symbol(ref s) => match syms.call_symbol(s, &Seg::new(), true) {
Ok(d) => if let Ctr::String(doc) = *d {
docs = doc.clone();
} else {
return Err("docs argument does not evaluate to a string".to_string())
}
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()))
},
_ => return Err("docs argument does not evaluate to a string".to_string())
_ => return Err(start_trace(
("def", "expected docs input to at least evaluate to a string if not be one")
.into()))
}
if let Ctr::Seg(ref s) = *iter.cdr {
iter = s;
} else {
return Err("not enough args".to_string())
return Err(start_trace(("def", "not enough inputs").into()))
}
let mut outer_scope_val: Seg = Seg::new();
@ -392,17 +436,23 @@ pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<C
outer_scope_val = Seg::from_mono(Box::new(*iter.car.clone()));
var_val_form = &outer_scope_val;
},
_ if !is_var => return Err("arg list must at least be a list".to_string()),
_ if !is_var => return Err(start_trace(("def", "expected a list of inputs").into())),
_ => unimplemented!(), // rustc is haunted and cursed
}
if is_var {
let var_val: Ctr;
let var_eval_result = *eval(var_val_form, syms)?;
match var_eval_result {
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 {
Ctr::Seg(ref s) if expand => var_val = *s.car.clone(),
Ctr::Seg(ref s) if !expand => var_val = Ctr::Seg(s.clone()),
_ => var_val = var_eval_result,
_ => var_val = var_eval_final,
}
let outer_seg = Seg::from_mono(Box::new(var_val.clone()));
@ -438,7 +488,9 @@ pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<C
false
}
}) {
return Err("all arguments defined for function must be of type symbol".to_string())
return Err(start_trace(
("def", "all inputs to function must be of type symbol")
.into()))
}
if let Ctr::Seg(ref eval_bodies) = *iter.cdr {
@ -452,6 +504,8 @@ pub fn store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<C
);
Ok(Ctr::None)
} else {
Err("expected one or more bodies to evaluate in function".to_string())
Err(start_trace(
("def", "expected one or more forms to evaluate in function body")
.into()))
}
}