use crate::can::env::Env; use crate::can::expr::Expr; use crate::can::problem::Problem; use crate::can::problem::RuntimeError::*; use crate::parse::ast::Base; use crate::subs::VarStore; use std::i64; #[inline(always)] pub fn int_expr_from_result( var_store: &VarStore, result: Result, env: &mut Env, ) -> Expr { match result { Ok(int) => Expr::Int(var_store.fresh(), int), Err(raw) => { let runtime_error = IntOutsideRange(raw.into()); env.problem(Problem::RuntimeError(runtime_error.clone())); Expr::RuntimeError(runtime_error) } } } #[inline(always)] pub fn float_expr_from_result( var_store: &VarStore, result: Result, env: &mut Env, ) -> Expr { match result { Ok(float) => Expr::Float(var_store.fresh(), float), Err(raw) => { let runtime_error = FloatOutsideRange(raw.into()); env.problem(Problem::RuntimeError(runtime_error.clone())); Expr::RuntimeError(runtime_error) } } } #[inline(always)] pub fn finish_parsing_int(raw: &str) -> Result { // Ignore underscores. raw.replace("_", "").parse::().map_err(|_| raw) } #[inline(always)] pub fn finish_parsing_base(raw: &str, base: Base) -> Result { let radix = match base { Base::Hex => 16, Base::Octal => 8, Base::Binary => 2, }; // Ignore underscores. i64::from_str_radix(raw.replace("_", "").as_str(), radix).map_err(|_| raw) } #[inline(always)] pub fn finish_parsing_float(raw: &str) -> Result { // Ignore underscores. match raw.replace("_", "").parse::() { Ok(float) if float.is_finite() => Ok(float), _ => Err(raw), } }