move isEmpty() into Kiss class

This commit is contained in:
2023-03-08 07:48:19 -07:00
parent 7dda864a9a
commit 3d8a1c1bc9
2 changed files with 19 additions and 15 deletions

View File

@@ -10,6 +10,7 @@ using kiss.Helpers;
#end
import kiss.Kiss;
import kiss.ReaderExp;
import kiss.Prelude;
import kiss.cloner.Cloner;
@@ -178,7 +179,8 @@ class AsyncEmbeddedScript {
Reader.readAndProcess(Stream.fromFile(scriptFile), k, (nextExp) -> {
var exprString = Reader.toString(nextExp.def);
var expr = Kiss.readerExpToHaxeExpr(nextExp, k);
if (Kiss.isEmpty(expr))
return;
#if debug
expr = macro { Prelude.print($v{exprString}); $expr; };
expr = expr.expr.withMacroPosOf(nextExp);

View File

@@ -399,24 +399,12 @@ class Kiss {
// readerExpToHaxeExpr must be called to process readermacro, alias, and macro definitions
macroUsed = false;
var expr = readerExpToHaxeExpr(nextExp, k);
// exps in the loaded file that actually become haxe expressions can be inserted into the
// file that loaded them at the position (load) was called.
// conditional compiler macros like (#when) tend to return empty blocks, or blocks containing empty blocks
// when they contain field forms, so this should also be ignored
function isEmpty(expr) {
switch (expr.expr) {
case EBlock([]):
case EBlock(blockExps):
for (exp in blockExps) {
if (!isEmpty(exp))
return false;
}
default:
return false;
}
return true;
}
// When calling from build(), we can't add all expressions to the (begin) returned by (load), because that will
// cause double-evaluation of field forms
if (loadAllExps) {
@@ -786,5 +774,19 @@ class Kiss {
return result;
}
public static function isEmpty(expr:Expr) {
switch (expr.expr) {
case EBlock([]):
case EBlock(blockExps):
for (exp in blockExps) {
if (!isEmpty(exp))
return false;
}
default:
return false;
}
return true;
}
#end
}