mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 23:31:12 +00:00
Extract can/ into its own crate, plus its deps
This commit is contained in:
parent
97e6affa88
commit
3b6ed43126
62 changed files with 910 additions and 418 deletions
70
compiler/can/src/num.rs
Normal file
70
compiler/can/src/num.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
use crate::env::Env;
|
||||
use crate::expr::Expr;
|
||||
use crate::problem::Problem;
|
||||
use crate::problem::RuntimeError::*;
|
||||
use roc_types::subs::VarStore;
|
||||
use roc_parse::ast::Base;
|
||||
use std::i64;
|
||||
|
||||
#[inline(always)]
|
||||
pub fn int_expr_from_result(
|
||||
var_store: &VarStore,
|
||||
result: Result<i64, &str>,
|
||||
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<f64, &str>,
|
||||
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<i64, &str> {
|
||||
// Ignore underscores.
|
||||
raw.replace("_", "").parse::<i64>().map_err(|_| raw)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn finish_parsing_base(raw: &str, base: Base) -> Result<i64, &str> {
|
||||
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<f64, &str> {
|
||||
// Ignore underscores.
|
||||
match raw.replace("_", "").parse::<f64>() {
|
||||
Ok(float) if float.is_finite() => Ok(float),
|
||||
_ => Err(raw),
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue