From 9df1593de958cc5a729a28a46b7afc3b6f4d721c Mon Sep 17 00:00:00 2001 From: sofia Date: Tue, 22 Jul 2025 23:20:38 +0300 Subject: [PATCH] Fix decimal parsing, add u128-support for integer parsing --- reid/src/ast/mod.rs | 2 +- reid/src/ast/parse.rs | 17 ++++++++++++----- reid/src/lexer.rs | 4 ++-- reid/src/mir/implement.rs | 2 +- reid/src/mir/mod.rs | 2 +- reid_src/float.reid | 2 +- 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index 238c086..bee921e 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -40,7 +40,7 @@ pub enum TypeKind { #[derive(Debug, Clone)] pub enum Literal { - Integer(u64), + Integer(u128), Decimal(f64), Bool(bool), String(String), diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index 247be06..dd0d1ef 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -23,7 +23,10 @@ impl Parse for Type { return Err(stream.expected_err("array length (number)")?); }; stream.expect(Token::BracketClose)?; - TypeKind::Array(Box::new(inner.0), length) + TypeKind::Array( + Box::new(inner.0), + length.parse().expect("Array length not parseable as u64!"), + ) } else if let Some(Token::Et) = stream.peek() { stream.expect(Token::Et)?; let mutable = if let Some(Token::MutKeyword) = stream.peek() { @@ -157,15 +160,19 @@ impl Parse for PrimaryExpression { let Some(Token::DecimalValue(fractional)) = stream.next() else { return Err(stream.expected_err("fractional part")?); }; - let log = (fractional as f64).log10().ceil() as u32; - let value = (*v as f64) + (fractional as f64) / (10u64.pow(log) as f64); Expression( - Kind::Literal(Literal::Decimal(value)), + Kind::Literal(Literal::Decimal( + format!("{}.{}", v, fractional) + .parse() + .expect("Decimal is not parseable as f64!"), + )), stream.get_range().unwrap(), ) } else { Expression( - Kind::Literal(Literal::Integer(*v)), + Kind::Literal(Literal::Integer( + v.parse().expect("Integer is not parseable as u128!"), + )), stream.get_range().unwrap(), ) } diff --git a/reid/src/lexer.rs b/reid/src/lexer.rs index 3689b22..79f0bbc 100644 --- a/reid/src/lexer.rs +++ b/reid/src/lexer.rs @@ -7,7 +7,7 @@ pub enum Token { /// Values Identifier(String), /// Number with at most one decimal point - DecimalValue(u64), + DecimalValue(String), /// Some character literal that was surrounded by 'single-quotes'. CharLit(String), /// Some string literal that was surrounded by "double-quotes". @@ -318,7 +318,7 @@ pub fn tokenize>(to_tokenize: T) -> Result, Error value += &c.to_string(); cursor.next(); } - Token::DecimalValue(value.parse().expect("Decimal not parseable to u64")) + Token::DecimalValue(value) } '-' if cursor.first() == Some('>') => { cursor.next(); // Eat `>` diff --git a/reid/src/mir/implement.rs b/reid/src/mir/implement.rs index 418a7a6..361a3f2 100644 --- a/reid/src/mir/implement.rs +++ b/reid/src/mir/implement.rs @@ -163,7 +163,7 @@ impl TypeKind { TypeKind::U128 => 128, TypeKind::Void => 0, TypeKind::Char => 8, - TypeKind::Array(type_kind, len) => type_kind.size_of() * len, + TypeKind::Array(type_kind, len) => type_kind.size_of() * (*len as u64), TypeKind::CustomType(..) => 32, TypeKind::CodegenPtr(_) => 64, TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"), diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index bb1ce85..24578c2 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -171,7 +171,7 @@ pub enum Literal { #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] pub enum VagueLiteral { - Number(u64), + Number(u128), Decimal(f64), } diff --git a/reid_src/float.reid b/reid_src/float.reid index 903131c..d5c134c 100644 --- a/reid_src/float.reid +++ b/reid_src/float.reid @@ -5,5 +5,5 @@ pub fn OneHalf(var1: f32) -> f32 { } pub fn main() -> bool { - return (7.5 > 5.5); + return 7.5 > 5.001; }