Skip to content

Commit 179862d

Browse files
committed
wip: add missing trace for methods
need to fix: avoid "toString()"
1 parent aedc488 commit 179862d

File tree

2 files changed

+103
-6
lines changed

2 files changed

+103
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ class Lowering(using TL, Raise, Elaborator.State):
128128
Define(ClsLikeDefn(cls.sym, syntax.Cls,
129129
mtds.flatMap: td =>
130130
td.body.map: bod =>
131-
FunDefn(td.sym, td.params, term(bod)(Ret))
131+
val (paramLists, bodyBlock) = setupFunctionDef(td.params, bod, S(td.sym.nme))
132+
FunDefn(td.sym, paramLists, bodyBlock)
132133
,
133134
privateFlds,
134135
publicFlds,
@@ -411,11 +412,11 @@ trait LoweringTraceLog
411412
val resSym = TempSymbol(N, dbgNme = "traceLogRes")
412413
val retMsgSym = TempSymbol(N, dbgNme = "traceLogRetMsg")
413414

414-
val psSyms = params.params.zipWithIndex.flatMap:
415-
(p, i) => if i == params.params.length - 1
416-
then Arg(false, Value.Ref((p.sym))) :: Arg(false, Value.Lit(Tree.StrLit(")"))) :: Nil
417-
else Arg(false, Value.Ref((p.sym))) :: Arg(false, Value.Lit(Tree.StrLit(", "))) :: Nil
418-
415+
val psSyms = params.params.zipWithIndex.foldRight[Ls[Arg]](Arg(false, Value.Lit(Tree.StrLit(")"))) :: Nil){
416+
case ((p, i), acc) => if i == params.params.length - 1
417+
then Arg(false, Value.Ref((p.sym))) :: acc
418+
else Arg(false, Value.Ref((p.sym))) :: Arg(false, Value.Lit(Tree.StrLit(", "))) :: acc
419+
}
419420

420421
assignStmts(
421422
enterMsgSym -> Call(

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,99 @@ f(1,1)(1)
133133
//│ > | return: 0
134134
//│ > return: 2
135135
//│ = 2
136+
137+
138+
// FIXME: problem of infinite loop..?
139+
:sjs
140+
abstract class E: S | N
141+
class S(x) with
142+
fun toString() = "S(" + x + ")"
143+
class N() with
144+
fun toString() = "N"
145+
//│ JS:
146+
//│ let tmp;
147+
//│ tmp = this.Predef.TraceLogger.resetIndent(0) ?? null;
148+
//│ this.E = class E {
149+
//│ constructor() {
150+
//│
151+
//│ }
152+
//│ toString() { return "E"; }
153+
//│ };
154+
//│ this.S = function S(...args1) { return new S.class(...args1); };
155+
//│ this.S.class = class S {
156+
//│ constructor(x) {
157+
//│ this.x = x;
158+
//│
159+
//│ }
160+
//│ toString(...args) {
161+
//│ globalThis.Predef.checkArgs("toString", 0, true, args.length);
162+
//│ let traceLogEnterMsg, traceLogPrevIndent, traceLogRes, traceLogRetMsg, tmp1, tmp2, tmp3, tmp4;
163+
//│ traceLogEnterMsg = globalThis.String.prototype.concat.call("calling: toString(", ")") ?? null;
164+
//│ tmp1 = globalThis.Predef.TraceLogger.log(traceLogEnterMsg) ?? null;
165+
//│ traceLogPrevIndent = globalThis.Predef.TraceLogger.indent() ?? null;
166+
//│ tmp2 = "S(" + this.x;
167+
//│ traceLogRes = tmp2 + ")";
168+
//│ traceLogRetMsg = globalThis.String.prototype.concat.call("return: ", traceLogRes) ?? null;
169+
//│ tmp3 = globalThis.Predef.TraceLogger.resetIndent(traceLogPrevIndent) ?? null;
170+
//│ tmp4 = globalThis.Predef.TraceLogger.log(traceLogRetMsg) ?? null;
171+
//│ return traceLogRes;
172+
//│ }
173+
//│ };
174+
//│ this.N = function N(...args1) { return new N.class(...args1); };
175+
//│ this.N.class = class N {
176+
//│ constructor() {
177+
//│
178+
//│ }
179+
//│ toString(...args) {
180+
//│ globalThis.Predef.checkArgs("toString", 0, true, args.length);
181+
//│ let traceLogEnterMsg, traceLogPrevIndent, traceLogRes, traceLogRetMsg, tmp1, tmp2, tmp3;
182+
//│ traceLogEnterMsg = globalThis.String.prototype.concat.call("calling: toString(", ")") ?? null;
183+
//│ tmp1 = globalThis.Predef.TraceLogger.log(traceLogEnterMsg) ?? null;
184+
//│ traceLogPrevIndent = globalThis.Predef.TraceLogger.indent() ?? null;
185+
//│ traceLogRes = "N";
186+
//│ traceLogRetMsg = globalThis.String.prototype.concat.call("return: ", traceLogRes) ?? null;
187+
//│ tmp2 = globalThis.Predef.TraceLogger.resetIndent(traceLogPrevIndent) ?? null;
188+
//│ tmp3 = globalThis.Predef.TraceLogger.log(traceLogRetMsg) ?? null;
189+
//│ return traceLogRes;
190+
//│ }
191+
//│ };
192+
//│ undefined
193+
194+
:sjs
195+
fun id(x) = x
196+
id(S(S(N())))
197+
//│ JS:
198+
//│ let tmp, tmp1, tmp2, tmp3;
199+
//│ tmp = this.Predef.TraceLogger.resetIndent(0) ?? null;
200+
//│ function id(...args) {
201+
//│ globalThis.Predef.checkArgs("id", 1, true, args.length);
202+
//│ let x = args[0];
203+
//│ let traceLogEnterMsg, traceLogPrevIndent, traceLogRes, traceLogRetMsg, tmp4, tmp5, tmp6;
204+
//│ traceLogEnterMsg = globalThis.String.prototype.concat.call("calling: id(", x, ")") ?? null;
205+
//│ tmp4 = globalThis.Predef.TraceLogger.log(traceLogEnterMsg) ?? null;
206+
//│ traceLogPrevIndent = globalThis.Predef.TraceLogger.indent() ?? null;
207+
//│ traceLogRes = x;
208+
//│ traceLogRetMsg = globalThis.String.prototype.concat.call("return: ", traceLogRes) ?? null;
209+
//│ tmp5 = globalThis.Predef.TraceLogger.resetIndent(traceLogPrevIndent) ?? null;
210+
//│ tmp6 = globalThis.Predef.TraceLogger.log(traceLogRetMsg) ?? null;
211+
//│ return traceLogRes;
212+
//│ }
213+
//│ tmp1 = this.N() ?? null;
214+
//│ tmp2 = this.S(tmp1) ?? null;
215+
//│ tmp3 = this.S(tmp2) ?? null;
216+
//│ this.id(tmp3) ?? null
217+
//│ > calling: toString()
218+
//│ > | calling: toString()
219+
//│ > | | calling: toString()
220+
//│ > | | return: N
221+
//│ > | return: S(N)
222+
//│ > return: S(S(N))
223+
//│ > calling: id(S(S(N)))
224+
//│ > | calling: toString()
225+
//│ > | | calling: toString()
226+
//│ > | | | calling: toString()
227+
//│ > | | | return: N
228+
//│ > | | return: S(N)
229+
//│ > | return: S(S(N))
230+
//│ > return: S(S(N))
231+
//│ = S { x: S { x: N {} } }

0 commit comments

Comments
 (0)