Skip to content

Commit a88733b

Browse files
committed
Show diff-test results using MLscript pretty-printer instead of NodeJS console output
1 parent f2e6a48 commit a88733b

File tree

154 files changed

+1198
-1790
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+1198
-1790
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ sealed abstract class Block extends Product with AutoLocated:
5757
case HandleBlock(lhs, res, par, cls, handlers, bdy, rst) => 1 + handlers.map(_.body.size).sum + bdy.size + rst.size
5858

5959
// TODO conserve if no changes
60-
def mapTail(f: BlockTail => BlockTail): Block = this match
60+
def mapTail(f: BlockTail => Block): Block = this match
6161
case b: BlockTail => f(b)
6262
case Begin(sub, rst) => Begin(sub, rst.mapTail(f))
6363
case Assign(lhs, rhs, rst) => Assign(lhs, rhs, rst.mapTail(f))
@@ -68,6 +68,13 @@ sealed abstract class Block extends Product with AutoLocated:
6868
Match(scrut, arms.map(_ -> _.mapTail(f)), dflt.map(_.mapTail(f)), rst)
6969
case Match(scrut, arms, dflt, rst) =>
7070
Match(scrut, arms, dflt, rst.mapTail(f))
71+
case Label(label, body, rest) => Label(label, body, rest.mapTail(f))
72+
case af @ AssignField(lhs, nme, rhs, rest) =>
73+
AssignField(lhs, nme, rhs, rest.mapTail(f))(af.symbol)
74+
case adf @ AssignDynField(lhs, fld, arrayIdx, rhs, rest) =>
75+
AssignDynField(lhs, fld, arrayIdx, rhs, rest.mapTail(f))
76+
case tb @ TryBlock(sub, fin, rest) =>
77+
TryBlock(sub, fin, rest.mapTail(f))
7178

7279
lazy val freeVars: Set[Local] = this match
7380
case Match(scrut, arms, dflt, rest) =>
@@ -143,7 +150,7 @@ case class Return(res: Result, implct: Bool) extends BlockTail
143150

144151
case class Throw(exc: Result) extends BlockTail
145152

146-
case class Label(label: Local, body: Block, rest: Block) extends BlockTail
153+
case class Label(label: Local, body: Block, rest: Block) extends Block
147154

148155
case class Break(label: Local) extends BlockTail
149156
case class Continue(label: Local) extends BlockTail
@@ -276,7 +283,8 @@ case class Call(fun: Path, args: Ls[Arg])(val isMlsFun: Bool) extends Result
276283
case class Instantiate(cls: Path, args: Ls[Path]) extends Result
277284

278285
sealed abstract class Path extends Result:
279-
def selN(id: Tree.Ident) = Select(this, id)(N)
286+
def selN(id: Tree.Ident): Path = Select(this, id)(N)
287+
def selSN(id: Str): Path = selN(new Tree.Ident(id))
280288
def asArg = Arg(false, this)
281289

282290
case class Select(qual: Path, name: Tree.Ident)(val symbol: Opt[FieldSymbol]) extends Path with ProductWithExtraInfo:

hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ object Elaborator:
2424
"==", "!=", "<", "<=", ">", ">=",
2525
"===", "!==",
2626
"&&", "||")
27-
val unaryOps = Set("-", "+", "!", "~")
27+
val unaryOps = Set("-", "+", "!", "~", "typeof")
2828
val anyOps = Set("super")
2929
val builtins = binaryOps ++ unaryOps ++ anyOps
3030
val aliasOps = Map(

hkmc2/shared/src/main/scala/hkmc2/utils/ReplHost.scala

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,8 @@ class ReplHost(rootPath: Str)(using TL) {
108108
// Send the code
109109
send(wrapped)
110110
(parseQueryResult() match
111-
case intermediate: Str =>
112-
val lines = intermediate.splitSane('\n')
113-
val result = lines.lastOption.getOrElse("")
114-
ReplHost.Result(result, if lines.size < 2 then N else S(lines.init.mkString("\n")))
111+
case output: Str =>
112+
ReplHost.Result(output)
115113
case error: ReplHost.Error => error
116114
, consumeStderr())
117115

@@ -124,7 +122,7 @@ class ReplHost(rootPath: Str)(using TL) {
124122
def execute(code: Str): ReplHost.Reply = {
125123
send(code)
126124
collectUntilPrompt() match
127-
case res: Str => ReplHost.Result(res, None)
125+
case res: Str => ReplHost.Result(res)
128126
case error: ReplHost.Error => error
129127
}
130128

@@ -160,15 +158,10 @@ object ReplHost {
160158
* Represents a successful reply from Node.js.
161159
*
162160
* @param content the reply content, i.e. the final result
163-
* @param intermediate the intermediate evaluation results
164161
*/
165-
final case class Result(content: Str, intermediate: Opt[Str]) extends Reply {
162+
final case class Result(content: Str) extends Reply {
166163
override def map(f: Str => Reply): Reply = f(content)
167-
override def toString(): Str = s"[success] $content${
168-
intermediate match
169-
case None | Some("") => s""
170-
case Some(str) => s" (with output)"
171-
}"
164+
override def toString(): Str = s"[success] $content"
172165
}
173166

174167
/**

hkmc2/shared/src/test/mlscript-compile/Predef.mjs

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ Predef1 = class Predef {
195195
tmp1 = tmp(...xs) ?? null;
196196
return globalThis.console.log(...tmp1) ?? null;
197197
}
198+
static printRaw(x4) {
199+
let tmp;
200+
tmp = Predef.render(x4);
201+
return globalThis.console.log(tmp) ?? null;
202+
}
198203
static interleave(sep) {
199204
return (...args) => {
200205
let res, len, i, scrut, idx, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
@@ -243,21 +248,95 @@ Predef1 = class Predef {
243248
}
244249
}
245250
static render(arg1) {
246-
let tmp, tmp1, tmp2, tmp3, tmp4;
247-
if (arg1 instanceof globalThis.Array) {
248-
tmp = Predef.fold((arg11, arg2) => {
249-
return arg11 + arg2;
250-
});
251-
tmp1 = Predef.interleave(", ");
252-
tmp2 = Predef.map(Predef.render);
253-
tmp3 = tmp2(...arg1) ?? null;
254-
tmp4 = tmp1(...tmp3) ?? null;
255-
return tmp("[", ...tmp4, "]") ?? null;
251+
let ts, p, scrut, scrut1, scrut2, nme, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15, tmp16, tmp17, tmp18, tmp19, tmp20;
252+
if (arg1 === undefined) {
253+
return "undefined";
256254
} else {
257-
if (typeof arg1 === 'string') {
258-
return globalThis.JSON.stringify(arg1) ?? null;
255+
if (arg1 === null) {
256+
return "null";
259257
} else {
260-
return globalThis.String(arg1);
258+
if (arg1 instanceof globalThis.Array) {
259+
tmp = Predef.fold((arg11, arg2) => {
260+
return arg11 + arg2;
261+
});
262+
tmp1 = Predef.interleave(", ");
263+
tmp2 = Predef.map(Predef.render);
264+
tmp3 = tmp2(...arg1) ?? null;
265+
tmp4 = tmp1(...tmp3) ?? null;
266+
return tmp("[", ...tmp4, "]") ?? null;
267+
} else {
268+
if (typeof arg1 === 'string') {
269+
return globalThis.JSON.stringify(arg1) ?? null;
270+
} else {
271+
if (arg1 instanceof globalThis.Set) {
272+
tmp5 = Predef.fold((arg11, arg2) => {
273+
return arg11 + arg2;
274+
});
275+
tmp6 = Predef.interleave(", ");
276+
tmp7 = Predef.map(Predef.render);
277+
tmp8 = tmp7(...arg1) ?? null;
278+
tmp9 = tmp6(...tmp8) ?? null;
279+
return tmp5("Set{", ...tmp9, "}") ?? null;
280+
} else {
281+
if (arg1 instanceof globalThis.Map) {
282+
tmp10 = Predef.fold((arg11, arg2) => {
283+
return arg11 + arg2;
284+
});
285+
tmp11 = Predef.interleave(", ");
286+
tmp12 = Predef.map(Predef.render);
287+
tmp13 = tmp12(...arg1) ?? null;
288+
tmp14 = tmp11(...tmp13) ?? null;
289+
return tmp10("Map{", ...tmp14, "}") ?? null;
290+
} else {
291+
if (arg1 instanceof globalThis.Function) {
292+
p = globalThis.Object.getOwnPropertyDescriptor(arg1, "prototype");
293+
if (p instanceof globalThis.Object) {
294+
scrut = p["writable"];
295+
if (scrut === true) {
296+
tmp15 = true;
297+
} else {
298+
tmp15 = false;
299+
}
300+
} else {
301+
tmp15 = false;
302+
}
303+
if (p === undefined) {
304+
tmp16 = true;
305+
} else {
306+
tmp16 = false;
307+
}
308+
scrut1 = tmp15 || tmp16;
309+
if (scrut1 === true) {
310+
scrut2 = arg1.name;
311+
if (scrut2 === "") {
312+
tmp17 = "";
313+
} else {
314+
nme = scrut2;
315+
tmp17 = " " + nme;
316+
}
317+
tmp18 = "[function" + tmp17;
318+
return tmp18 + "]";
319+
} else {
320+
return globalThis.String(arg1);
321+
}
322+
} else {
323+
if (arg1 instanceof globalThis.Object) {
324+
return globalThis.String(arg1);
325+
} else {
326+
ts = arg1["toString"];
327+
if (ts === undefined) {
328+
tmp19 = typeof arg1;
329+
tmp20 = "[" + tmp19;
330+
return tmp20 + "]";
331+
} else {
332+
return ts.call(arg1) ?? null;
333+
}
334+
}
335+
}
336+
}
337+
}
338+
}
339+
}
261340
}
262341
}
263342
}

hkmc2/shared/src/test/mlscript-compile/Predef.mls

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ fun pass3(f)(...xs) = f(xs.0, xs.1, xs.2)
2424
fun print(...xs) =
2525
console.log(...map(renderAsStr)(...xs))
2626

27+
fun printRaw(x) =
28+
console.log(render(x))
29+
2730
fun interleave(sep)(...args) =
2831
if args.length === 0 then [] else...
2932
let
@@ -41,10 +44,25 @@ fun interleave(sep)(...args) =
4144
fun renderAsStr(arg) =
4245
if arg is Str then arg else render(arg)
4346

44-
fun render(arg) = if arg is
45-
Array then fold(+)("[", ...interleave(", ")(...map(render)(...arg)), "]")
46-
Str then JSON.stringify(arg)
47-
else String(arg)
47+
fun render(arg) = if
48+
arg is
49+
undefined then "undefined"
50+
null then "null"
51+
Array then fold(+)("[", ...interleave(", ")(...map(render)(...arg)), "]")
52+
Str then JSON.stringify(arg)
53+
Set then fold(+)("Set{", ...interleave(", ")(...map(render)(...arg)), "}")
54+
Map then fold(+)("Map{", ...interleave(", ")(...map(render)(...arg)), "}")
55+
Function and
56+
let p = Object.getOwnPropertyDescriptor(arg, "prototype")
57+
(p is Object and p.("writable")) || (p is undefined) then
58+
"[function" + (if arg.name is
59+
"" then ""
60+
nme then " " + nme
61+
) + "]"
62+
Object then String(arg)
63+
let ts = arg.("toString") // not accessing as `arg.toString` to avoid the sanity check
64+
ts is undefined then "[" + typeof(arg) + "]"
65+
else ts.call(arg)
4866

4967
val assert = console.assert
5068

hkmc2/shared/src/test/mlscript/apps/AccountingTest.mls

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,15 @@ import "../../mlscript-compile/apps/Accounting.mls"
44

55

66
val acc = new Accounting
7-
//│ > Accounting {
8-
//│ > warnings: [],
9-
//│ > Project: [Function: Project] { class: [class Project] },
10-
//│ > Line: [Function: Line] { class: [class Line] },
11-
//│ > lines: [],
12-
//│ > Report: [Function: Report] { class: [class Report] }
13-
//│ acc = }
7+
//│ acc = Accounting
148

159
val proj = acc.Project("P1")
16-
//│ proj = Project { num: 'P1' }
10+
//│ proj = Project("P1")
1711

1812
val l1 = acc.mkLine("L1", proj, 200_000, true)
1913
val l2 = acc.mkLine("L2", proj, 1_000_000, true)
20-
//│ > Line {
21-
//│ > name: 'L1',
22-
//│ > proj: Project { num: 'P1' },
23-
//│ > starting_balance: 200000,
24-
//│ > isMatchable: true,
25-
//│ > balance: 200000
26-
//│ l1 = }
27-
//│ > Line {
28-
//│ > name: 'L2',
29-
//│ > proj: Project { num: 'P1' },
30-
//│ > starting_balance: 1000000,
31-
//│ > isMatchable: true,
32-
//│ > balance: 1000000
33-
//│ l2 = }
14+
//│ l1 = Line("L1", Project("P1"), 200000, true)
15+
//│ l2 = Line("L2", Project("P1"), 1000000, true)
3416

3517

3618
:...

hkmc2/shared/src/test/mlscript/apps/CSVTest.mls

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,28 @@ import "../../mlscript-compile/apps/CSV.mls"
44

55

66
let csv = CSV()
7-
//│ > CSV {
8-
//│ > strDelimiter: ',',
9-
//│ > objPattern: /(\,|\r?\n|\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^"\,\r\n]*))/gi
10-
//│ csv = }
7+
//│ csv = CSV(",")
118

129
csv.strDelimiter
13-
//│ = ','
10+
//│ = ","
1411

1512
csv.objPattern
1613
//│ = /(\,|\r?\n|\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^"\,\r\n]*))/gi
1714

1815
csv.toArrays("a,b,c")
19-
//│ = [ [ 'a', 'b', 'c' ] ]
16+
//│ = [["a", "b", "c"]]
2017

2118
csv.toArrays("a,b,c\n1,2,3\n4,5,6")
22-
//│ = [ [ 'a', 'b', 'c' ], [ '1', '2', '3' ], [ '4', '5', '6' ] ]
19+
//│ = [["a", "b", "c"], ["1", "2", "3"], ["4", "5", "6"]]
2320

2421
csv.toArrays("a,b,c\n\n1,2,3\n4,5,6\n")
25-
//│ > [
26-
//│ > [ 'a', 'b', 'c' ],
27-
//│ > [ '' ],
28-
//│ > [ '1', '2', '3' ],
29-
//│ > [ '4', '5', '6' ],
30-
//│ > [ '' ]
31-
//│ = ]
22+
//│ = [["a", "b", "c"], [""], ["1", "2", "3"], ["4", "5", "6"], [""]]
3223

3324
csv.toArrays(",")
34-
//│ = [ [ '' ] ]
25+
//│ = [[""]]
3526

3627
csv.toArrays(",,")
37-
//│ = [ [ '', '' ] ]
28+
//│ = [["", ""]]
3829

3930

4031

0 commit comments

Comments
 (0)