Fix expression-indexing
This commit is contained in:
		
							parent
							
								
									d2cf97af66
								
							
						
					
					
						commit
						e4ce897f94
					
				| @ -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)?; | ||||
|  | ||||
| @ -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(); | ||||
|  | ||||
| @ -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]; | ||||
| } | ||||
|  | ||||
| @ -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]; | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user