From b288fee2d585f65652c7ff97764b6bc717550c27 Mon Sep 17 00:00:00 2001 From: sofia Date: Fri, 25 Jul 2025 03:04:04 +0300 Subject: [PATCH] Fix parsing of explicitly typed literals --- reid-llvm-lib/src/compile.rs | 2 - reid/src/ast/parse.rs | 196 +++++++++++++++++++---------------- reid/src/codegen.rs | 1 - reid/src/mir/typecheck.rs | 3 - 4 files changed, 108 insertions(+), 94 deletions(-) diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index 1c1cd3d..fc29261 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -728,7 +728,6 @@ impl BlockHolder { )) .compile(module, function, block_ref); - dbg!(&self.value, &self.data.terminator_location); if let Some(location) = &self.data.terminator_location { LLVMInstructionSetDebugLoc( term_instr.value_ref, @@ -839,7 +838,6 @@ impl InstructionHolder { FCmp(pred, lhs, rhs) => { let lhs = module.values.get(&lhs).unwrap(); let rhs_val = module.values.get(&rhs).unwrap().value_ref; - dbg!(pred.as_llvm_unsorted_float()); LLVMBuildFCmp( module.builder_ref, pred.as_llvm_unsorted_float(), diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index 7e36869..bf18f2a 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -99,80 +99,72 @@ impl Parse for UnaryOperator { fn specific_float_lit(value: f64, stream: &mut TokenStream) -> Result { use ExpressionKind as Kind; - let specific_lit = if let Ok(ty) = stream.parse::() { - dbg!(&ty); - match ty.0 { - TypeKind::F16 => Some(SpecificLiteral::F16(value as f32)), - TypeKind::F32 => Some(SpecificLiteral::F32(value as f32)), - TypeKind::F32B => Some(SpecificLiteral::F32B(value as f32)), - TypeKind::F64 => Some(SpecificLiteral::F64(value as f64)), - TypeKind::F128 => Some(SpecificLiteral::F128(value as f64)), - TypeKind::F80 => Some(SpecificLiteral::F80(value as f64)), - TypeKind::F128PPC => Some(SpecificLiteral::F128PPC(value as f64)), - TypeKind::Array(..) => None, - TypeKind::Ptr(..) => None, - TypeKind::Custom(..) => None, - TypeKind::Borrow(..) => None, - _ => return Err(stream.expected_err("integer-compatible type")?), - } - } else { - None + let lit = match stream + .parse_if::(|t| match t.0 { + TypeKind::F16 => true, + TypeKind::F32B => true, + TypeKind::F32 => true, + TypeKind::F64 => true, + TypeKind::F128 => true, + TypeKind::F80 => true, + TypeKind::F128PPC => true, + _ => false, + })? + .0 + { + TypeKind::F16 => SpecificLiteral::F16(value as f32), + TypeKind::F32 => SpecificLiteral::F32(value as f32), + TypeKind::F32B => SpecificLiteral::F32B(value as f32), + TypeKind::F64 => SpecificLiteral::F64(value as f64), + TypeKind::F128 => SpecificLiteral::F128(value as f64), + TypeKind::F80 => SpecificLiteral::F80(value as f64), + TypeKind::F128PPC => SpecificLiteral::F128PPC(value as f64), + _ => Err(stream.expected_err("float-compatible type")?)?, }; - Ok(if let Some(lit) = specific_lit { - Expression( - Kind::Literal(Literal::Specific(lit)), - stream.get_range().unwrap(), - ) - } else { - Expression( - Kind::Literal(Literal::Decimal(value)), - stream.get_range().unwrap(), - ) - }) + Ok(Expression( + Kind::Literal(Literal::Specific(lit)), + stream.get_range().unwrap(), + )) } fn specific_int_lit(value: u128, stream: &mut TokenStream) -> Result { use ExpressionKind as Kind; - let specific_lit = if let Ok(ty) = stream.parse::() { - match ty.0 { - TypeKind::I8 => Some(SpecificLiteral::I8(value as i8)), - TypeKind::I16 => Some(SpecificLiteral::I16(value as i16)), - TypeKind::I32 => Some(SpecificLiteral::I32(value as i32)), - TypeKind::I64 => Some(SpecificLiteral::I64(value as i64)), - TypeKind::I128 => Some(SpecificLiteral::I128(value as i128)), - TypeKind::U8 => Some(SpecificLiteral::U8(value as u8)), - TypeKind::U16 => Some(SpecificLiteral::U16(value as u16)), - TypeKind::U32 => Some(SpecificLiteral::U32(value as u32)), - TypeKind::U64 => Some(SpecificLiteral::U64(value as u64)), - TypeKind::U128 => Some(SpecificLiteral::U128(value as u128)), - TypeKind::F16 => Some(SpecificLiteral::F16(value as f32)), - TypeKind::F32 => Some(SpecificLiteral::F32(value as f32)), - TypeKind::F32B => Some(SpecificLiteral::F32B(value as f32)), - TypeKind::F64 => Some(SpecificLiteral::F64(value as f64)), - TypeKind::F128 => Some(SpecificLiteral::F128(value as f64)), - TypeKind::F80 => Some(SpecificLiteral::F80(value as f64)), - TypeKind::F128PPC => Some(SpecificLiteral::F128PPC(value as f64)), - TypeKind::Array(..) => None, - TypeKind::Ptr(..) => None, - TypeKind::Custom(..) => None, - TypeKind::Borrow(..) => None, - _ => return Err(stream.expected_err("integer-compatible type")?), - } - } else { - None + let lit = match stream + .parse_if::(|t| match t.0 { + TypeKind::Char => false, + TypeKind::Array(_, _) => false, + TypeKind::Custom(_) => false, + TypeKind::Borrow(_, _) => false, + TypeKind::Ptr(_) => false, + TypeKind::Bool => false, + _ => true, + })? + .0 + { + TypeKind::I8 => SpecificLiteral::I8(value as i8), + TypeKind::I16 => SpecificLiteral::I16(value as i16), + TypeKind::I32 => SpecificLiteral::I32(value as i32), + TypeKind::I64 => SpecificLiteral::I64(value as i64), + TypeKind::I128 => SpecificLiteral::I128(value as i128), + TypeKind::U8 => SpecificLiteral::U8(value as u8), + TypeKind::U16 => SpecificLiteral::U16(value as u16), + TypeKind::U32 => SpecificLiteral::U32(value as u32), + TypeKind::U64 => SpecificLiteral::U64(value as u64), + TypeKind::U128 => SpecificLiteral::U128(value as u128), + TypeKind::F16 => SpecificLiteral::F16(value as f32), + TypeKind::F32 => SpecificLiteral::F32(value as f32), + TypeKind::F32B => SpecificLiteral::F32B(value as f32), + TypeKind::F64 => SpecificLiteral::F64(value as f64), + TypeKind::F128 => SpecificLiteral::F128(value as f64), + TypeKind::F80 => SpecificLiteral::F80(value as f64), + TypeKind::F128PPC => SpecificLiteral::F128PPC(value as f64), + _ => return Err(stream.expected_err("integer-compatible type")?), }; - Ok(if let Some(lit) = specific_lit { - Expression( - Kind::Literal(Literal::Specific(lit)), - stream.get_range().unwrap(), - ) - } else { - Expression( - Kind::Literal(Literal::Integer(value)), - stream.get_range().unwrap(), - ) - }) + Ok(Expression( + Kind::Literal(Literal::Specific(lit)), + stream.get_range().unwrap(), + )) } #[derive(Debug)] @@ -234,25 +226,43 @@ impl Parse for PrimaryExpression { } Token::BinaryValue(v) => { stream.next(); // Consume binary - specific_int_lit( - u128::from_str_radix(&v, 2).expect("Binary is not parseable as u128!"), - &mut stream, - )? + let value = + u128::from_str_radix(&v, 2).expect("Value is not parseable as u128!"); + if let Ok(expr) = specific_int_lit(value, &mut stream) { + expr + } else { + Expression( + Kind::Literal(Literal::Integer(value)), + stream.get_range().unwrap(), + ) + } } Token::OctalValue(v) => { stream.next(); // Consume octal - specific_int_lit( - u128::from_str_radix(&v, 8).expect("Octal is not parseable as u128!"), - &mut stream, - )? + let value = + u128::from_str_radix(&v, 8).expect("Value is not parseable as u128!"); + if let Ok(expr) = specific_int_lit(value, &mut stream) { + expr + } else { + Expression( + Kind::Literal(Literal::Integer(value)), + stream.get_range().unwrap(), + ) + } } Token::HexadecimalValue(v) => { stream.next(); // Consume hexadecimal - specific_int_lit( - u128::from_str_radix(&v, 16) - .expect("Hexadecimal is not parseable as u128!"), - &mut stream, - )? + + let value = + u128::from_str_radix(&v, 16).expect("Value is not parseable as u128!"); + if let Ok(expr) = specific_int_lit(value, &mut stream) { + expr + } else { + Expression( + Kind::Literal(Literal::Integer(value)), + stream.get_range().unwrap(), + ) + } } Token::DecimalValue(v) => { stream.next(); // Consume decimal @@ -266,15 +276,25 @@ impl Parse for PrimaryExpression { .parse() .expect("Decimal is not parseable as f64!"); - let a = specific_float_lit(value, &mut stream)?; - dbg!(&a); - a + if let Ok(expr) = specific_float_lit(value, &mut stream) { + expr + } else { + Expression( + Kind::Literal(Literal::Decimal(value)), + stream.get_range().unwrap(), + ) + } } else { - specific_int_lit( - u128::from_str_radix(&v, 10) - .expect("Integer is not parseable as u128!"), - &mut stream, - )? + let value = + u128::from_str_radix(&v, 10).expect("Value is not parseable as u128!"); + if let Ok(expr) = specific_int_lit(value, &mut stream) { + expr + } else { + Expression( + Kind::Literal(Literal::Integer(value)), + stream.get_range().unwrap(), + ) + } } } Token::StringLit(v) => { diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 2e64284..402de11 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -972,7 +972,6 @@ impl mir::Expression { Instr::Sub(lhs, mul) } }; - dbg!(&instr); Some(StackValue( StackValueKind::Immutable( scope diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index 465c15a..1fa47df 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -565,9 +565,6 @@ impl Expression { let lhs_type = state.or_else(lhs_res, TypeKind::Vague(Vague::Unknown), lhs.1); let rhs_res = rhs.typecheck(state, &typerefs, Some(&lhs_type)); let rhs_type = state.or_else(rhs_res, TypeKind::Vague(Vague::Unknown), rhs.1); - dbg!(&op, &hint_t); - dbg!(&lhs_type); - dbg!(&rhs_type); let both_t = lhs_type.collapse_into(&rhs_type)?;