Add compact UIR text representation
This commit is contained in:
parent
2607722686
commit
9926208b4a
|
@ -25,6 +25,18 @@ pub struct Program {
|
|||
defs: Vec<(Identifier, Vec<Instruction>)>,
|
||||
}
|
||||
|
||||
impl Display for Program {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for def in self.defs.iter() {
|
||||
writeln!(f, "PR {}", def.0)?;
|
||||
for inst in def.1.iter() {
|
||||
writeln!(f, "{}", inst)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// An instruction in untyped IR.
|
||||
#[derive(Debug)]
|
||||
enum Instruction {
|
||||
|
@ -135,6 +147,81 @@ enum Instruction {
|
|||
},
|
||||
}
|
||||
|
||||
impl Display for Instruction {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Instruction::Apply {
|
||||
target,
|
||||
func,
|
||||
argument,
|
||||
} => write!(f, "AP {target} <- {func} {argument}"),
|
||||
Instruction::Collect { target, source } => {
|
||||
write!(f, "CL {target} <- ")?;
|
||||
for s in source {
|
||||
write!(f, "{s},")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Instruction::Branch {
|
||||
target,
|
||||
constructor,
|
||||
iftrue,
|
||||
iffalse,
|
||||
} => {
|
||||
writeln!(f, "IF {constructor} = {target} THEN [")?;
|
||||
for inst in iftrue {
|
||||
writeln!(f, "{inst}")?;
|
||||
}
|
||||
writeln!(f, "] ELSE [")?;
|
||||
for inst in iffalse {
|
||||
writeln!(f, "{inst}")?;
|
||||
}
|
||||
write!(f, "] ENDIF")?;
|
||||
Ok(())
|
||||
}
|
||||
Instruction::DestructureData {
|
||||
source,
|
||||
constructor,
|
||||
targets,
|
||||
} => {
|
||||
write!(f, "DS {constructor} (")?;
|
||||
for target in targets {
|
||||
write!(f, "{target},")?;
|
||||
}
|
||||
write!(f, ") << {source}")?;
|
||||
Ok(())
|
||||
}
|
||||
Instruction::DestructureTuple { source, targets } => {
|
||||
write!(f, "DT (")?;
|
||||
for target in targets {
|
||||
write!(f, "{target},")?;
|
||||
}
|
||||
write!(f, ") <- {source}")?;
|
||||
Ok(())
|
||||
}
|
||||
Instruction::DefLambda {
|
||||
target,
|
||||
param,
|
||||
body,
|
||||
} => {
|
||||
writeln!(f, "DL {target} <- \\{param} [")?;
|
||||
for inst in body {
|
||||
writeln!(f, "{}", inst)?;
|
||||
}
|
||||
write!(f, "]")?;
|
||||
Ok(())
|
||||
},
|
||||
Instruction::Return { target } => {
|
||||
write!(f, "RT {target}")
|
||||
},
|
||||
Instruction::Unreachable => {
|
||||
write!(f, "UN")
|
||||
},
|
||||
Instruction::FixType { target, typ } => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A storage location in untyped IR.
|
||||
#[derive(Debug, Clone)]
|
||||
enum Location {
|
||||
|
@ -287,11 +374,23 @@ fn bind_pattern(counter: &mut u64, p: syntax::Pattern, l: &Location) -> Vec<Inst
|
|||
fn eval_expr(counter: &mut u64, e: syntax::Expr, l: &Location) -> Vec<Instruction> {
|
||||
match e {
|
||||
syntax::Expr::BinaryOp {
|
||||
kind,
|
||||
kind: _,
|
||||
left,
|
||||
right,
|
||||
translation,
|
||||
} => todo!(),
|
||||
} => eval_expr(
|
||||
counter,
|
||||
syntax::Expr::Application {
|
||||
func: Box::new(syntax::Expr::Application {
|
||||
func: Box::new(syntax::Expr::VariableReference(Identifier {
|
||||
elems: vec![translation],
|
||||
})),
|
||||
argument: left,
|
||||
}),
|
||||
argument: right,
|
||||
},
|
||||
l,
|
||||
),
|
||||
syntax::Expr::Application { func, argument } => {
|
||||
let func_e = temporary(counter);
|
||||
let arg_e = temporary(counter);
|
||||
|
|
|
@ -263,7 +263,7 @@ fn main() {
|
|||
let ast = chumsky::Parser::parse(&parser(&meta), source).map_err(ParserError)?;
|
||||
let untyped_ir = ast_to_untyped_ir(ast);
|
||||
|
||||
println!("{untyped_ir:#?}");
|
||||
println!("{untyped_ir}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// IR Test
|
||||
|
||||
def nul (a, b) = sin a;
|
||||
// def nul (a, b) = sin a;
|
||||
|
||||
def add (a, b) = a + b * c;
|
||||
|
|
Loading…
Reference in a new issue