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

@ -17,13 +17,14 @@
use crate::segment::{Ctr, Seg};
use crate::sym::SymTable;
use crate::error::{Traceback, start_trace};
use std::io::Write;
use std::io;
pub const ECHO_DOCSTRING: &str =
"traverses any number of arguments. Prints their evaluated values on a new line for each.";
pub fn echo_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
pub fn echo_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
ast.circuit(&mut |arg: &Ctr| match arg {
Ctr::String(s) => print!("{}", s) == (),
_ => print!("{}", arg) == (),
@ -36,7 +37,7 @@ pub const CONCAT_DOCSTRING: &str = "Iterates over N args of any type other than
converts each argument to a string. Combines all strings.
Returns final (combined) string.";
pub fn concat_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
pub fn concat_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
let mut string = String::from("");
if !ast.circuit(&mut |arg: &Ctr| {
match arg {
@ -52,7 +53,9 @@ pub fn concat_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
}
true
}) {
eprintln!("dont know what to do witha symbol here")
return Err(start_trace(
("concat", "highly suspicious that an input was an unevaluated symbol")
.into()))
}
return Ok(Ctr::String(string));
}
@ -61,7 +64,7 @@ pub const STRLEN_DOCSTRING: &str = "Takes a single arg of any type.
Arg is converted to a string if not already a string.
Returns string length of arg.";
pub fn strlen_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
pub fn strlen_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
match &*ast.car {
Ctr::Symbol(s) => Ok(Ctr::Integer(s.len() as i128)),
Ctr::String(s) => Ok(Ctr::Integer(s.len() as i128)),
@ -78,7 +81,7 @@ pub fn strlen_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
pub const STRCAST_DOCSTRING: &str = "Takes a single arg of any type.
Arg is converted to a string and returned.";
pub fn strcast_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
pub fn strcast_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
match &*ast.car {
Ctr::Symbol(s) => Ok(Ctr::String(s.clone())),
Ctr::String(_) => Ok(*ast.car.clone()),
@ -95,12 +98,14 @@ pub fn strcast_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String>
pub const SUBSTR_DOCSTRING: &str =
"Takes two strings. Returns true if string1 contains at least one instance of string2";
pub fn substr_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
pub fn substr_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
let parent_str: String;
if let Ctr::String(ref s) = *ast.car {
parent_str = s.to_string();
} else {
return Err("first argument must be a string".to_string());
return Err(start_trace(
("substr", "expected first input to be a string")
.into()))
}
let second_arg_obj: &Ctr;
@ -108,13 +113,17 @@ pub fn substr_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
if let Ctr::Seg(ref s) = *ast.cdr {
second_arg_obj = &*s.car;
} else {
return Err("impossible error: needs two arguments".to_string());
return Err(start_trace(
("substr", "expected two inputs")
.into()))
}
if let Ctr::String(ref s) = &*second_arg_obj {
child_str = s.clone();
} else {
return Err("second argument must be a string".to_string());
return Err(start_trace(
("substr", "expected second input to be a string")
.into()))
}
Ok(Ctr::Bool(parent_str.contains(&child_str)))
@ -123,12 +132,14 @@ pub fn substr_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
pub const SPLIT_DOCSTRING: &str = "Takes two strings. String 1 is a source string and string 2 is a delimiter.
Returns a list of substrings from string 1 that were found delimited by string 2.";
pub fn split_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
pub fn split_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
let parent_str: String;
if let Ctr::String(ref s) = *ast.car {
parent_str = s.to_string();
} else {
return Err("first argument must be a string".to_string());
return Err(start_trace(
("split", "expected first input to be a string")
.into()))
}
let second_arg_obj: &Ctr;
@ -136,13 +147,17 @@ pub fn split_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
if let Ctr::Seg(ref s) = *ast.cdr {
second_arg_obj = &*s.car;
} else {
return Err("impossible error: needs two arguments".to_string());
return Err(start_trace(
("split", "expected two inputs")
.into()))
}
if let Ctr::String(ref s) = &*second_arg_obj {
delim_str = s.clone();
} else {
return Err("second argument must be a string".to_string());
return Err(start_trace(
("split", "expected second input to be a string")
.into()))
}
let mut ret = Seg::new();
@ -157,7 +172,7 @@ pub const INPUT_DOCSTRING: &str = "Takes one argument (string) and prints it.
Then prompts for user input.
User input is returned as a string";
pub fn input_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
pub fn input_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
if let Ctr::String(ref s) = *ast.car {
print!("{}", s);
let _= io::stdout().flush();
@ -165,6 +180,8 @@ pub fn input_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
io::stdin().read_line(&mut input).expect("couldnt read user input");
Ok(Ctr::String(input.trim().to_string()))
} else {
Err("impossible: arg not string".to_string())
return Err(start_trace(
("input", "expected a string input to prompt user with")
.into()))
}
}