Fix parsing of explicitly typed literals

This commit is contained in:
Sofia 2025-07-25 03:04:04 +03:00
parent 242cf9fb2d
commit b288fee2d5
4 changed files with 108 additions and 94 deletions

View File

@ -728,7 +728,6 @@ impl BlockHolder {
)) ))
.compile(module, function, block_ref); .compile(module, function, block_ref);
dbg!(&self.value, &self.data.terminator_location);
if let Some(location) = &self.data.terminator_location { if let Some(location) = &self.data.terminator_location {
LLVMInstructionSetDebugLoc( LLVMInstructionSetDebugLoc(
term_instr.value_ref, term_instr.value_ref,
@ -839,7 +838,6 @@ impl InstructionHolder {
FCmp(pred, lhs, rhs) => { FCmp(pred, lhs, rhs) => {
let lhs = module.values.get(&lhs).unwrap(); let lhs = module.values.get(&lhs).unwrap();
let rhs_val = module.values.get(&rhs).unwrap().value_ref; let rhs_val = module.values.get(&rhs).unwrap().value_ref;
dbg!(pred.as_llvm_unsorted_float());
LLVMBuildFCmp( LLVMBuildFCmp(
module.builder_ref, module.builder_ref,
pred.as_llvm_unsorted_float(), pred.as_llvm_unsorted_float(),

View File

@ -99,80 +99,72 @@ impl Parse for UnaryOperator {
fn specific_float_lit(value: f64, stream: &mut TokenStream) -> Result<Expression, Error> { fn specific_float_lit(value: f64, stream: &mut TokenStream) -> Result<Expression, Error> {
use ExpressionKind as Kind; use ExpressionKind as Kind;
let specific_lit = if let Ok(ty) = stream.parse::<Type>() { let lit = match stream
dbg!(&ty); .parse_if::<Type, _>(|t| match t.0 {
match ty.0 { TypeKind::F16 => true,
TypeKind::F16 => Some(SpecificLiteral::F16(value as f32)), TypeKind::F32B => true,
TypeKind::F32 => Some(SpecificLiteral::F32(value as f32)), TypeKind::F32 => true,
TypeKind::F32B => Some(SpecificLiteral::F32B(value as f32)), TypeKind::F64 => true,
TypeKind::F64 => Some(SpecificLiteral::F64(value as f64)), TypeKind::F128 => true,
TypeKind::F128 => Some(SpecificLiteral::F128(value as f64)), TypeKind::F80 => true,
TypeKind::F80 => Some(SpecificLiteral::F80(value as f64)), TypeKind::F128PPC => true,
TypeKind::F128PPC => Some(SpecificLiteral::F128PPC(value as f64)), _ => false,
TypeKind::Array(..) => None, })?
TypeKind::Ptr(..) => None, .0
TypeKind::Custom(..) => None, {
TypeKind::Borrow(..) => None, TypeKind::F16 => SpecificLiteral::F16(value as f32),
_ => return Err(stream.expected_err("integer-compatible type")?), TypeKind::F32 => SpecificLiteral::F32(value as f32),
} TypeKind::F32B => SpecificLiteral::F32B(value as f32),
} else { TypeKind::F64 => SpecificLiteral::F64(value as f64),
None 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 { Ok(Expression(
Expression(
Kind::Literal(Literal::Specific(lit)), Kind::Literal(Literal::Specific(lit)),
stream.get_range().unwrap(), stream.get_range().unwrap(),
) ))
} else {
Expression(
Kind::Literal(Literal::Decimal(value)),
stream.get_range().unwrap(),
)
})
} }
fn specific_int_lit(value: u128, stream: &mut TokenStream) -> Result<Expression, Error> { fn specific_int_lit(value: u128, stream: &mut TokenStream) -> Result<Expression, Error> {
use ExpressionKind as Kind; use ExpressionKind as Kind;
let specific_lit = if let Ok(ty) = stream.parse::<Type>() { let lit = match stream
match ty.0 { .parse_if::<Type, _>(|t| match t.0 {
TypeKind::I8 => Some(SpecificLiteral::I8(value as i8)), TypeKind::Char => false,
TypeKind::I16 => Some(SpecificLiteral::I16(value as i16)), TypeKind::Array(_, _) => false,
TypeKind::I32 => Some(SpecificLiteral::I32(value as i32)), TypeKind::Custom(_) => false,
TypeKind::I64 => Some(SpecificLiteral::I64(value as i64)), TypeKind::Borrow(_, _) => false,
TypeKind::I128 => Some(SpecificLiteral::I128(value as i128)), TypeKind::Ptr(_) => false,
TypeKind::U8 => Some(SpecificLiteral::U8(value as u8)), TypeKind::Bool => false,
TypeKind::U16 => Some(SpecificLiteral::U16(value as u16)), _ => true,
TypeKind::U32 => Some(SpecificLiteral::U32(value as u32)), })?
TypeKind::U64 => Some(SpecificLiteral::U64(value as u64)), .0
TypeKind::U128 => Some(SpecificLiteral::U128(value as u128)), {
TypeKind::F16 => Some(SpecificLiteral::F16(value as f32)), TypeKind::I8 => SpecificLiteral::I8(value as i8),
TypeKind::F32 => Some(SpecificLiteral::F32(value as f32)), TypeKind::I16 => SpecificLiteral::I16(value as i16),
TypeKind::F32B => Some(SpecificLiteral::F32B(value as f32)), TypeKind::I32 => SpecificLiteral::I32(value as i32),
TypeKind::F64 => Some(SpecificLiteral::F64(value as f64)), TypeKind::I64 => SpecificLiteral::I64(value as i64),
TypeKind::F128 => Some(SpecificLiteral::F128(value as f64)), TypeKind::I128 => SpecificLiteral::I128(value as i128),
TypeKind::F80 => Some(SpecificLiteral::F80(value as f64)), TypeKind::U8 => SpecificLiteral::U8(value as u8),
TypeKind::F128PPC => Some(SpecificLiteral::F128PPC(value as f64)), TypeKind::U16 => SpecificLiteral::U16(value as u16),
TypeKind::Array(..) => None, TypeKind::U32 => SpecificLiteral::U32(value as u32),
TypeKind::Ptr(..) => None, TypeKind::U64 => SpecificLiteral::U64(value as u64),
TypeKind::Custom(..) => None, TypeKind::U128 => SpecificLiteral::U128(value as u128),
TypeKind::Borrow(..) => None, 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")?), _ => return Err(stream.expected_err("integer-compatible type")?),
}
} else {
None
}; };
Ok(if let Some(lit) = specific_lit { Ok(Expression(
Expression(
Kind::Literal(Literal::Specific(lit)), Kind::Literal(Literal::Specific(lit)),
stream.get_range().unwrap(), stream.get_range().unwrap(),
) ))
} else {
Expression(
Kind::Literal(Literal::Integer(value)),
stream.get_range().unwrap(),
)
})
} }
#[derive(Debug)] #[derive(Debug)]
@ -234,25 +226,43 @@ impl Parse for PrimaryExpression {
} }
Token::BinaryValue(v) => { Token::BinaryValue(v) => {
stream.next(); // Consume binary stream.next(); // Consume binary
specific_int_lit( let value =
u128::from_str_radix(&v, 2).expect("Binary is not parseable as u128!"), u128::from_str_radix(&v, 2).expect("Value is not parseable as u128!");
&mut stream, 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) => { Token::OctalValue(v) => {
stream.next(); // Consume octal stream.next(); // Consume octal
specific_int_lit( let value =
u128::from_str_radix(&v, 8).expect("Octal is not parseable as u128!"), u128::from_str_radix(&v, 8).expect("Value is not parseable as u128!");
&mut stream, 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) => { Token::HexadecimalValue(v) => {
stream.next(); // Consume hexadecimal stream.next(); // Consume hexadecimal
specific_int_lit(
u128::from_str_radix(&v, 16) let value =
.expect("Hexadecimal is not parseable as u128!"), u128::from_str_radix(&v, 16).expect("Value is not parseable as u128!");
&mut stream, 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) => { Token::DecimalValue(v) => {
stream.next(); // Consume decimal stream.next(); // Consume decimal
@ -266,15 +276,25 @@ impl Parse for PrimaryExpression {
.parse() .parse()
.expect("Decimal is not parseable as f64!"); .expect("Decimal is not parseable as f64!");
let a = specific_float_lit(value, &mut stream)?; if let Ok(expr) = specific_float_lit(value, &mut stream) {
dbg!(&a); expr
a
} else { } else {
specific_int_lit( Expression(
u128::from_str_radix(&v, 10) Kind::Literal(Literal::Decimal(value)),
.expect("Integer is not parseable as u128!"), stream.get_range().unwrap(),
&mut stream, )
)? }
} else {
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) => { Token::StringLit(v) => {

View File

@ -972,7 +972,6 @@ impl mir::Expression {
Instr::Sub(lhs, mul) Instr::Sub(lhs, mul)
} }
}; };
dbg!(&instr);
Some(StackValue( Some(StackValue(
StackValueKind::Immutable( StackValueKind::Immutable(
scope scope

View File

@ -565,9 +565,6 @@ impl Expression {
let lhs_type = state.or_else(lhs_res, TypeKind::Vague(Vague::Unknown), lhs.1); 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_res = rhs.typecheck(state, &typerefs, Some(&lhs_type));
let rhs_type = state.or_else(rhs_res, TypeKind::Vague(Vague::Unknown), rhs.1); 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)?; let both_t = lhs_type.collapse_into(&rhs_type)?;