opportunistic move
should be way faster now
This commit is contained in:
@@ -6,48 +6,38 @@ use crate::foreign::ExternError;
|
||||
use crate::interner::Interner;
|
||||
use crate::interpreted::Clause;
|
||||
use crate::parse::{float_parser, int_parser};
|
||||
use crate::systems::cast_exprinst::with_lit;
|
||||
use crate::systems::cast_exprinst::get_literal;
|
||||
use crate::systems::AssertionError;
|
||||
use crate::{define_fn, ConstTree, Literal};
|
||||
|
||||
define_fn! {
|
||||
/// parse a number. Accepts the same syntax Orchid does.
|
||||
ToFloat = |x| with_lit(x, |l| match l {
|
||||
Literal::Str(s) => float_parser()
|
||||
ToFloat = |x| match get_literal(x)? {
|
||||
(Literal::Str(s), loc) => float_parser()
|
||||
.parse(s.as_str())
|
||||
.map_err(|_| AssertionError::ext(
|
||||
x.clone(),
|
||||
"cannot be parsed into a float"
|
||||
)),
|
||||
Literal::Num(n) => Ok(*n),
|
||||
Literal::Uint(i) => NotNan::new(*i as f64)
|
||||
.map_err(|_| AssertionError::ext(loc, "float syntax")),
|
||||
(Literal::Num(n), _) => Ok(n),
|
||||
(Literal::Uint(i), _) => NotNan::new(i as f64)
|
||||
.map_err(|_| ArithmeticError::NaN.into_extern()),
|
||||
}).map(|nn| Literal::Num(nn).into())
|
||||
}
|
||||
}.map(|nn| Literal::Num(nn).into());
|
||||
|
||||
define_fn! {
|
||||
/// Parse an unsigned integer. Accepts the same formats Orchid does. If the
|
||||
/// input is a number, floors it.
|
||||
ToUint = |x| with_lit(x, |l| match l {
|
||||
Literal::Str(s) => int_parser()
|
||||
ToUint = |x| match get_literal(x)? {
|
||||
(Literal::Str(s), loc) => int_parser()
|
||||
.parse(s.as_str())
|
||||
.map_err(|_| AssertionError::ext(
|
||||
x.clone(),
|
||||
"cannot be parsed into an unsigned int",
|
||||
)),
|
||||
Literal::Num(n) => Ok(n.floor() as u64),
|
||||
Literal::Uint(i) => Ok(*i),
|
||||
}).map(|u| Literal::Uint(u).into())
|
||||
}
|
||||
.map_err(|_| AssertionError::ext(loc, "int syntax")),
|
||||
(Literal::Num(n), _) => Ok(n.floor() as u64),
|
||||
(Literal::Uint(i), _) => Ok(i),
|
||||
}.map(|u| Literal::Uint(u).into());
|
||||
|
||||
define_fn! {
|
||||
/// Convert a literal to a string using Rust's conversions for floats, chars and
|
||||
/// uints respectively
|
||||
ToString = |x| with_lit(x, |l| Ok(match l {
|
||||
Literal::Uint(i) => Literal::Str(i.to_string().into()),
|
||||
Literal::Num(n) => Literal::Str(n.to_string().into()),
|
||||
s@Literal::Str(_) => s.clone(),
|
||||
})).map(Clause::from)
|
||||
ToString = |x| Ok(match get_literal(x)?.0 {
|
||||
Literal::Uint(i) => Clause::from(Literal::Str(i.to_string().into())),
|
||||
Literal::Num(n) => Clause::from(Literal::Str(n.to_string().into())),
|
||||
s@Literal::Str(_) => Clause::from(s),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn conv(i: &Interner) -> ConstTree {
|
||||
|
||||
Reference in New Issue
Block a user