Add typecheck/infer for pointers
This commit is contained in:
		
							parent
							
								
									3b43689650
								
							
						
					
					
						commit
						a49105b07a
					
				| @ -397,6 +397,9 @@ impl Collapsable for TypeKind { | ||||
|                     )) | ||||
|                 } | ||||
|             } | ||||
|             (TypeKind::Ptr(val1), TypeKind::Ptr(val2)) => { | ||||
|                 Ok(TypeKind::Ptr(Box::new(val1.collapse_into(val2)?))) | ||||
|             } | ||||
|             _ => Err(ErrorKind::TypesIncompatible(self.clone(), other.clone())), | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @ -534,16 +534,17 @@ impl Expression { | ||||
|                 // TODO it could be possible to check length against constants..
 | ||||
| 
 | ||||
|                 let expr_t = expression.typecheck(state, typerefs, hint_t)?; | ||||
|                 if let TypeKind::Array(inferred_ty, _) = expr_t { | ||||
|                     let ty = state.or_else( | ||||
|                         elem_ty.resolve_ref(typerefs).collapse_into(&inferred_ty), | ||||
|                         TypeKind::Vague(Vague::Unknown), | ||||
|                         self.1, | ||||
|                     ); | ||||
|                     *elem_ty = ty.clone(); | ||||
|                     Ok(ty) | ||||
|                 } else { | ||||
|                     Err(ErrorKind::TriedIndexingNonArray(expr_t)) | ||||
|                 match expr_t { | ||||
|                     TypeKind::Array(inferred_ty, _) | TypeKind::Ptr(inferred_ty) => { | ||||
|                         let ty = state.or_else( | ||||
|                             elem_ty.resolve_ref(typerefs).collapse_into(&inferred_ty), | ||||
|                             TypeKind::Vague(Vague::Unknown), | ||||
|                             self.1, | ||||
|                         ); | ||||
|                         *elem_ty = ty.clone(); | ||||
|                         Ok(ty) | ||||
|                     } | ||||
|                     _ => Err(ErrorKind::TriedIndexingNonArray(expr_t)), | ||||
|                 } | ||||
|             } | ||||
|             ExprKind::Array(expressions) => { | ||||
|  | ||||
| @ -267,7 +267,7 @@ impl Expression { | ||||
|                 // need for further resolution.
 | ||||
|                 let kind = expr_ty.resolve_weak().unwrap(); | ||||
|                 match kind { | ||||
|                     Array(type_kind, _) => { | ||||
|                     Array(type_kind, _) | Ptr(type_kind) => { | ||||
|                         let elem_ty = type_refs.from_type(&type_kind).unwrap(); | ||||
|                         *index_ty = elem_ty.as_type().clone(); | ||||
|                         Ok(elem_ty) | ||||
|  | ||||
| @ -3,7 +3,7 @@ | ||||
| 
 | ||||
| extern fn malloc(size: u64) -> *u8; | ||||
| 
 | ||||
| fn main() -> u16 { | ||||
| fn main() -> u8 { | ||||
|     let ptr = malloc(4); | ||||
| 
 | ||||
|     return ptr[0]; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user