refactor(sema): Separated from ir

This commit is contained in:
2026-05-31 20:59:18 +08:00
parent 1eca4a225b
commit c42575c1c6
10 changed files with 829 additions and 260 deletions
+51
View File
@@ -0,0 +1,51 @@
use std::fmt::Display;
use crate::{ast::types::Type as AstType, ir::types::IRType};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SemaType {
I32,
I1,
Void,
}
impl SemaType {
pub fn get_elevate_result(lhs: SemaType, rhs: SemaType) -> Option<SemaType> {
if lhs == rhs {
Some(lhs)
} else if (lhs == SemaType::I32 && rhs == SemaType::I1) || (lhs == SemaType::I1 && rhs == SemaType::I32) {
Some(SemaType::I32)
} else {
None
}
}
}
impl Display for SemaType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SemaType::I32 => write!(f, "i32"),
SemaType::I1 => write!(f, "i1"),
SemaType::Void => write!(f, "void"),
}
}
}
impl From<AstType> for SemaType {
fn from(value: AstType) -> Self {
match value {
AstType::Int => SemaType::I32,
AstType::Void => SemaType::Void,
}
}
}
impl From<SemaType> for IRType {
fn from(value: SemaType) -> Self {
match value {
SemaType::I32 => IRType::I32,
SemaType::I1 => IRType::I1,
SemaType::Void => IRType::Void,
}
}
}