From 8da32c25c5322d389b4e593e9f9b8168b7ecd0ce Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 21 Jul 2025 10:07:56 +0300 Subject: [PATCH] Make borrows work as function parameters as well --- reid/src/ast/mod.rs | 1 + reid/src/ast/parse.rs | 4 ++++ reid/src/ast/process.rs | 5 ++++- reid/src/codegen.rs | 2 +- reid/src/mir/typecheck.rs | 2 +- reid_src/borrow_hard.reid | 13 +++++++++++++ 6 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 reid_src/borrow_hard.reid diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index 56b5a2f..665f286 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -27,6 +27,7 @@ pub enum TypeKind { String, Array(Box, u64), Custom(String), + Borrow(Box), } #[derive(Debug, Clone)] diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index 26f374d..be2d6ab 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -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::()?; + TypeKind::Borrow(Box::new(inner.0)) } else { if let Some(Token::Identifier(ident)) = stream.next() { match &*ident { diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index a871ffa..3b872b5 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::{path::PathBuf, process}; use crate::{ ast::{self}, @@ -287,6 +287,9 @@ impl From 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()))) + } } } } diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 4af97b9..6343017 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -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())), ), ); diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index b407b2e..1d5232e 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -162,7 +162,7 @@ impl FunctionDefinition { param.0.clone(), ScopeVariable { ty: param_t, - mutable: false, + mutable: true, }, ) .or(Err(ErrorKind::VariableAlreadyDefined(param.0.clone()))); diff --git a/reid_src/borrow_hard.reid b/reid_src/borrow_hard.reid new file mode 100644 index 0000000..87545b5 --- /dev/null +++ b/reid_src/borrow_hard.reid @@ -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; +}