From 9926208b4aa74d724e7a7d39af5eaafcc06f5604 Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Sat, 27 Aug 2022 23:21:05 -0600 Subject: [PATCH] Add compact UIR text representation --- drimc_rs/src/ir_untyped.rs | 103 ++++++++++++++++++++++++++++++++++++- drimc_rs/src/main.rs | 2 +- drimc_rs/uir_test.drim | 4 +- 3 files changed, 105 insertions(+), 4 deletions(-) diff --git a/drimc_rs/src/ir_untyped.rs b/drimc_rs/src/ir_untyped.rs index 2affdce..ef2d651 100644 --- a/drimc_rs/src/ir_untyped.rs +++ b/drimc_rs/src/ir_untyped.rs @@ -25,6 +25,18 @@ pub struct Program { defs: Vec<(Identifier, Vec)>, } +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 Vec { 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); diff --git a/drimc_rs/src/main.rs b/drimc_rs/src/main.rs index 1273d60..65d134e 100644 --- a/drimc_rs/src/main.rs +++ b/drimc_rs/src/main.rs @@ -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(()) } diff --git a/drimc_rs/uir_test.drim b/drimc_rs/uir_test.drim index c1c973f..8a872c1 100644 --- a/drimc_rs/uir_test.drim +++ b/drimc_rs/uir_test.drim @@ -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;