type checking !?!?

This commit is contained in:
2025-03-22 14:40:32 -04:00
parent 606cb30c6b
commit 7f809d797c
44 changed files with 664 additions and 314 deletions

View File

@@ -1,8 +1,8 @@
use super::{FnLowerable, Node, PFunction, ParserMsg, ParserOutput};
use super::{CompilerMsg, CompilerOutput, FileSpan, FnLowerable, Node, PFunction};
use crate::{
ir::{
FileSpan, FnDef, FnID, IRInstructions, IRUFunction, IRUInstruction, Idents, NamespaceGuard,
Origin, Type, VarDef, VarID,
FnDef, FnID, IRInstructions, IRUFunction, IRUInstruction, Idents, NamespaceGuard, Origin,
Type, VarDef, VarID, VarInst,
},
parser,
};
@@ -11,7 +11,7 @@ impl Node<PFunction> {
pub fn lower_header(
&self,
map: &mut NamespaceGuard,
output: &mut ParserOutput,
output: &mut CompilerOutput,
) -> Option<FnID> {
self.as_ref()?.lower_header(map, output)
}
@@ -19,7 +19,7 @@ impl Node<PFunction> {
&self,
id: FnID,
map: &mut NamespaceGuard,
output: &mut ParserOutput,
output: &mut CompilerOutput,
) -> Option<IRUFunction> {
Some(self.as_ref()?.lower_body(id, map, output))
}
@@ -29,7 +29,7 @@ impl PFunction {
pub fn lower_header(
&self,
map: &mut NamespaceGuard,
output: &mut ParserOutput,
output: &mut CompilerOutput,
) -> Option<FnID> {
let header = self.header.as_ref()?;
let name = header.name.as_ref()?;
@@ -59,7 +59,7 @@ impl PFunction {
&self,
id: FnID,
map: &mut NamespaceGuard,
output: &mut ParserOutput,
output: &mut CompilerOutput,
) -> IRUFunction {
let mut instructions = IRInstructions::new();
let def = map.get_fn(id).clone();
@@ -71,7 +71,7 @@ impl PFunction {
span: self.body.span,
};
if let Some(src) = self.body.lower(&mut ctx) {
instructions.push(IRUInstruction::Ret { src });
instructions.push(IRUInstruction::Ret { src }, src.span);
}
IRUFunction::new(def.name.clone(), args, instructions)
}
@@ -80,7 +80,7 @@ impl PFunction {
pub struct FnLowerCtx<'a, 'n> {
pub map: &'a mut NamespaceGuard<'n>,
pub instructions: &'a mut IRInstructions,
pub output: &'a mut ParserOutput,
pub output: &'a mut CompilerOutput,
pub span: FileSpan,
}
@@ -101,7 +101,7 @@ impl<'n> FnLowerCtx<'_, 'n> {
}
res
}
pub fn get_var(&mut self, node: &Node<parser::PIdent>) -> Option<VarID> {
pub fn get_var(&mut self, node: &Node<parser::PIdent>) -> Option<VarInst> {
let ids = self.get(node)?;
if ids.var.is_none() {
self.err_at(
@@ -112,19 +112,25 @@ impl<'n> FnLowerCtx<'_, 'n> {
),
);
}
ids.var
ids.var.map(|id| VarInst {
id,
span: node.span,
})
}
pub fn err(&mut self, msg: String) {
self.output.err(ParserMsg::from_span(self.span, msg))
self.output.err(CompilerMsg::from_span(self.span, msg))
}
pub fn err_at(&mut self, span: FileSpan, msg: String) {
self.output.err(ParserMsg::from_span(span, msg))
self.output.err(CompilerMsg::from_span(span, msg))
}
pub fn temp(&mut self, ty: Type) -> VarID {
pub fn temp(&mut self, ty: Type) -> VarInst {
self.map.temp_var(self.span, ty)
}
pub fn push(&mut self, i: IRUInstruction) {
self.instructions.push(i);
self.instructions.push(i, self.span);
}
pub fn push_at(&mut self, i: IRUInstruction, span: FileSpan) {
self.instructions.push(i, span);
}
pub fn sub<'b>(&'b mut self) -> FnLowerCtx<'b, 'n> {
FnLowerCtx {