(or... )
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
-cp src
|
||||
-lib hscript
|
||||
-lib uuid
|
||||
--macro nullSafety("kiss", Strict)
|
||||
--main kiss.Main
|
||||
--interp
|
||||
@@ -4,9 +4,11 @@ import haxe.macro.Expr;
|
||||
import haxe.macro.Context;
|
||||
import hscript.Parser;
|
||||
import hscript.Interp;
|
||||
import uuid.Uuid;
|
||||
import kiss.Reader;
|
||||
import kiss.Kiss;
|
||||
|
||||
using uuid.Uuid;
|
||||
using kiss.Reader;
|
||||
using kiss.Helpers;
|
||||
|
||||
@@ -53,6 +55,25 @@ class Macros {
|
||||
|
||||
macros["cond"] = cond;
|
||||
|
||||
// (or... ) uses (cond... ) under the hood
|
||||
macros["or"] = (args:Array<ReaderExp>, k) -> {
|
||||
var uniqueVarName = "_" + Uuid.v4().toShort();
|
||||
var uniqueVarSymbol = Symbol(uniqueVarName).withPos(args[0].pos);
|
||||
|
||||
CallExp(Symbol("begin").withPos(args[0].pos), [
|
||||
CallExp(Symbol("deflocal").withPos(args[0].pos), [
|
||||
TypedExp("Any", uniqueVarSymbol).withPos(args[0].pos),
|
||||
Symbol("null").withPos(args[0].pos)
|
||||
]).withPos(args[0].pos),
|
||||
CallExp(Symbol("cond").withPos(args[0].pos), [
|
||||
for (arg in args) {
|
||||
CallExp(CallExp(Symbol("set").withPos(args[0].pos), [uniqueVarSymbol, arg]).withPos(args[0].pos),
|
||||
[uniqueVarSymbol]).withPos(args[0].pos);
|
||||
}
|
||||
]).withPos(args[0].pos)
|
||||
]).withPos(args[0].pos);
|
||||
};
|
||||
|
||||
// Under the hood, (defmacrofun ...) defines a runtime function that accepts Quote arguments and a special form that quotes the arguments to macrofun calls
|
||||
macros["defmacrofun"] = (exps:Array<ReaderExp>, k:KissState) -> {
|
||||
if (exps.length < 3)
|
||||
|
||||
@@ -164,4 +164,8 @@ class BasicTestCase extends Test {
|
||||
Assert.equals("this", BasicTestCase.myCond3);
|
||||
Assert.equals(null, BasicTestCase.myCondFallthrough);
|
||||
}
|
||||
|
||||
function testOr() {
|
||||
Assert.equals(5, BasicTestCase.myOr1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,4 +122,6 @@
|
||||
(true "default")))
|
||||
|
||||
(defvar myCondFallthrough (cond
|
||||
(false "not this")))
|
||||
(false "not this")))
|
||||
|
||||
(defvar myOr1 (or null 5))
|
||||
Reference in New Issue
Block a user