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())),
|
_ => Err(ErrorKind::TypesIncompatible(self.clone(), other.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -534,16 +534,17 @@ impl Expression {
|
|||||||
// 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)?;
|
||||||
if let TypeKind::Array(inferred_ty, _) = expr_t {
|
match expr_t {
|
||||||
let ty = state.or_else(
|
TypeKind::Array(inferred_ty, _) | TypeKind::Ptr(inferred_ty) => {
|
||||||
elem_ty.resolve_ref(typerefs).collapse_into(&inferred_ty),
|
let ty = state.or_else(
|
||||||
TypeKind::Vague(Vague::Unknown),
|
elem_ty.resolve_ref(typerefs).collapse_into(&inferred_ty),
|
||||||
self.1,
|
TypeKind::Vague(Vague::Unknown),
|
||||||
);
|
self.1,
|
||||||
*elem_ty = ty.clone();
|
);
|
||||||
Ok(ty)
|
*elem_ty = ty.clone();
|
||||||
} else {
|
Ok(ty)
|
||||||
Err(ErrorKind::TriedIndexingNonArray(expr_t))
|
}
|
||||||
|
_ => Err(ErrorKind::TriedIndexingNonArray(expr_t)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ExprKind::Array(expressions) => {
|
ExprKind::Array(expressions) => {
|
||||||
|
@ -267,7 +267,7 @@ impl Expression {
|
|||||||
// need for further resolution.
|
// need for further resolution.
|
||||||
let kind = expr_ty.resolve_weak().unwrap();
|
let kind = expr_ty.resolve_weak().unwrap();
|
||||||
match kind {
|
match kind {
|
||||||
Array(type_kind, _) => {
|
Array(type_kind, _) | Ptr(type_kind) => {
|
||||||
let elem_ty = type_refs.from_type(&type_kind).unwrap();
|
let elem_ty = type_refs.from_type(&type_kind).unwrap();
|
||||||
*index_ty = elem_ty.as_type().clone();
|
*index_ty = elem_ty.as_type().clone();
|
||||||
Ok(elem_ty)
|
Ok(elem_ty)
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
extern fn malloc(size: u64) -> *u8;
|
extern fn malloc(size: u64) -> *u8;
|
||||||
|
|
||||||
fn main() -> u16 {
|
fn main() -> u8 {
|
||||||
let ptr = malloc(4);
|
let ptr = malloc(4);
|
||||||
|
|
||||||
return ptr[0];
|
return ptr[0];
|
||||||
|
Loading…
Reference in New Issue
Block a user