mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 16:21:11 +00:00
Merge pull request #3081 from rtfeldman/issue-2845-dec-literal
Issue 2845 dec literal
This commit is contained in:
commit
222967d749
6 changed files with 37 additions and 21 deletions
|
@ -77,7 +77,10 @@ pub fn expr_to_expr2<'a>(
|
|||
}
|
||||
Num(string) => {
|
||||
match finish_parsing_num(string) {
|
||||
Ok(ParsedNumResult::UnknownNum(int, _) | ParsedNumResult::Int(int, _)) => {
|
||||
Ok((
|
||||
parsed,
|
||||
ParsedNumResult::UnknownNum(int, _) | ParsedNumResult::Int(int, _),
|
||||
)) => {
|
||||
let expr = Expr2::SmallInt {
|
||||
number: IntVal::I64(match int {
|
||||
IntValue::U128(_) => todo!(),
|
||||
|
@ -86,16 +89,16 @@ pub fn expr_to_expr2<'a>(
|
|||
var: env.var_store.fresh(),
|
||||
// TODO non-hardcode
|
||||
style: IntStyle::Decimal,
|
||||
text: PoolStr::new(string, env.pool),
|
||||
text: PoolStr::new(parsed, env.pool),
|
||||
};
|
||||
|
||||
(expr, Output::default())
|
||||
}
|
||||
Ok(ParsedNumResult::Float(float, _)) => {
|
||||
Ok((parsed, ParsedNumResult::Float(float, _))) => {
|
||||
let expr = Expr2::Float {
|
||||
number: FloatVal::F64(float),
|
||||
var: env.var_store.fresh(),
|
||||
text: PoolStr::new(string, env.pool),
|
||||
text: PoolStr::new(parsed, env.pool),
|
||||
};
|
||||
|
||||
(expr, Output::default())
|
||||
|
|
|
@ -193,7 +193,7 @@ pub fn to_pattern2<'a>(
|
|||
let problem = MalformedPatternProblem::MalformedInt;
|
||||
malformed_pattern(env, problem, region)
|
||||
}
|
||||
Ok(ParsedNumResult::UnknownNum(int, _bound)) => {
|
||||
Ok((_, ParsedNumResult::UnknownNum(int, _bound))) => {
|
||||
Pattern2::NumLiteral(
|
||||
env.var_store.fresh(),
|
||||
match int {
|
||||
|
@ -202,13 +202,13 @@ pub fn to_pattern2<'a>(
|
|||
},
|
||||
)
|
||||
}
|
||||
Ok(ParsedNumResult::Int(int, _bound)) => {
|
||||
Ok((_, ParsedNumResult::Int(int, _bound))) => {
|
||||
Pattern2::IntLiteral(IntVal::I64(match int {
|
||||
IntValue::U128(_) => todo!(),
|
||||
IntValue::I128(n) => n as i64, // FIXME
|
||||
}))
|
||||
}
|
||||
Ok(ParsedNumResult::Float(int, _bound)) => {
|
||||
Ok((_, ParsedNumResult::Float(int, _bound))) => {
|
||||
Pattern2::FloatLiteral(FloatVal::F64(int))
|
||||
}
|
||||
},
|
||||
|
|
|
@ -422,12 +422,7 @@ pub fn canonicalize_expr<'a>(
|
|||
|
||||
let (expr, output) = match expr {
|
||||
&ast::Expr::Num(str) => {
|
||||
let answer = num_expr_from_result(
|
||||
var_store,
|
||||
finish_parsing_num(str).map(|result| (str, result)),
|
||||
region,
|
||||
env,
|
||||
);
|
||||
let answer = num_expr_from_result(var_store, finish_parsing_num(str), region, env);
|
||||
|
||||
(answer, Output::default())
|
||||
}
|
||||
|
|
|
@ -107,10 +107,14 @@ pub enum ParsedNumResult {
|
|||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn finish_parsing_num(raw: &str) -> Result<ParsedNumResult, (&str, IntErrorKind)> {
|
||||
pub fn finish_parsing_num(raw: &str) -> Result<(&str, ParsedNumResult), (&str, IntErrorKind)> {
|
||||
// Ignore underscores.
|
||||
let radix = 10;
|
||||
from_str_radix(raw.replace('_', "").as_str(), radix).map_err(|e| (raw, e))
|
||||
let (_, raw_without_suffix) = parse_literal_suffix(raw);
|
||||
match from_str_radix(raw.replace('_', "").as_str(), radix) {
|
||||
Ok(result) => Ok((raw_without_suffix, result)),
|
||||
Err(e) => Err((raw, e)),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
|
|
@ -360,20 +360,20 @@ pub fn canonicalize_pattern<'a>(
|
|||
let problem = MalformedPatternProblem::MalformedInt;
|
||||
malformed_pattern(env, problem, region)
|
||||
}
|
||||
Ok(ParsedNumResult::UnknownNum(int, bound)) => {
|
||||
Pattern::NumLiteral(var_store.fresh(), (str).into(), int, bound)
|
||||
Ok((parsed, ParsedNumResult::UnknownNum(int, bound))) => {
|
||||
Pattern::NumLiteral(var_store.fresh(), (parsed).into(), int, bound)
|
||||
}
|
||||
Ok(ParsedNumResult::Int(int, bound)) => Pattern::IntLiteral(
|
||||
Ok((parsed, ParsedNumResult::Int(int, bound))) => Pattern::IntLiteral(
|
||||
var_store.fresh(),
|
||||
var_store.fresh(),
|
||||
(str).into(),
|
||||
(parsed).into(),
|
||||
int,
|
||||
bound,
|
||||
),
|
||||
Ok(ParsedNumResult::Float(float, bound)) => Pattern::FloatLiteral(
|
||||
Ok((parsed, ParsedNumResult::Float(float, bound))) => Pattern::FloatLiteral(
|
||||
var_store.fresh(),
|
||||
var_store.fresh(),
|
||||
(str).into(),
|
||||
(parsed).into(),
|
||||
float,
|
||||
bound,
|
||||
),
|
||||
|
|
|
@ -3331,6 +3331,20 @@ fn dec_float_suffix() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||
fn dec_no_decimal() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
3dec
|
||||
"#
|
||||
),
|
||||
RocDec::from_str_to_i128_unsafe("3.0"),
|
||||
i128
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||
fn ceiling_to_u32() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue