Skip to content

Commit aedc488

Browse files
committed
always restore indent level to zero at the beginning of each worksheet block
1 parent 115efff commit aedc488

File tree

2 files changed

+54
-43
lines changed

2 files changed

+54
-43
lines changed

hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,27 @@ trait LoweringSelSanityChecks
367367
trait LoweringTraceLog
368368
(instrument: Bool)(using TL, Raise, Elaborator.State)
369369
extends Lowering:
370+
371+
private def selFromGlobalThis(path: Str*): Path =
372+
path.foldLeft[Path](Value.Ref(State.globalThisSymbol)):
373+
(qual, name) => Select(qual, Tree.Ident(name))(N)
374+
375+
private def assignStmts(stmts: (Local, Result)*)(rest: Block) =
376+
stmts.foldRight(rest):
377+
case ((sym, res), acc) => Assign(sym, res, acc)
378+
379+
private val traceLogFn = selFromGlobalThis("Predef", "TraceLogger", "log")
380+
private val traceLogIndentFn = selFromGlobalThis("Predef", "TraceLogger", "indent")
381+
private val traceLogResetFn = selFromGlobalThis("Predef", "TraceLogger", "resetIndent")
382+
private val strConcatFn = selFromGlobalThis("String", "prototype", "concat", "call")
383+
384+
override def topLevel(t: st): Block =
385+
if instrument then
386+
assignStmts(
387+
TempSymbol(N) -> Call(traceLogResetFn, Arg(false, Value.Lit(Tree.IntLit(0))) :: Nil),
388+
)(term(t)(ImplctRet)(using Subst.empty))
389+
else
390+
super.topLevel(t)
370391

371392
override def setupFunctionDef(paramLists: List[ParamList], bodyTerm: st, name: Option[Str])(using Subst): (List[ParamList], Block) =
372393
if instrument then
@@ -385,20 +406,6 @@ trait LoweringTraceLog
385406
go(paramLists.reverse, bod)
386407

387408
def setupFunctionBody(params: ParamList, bod: Term, name: Option[Str])(using Subst): Block =
388-
389-
def selFromGlobalThis(path: Str*): Path =
390-
path.foldLeft[Path](Value.Ref(State.globalThisSymbol)):
391-
(qual, name) => Select(qual, Tree.Ident(name))(N)
392-
393-
def assignStmts(stmts: (Local, Result)*)(rest: Block) =
394-
stmts.foldRight(rest):
395-
case ((sym, res), acc) => Assign(sym, res, acc)
396-
397-
val traceLogFn = selFromGlobalThis("Predef", "TraceLogger", "log")
398-
val traceLogIndentFn = selFromGlobalThis("Predef", "TraceLogger", "indent")
399-
val traceLogResetFn = selFromGlobalThis("Predef", "TraceLogger", "resetIndent")
400-
val strConcatFn = selFromGlobalThis("String", "prototype", "concat", "call")
401-
402409
val enterMsgSym = TempSymbol(N, dbgNme = "traceLogEnterMsg")
403410
val prevIndentLvlSym = TempSymbol(N, dbgNme = "traceLogPrevIndent")
404411
val resSym = TempSymbol(N, dbgNme = "traceLogRes")

hkmc2/shared/src/test/mlscript/codegen/TraceLog.mls

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,29 @@ fun fib(a) = if
66
a <= 1 then a
77
else fib(a - 1) + fib(a - 2)
88
//│ JS:
9+
//│ let tmp;
10+
//│ tmp = this.Predef.TraceLogger.resetIndent(0) ?? null;
911
//│ function fib(...args) {
1012
//│ globalThis.Predef.checkArgs("fib", 1, true, args.length);
1113
//│ let a = args[0];
12-
//│ let scrut, traceLogEnterMsg, traceLogPrevIndent, traceLogRes, traceLogRetMsg, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
14+
//│ let scrut, traceLogEnterMsg, traceLogPrevIndent, traceLogRes, traceLogRetMsg, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
1315
//│ traceLogEnterMsg = globalThis.String.prototype.concat.call("calling: fib(", a, ")") ?? null;
14-
//│ tmp = globalThis.Predef.TraceLogger.log(traceLogEnterMsg) ?? null;
16+
//│ tmp1 = globalThis.Predef.TraceLogger.log(traceLogEnterMsg) ?? null;
1517
//│ traceLogPrevIndent = globalThis.Predef.TraceLogger.indent() ?? null;
1618
//│ scrut = a <= 1;
1719
//│ if (scrut) {
18-
//│ tmp1 = a;
20+
//│ tmp2 = a;
1921
//│ } else {
20-
//│ tmp2 = a - 1;
21-
//│ tmp3 = globalThis.fib(tmp2) ?? null;
22-
//│ tmp4 = a - 2;
23-
//│ tmp5 = globalThis.fib(tmp4) ?? null;
24-
//│ tmp1 = tmp3 + tmp5;
22+
//│ tmp3 = a - 1;
23+
//│ tmp4 = globalThis.fib(tmp3) ?? null;
24+
//│ tmp5 = a - 2;
25+
//│ tmp6 = globalThis.fib(tmp5) ?? null;
26+
//│ tmp2 = tmp4 + tmp6;
2527
//│ }
26-
//│ traceLogRes = tmp1;
28+
//│ traceLogRes = tmp2;
2729
//│ traceLogRetMsg = globalThis.String.prototype.concat.call("return: ", traceLogRes) ?? null;
28-
//│ tmp6 = globalThis.Predef.TraceLogger.resetIndent(traceLogPrevIndent) ?? null;
29-
//│ tmp7 = globalThis.Predef.TraceLogger.log(traceLogRetMsg) ?? null;
30+
//│ tmp7 = globalThis.Predef.TraceLogger.resetIndent(traceLogPrevIndent) ?? null;
31+
//│ tmp8 = globalThis.Predef.TraceLogger.log(traceLogRetMsg) ?? null;
3032
//│ return traceLogRes;
3133
//│ }
3234
//│ undefined
@@ -86,32 +88,34 @@ fun g(z) =
8688
fun f(x, y)(z) =
8789
x + y + g(z)
8890
//│ JS:
91+
//│ let tmp;
92+
//│ tmp = this.Predef.TraceLogger.resetIndent(0) ?? null;
8993
//│ function f(...args) {
9094
//│ globalThis.Predef.checkArgs("f", 2, true, args.length);
9195
//│ let x = args[0];
9296
//│ let y = args[1];
93-
//│ let traceLogEnterMsg, traceLogPrevIndent, traceLogRes, traceLogRetMsg, tmp, tmp1, tmp2;
97+
//│ let traceLogEnterMsg, traceLogPrevIndent, traceLogRes, traceLogRetMsg, tmp1, tmp2, tmp3;
9498
//│ traceLogEnterMsg = globalThis.String.prototype.concat.call("calling: f(", x, ", ", y, ")") ?? null;
95-
//│ tmp = globalThis.Predef.TraceLogger.log(traceLogEnterMsg) ?? null;
99+
//│ tmp1 = globalThis.Predef.TraceLogger.log(traceLogEnterMsg) ?? null;
96100
//│ traceLogPrevIndent = globalThis.Predef.TraceLogger.indent() ?? null;
97101
//│ traceLogRes = (...args1) => {
98102
//│ globalThis.Predef.checkArgs("", 1, true, args1.length);
99103
//│ let z = args1[0];
100-
//│ let traceLogEnterMsg1, traceLogPrevIndent1, traceLogRes1, traceLogRetMsg1, tmp3, tmp4, tmp5, tmp6, tmp7;
104+
//│ let traceLogEnterMsg1, traceLogPrevIndent1, traceLogRes1, traceLogRetMsg1, tmp4, tmp5, tmp6, tmp7, tmp8;
101105
//│ traceLogEnterMsg1 = globalThis.String.prototype.concat.call("calling: [arrow function](", z, ")") ?? null;
102-
//│ tmp3 = globalThis.Predef.TraceLogger.log(traceLogEnterMsg1) ?? null;
106+
//│ tmp4 = globalThis.Predef.TraceLogger.log(traceLogEnterMsg1) ?? null;
103107
//│ traceLogPrevIndent1 = globalThis.Predef.TraceLogger.indent() ?? null;
104-
//│ tmp4 = x + y;
105-
//│ tmp5 = globalThis.g(z) ?? null;
106-
//│ traceLogRes1 = tmp4 + tmp5;
108+
//│ tmp5 = x + y;
109+
//│ tmp6 = globalThis.g(z) ?? null;
110+
//│ traceLogRes1 = tmp5 + tmp6;
107111
//│ traceLogRetMsg1 = globalThis.String.prototype.concat.call("return: ", traceLogRes1) ?? null;
108-
//│ tmp6 = globalThis.Predef.TraceLogger.resetIndent(traceLogPrevIndent1) ?? null;
109-
//│ tmp7 = globalThis.Predef.TraceLogger.log(traceLogRetMsg1) ?? null;
112+
//│ tmp7 = globalThis.Predef.TraceLogger.resetIndent(traceLogPrevIndent1) ?? null;
113+
//│ tmp8 = globalThis.Predef.TraceLogger.log(traceLogRetMsg1) ?? null;
110114
//│ return traceLogRes1;
111115
//│ };
112116
//│ traceLogRetMsg = globalThis.String.prototype.concat.call("return: ", traceLogRes) ?? null;
113-
//│ tmp1 = globalThis.Predef.TraceLogger.resetIndent(traceLogPrevIndent) ?? null;
114-
//│ tmp2 = globalThis.Predef.TraceLogger.log(traceLogRetMsg) ?? null;
117+
//│ tmp2 = globalThis.Predef.TraceLogger.resetIndent(traceLogPrevIndent) ?? null;
118+
//│ tmp3 = globalThis.Predef.TraceLogger.log(traceLogRetMsg) ?? null;
115119
//│ return traceLogRes;
116120
//│ }
117121
//│ undefined
@@ -120,12 +124,12 @@ fun f(x, y)(z) =
120124
f(1,2)(0)
121125
//│ ═══[RUNTIME ERROR] Error: oops
122126

123-
// FIXME: always restore indent level at the beginning of each worksheet block
127+
124128
f(1,1)(1)
125-
//│ > | | calling: f(1, 1)
126-
//│ > | | return: (...args1) => { globalThis.Predef.checkArgs("", 1, true, args1.length); let z = args1[0]; let traceLogEnterMsg1, traceLogPrevIndent1, traceLogRes1, traceLogRetMsg1, tmp3, tmp4, tmp5, tmp6, tmp7; traceLogEnterMsg1 = globalThis.String.prototype.concat.call("calling: [arrow function](", z, ")") ?? null; tmp3 = globalThis.Predef.TraceLogger.log(traceLogEnterMsg1) ?? null; traceLogPrevIndent1 = globalThis.Predef.TraceLogger.indent() ?? null; tmp4 = x + y; tmp5 = globalThis.g(z) ?? null; traceLogRes1 = tmp4 + tmp5; traceLogRetMsg1 = globalThis.String.prototype.concat.call("return: ", traceLogRes1) ?? null; tmp6 = globalThis.Predef.TraceLogger.resetIndent(traceLogPrevIndent1) ?? null; tmp7 = globalThis.Predef.TraceLogger.log(traceLogRetMsg1) ?? null; return traceLogRes1; }
127-
//│ > | | calling: [arrow function](1)
128-
//│ > | | | calling: g(1)
129-
//│ > | | | return: 0
130-
//│ > | | return: 2
129+
//│ > calling: f(1, 1)
130+
//│ > return: (...args1) => { globalThis.Predef.checkArgs("", 1, true, args1.length); let z = args1[0]; let traceLogEnterMsg1, traceLogPrevIndent1, traceLogRes1, traceLogRetMsg1, tmp4, tmp5, tmp6, tmp7, tmp8; traceLogEnterMsg1 = globalThis.String.prototype.concat.call("calling: [arrow function](", z, ")") ?? null; tmp4 = globalThis.Predef.TraceLogger.log(traceLogEnterMsg1) ?? null; traceLogPrevIndent1 = globalThis.Predef.TraceLogger.indent() ?? null; tmp5 = x + y; tmp6 = globalThis.g(z) ?? null; traceLogRes1 = tmp5 + tmp6; traceLogRetMsg1 = globalThis.String.prototype.concat.call("return: ", traceLogRes1) ?? null; tmp7 = globalThis.Predef.TraceLogger.resetIndent(traceLogPrevIndent1) ?? null; tmp8 = globalThis.Predef.TraceLogger.log(traceLogRetMsg1) ?? null; return traceLogRes1; }
131+
//│ > calling: [arrow function](1)
132+
//│ > | calling: g(1)
133+
//│ > | return: 0
134+
//│ > return: 2
131135
//│ = 2

0 commit comments

Comments
 (0)