type checking !?!?
This commit is contained in:
@@ -1,31 +1,48 @@
|
||||
use super::{func::FnLowerCtx, FnLowerable, PExpr, UnaryOp};
|
||||
use crate::ir::{IRUInstruction, Size, Type, VarID};
|
||||
use crate::ir::{DataDef, IRUInstruction, Origin, Type, VarInst};
|
||||
|
||||
impl FnLowerable for PExpr {
|
||||
type Output = VarID;
|
||||
fn lower(&self, ctx: &mut FnLowerCtx) -> Option<VarID> {
|
||||
type Output = VarInst;
|
||||
fn lower(&self, ctx: &mut FnLowerCtx) -> Option<VarInst> {
|
||||
Some(match self {
|
||||
PExpr::Lit(l) => match l.as_ref()? {
|
||||
super::PLiteral::String(s) => {
|
||||
let dest = ctx.map.temp_var(l.span, Type::Bits(8).slice());
|
||||
let data = s.as_bytes().to_vec();
|
||||
let len = data.len() as Size;
|
||||
let src = ctx.map.def_data(data);
|
||||
ctx.push(IRUInstruction::LoadSlice { dest, src, len });
|
||||
let src = ctx.map.def_data(
|
||||
DataDef {
|
||||
ty: Type::Bits(8).arr(data.len() as u32),
|
||||
origin: Origin::File(l.span),
|
||||
},
|
||||
data,
|
||||
);
|
||||
ctx.push(IRUInstruction::LoadSlice { dest, src });
|
||||
dest
|
||||
}
|
||||
super::PLiteral::Char(c) => {
|
||||
let dest = ctx.map.temp_var(l.span, Type::Bits(8).slice());
|
||||
let src = ctx.map.def_data(c.to_string().as_bytes().to_vec());
|
||||
let ty = Type::Bits(8);
|
||||
let dest = ctx.map.temp_var(l.span, ty.clone());
|
||||
let src = ctx.map.def_data(
|
||||
DataDef {
|
||||
ty,
|
||||
origin: Origin::File(l.span),
|
||||
},
|
||||
c.to_string().as_bytes().to_vec(),
|
||||
);
|
||||
ctx.push(IRUInstruction::LoadData { dest, src });
|
||||
dest
|
||||
}
|
||||
super::PLiteral::Number(n) => {
|
||||
// TODO: temp
|
||||
let ty = Type::Bits(64);
|
||||
let dest = ctx.map.temp_var(l.span, Type::Bits(64));
|
||||
let src = ctx
|
||||
.map
|
||||
.def_data(n.whole.parse::<i64>().unwrap().to_le_bytes().to_vec());
|
||||
let src = ctx.map.def_data(
|
||||
DataDef {
|
||||
ty,
|
||||
origin: Origin::File(l.span),
|
||||
},
|
||||
n.whole.parse::<i64>().unwrap().to_le_bytes().to_vec(),
|
||||
);
|
||||
ctx.push(IRUInstruction::LoadData { dest, src });
|
||||
dest
|
||||
}
|
||||
@@ -44,7 +61,7 @@ impl FnLowerable for PExpr {
|
||||
let res = e.lower(ctx)?;
|
||||
match op {
|
||||
UnaryOp::Ref => {
|
||||
let temp = ctx.temp(ctx.map.get_var(res).ty.clone());
|
||||
let temp = ctx.temp(ctx.map.get_var(res.id).ty.clone());
|
||||
ctx.push(IRUInstruction::Ref {
|
||||
dest: temp,
|
||||
src: res,
|
||||
@@ -52,7 +69,7 @@ impl FnLowerable for PExpr {
|
||||
temp
|
||||
}
|
||||
UnaryOp::Deref => {
|
||||
let t = &ctx.map.get_var(res).ty;
|
||||
let t = &ctx.map.get_var(res.id).ty;
|
||||
let Type::Ref(inner) = t else {
|
||||
ctx.err(format!(
|
||||
"Cannot dereference type {:?}",
|
||||
@@ -77,7 +94,7 @@ impl FnLowerable for PExpr {
|
||||
let arg = arg.lower(ctx)?;
|
||||
nargs.push(arg);
|
||||
}
|
||||
let def = ctx.map.get_fn_var(fe);
|
||||
let def = ctx.map.get_fn_var(fe.id);
|
||||
let ty = match def {
|
||||
Some(def) => def.ret.clone(),
|
||||
None => {
|
||||
@@ -85,7 +102,7 @@ impl FnLowerable for PExpr {
|
||||
e.span,
|
||||
format!(
|
||||
"Expected function, found {}",
|
||||
ctx.map.type_name(&ctx.map.get_var(fe).ty)
|
||||
ctx.map.type_name(&ctx.map.get_var(fe.id).ty)
|
||||
),
|
||||
);
|
||||
Type::Error
|
||||
@@ -100,6 +117,7 @@ impl FnLowerable for PExpr {
|
||||
temp
|
||||
}
|
||||
PExpr::Group(e) => e.lower(ctx)?,
|
||||
PExpr::Construct(c) => todo!(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user