Add parsing to floats

This commit is contained in:
Sofia 2025-07-21 16:18:14 +03:00
parent 96947be97a
commit bcc93dd60c
5 changed files with 49 additions and 7 deletions

View File

@ -24,6 +24,13 @@ pub enum TypeKind {
U32,
U64,
U128,
F16,
F16B,
F32,
F64,
F128,
F80,
F128PPC,
String,
Array(Box<TypeKind>, u64),
Custom(String),
@ -33,7 +40,8 @@ pub enum TypeKind {
#[derive(Debug, Clone)]
pub enum Literal {
Number(u64),
Integer(u64),
Decimal(f64),
Bool(bool),
String(String),
}

View File

@ -53,6 +53,13 @@ impl Parse for Type {
"u32" => TypeKind::U32,
"u64" => TypeKind::U64,
"u128" => TypeKind::U128,
"f16" => TypeKind::F16,
"f16b" => TypeKind::F16B,
"f32" => TypeKind::F32,
"f64" => TypeKind::F64,
"f80" => TypeKind::F80,
"f128" => TypeKind::F128,
"f128ppc" => TypeKind::F128PPC,
"string" => TypeKind::String,
_ => TypeKind::Custom(ident),
}
@ -148,10 +155,25 @@ impl Parse for PrimaryExpression {
Expression(Kind::VariableName(v.clone()), stream.get_range().unwrap())
}
}
Token::DecimalValue(v) => Expression(
Kind::Literal(Literal::Number(*v)),
stream.get_range().unwrap(),
),
Token::DecimalValue(v) => {
if let Some(Token::Dot) = stream.peek() {
stream.next(); // Consume dot
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)),
stream.get_range_prev().unwrap(),
)
} else {
Expression(
Kind::Literal(Literal::Integer(*v)),
stream.get_range_prev().unwrap(),
)
}
}
Token::StringLit(v) => Expression(
Kind::Literal(Literal::String(v.clone())),
stream.get_range().unwrap(),

View File

@ -282,9 +282,10 @@ impl ast::BinaryOperator {
impl ast::Literal {
fn mir(&self) -> mir::Literal {
match &self {
ast::Literal::Number(v) => mir::Literal::Vague(mir::VagueLiteral::Number(*v)),
ast::Literal::Integer(v) => mir::Literal::Vague(mir::VagueLiteral::Number(*v)),
ast::Literal::Bool(v) => mir::Literal::Bool(*v),
ast::Literal::String(val) => mir::Literal::String(val.clone()),
ast::Literal::Decimal(_) => todo!(),
}
}
}
@ -314,6 +315,13 @@ impl From<ast::TypeKind> for mir::TypeKind {
ast::TypeKind::Ptr(type_kind) => {
mir::TypeKind::UserPtr(Box::new(mir::TypeKind::from(*type_kind.clone())))
}
ast::TypeKind::F16 => todo!(),
ast::TypeKind::F16B => todo!(),
ast::TypeKind::F32 => todo!(),
ast::TypeKind::F64 => todo!(),
ast::TypeKind::F128 => todo!(),
ast::TypeKind::F80 => todo!(),
ast::TypeKind::F128PPC => todo!(),
}
}
}

View File

@ -412,7 +412,6 @@ impl Expression {
let both_t = lhs_type.collapse_into(&rhs_type)?;
dbg!(&op, &both_t, both_t.signed(), lhs.is_zero(), rhs.is_zero());
if *op == BinaryOperator::Minus && !lhs_type.signed() {
if let (Some(lhs_val), Some(rhs_val)) = (lhs.num_value(), rhs.num_value()) {
if lhs_val < rhs_val {

5
reid_src/float.reid Normal file
View File

@ -0,0 +1,5 @@
// Arithmetic, function calls and imports!
fn OneHalf(var1: f32) -> f32 {
return var1 * 1.5;
}