Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/compiler_typing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeReference>, Option<TypeReference>);
Expand Down
42 changes: 42 additions & 0 deletions compiler/compiler_typing/src/stated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +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 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(())
}
}