Make borrows work as function parameters as well

This commit is contained in:
Sofia 2025-07-21 10:07:56 +03:00
parent 2e829bc8a9
commit 8da32c25c5
6 changed files with 24 additions and 3 deletions

View File

@ -27,6 +27,7 @@ pub enum TypeKind {
String,
Array(Box<TypeKind>, u64),
Custom(String),
Borrow(Box<TypeKind>),
}
#[derive(Debug, Clone)]

View File

@ -24,6 +24,10 @@ impl Parse for Type {
};
stream.expect(Token::BracketClose)?;
TypeKind::Array(Box::new(inner.0), length)
} else if let Some(Token::Et) = stream.peek() {
stream.expect(Token::Et)?;
let inner = stream.parse::<Type>()?;
TypeKind::Borrow(Box::new(inner.0))
} else {
if let Some(Token::Identifier(ident)) = stream.next() {
match &*ident {

View File

@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::{path::PathBuf, process};
use crate::{
ast::{self},
@ -287,6 +287,9 @@ impl From<ast::TypeKind> for mir::TypeKind {
}
ast::TypeKind::String => mir::TypeKind::StringPtr,
ast::TypeKind::Custom(name) => mir::TypeKind::CustomType(name.clone()),
ast::TypeKind::Borrow(type_kind) => {
mir::TypeKind::Borrow(Box::new(mir::TypeKind::from(*type_kind.clone())))
}
}
}
}

View File

@ -360,7 +360,7 @@ impl mir::Module {
stack_values.insert(
p_name.clone(),
StackValue(
StackValueKind::Immutable(alloca),
StackValueKind::Mutable(alloca),
TypeKind::Ptr(Box::new(p_ty.clone())),
),
);

View File

@ -162,7 +162,7 @@ impl FunctionDefinition {
param.0.clone(),
ScopeVariable {
ty: param_t,
mutable: false,
mutable: true,
},
)
.or(Err(ErrorKind::VariableAlreadyDefined(param.0.clone())));

13
reid_src/borrow_hard.reid Normal file
View File

@ -0,0 +1,13 @@
// Arithmetic, function calls and imports!
fn changer(param: &u32) {
*param = 17;
}
fn main() -> u32 {
let mut value = 6;
changer(&value);
return value;
}