diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index 93edf9f..b2c97cc 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -503,13 +503,17 @@ impl Expression { Ok((_, ty)) => Ok(ty), 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 let hint_t = hint_t.map(|t| match t { TypeKind::Array(type_kind, _) => &type_kind, _ => 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.. let expr_t = expression.typecheck(state, typerefs, hint_t)?; diff --git a/reid/src/mir/typeinference.rs b/reid/src/mir/typeinference.rs index dd06051..7192def 100644 --- a/reid/src/mir/typeinference.rs +++ b/reid/src/mir/typeinference.rs @@ -105,6 +105,7 @@ impl Block { (var_ref.as_mut(), expr_ty_ref.as_mut()) { var_ref.narrow(&expr_ty_ref); + dbg!(var_ref); } } StmtKind::Set(lhs, rhs) => { @@ -252,9 +253,13 @@ impl Expression { 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)?; + // 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 // need for further resolution. let kind = expr_ty.resolve_weak().unwrap(); diff --git a/reid_src/array.reid b/reid_src/array.reid index f25f0a6..1062f7f 100644 --- a/reid_src/array.reid +++ b/reid_src/array.reid @@ -1,13 +1,13 @@ // Arithmetic, function calls and imports! -fn array() -> [[[u16; 4]; 1]; 1] { - return [[[10, 15, 7, 9]]]; +fn array() -> [u16; 4] { + return [10, 15, 7, 9]; } fn main() -> u16 { let mut list = array(); - list[0][0][3] = 5; + let a = 1; - return list[0][0][3]; + return list[a + 1]; } diff --git a/reid_src/array_structs.reid b/reid_src/array_structs.reid index 179c7fa..89c001b 100644 --- a/reid_src/array_structs.reid +++ b/reid_src/array_structs.reid @@ -11,7 +11,10 @@ fn main() -> u32 { 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]; }