Skip to content

Commit

Permalink
Fix pprint
Browse files Browse the repository at this point in the history
  • Loading branch information
samestep committed Jul 28, 2023
1 parent 3848739 commit f127245
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 52 deletions.
44 changes: 19 additions & 25 deletions crates/web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,12 @@ pub fn pprint(f: &Func) -> Result<String, JsError> {
rose::Binop::Mul => writeln!(&mut s, "x{} * x{}", left.var(), right.var())?,
rose::Binop::Div => writeln!(&mut s, "x{} / x{}", left.var(), right.var())?,
},
rose::Expr::Select { cond, then, els } => {
writeln!(&mut s, "x{} ? x{} : x{}", cond.var(), then.var(), els.var())?
}
rose::Expr::Call { func, arg } => {
writeln!(&mut s, "f{}(x{})", func.func(), arg.var())?
}
rose::Expr::If { cond, then, els } => {
writeln!(&mut s, "if x{} {{", cond.var())?;
for _ in 0..spaces {
write!(&mut s, " ")?;
}
let x = def.blocks[then.block()].arg.var();
writeln!(&mut s, " x{x}: T{}", def.vars[x].ty())?;
print_block(s, def, spaces + 2, *then)?;
for _ in 0..spaces {
write!(&mut s, " ")?;
}
writeln!(&mut s, "}} else {{")?;
for _ in 0..spaces {
write!(&mut s, " ")?;
}
let y = def.blocks[els.block()].arg.var();
writeln!(&mut s, " x{y}: T{}", def.vars[y].ty())?;
print_block(s, def, spaces + 2, *els)?;
for _ in 0..spaces {
write!(&mut s, " ")?;
}
writeln!(&mut s, "}}")?
}
rose::Expr::For { index, body } => {
writeln!(
&mut s,
Expand All @@ -163,8 +143,21 @@ pub fn pprint(f: &Func) -> Result<String, JsError> {
}
writeln!(&mut s, "}}")?
}
rose::Expr::Accum { var, vector, body } => {
writeln!(&mut s, "accum x{}: T{} {{", var.var(), vector.ty())?;
rose::Expr::Read { var, body } => {
writeln!(&mut s, "read x{} {{", var.var())?;
for _ in 0..spaces {
write!(&mut s, " ")?;
}
let x = def.blocks[body.block()].arg.var();
writeln!(&mut s, " x{x}: T{}", def.vars[x].ty())?;
print_block(s, def, spaces + 2, *body)?;
for _ in 0..spaces {
write!(&mut s, " ")?;
}
writeln!(&mut s, "}}")?
}
rose::Expr::Accum { var, shape, body } => {
writeln!(&mut s, "accum x{} from x{} {{", var.var(), shape.var())?;
for _ in 0..spaces {
write!(&mut s, " ")?;
}
Expand All @@ -176,6 +169,7 @@ pub fn pprint(f: &Func) -> Result<String, JsError> {
}
writeln!(&mut s, "}}")?
}
rose::Expr::Ask { var } => writeln!(&mut s, "ask x{}", var.var())?,
rose::Expr::Add { accum, addend } => {
writeln!(&mut s, "x{} += x{}", accum.var(), addend.var())?
}
Expand Down
39 changes: 12 additions & 27 deletions packages/core/src/debug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
abs,
add,
and,
cond,
div,
eq,
fn,
Expand All @@ -20,6 +19,7 @@ import {
neq,
not,
or,
select,
sqrt,
sub,
xor,
Expand All @@ -38,17 +38,9 @@ describe("pprint", () => {
test("if", () => {
const f = fn([Real, Real], Real, (x, y) => {
const p = lt(x, y);
const z = cond(
p,
() => {
const a = mul(x, y);
return add(a, x);
},
() => {
const b = sub(y, x);
return mul(b, y);
},
);
const a = mul(x, y);
const b = sub(y, x);
const z = select(p, add(a, x), mul(b, y));
const w = add(z, x);
return add(y, w);
});
Expand All @@ -58,25 +50,18 @@ describe("pprint", () => {
T0 = Bool
T1 = F64
T2 = (T1, T1)
T3 = Unit
x0: T2 -> T1 {
x1: T1 = x0.0
x2: T1 = x0.1
x3: T0 = x1 < x2
x10: T1 = if x3 {
x4: T3
x5: T1 = x1 * x2
x6: T1 = x5 + x1
x6
} else {
x7: T3
x8: T1 = x2 - x1
x9: T1 = x8 * x2
x9
}
x11: T1 = x10 + x1
x12: T1 = x2 + x11
x12
x4: T1 = x1 * x2
x5: T1 = x2 - x1
x6: T1 = x4 + x1
x7: T1 = x5 * x2
x8: T1 = x3 ? x6 : x7
x9: T1 = x8 + x1
x10: T1 = x2 + x9
x10
}
`.trimStart(),
);
Expand Down

0 comments on commit f127245

Please sign in to comment.