(and ...)

This commit is contained in:
2020-11-25 11:55:53 -07:00
parent 70ff9b7e1b
commit 2e85baa048
4 changed files with 39 additions and 0 deletions

View File

@@ -75,6 +75,31 @@ class Macros {
]).withPos(args[0].pos);
};
// (and... uses (cond... ) and (not ...) under the hood)
macros["and"] = (args:Array<ReaderExp>, k) -> {
var uniqueVarName = "_" + Uuid.v4().toShort();
var uniqueVarSymbol = Symbol(uniqueVarName).withPos(args[0].pos);
var condCases = [
for (arg in args) {
CallExp(CallExp(Symbol("not").withPos(args[0].pos),
[
CallExp(Symbol("set").withPos(args[0].pos), [uniqueVarSymbol, arg]).withPos(args[0].pos)
]).withPos(args[0].pos), [Symbol("null").withPos(args[0].pos)]).withPos(args[0].pos);
}
];
condCases.push(CallExp(Symbol("true").withPos(args[0].pos), [uniqueVarSymbol]).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),
MetaExp("mut").withPos(args[0].pos),
Symbol("null").withPos(args[0].pos)
]).withPos(args[0].pos),
CallExp(Symbol("cond").withPos(args[0].pos), condCases).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)

View File

@@ -174,6 +174,12 @@ class BasicTestCase extends Test {
Assert.equals(5, BasicTestCase.myOr1);
}
function testAnd() {
Assert.equals(6, BasicTestCase.myAnd1);
Assert.equals(null, BasicTestCase.myAnd2);
Assert.equals(null, BasicTestCase.myAnd3);
}
function testNot() {
Assert.equals(false, BasicTestCase.myNot1);
Assert.equals(false, BasicTestCase.myNot2);

View File

@@ -130,6 +130,10 @@
(defvar myOr1 (or null 5))
(defvar myAnd1 (and 5 6))
(defvar myAnd2 (and false 5 6))
(defvar myAnd3 (and 5 false 6))
(defun mySetLocal []
(deflocal loc &mut "one thing")
(set loc "another thing")

View File

@@ -0,0 +1,4 @@
(defun myFun []
(deflocal something 5)
// TODO This comment causes a hard-to-track-down error!
)