Fix expression-indexing

This commit is contained in:
Sofia 2025-07-16 23:29:15 +03:00
parent d2cf97af66
commit e4ce897f94
4 changed files with 19 additions and 7 deletions

View File

@ -503,13 +503,17 @@ impl Expression {
Ok((_, ty)) => Ok(ty), Ok((_, ty)) => Ok(ty),
Err(e) => Err(e), Err(e) => Err(e),
}, },
ExprKind::Indexed(expression, elem_ty, _) => { ExprKind::Indexed(expression, elem_ty, idx_expr) => {
// Try to unwrap hint type from array if possible // Try to unwrap hint type from array if possible
let hint_t = hint_t.map(|t| match t { let hint_t = hint_t.map(|t| match t {
TypeKind::Array(type_kind, _) => &type_kind, TypeKind::Array(type_kind, _) => &type_kind,
_ => t, _ => t,
}); });
// Typecheck and narrow index-expression
let idx_expr_res = idx_expr.typecheck(state, typerefs, Some(&TypeKind::U32));
state.ok(idx_expr_res, idx_expr.1);
// TODO it could be possible to check length against constants.. // TODO it could be possible to check length against constants..
let expr_t = expression.typecheck(state, typerefs, hint_t)?; let expr_t = expression.typecheck(state, typerefs, hint_t)?;

View File

@ -105,6 +105,7 @@ impl Block {
(var_ref.as_mut(), expr_ty_ref.as_mut()) (var_ref.as_mut(), expr_ty_ref.as_mut())
{ {
var_ref.narrow(&expr_ty_ref); var_ref.narrow(&expr_ty_ref);
dbg!(var_ref);
} }
} }
StmtKind::Set(lhs, rhs) => { StmtKind::Set(lhs, rhs) => {
@ -252,9 +253,13 @@ impl Expression {
ReturnKind::Soft => Ok(block_ref.1), ReturnKind::Soft => Ok(block_ref.1),
} }
} }
ExprKind::Indexed(expression, index_ty, _) => { ExprKind::Indexed(expression, index_ty, idx_expr) => {
let expr_ty = expression.infer_types(state, type_refs)?; let expr_ty = expression.infer_types(state, type_refs)?;
// Infer and narrow types used for indexing
let mut idx_ty = idx_expr.infer_types(state, type_refs)?;
idx_ty.narrow(&type_refs.from_type(&U32).unwrap());
// Check that the resolved type is at least an array, no // Check that the resolved type is at least an array, no
// need for further resolution. // need for further resolution.
let kind = expr_ty.resolve_weak().unwrap(); let kind = expr_ty.resolve_weak().unwrap();

View File

@ -1,13 +1,13 @@
// Arithmetic, function calls and imports! // Arithmetic, function calls and imports!
fn array() -> [[[u16; 4]; 1]; 1] { fn array() -> [u16; 4] {
return [[[10, 15, 7, 9]]]; return [10, 15, 7, 9];
} }
fn main() -> u16 { fn main() -> u16 {
let mut list = array(); let mut list = array();
list[0][0][3] = 5; let a = 1;
return list[0][0][3]; return list[a + 1];
} }

View File

@ -11,7 +11,10 @@ fn main() -> u32 {
second: [6, 3, 17, 8], second: [6, 3, 17, 8],
}]; }];
value[0].second[2] = 99; let val1 = 0;
let val2 = 1;
value[val1].second[val2 + 1] = 99;
return value[0].second[2]; return value[0].second[2];
} }