fix (and) and (or) for c# and c++

This commit is contained in:
2022-03-28 18:53:48 -06:00
parent d6b4261d06
commit c8d42e5258
2 changed files with 36 additions and 54 deletions

View File

@@ -236,71 +236,49 @@ class Macros {
macros["cond"] = cond.bind("cond", "if");
macros["#cond"] = cond.bind("#cond", "#if");
// (or... ) uses (cond... ) under the hood
macros["or"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k) -> {
wholeExp.checkNumArgs(2, null, "(or [v1] [v2] [values...])");
function _or(wholeExp:ReaderExp, args:Array<ReaderExp>, k) {
wholeExp.checkNumArgs(1, null, "(or <v1> <values...>)");
var b = wholeExp.expBuilder();
var uniqueVarSymbol = b.symbol();
var lastValue = args.pop();
var firstVal = args.shift();
b.begin([
b.call(b.symbol("localVar"), [
b.meta("mut", b.typed("Dynamic", uniqueVarSymbol)),
b.symbol("null")
]),
b.call(
b.symbol("cond"), [
for (arg in args) {
b.call(
b.call(b.symbol("set"), [
uniqueVarSymbol,
arg
]), [
uniqueVarSymbol
]);
}
].concat([
b.call(
b.symbol("true"), [
lastValue
])
]))
]);
var body = if (args.length > 0) {
[
b.callSymbol("if", [uniqueVarSymbol, uniqueVarSymbol, _or(wholeExp, args, k)])
];
} else {
// If nothing is truthy, return the last one
[uniqueVarSymbol];
};
return b.let([b.typed("Dynamic", uniqueVarSymbol), firstVal], body);
};
// (and... uses (cond... ) and (not ...) under the hood)
macros["and"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k) -> {
wholeExp.checkNumArgs(2, null, "(and [v1] [v2] [values...])");
macros["or"] = _or;
function _and(wholeExp:ReaderExp, args:Array<ReaderExp>, k) {
wholeExp.checkNumArgs(1, null, "(and <v1> <values...>)");
var b = wholeExp.expBuilder();
var uniqueVarSymbol = b.symbol();
var firstVal = args.shift();
var condCases = [
for (arg in args) {
b.call(
b.call(
b.symbol("not"), [
b.call(
b.symbol("set"), [uniqueVarSymbol, arg])
]), [
b.symbol("null")
]);
}
];
condCases.push(b.call(b.symbol("true"), [uniqueVarSymbol]));
var body = if (args.length > 0) {
[
b.callSymbol("if", [uniqueVarSymbol, _and(wholeExp, args, k), uniqueVarSymbol])
];
} else {
// If nothing is truthy, return the last one
[uniqueVarSymbol];
};
b.begin([
b.call(
b.symbol("localVar"), [
b.meta("mut", b.typed("Dynamic", uniqueVarSymbol)),
b.symbol("null")
]),
b.call(
b.symbol("cond"),
condCases)
]);
};
return b.let([b.typed("Dynamic", uniqueVarSymbol), firstVal], body);
}
macros["and"] = _and;
function arraySet(wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) {
var b = wholeExp.expBuilder();

View File

@@ -80,7 +80,11 @@
])))
(function _testTruthy []
// Expressions that have failed in the past:
(Assert.isTrue ?(or false (and 642565506 905012177) "vaxricnakvc" [431724544 383128908 868813810] 169132392 "" ["ftmlkbdx" "vdaziji" "zm"]))
// 10 new expressions:
(macroRepeat (Assert.isTrue ?(randomTruthyExp)) 10))
(function _testFalsy []
// 10 new expressions:
(macroRepeat (Assert.isFalse ?(randomFalsyExp)) 10))