trace special form. close #45
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
@@ -346,6 +346,10 @@ class BasicTestCase extends Test {
|
||||
function testWhileLet() {
|
||||
_testWhileLet();
|
||||
}
|
||||
|
||||
function testTrace() {
|
||||
_testTrace();
|
||||
}
|
||||
}
|
||||
|
||||
class BasicObject {
|
||||
|
@@ -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")))
|
Reference in New Issue
Block a user