Skip to content

Commit

Permalink
Impl inferred floats
Browse files Browse the repository at this point in the history
  • Loading branch information
Amejonah1200 committed Sep 19, 2023
1 parent 38abf6a commit f4c707e
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 104 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ either = "1.9"
lasso = "0.7"
indextree = "4.6"
slotmap = "1.0.6"
ariadne = { git = "https://github.com/APLanguage/ariadne.git", rev = "bdc392368fb45e71aff0feca58d0bf66f56698ae"}
ariadne = { git = "https://github.com/APLanguage/ariadne.git", rev = "bdc392368fb45e71aff0feca58d0bf66f56698ae" }
itertools = "0.11"
toml = "0.7.6"
serde = { version = "1.0", features = ["derive"] }
same-file = "1.0.6"
strum = "0.25"
strum_macros = "0.25"
bigdecimal = { git = "https://github.com/APLanguage/bigdecimal-rs.git", rev = "864927641721a39299e460deebdc85d01ea0930b" }

[target.'cfg(windows)'.dependencies.winapi-util]
version = "0.1.5"
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ fn main() {

},
NumberCouldntBeParsedToDefault(span) => rep
.with_message("Couldn't parse number")
.with_message("Couldn't parse number.")
.with_label(ariadne::Label::new((&input_name as &str, span.into_range()))
.with_message("Couldn't be parsed as number.").with_color(ariadne::Color::Red)),
}
Expand Down
19 changes: 14 additions & 5 deletions src/parsing/parsers/number.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{
num::{ParseFloatError, ParseIntError},
str::FromStr,
sync::OnceLock,
};

use bigdecimal::{num_bigint::ParseBigIntError, BigDecimal, ParseBigDecimalError};
use either::Either;
use lasso::Rodeo;
use num::BigInt;
Expand All @@ -23,7 +25,7 @@ pub enum NumberLiteral {
Unsigned(u64, LiteralWidth),
Signed(i64, LiteralWidth),
Float(f64, LiteralWidth),
Inferred(Either<BigInt, ()>),
Inferred(Either<BigInt, BigDecimal>),
}
impl NumberLiteral {
fn inferred_of_u64(input: u64) -> NumberLiteral {
Expand Down Expand Up @@ -114,6 +116,8 @@ pub type NumberLiteralResult = Result<NumberLiteral, NumberLiteralError>;
pub enum NumberLiteralError {
ParseInt(ParseIntError, Option<(LiteralType, LiteralWidth)>),
ParseFloat(ParseFloatError, Option<(LiteralType, LiteralWidth)>),
ParseBigInt(ParseBigIntError, Option<(LiteralType, LiteralWidth)>),
ParseBigFloat(ParseBigDecimalError, Option<(LiteralType, LiteralWidth)>),
UnsignedIntWidthError(LiteralWidthError),
SignedIntWidthError(LiteralWidthError),
FloatWidthError(LiteralWidthError),
Expand Down Expand Up @@ -178,11 +182,16 @@ pub fn parse_complex_number(_rodeo: &mut Rodeo, input: &str) -> NumberLiteralRes
number_part = "-".to_owned() + &number_part
}
if num_type_width.is_none() {
return Ok(NumberLiteral::Inferred(
BigInt::parse_bytes(number_part.as_bytes(), radix)
return if number_part.contains('.') {
BigDecimal::from_str(&number_part)
.map(Either::Right)
.map_err(|err| NumberLiteralError::ParseBigFloat(err, None))
} else {
BigInt::from_str_radix(&number_part, radix)
.map(Either::Left)
.unwrap_or(Either::Right(())),
));
.map_err(|err| NumberLiteralError::ParseBigInt(err, None))
}
.map(NumberLiteral::Inferred);
};
let (num_type, width) = num_type_width.unwrap();
let width = LiteralWidth::try_from(width).map_err(num_type.width_error())?;
Expand Down
Loading

0 comments on commit f4c707e

Please sign in to comment.