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) => {
|
Num(string) => {
|
||||||
match finish_parsing_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 {
|
let expr = Expr2::SmallInt {
|
||||||
number: IntVal::I64(match int {
|
number: IntVal::I64(match int {
|
||||||
IntValue::U128(_) => todo!(),
|
IntValue::U128(_) => todo!(),
|
||||||
|
@ -86,16 +89,16 @@ pub fn expr_to_expr2<'a>(
|
||||||
var: env.var_store.fresh(),
|
var: env.var_store.fresh(),
|
||||||
// TODO non-hardcode
|
// TODO non-hardcode
|
||||||
style: IntStyle::Decimal,
|
style: IntStyle::Decimal,
|
||||||
text: PoolStr::new(string, env.pool),
|
text: PoolStr::new(parsed, env.pool),
|
||||||
};
|
};
|
||||||
|
|
||||||
(expr, Output::default())
|
(expr, Output::default())
|
||||||
}
|
}
|
||||||
Ok(ParsedNumResult::Float(float, _)) => {
|
Ok((parsed, ParsedNumResult::Float(float, _))) => {
|
||||||
let expr = Expr2::Float {
|
let expr = Expr2::Float {
|
||||||
number: FloatVal::F64(float),
|
number: FloatVal::F64(float),
|
||||||
var: env.var_store.fresh(),
|
var: env.var_store.fresh(),
|
||||||
text: PoolStr::new(string, env.pool),
|
text: PoolStr::new(parsed, env.pool),
|
||||||
};
|
};
|
||||||
|
|
||||||
(expr, Output::default())
|
(expr, Output::default())
|
||||||
|
|
|
@ -193,7 +193,7 @@ pub fn to_pattern2<'a>(
|
||||||
let problem = MalformedPatternProblem::MalformedInt;
|
let problem = MalformedPatternProblem::MalformedInt;
|
||||||
malformed_pattern(env, problem, region)
|
malformed_pattern(env, problem, region)
|
||||||
}
|
}
|
||||||
Ok(ParsedNumResult::UnknownNum(int, _bound)) => {
|
Ok((_, ParsedNumResult::UnknownNum(int, _bound))) => {
|
||||||
Pattern2::NumLiteral(
|
Pattern2::NumLiteral(
|
||||||
env.var_store.fresh(),
|
env.var_store.fresh(),
|
||||||
match int {
|
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 {
|
Pattern2::IntLiteral(IntVal::I64(match int {
|
||||||
IntValue::U128(_) => todo!(),
|
IntValue::U128(_) => todo!(),
|
||||||
IntValue::I128(n) => n as i64, // FIXME
|
IntValue::I128(n) => n as i64, // FIXME
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
Ok(ParsedNumResult::Float(int, _bound)) => {
|
Ok((_, ParsedNumResult::Float(int, _bound))) => {
|
||||||
Pattern2::FloatLiteral(FloatVal::F64(int))
|
Pattern2::FloatLiteral(FloatVal::F64(int))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -422,12 +422,7 @@ pub fn canonicalize_expr<'a>(
|
||||||
|
|
||||||
let (expr, output) = match expr {
|
let (expr, output) = match expr {
|
||||||
&ast::Expr::Num(str) => {
|
&ast::Expr::Num(str) => {
|
||||||
let answer = num_expr_from_result(
|
let answer = num_expr_from_result(var_store, finish_parsing_num(str), region, env);
|
||||||
var_store,
|
|
||||||
finish_parsing_num(str).map(|result| (str, result)),
|
|
||||||
region,
|
|
||||||
env,
|
|
||||||
);
|
|
||||||
|
|
||||||
(answer, Output::default())
|
(answer, Output::default())
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,10 +107,14 @@ pub enum ParsedNumResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[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.
|
// Ignore underscores.
|
||||||
let radix = 10;
|
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)]
|
#[inline(always)]
|
||||||
|
|
|
@ -360,20 +360,20 @@ pub fn canonicalize_pattern<'a>(
|
||||||
let problem = MalformedPatternProblem::MalformedInt;
|
let problem = MalformedPatternProblem::MalformedInt;
|
||||||
malformed_pattern(env, problem, region)
|
malformed_pattern(env, problem, region)
|
||||||
}
|
}
|
||||||
Ok(ParsedNumResult::UnknownNum(int, bound)) => {
|
Ok((parsed, ParsedNumResult::UnknownNum(int, bound))) => {
|
||||||
Pattern::NumLiteral(var_store.fresh(), (str).into(), 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(),
|
||||||
var_store.fresh(),
|
var_store.fresh(),
|
||||||
(str).into(),
|
(parsed).into(),
|
||||||
int,
|
int,
|
||||||
bound,
|
bound,
|
||||||
),
|
),
|
||||||
Ok(ParsedNumResult::Float(float, bound)) => Pattern::FloatLiteral(
|
Ok((parsed, ParsedNumResult::Float(float, bound))) => Pattern::FloatLiteral(
|
||||||
var_store.fresh(),
|
var_store.fresh(),
|
||||||
var_store.fresh(),
|
var_store.fresh(),
|
||||||
(str).into(),
|
(parsed).into(),
|
||||||
float,
|
float,
|
||||||
bound,
|
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]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||||
fn ceiling_to_u32() {
|
fn ceiling_to_u32() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue