Skip to content

Commit

Permalink
Add rudimentary CSV parsing helper module + various fixes & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LPTK committed Jan 4, 2025
1 parent e37d8f8 commit 8aed187
Show file tree
Hide file tree
Showing 27 changed files with 821 additions and 73 deletions.
30 changes: 15 additions & 15 deletions hkmc2/jvm/src/test/scala/hkmc2/JSBackendDiffMaker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,21 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
val (reply, stderr) = host.query(queryStr, !expectRuntimeOrCodeGenErrors && fixme.isUnset && todo.isUnset)
reply match
case ReplHost.Result(content, stdout) =>
if silent.isUnset then
stdout match
case None | Some("") =>
case Some(str) =>
str.splitSane('\n').foreach: line =>
output(s"> ${line}")
content match
case "undefined" =>
case "null" =>
stdout match
case None | Some("") =>
case Some(str) =>
str.splitSane('\n').foreach: line =>
output(s"> ${line}")
content match
case "undefined" =>
case "null" =>
case _ =>
expect.get match
case S(expected) if content != expected => raise:
ErrorReport(msg"Expected: ${expected}, got: ${content}" -> N :: Nil,
source = Diagnostic.Source.Runtime)
case _ =>
expect.get match
case S(expected) if content != expected => raise:
ErrorReport(msg"Expected: ${expected}, got: ${content}" -> N :: Nil,
source = Diagnostic.Source.Runtime)
case _ => output(s"$prefix= ${content}")
if silent.isUnset then output(s"$prefix= ${content}")
case ReplHost.Empty =>
case ReplHost.Unexecuted(message) => ???
case ReplHost.Error(isSyntaxError, message, otherOutputs) =>
Expand Down Expand Up @@ -162,7 +162,7 @@ abstract class JSBackendDiffMaker extends MLsDiffMaker:
if ts.trmImplTree.exists(_.k.isInstanceOf[syntax.ValLike]) => S((nme, ts))
case _ => N
case _ => N
definedValues.toSeq.sortBy(_._1).foreach: (nme, sym) =>
if silent.isUnset then definedValues.toSeq.sortBy(_._1).foreach: (nme, sym) =>
val le = codegen.Return(codegen.Value.Ref(sym), implct = true)
val je = nestedScp.givenIn:
jsb.block(le)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object Elaborator:
",",
"+", "-", "*", "/", "%",
"==", "!=", "<", "<=", ">", ">=",
"===",
"===", "!==",
"&&", "||")
private val unaryOps = Set("-", "+", "!", "~")
private val anyOps = Set("super")
Expand Down
6 changes: 5 additions & 1 deletion hkmc2/shared/src/main/scala/hkmc2/syntax/ParseRule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ class ParseRules(using State):
case (body, _) => Open(body)}*),
modified(`abstract`, Kw(`class`)(typeDeclBody(Cls))),
modified(`mut`),
modified(`do`),
Kw(`do`):
ParseRule(s"`do` keyword")(
exprOrBlk(ParseRule(s"`do` body")(End(()))):
case (body, ()) => Tree.Modified(`do`, N, body)
*),
modified(`virtual`),
modified(`override`),
modified(`declare`),
Expand Down
3 changes: 2 additions & 1 deletion hkmc2/shared/src/main/scala/hkmc2/syntax/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ abstract class Parser(
case _ => S(expr(prec))
Spread(if dotDotDot then Keyword.`...` else Keyword.`..`, S(loc), bod)
case (tok, loc) :: _ =>
TODO(tok)
err((msg"Expected an expression; found new line instead" -> S(loc) :: Nil))
errExpr
case Nil =>
err((msg"Expected an expression; found end of input instead" -> lastLoc :: Nil))
errExpr
Expand Down
31 changes: 23 additions & 8 deletions hkmc2/shared/src/test/mlscript-compile/Predef.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,33 @@ const Predef$class = class Predef {
return f5.call(receiver1, ...args);
};
}
print(x4) {
pass1(f6) {
return (...xs) => {
return f6(xs[0]) ?? null;
};
}
pass2(f7) {
return (...xs) => {
return f7(xs[0], xs[1]) ?? null;
};
}
pass3(f8) {
return (...xs) => {
return f8(xs[0], xs[1], xs[2]) ?? null;
};
}
print(...xs) {
let tmp;
tmp = String(x4);
return console.log(tmp) ?? null;
tmp = xs.map(String) ?? null;
return console.log(...tmp) ?? null;
}
tupleSlice(xs, i, j) {
tupleSlice(xs1, i, j) {
let tmp;
tmp = xs.length - j;
return globalThis.Array.prototype.slice.call(xs, i, tmp) ?? null;
tmp = xs1.length - j;
return globalThis.Array.prototype.slice.call(xs1, i, tmp) ?? null;
}
tupleGet(xs1, i1) {
return globalThis.Array.prototype.at.call(xs1, i1);
tupleGet(xs2, i1) {
return globalThis.Array.prototype.at.call(xs2, i1);
}
stringStartsWith(string, prefix) {
return string.startsWith(prefix) ?? null;
Expand Down
8 changes: 6 additions & 2 deletions hkmc2/shared/src/test/mlscript-compile/Predef.mls
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ fun (.) passTo(receiver, f)(...args) = f(receiver, ...args)

fun (|>.) call(receiver, f)(...args) = f.call(receiver, ...args)

fun print(x) =
console.log(String(x))
fun pass1(f)(...xs) = f(xs.0)
fun pass2(f)(...xs) = f(xs.0, xs.1)
fun pass3(f)(...xs) = f(xs.0, xs.1, xs.2)

fun print(...xs) =
console.log(...xs.map(String))

val assert = console.assert

Expand Down
7 changes: 5 additions & 2 deletions hkmc2/shared/src/test/mlscript-compile/Str.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const Str$class = class Str {
constructor() {}
concat(a, b) {
concat2(a, b) {
return a + b;
}
string(value) {
concat(...xs) {
return xs.join("") ?? null;
}
from(value) {
return globalThis.String(value) ?? null;
}
toString() { return "Str"; }
Expand Down
8 changes: 6 additions & 2 deletions hkmc2/shared/src/test/mlscript-compile/Str.mls
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

module Str with ...

fun (~) concat(a, b) = a + b

fun string(value) = globalThis.String(value)
fun (~) concat2(a, b) = a + b

fun concat(...xs) = xs.join("")

fun from(value) = globalThis.String(value)

60 changes: 30 additions & 30 deletions hkmc2/shared/src/test/mlscript-compile/apps/Accounting.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class Accounting {
let scrut, tmp, tmp1, tmp2, tmp3, tmp4;
scrut = this.balance > 10000;
if (scrut) {
tmp = Str.concat("> **\u2757\uFE0F** Unspent balance of ", this.name);
tmp1 = Str.concat(tmp, ": `");
tmp = Str.concat2("> **\u2757\uFE0F** Unspent balance of ", this.name);
tmp1 = Str.concat2(tmp, ": `");
tmp2 = this$Accounting.display(this.balance);
tmp3 = Str.concat(tmp1, tmp2);
tmp4 = Str.concat(tmp3, "`");
tmp3 = Str.concat2(tmp1, tmp2);
tmp4 = Str.concat2(tmp3, "`");
return this$Accounting.warnings.push(tmp4) ?? null;
} else {
return null;
Expand All @@ -64,42 +64,42 @@ class Accounting {
}
wln(txt1) {
let tmp;
tmp = Str.concat(txt1, "\n");
tmp = Str.concat2(txt1, "\n");
return fs.appendFileSync(this.fileName, tmp);
}
init() {
let tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13;
tmp = this.wln("");
tmp1 = Str.concat("|", "Year");
tmp2 = Str.concat(tmp1, "|");
tmp1 = Str.concat2("|", "Year");
tmp2 = Str.concat2(tmp1, "|");
tmp3 = this$Accounting.lines.map((x) => {
return x.name;
}) ?? null;
tmp4 = tmp3.join("|") ?? null;
tmp5 = Str.concat(tmp2, tmp4);
tmp6 = Str.concat(tmp5, "|");
tmp5 = Str.concat2(tmp2, tmp4);
tmp6 = Str.concat2(tmp5, "|");
tmp7 = this.wln(tmp6);
tmp8 = Str.concat("|", "---");
tmp9 = Str.concat(tmp8, "|");
tmp8 = Str.concat2("|", "---");
tmp9 = Str.concat2(tmp8, "|");
tmp10 = this$Accounting.lines.map((x) => {
return "--:";
}) ?? null;
tmp11 = tmp10.join("|") ?? null;
tmp12 = Str.concat(tmp9, tmp11);
tmp13 = Str.concat(tmp12, "|");
tmp12 = Str.concat2(tmp9, tmp11);
tmp13 = Str.concat2(tmp12, "|");
return this.wln(tmp13);
}
snapShot(label) {
let tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
tmp = String(label) ?? null;
tmp1 = Str.concat("|", tmp);
tmp2 = Str.concat(tmp1, "|");
tmp1 = Str.concat2("|", tmp);
tmp2 = Str.concat2(tmp1, "|");
tmp3 = this$Accounting.lines.map((x) => {
return this$Accounting.display(x.balance);
}) ?? null;
tmp4 = tmp3.join("|") ?? null;
tmp5 = Str.concat(tmp2, tmp4);
tmp6 = Str.concat(tmp5, "|");
tmp5 = Str.concat2(tmp2, tmp4);
tmp6 = Str.concat2(tmp5, "|");
return this.wln(tmp6);
}
wrapUp() {
Expand All @@ -112,14 +112,14 @@ class Accounting {
}) ?? null;
tmp2 = this.wln("### Remaining Available Funds");
tmp3 = this.wln("");
tmp4 = Str.concat("|", "Summary");
tmp5 = Str.concat(tmp4, "| |");
tmp4 = Str.concat2("|", "Summary");
tmp5 = Str.concat2(tmp4, "| |");
tmp6 = this.wln(tmp5);
tmp7 = Str.concat("|", "---");
tmp8 = Str.concat(tmp7, "|--:|");
tmp7 = Str.concat2("|", "---");
tmp8 = Str.concat2(tmp7, "|--:|");
tmp9 = this.wln(tmp8);
tmp10 = Str.concat("|", "Matchable");
tmp11 = Str.concat(tmp10, "|");
tmp10 = Str.concat2("|", "Matchable");
tmp11 = Str.concat2(tmp10, "|");
tmp12 = this$Accounting.lines.filter((x) => {
return x.isMatchable;
}) ?? null;
Expand All @@ -130,11 +130,11 @@ class Accounting {
return a + b;
}, 0);
tmp15 = this$Accounting.display(tmp14);
tmp16 = Str.concat(tmp11, tmp15);
tmp17 = Str.concat(tmp16, "|");
tmp16 = Str.concat2(tmp11, tmp15);
tmp17 = Str.concat2(tmp16, "|");
tmp18 = this.wln(tmp17);
tmp19 = Str.concat("|", "Non-matchable");
tmp20 = Str.concat(tmp19, "|");
tmp19 = Str.concat2("|", "Non-matchable");
tmp20 = Str.concat2(tmp19, "|");
tmp21 = this$Accounting.lines.filter((x) => {
return Predef.not(x.isMatchable);
}) ?? null;
Expand All @@ -145,8 +145,8 @@ class Accounting {
return a + b;
}, 0);
tmp24 = this$Accounting.display(tmp23);
tmp25 = Str.concat(tmp20, tmp24);
tmp26 = Str.concat(tmp25, "|");
tmp25 = Str.concat2(tmp20, tmp24);
tmp26 = Str.concat2(tmp25, "|");
return this.wln(tmp26);
}
toString() { return "Report(" + this.fileName + ")"; }
Expand All @@ -171,7 +171,7 @@ class Accounting {
tmp1 = report.init() ?? null;
tmp2 = k(report) ?? null;
tmp3 = report.wrapUp() ?? null;
tmp4 = Str.concat("Report written to ", filename);
tmp4 = Str.concat2("Report written to ", filename);
return Predef.print(tmp4);
}
toString() { return "Accounting"; }
Expand Down
58 changes: 58 additions & 0 deletions hkmc2/shared/src/test/mlscript-compile/apps/CSV.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Str from "./../Str.mjs";
import Predef from "./../Predef.mjs";
function CSV(strDelimiter1) { return new CSV.class(strDelimiter1); }
CSV.class = class CSV {
constructor(strDelimiter) {
this.strDelimiter = strDelimiter;
let tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
tmp = this.strDelimiter || ",";
this.strDelimiter = tmp;
tmp1 = "(\\" + this.strDelimiter;
tmp2 = tmp1 + "|\\r?\\n|\\r|^)";
tmp3 = "([^\"\\" + this.strDelimiter;
tmp4 = tmp3 + "\\r\\n]*))";
tmp5 = "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" + tmp4;
tmp6 = tmp2 + tmp5;
tmp7 = new RegExp(tmp6, "gi");
this.objPattern = tmp7;
}
toArrays(strData) {
let arrData, arrMatches, scrut, strMatchedDelimiter, scrut1, strMatchedValue, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
arrData = [
[]
];
tmp7: while (true) {
arrMatches = this.objPattern.exec(strData) ?? null;
scrut = arrMatches !== null;
if (scrut) {
strMatchedDelimiter = arrMatches[1];
tmp = strMatchedDelimiter != this.strDelimiter;
scrut1 = strMatchedDelimiter.length && tmp;
if (scrut1) {
tmp1 = arrData.push([]) ?? null;
} else {
tmp1 = null;
}
scrut2 = arrMatches[2];
if (scrut2) {
tmp2 = new RegExp("\"\"", "g");
tmp3 = arrMatches[2].replace(tmp2, "\"");
} else {
tmp3 = arrMatches[3];
}
strMatchedValue = tmp3;
tmp4 = arrData.length - 1;
tmp5 = arrData.at(tmp4) ?? null;
tmp6 = tmp5.push(strMatchedValue) ?? null;
continue tmp7;
} else {
tmp6 = null;
}
break;
}
return arrData;
}
toString() { return "CSV(" + this.strDelimiter + ")"; }
};
null
export default CSV;
Loading

0 comments on commit 8aed187

Please sign in to comment.