Compare commits

...

3 Commits

Author SHA1 Message Date
9a4f0dc5d8 Allow for cloning inner values 2025-07-17 00:05:37 +03:00
965ad5797f Implement rudamentary borrow 2025-07-16 23:49:28 +03:00
e4ce897f94 Fix expression-indexing 2025-07-16 23:29:15 +03:00
7 changed files with 59 additions and 18 deletions

View File

@ -265,7 +265,7 @@ impl mir::Statement {
) -> Option<InstructionValue> { ) -> Option<InstructionValue> {
match &self.0 { match &self.0 {
mir::StmtKind::Let(NamedVariableRef(ty, name, _), mutable, expression) => { mir::StmtKind::Let(NamedVariableRef(ty, name, _), mutable, expression) => {
let value = expression.codegen(scope, state).unwrap(); let value = expression.codegen(scope, &state.load(true)).unwrap();
scope.stack_values.insert( scope.stack_values.insert(
name.clone(), name.clone(),
StackValue( StackValue(
@ -415,8 +415,14 @@ impl mir::Expression {
} }
} }
mir::ExprKind::Indexed(expression, val_t, idx_expr) => { mir::ExprKind::Indexed(expression, val_t, idx_expr) => {
let array = expression.codegen(scope, state)?; dbg!(&expression, &idx_expr);
let idx = idx_expr.codegen(scope, state)?; let array = expression
.codegen(scope, state)
.expect("array returned none!");
let idx = idx_expr
.codegen(scope, state)
.expect("index returned none!");
let mut ptr = scope let mut ptr = scope
.block .block
.build(Instr::GetElemPtr(array, vec![idx])) .build(Instr::GetElemPtr(array, vec![idx]))
@ -468,14 +474,19 @@ impl mir::Expression {
Some(array) Some(array)
} }
mir::ExprKind::Accessed(expression, type_kind, field) => { mir::ExprKind::Accessed(expression, type_kind, field) => {
let struct_val = expression.codegen(scope, &mut state.load(true))?; let struct_val = expression.codegen(scope, &mut state.load(true)).unwrap();
let struct_ty = expression.return_type().ok()?.1.known().ok()?; let struct_ty = expression
let TypeKind::CustomType(name) = struct_ty else { .return_type()
return None; .map(|r| r.1.known())
.unwrap()
.unwrap();
let TypeKind::CustomType(name) = struct_ty.deref_borrow() else {
panic!("tried accessing non-custom-type");
}; };
let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&name)?; let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&name).unwrap();
let idx = struct_ty.find_index(field)?; let idx = struct_ty.find_index(field).unwrap();
let mut value = scope let mut value = scope
.block .block
@ -644,6 +655,9 @@ impl TypeKind {
TypeDefinitionKind::Struct(_) => Type::Ptr(Box::new(custom_t)), TypeDefinitionKind::Struct(_) => Type::Ptr(Box::new(custom_t)),
} }
} }
TypeKind::Borrow(type_kind) => {
Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs)))
}
} }
} }
} }

View File

@ -149,7 +149,7 @@ impl Expression {
Indexed(expression, _, _) => { Indexed(expression, _, _) => {
let expr_type = expression.return_type()?; let expr_type = expression.return_type()?;
if let (_, TypeKind::Array(elem_ty, _)) = expr_type { if let (_, TypeKind::Array(elem_ty, _)) = expr_type {
Ok((ReturnKind::Soft, *elem_ty)) Ok((ReturnKind::Soft, TypeKind::Borrow(Box::new(*elem_ty))))
} else { } else {
Err(ReturnTypeOther::IndexingNonArray(expression.1)) Err(ReturnTypeOther::IndexingNonArray(expression.1))
} }
@ -165,7 +165,10 @@ impl Expression {
TypeKind::Array(Box::new(first.1), expressions.len() as u64), TypeKind::Array(Box::new(first.1), expressions.len() as u64),
)) ))
} }
Accessed(_, type_kind, _) => Ok((ReturnKind::Soft, type_kind.clone())), Accessed(_, type_kind, _) => Ok((
ReturnKind::Soft,
TypeKind::Borrow(Box::new(type_kind.clone())),
)),
Struct(name, _) => Ok((ReturnKind::Soft, TypeKind::CustomType(name.clone()))), Struct(name, _) => Ok((ReturnKind::Soft, TypeKind::CustomType(name.clone()))),
} }
} }
@ -267,6 +270,13 @@ impl TypeKind {
_ => resolved, _ => resolved,
} }
} }
pub fn deref_borrow(&self) -> TypeKind {
match self {
TypeKind::Borrow(type_kind) => *type_kind.clone(),
_ => self.clone(),
}
}
} }
#[derive(Debug, Clone, thiserror::Error)] #[derive(Debug, Clone, thiserror::Error)]

View File

@ -65,6 +65,8 @@ pub enum TypeKind {
StringPtr, StringPtr,
#[error("[{0}; {1}]")] #[error("[{0}; {1}]")]
Array(Box<TypeKind>, u64), Array(Box<TypeKind>, u64),
#[error("Borrow({0})")]
Borrow(Box<TypeKind>),
#[error("{0}")] #[error("{0}")]
CustomType(String), CustomType(String),
#[error(transparent)] #[error(transparent)]

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,13 @@ 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;
let mut b = value[val1];
b.second[2] = 99;
return value[0].second[2]; return value[0].second[2];
} }