INITIAL GENERICS IMPL

This commit is contained in:
2025-04-15 03:21:57 -04:00
parent 993458f4be
commit 329b1d86ac
18 changed files with 607 additions and 381 deletions

View File

@@ -1,6 +1,6 @@
use crate::ir::{Type, UProgram, UStruct, UVar, VarInst};
use crate::ir::{Type, UProgram, UVar, VarInst};
use super::{CompilerMsg, CompilerOutput, FileSpan, Node, PType, PVarDef};
use super::{CompilerOutput, Node, PVarDef};
impl Node<PVarDef> {
pub fn lower(&self, program: &mut UProgram, output: &mut CompilerOutput) -> Option<VarInst> {
@@ -27,54 +27,3 @@ impl Node<PVarDef> {
})
}
}
impl Node<PType> {
pub fn lower(&self, namespace: &mut UProgram, output: &mut CompilerOutput) -> Type {
self.as_ref()
.map(|t| t.lower(namespace, output, self.span))
.unwrap_or(Type::Error)
}
}
impl PType {
pub fn lower(
&self,
namespace: &mut UProgram,
output: &mut CompilerOutput,
span: FileSpan,
) -> Type {
let Some(name) = self.name.as_ref() else {
return Type::Error;
};
match namespace
.get_idents(name)
.and_then(|ids| ids.get::<UStruct>())
{
Some(id) => {
let args = self
.args
.iter()
.map(|n| n.lower(namespace, output))
.collect();
Type::Struct { id, args }
}
None => {
if let Ok(num) = name.parse::<u32>() {
Type::Bits(num)
} else {
match name.as_str() {
"slice" => {
let inner = self.args[0].lower(namespace, output);
Type::Slice(Box::new(inner))
}
"_" => Type::Infer,
_ => {
output.err(CompilerMsg::from_span(span, "Type not found".to_string()));
Type::Error
}
}
}
}
}
}
}