add more graceful errors (w/ stacktrace) to stdlib

This commit is contained in:
Mark 2024-06-20 15:52:57 +02:00
parent 9e7bbc110e
commit f59c0941f5
6 changed files with 26 additions and 24 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "mers" name = "mers"
version = "0.8.9" version = "0.8.10"
edition = "2021" edition = "2021"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
description = "dynamically typed but type-checked programming language" description = "dynamically typed but type-checked programming language"
@ -11,7 +11,7 @@ repository = "https://github.com/Dummi26/mers"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
mers_lib = "0.8.9" mers_lib = "0.8.10"
# mers_lib = { path = "../mers_lib" } # mers_lib = { path = "../mers_lib" }
clap = { version = "4.3.19", features = ["derive"] } clap = { version = "4.3.19", features = ["derive"] }
colored = "2.1.0" colored = "2.1.0"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "mers_lib" name = "mers_lib"
version = "0.8.9" version = "0.8.10"
edition = "2021" edition = "2021"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
description = "library to use the mers language in other projects" description = "library to use the mers language in other projects"

View File

@ -93,7 +93,7 @@ impl Config {
} else if let Some(data::float::Float(n)) = a.as_any().downcast_ref() { } else if let Some(data::float::Float(n)) = a.as_any().downcast_ref() {
Duration::from_secs_f64(*n) Duration::from_secs_f64(*n)
} else { } else {
unreachable!("sleep called on non-int/non-float") return Err("sleep called on non-int/non-float".into());
}); });
Ok(Data::empty_tuple()) Ok(Data::empty_tuple())
}), }),
@ -154,7 +154,7 @@ impl Config {
// -1 if more elements than isize can represent // -1 if more elements than isize can represent
i.take(isize::MAX as usize + 1).count() as isize i.take(isize::MAX as usize + 1).count() as isize
} else { } else {
unreachable!("called len on {a:?}, which isn't a tuple or a string") return Err("called len on {a:?}, which isn't a tuple or a string".into());
}))) })))
}), }),
inner_statements: None, inner_statements: None,

View File

@ -97,15 +97,15 @@ impl Config {
} }
Ok(Data::empty_tuple()) Ok(Data::empty_tuple())
} else { } else {
unreachable!( return Err(
"for_each called on tuple not containing iterable and function" "for_each called on tuple not containing iterable and function".into()
) );
} }
} else { } else {
unreachable!("for_each called on tuple with len < 2") return Err("for_each called on tuple with len < 2".into());
} }
} else { } else {
unreachable!("for_each called on non-tuple") return Err("for_each called on non-tuple".into());
} }
}), }),
inner_statements: None, inner_statements: None,
@ -223,13 +223,13 @@ fn genfunc_iter_and_arg<T: MersType, D: MersData>(
if let Some(f) = f.get().as_any().downcast_ref::<D>() { if let Some(f) = f.get().as_any().downcast_ref::<D>() {
Ok(Data::new(Iter(fd(f), v.clone()))) Ok(Data::new(Iter(fd(f), v.clone())))
} else { } else {
unreachable!("{name} called on tuple not containing function") return Err("{name} called on tuple not containing function".into());
} }
} else { } else {
unreachable!("{name} called on tuple with len < 2") return Err("{name} called on tuple with len < 2".into());
} }
} else { } else {
unreachable!("{name} called on non-tuple") return Err("{name} called on non-tuple".into());
} }
}), }),
inner_statements: None, inner_statements: None,

View File

@ -123,7 +123,7 @@ impl Config {
-1 -1
} else { 0 } else { 0
} }
} else { unreachable!("called signum on non-number type")}))) } else { return Err("called signum on non-number type".into()); })))
}), }),
inner_statements: None, inner_statements: None,
})) .add_var("div".to_string(), Data::new(data::function::Function { })) .add_var("div".to_string(), Data::new(data::function::Function {
@ -141,9 +141,9 @@ impl Config {
(Some(data::int::Int(l)), None, None, Some(data::float::Float(r))) => Ok(Data::new(data::float::Float(*l as f64 / r))), (Some(data::int::Int(l)), None, None, Some(data::float::Float(r))) => Ok(Data::new(data::float::Float(*l as f64 / r))),
(None, Some(data::float::Float(l)), Some(data::int::Int(r)), None) => Ok(Data::new(data::float::Float(l / *r as f64))), (None, Some(data::float::Float(l)), Some(data::int::Int(r)), None) => Ok(Data::new(data::float::Float(l / *r as f64))),
(None, Some(data::float::Float(l)), None, Some(data::float::Float(r))) => Ok(Data::new(data::float::Float(l / r))), (None, Some(data::float::Float(l)), None, Some(data::float::Float(r))) => Ok(Data::new(data::float::Float(l / r))),
_ => unreachable!(), _ => return Err("at least one of the arguments to div were neither an int nor a float".into()),
} }
} else { unreachable!() }), } else { return Err("argument to div was not a tuple".into()); }),
inner_statements: None, inner_statements: None,
})).add_var("modulo".to_string(), Data::new(data::function::Function { })).add_var("modulo".to_string(), Data::new(data::function::Function {
info: Arc::new(program::run::Info::neverused()), info: Arc::new(program::run::Info::neverused()),
@ -160,9 +160,9 @@ impl Config {
(Some(data::int::Int(l)), None, None, Some(data::float::Float(r))) => Ok(Data::new(data::float::Float(*l as f64 % r))), (Some(data::int::Int(l)), None, None, Some(data::float::Float(r))) => Ok(Data::new(data::float::Float(*l as f64 % r))),
(None, Some(data::float::Float(l)), Some(data::int::Int(r)), None) => Ok(Data::new(data::float::Float(l % *r as f64))), (None, Some(data::float::Float(l)), Some(data::int::Int(r)), None) => Ok(Data::new(data::float::Float(l % *r as f64))),
(None, Some(data::float::Float(l)), None, Some(data::float::Float(r))) => Ok(Data::new(data::float::Float(l % r))), (None, Some(data::float::Float(l)), None, Some(data::float::Float(r))) => Ok(Data::new(data::float::Float(l % r))),
_ => unreachable!(), _ => return Err("at least one of the arguments to modulo were neither an int nor a float".into()),
} }
} else { unreachable!() }), } else { return Err("argument to modulo was not a tuple".into()) }),
inner_statements: None, inner_statements: None,
})) }))
.add_var( .add_var(
@ -223,7 +223,7 @@ impl Config {
Data::new(data::int::Int(sumi)) Data::new(data::int::Int(sumi))
}) })
} else { } else {
unreachable!("sum called on non-tuple") return Err("sum called on non-tuple".into());
} }
}), }),
inner_statements: None, inner_statements: None,
@ -298,7 +298,7 @@ impl Config {
Data::new(data::int::Int(sumi)) Data::new(data::int::Int(sumi))
}) })
} else { } else {
unreachable!("sum called on non-tuple") return Err("sum called on non-tuple".into());
} }
}), }),
inner_statements: None, inner_statements: None,
@ -362,7 +362,7 @@ impl Config {
Data::new(data::int::Int(prodi)) Data::new(data::int::Int(prodi))
}) })
} else { } else {
unreachable!("product called on non-tuple") return Err("product called on non-tuple".into());
} }
}), }),
inner_statements: None, inner_statements: None,
@ -436,7 +436,9 @@ fn ltgtoe_function(
} else if let Some(data::float::Float(v)) = item.as_any().downcast_ref() { } else if let Some(data::float::Float(v)) = item.as_any().downcast_ref() {
IntOrFloatOrNothing::Float(*v) IntOrFloatOrNothing::Float(*v)
} else { } else {
unreachable!() return Err(
"one of the (l/g)t[oe] function argument iterator elements were neither int nor float".into(),
);
}; };
if op(prev, new) { if op(prev, new) {
prev = new; prev = new;

View File

@ -57,7 +57,7 @@ impl Config {
move || f.run(Data::empty_tuple()), move || f.run(Data::empty_tuple()),
))))))) )))))))
} else { } else {
unreachable!("thread called, but arg wasn't a function"); return Err("thread called, but arg wasn't a function".into());
} }
}), }),
inner_statements: None, inner_statements: None,