trace special form. close #45

This commit is contained in:
2022-06-05 16:22:06 +00:00
parent ced512a4e4
commit b8ea8a81d3
4 changed files with 30 additions and 2 deletions

View File

@@ -398,12 +398,17 @@ class Prelude {
public static var printStr:(String) -> Void = _printStr;
public static function print<T>(v:T, label = ""):T {
public static function withLabel(v:Any, label = "") {
var toPrint = label;
if (label.length > 0) {
toPrint += ": ";
}
toPrint += Std.string(v);
return toPrint;
}
public static function print<T>(v:T, label = ""):T {
var toPrint = withLabel(v, label);
printStr(toPrint);
return v;
}

View File

@@ -449,6 +449,22 @@ class SpecialForms {
ECast(e, t).withMacroPosOf(wholeExp);
}
k.doc("trace", 1, 2, "(trace <value> <?label>)");
map["trace"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
var b = wholeExp.expBuilder();
var label = if (exps.length > 1) exps[1] else b.str("");
var label = k.convert(label);
EBlock([
EVars([
toVar(b.symbol("v"), exps[0], k)
]).withMacroPosOf(wholeExp),
ECall(EConst(CIdent("trace")).withMacroPosOf(wholeExp), [
macro kiss.Prelude.withLabel(v, $label)
]).withMacroPosOf(wholeExp),
k.convert(exps[0])
]).withMacroPosOf(wholeExp);
};
return map;
}

View File

@@ -346,6 +346,10 @@ class BasicTestCase extends Test {
function testWhileLet() {
_testWhileLet();
}
function testTrace() {
_testTrace();
}
}
class BasicObject {

View File

@@ -637,4 +637,7 @@ From:[(assert false (+ \"false \" \"should \" \"have \" \"been \" \"true\"))]" m
stream (Stream.fromString "a\nb\nc")]
(whileLet [(Some line) (stream.takeLine)]
(Assert.equals (nth lines idx) line)
(+= idx 1))))
(+= idx 1))))
(function _testTrace []
(Assert.equals 5 (trace 5 "num")))