From 83d878fa588f74f0b7c12ab1d15b67bec29cf2c0 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Sat, 21 Mar 2026 15:15:57 +0100 Subject: [PATCH 1/2] feat: added StatedType for inferrence --- compiler/compiler_typing/src/lib.rs | 1 + compiler/compiler_typing/src/stated.rs | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 compiler/compiler_typing/src/stated.rs diff --git a/compiler/compiler_typing/src/lib.rs b/compiler/compiler_typing/src/lib.rs index 7c4851e8..a8f64953 100644 --- a/compiler/compiler_typing/src/lib.rs +++ b/compiler/compiler_typing/src/lib.rs @@ -9,6 +9,7 @@ pub mod enums; pub mod references; pub mod utils; pub mod storage; +pub mod stated; /// A function contained within a type. pub type TypedFunction = (Vec, Option); diff --git a/compiler/compiler_typing/src/stated.rs b/compiler/compiler_typing/src/stated.rs new file mode 100644 index 00000000..0ccde00e --- /dev/null +++ b/compiler/compiler_typing/src/stated.rs @@ -0,0 +1,9 @@ +//! Declarations for stated types. + +use crate::tree::Type; + +/// Represents a variable type. Can either be inferred or fully enforced +pub struct StatedType { + pub raw_type: Type, + pub infered: bool +} \ No newline at end of file From 2bd79c781dd34526802807604cbb4ac18c2c2865 Mon Sep 17 00:00:00 2001 From: Zffu <103074097+Zffu@users.noreply.github.com> Date: Sat, 21 Mar 2026 15:23:31 +0100 Subject: [PATCH 2/2] feat: added type inference --- compiler/compiler_typing/src/stated.rs | 35 +++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/compiler/compiler_typing/src/stated.rs b/compiler/compiler_typing/src/stated.rs index 0ccde00e..456a7a9c 100644 --- a/compiler/compiler_typing/src/stated.rs +++ b/compiler/compiler_typing/src/stated.rs @@ -1,9 +1,42 @@ //! Declarations for stated types. +use compiler_errors::{IR_TRANSMUTATION, errs::{BaseResult, base::BaseError}}; + use crate::tree::Type; /// Represents a variable type. Can either be inferred or fully enforced pub struct StatedType { pub raw_type: Type, - pub infered: bool + pub inferred: bool +} + +impl StatedType { + pub fn make_inferred(raw: Type) -> Self { + StatedType { raw_type: raw, inferred: true } + } + + pub fn make_enforced(raw: Type) -> Self { + StatedType { raw_type: raw, inferred: false } + } + + /// Infers the contained type based on the given required type. + /// + /// # Usage + /// This function should be used each time the type is checked + /// + /// # Transmutation & Inference + /// The role of this function is to handle type inference. Whenever a type that is compatible with the inferred type is passed, the enforced type will become the required one. + /// This will be used with things like constants to avoid using the numerical type suffix (eg: `_s32`) + pub fn infer(&mut self, raw_other: &Type) -> BaseResult<()> { + if !self.raw_type.can_transmute(raw_other) { + return Err(BaseError::err(IR_TRANSMUTATION!().to_string())); + } + + if self.inferred { + self.raw_type = raw_other.clone(); + self.inferred = false; + } + + return Ok(()) + } } \ No newline at end of file