BRANCHING (TURING COMPLETE????)

This commit is contained in:
2025-03-29 15:08:15 -04:00
parent 021434d2f1
commit f57af3b2b5
25 changed files with 780 additions and 486 deletions

View File

@@ -10,9 +10,9 @@ impl FnLowerable for PExpr {
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 dest = ctx.program.temp_var(l.span, Type::Bits(8).slice());
let data = s.as_bytes().to_vec();
let src = ctx.map.def_data(
let src = ctx.program.def_data(
DataDef {
ty: Type::Bits(8).arr(data.len() as u32),
origin: Origin::File(l.span),
@@ -25,8 +25,8 @@ impl FnLowerable for PExpr {
}
super::PLiteral::Char(c) => {
let ty = Type::Bits(8);
let dest = ctx.map.temp_var(l.span, ty.clone());
let src = ctx.map.def_data(
let dest = ctx.program.temp_var(l.span, ty.clone());
let src = ctx.program.def_data(
DataDef {
ty,
origin: Origin::File(l.span),
@@ -40,8 +40,8 @@ impl FnLowerable for PExpr {
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(
let dest = ctx.program.temp_var(l.span, Type::Bits(64));
let src = ctx.program.def_data(
DataDef {
ty,
origin: Origin::File(l.span),
@@ -60,12 +60,15 @@ impl FnLowerable for PExpr {
PExpr::BinaryOp(op, e1, e2) => {
let res1 = e1.lower(ctx)?;
if *op == PInfixOp::Access {
let sty = &ctx.map.get_var(res1.id).ty;
let sty = &ctx.program.get_var(res1.id).ty;
let Type::Concrete(tid) = sty else {
ctx.err(format!("Type {:?} has no fields", ctx.map.type_name(sty)));
ctx.err(format!(
"Type {:?} has no fields",
ctx.program.type_name(sty)
));
return None;
};
let struc = ctx.map.get_struct(*tid);
let struc = ctx.program.get_struct(*tid);
let Some(box PExpr::Ident(ident)) = &e2.inner else {
ctx.err(format!("Field accesses must be identifiers",));
return None;
@@ -84,14 +87,29 @@ impl FnLowerable for PExpr {
temp
} else {
let res2 = e2.lower(ctx)?;
todo!()
match op {
PInfixOp::Add => todo!(),
PInfixOp::Sub => todo!(),
PInfixOp::Mul => todo!(),
PInfixOp::Div => todo!(),
PInfixOp::LessThan => todo!(),
PInfixOp::GreaterThan => todo!(),
PInfixOp::Access => todo!(),
PInfixOp::Assign => {
ctx.push(IRUInstruction::Mv {
dest: res1,
src: res2,
});
res1
}
}
}
}
PExpr::UnaryOp(op, e) => {
let res = e.lower(ctx)?;
match op {
UnaryOp::Ref => {
let temp = ctx.temp(ctx.map.get_var(res.id).ty.clone().rf());
let temp = ctx.temp(ctx.program.get_var(res.id).ty.clone().rf());
ctx.push(IRUInstruction::Ref {
dest: temp,
src: res,
@@ -99,11 +117,11 @@ impl FnLowerable for PExpr {
temp
}
UnaryOp::Deref => {
let t = &ctx.map.get_var(res.id).ty;
let t = &ctx.program.get_var(res.id).ty;
let Type::Ref(inner) = t else {
ctx.err(format!(
"Cannot dereference type {:?}",
ctx.map.type_name(t)
ctx.program.type_name(t)
));
return None;
};
@@ -124,7 +142,7 @@ impl FnLowerable for PExpr {
let arg = arg.lower(ctx)?;
nargs.push(arg);
}
let def = ctx.map.get_fn_var(fe.id);
let def = ctx.program.get_fn_var(fe.id);
let ty = match def {
Some(def) => def.ret.clone(),
None => {
@@ -132,7 +150,7 @@ impl FnLowerable for PExpr {
e.span,
format!(
"Expected function, found {}",
ctx.map.type_name(&ctx.map.get_var(fe.id).ty)
ctx.program.type_name(&ctx.program.get_var(fe.id).ty)
),
);
Type::Error
@@ -148,6 +166,29 @@ impl FnLowerable for PExpr {
}
PExpr::Group(e) => e.lower(ctx)?,
PExpr::Construct(c) => c.lower(ctx)?,
PExpr::If(cond, body) => {
let cond = cond.lower(ctx)?;
ctx.program.push();
let mut body_ctx = ctx.branch();
body.lower(&mut body_ctx);
let body = body_ctx.instructions;
ctx.program.pop();
ctx.push(IRUInstruction::If { cond, body });
return None;
}
PExpr::Loop(body) => {
ctx.program.push();
let mut body_ctx = ctx.branch();
body.lower(&mut body_ctx);
let body = body_ctx.instructions;
ctx.program.pop();
ctx.push(IRUInstruction::Loop { body });
return None;
}
PExpr::Break => {
ctx.push(IRUInstruction::Break);
return None;
}
})
}
}