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, String,
Array(Box<TypeKind>, u64), Array(Box<TypeKind>, u64),
Custom(String), Custom(String),
Borrow(Box<TypeKind>),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View File

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

View File

@ -1,4 +1,4 @@
use std::path::PathBuf; use std::{path::PathBuf, process};
use crate::{ use crate::{
ast::{self}, ast::{self},
@ -287,6 +287,9 @@ impl From<ast::TypeKind> for mir::TypeKind {
} }
ast::TypeKind::String => mir::TypeKind::StringPtr, ast::TypeKind::String => mir::TypeKind::StringPtr,
ast::TypeKind::Custom(name) => mir::TypeKind::CustomType(name.clone()), 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( stack_values.insert(
p_name.clone(), p_name.clone(),
StackValue( StackValue(
StackValueKind::Immutable(alloca), StackValueKind::Mutable(alloca),
TypeKind::Ptr(Box::new(p_ty.clone())), TypeKind::Ptr(Box::new(p_ty.clone())),
), ),
); );

View File

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