diff --git a/Nasal/props.nas b/Nasal/props.nas index 5f75c71d6..4ea9fb3d2 100644 --- a/Nasal/props.nas +++ b/Nasal/props.nas @@ -84,7 +84,7 @@ Node._setChildren = func { if(typeof(val) == "scalar") { subnode.setValue(val); } elsif(typeof(val) == "hash") { subnode.setValues(val); } elsif(typeof(val) == "vector") { - for(i=0; i property branch according to the rules +# set out in $FG_ROOT/Docs/README.condition. +# +var condition = func(p) { + if(!isa(p, props.Node)) { p = props.globals.getNode(p); } + return _cond_and(p) +} + +var _cond_and = func(p) { + foreach(var c; p.getChildren()) { + if(!_cond(c)) { return 0; } + } + return 1; +} + +var _cond_or = func(p) { + foreach(var c; p.getChildren()) { + if(_cond(c)) { return 1; } + } + return 0; +} + +var _cond = func(p) { + var n = p.getName(); + if(n == "not") { return !_cond_and(p); } + if(n == "and") { return _cond_and(p); } + if(n == "or") { return _cond_or(p); } + if(n == "equals") { return _cond_cmp(p, "EQ"); } + if(n == "not-equals") { return !_cond_cmp(p, "EQ"); } + if(n == "less-than") { return _cond_cmp(p, "LT"); } + if(n == "greater-than") { return _cond_cmp(p, "GT"); } + if(n == "less-than-equals") { return !_cond_cmp(p, "GT"); } + if(n == "greater-than-equals") { return !_cond_cmp(p, "LT"); } + if(n == "property") { return getprop(p.getValue()); } + printlog("alert", "condition: invalid operator ", n); + dump(p); + return nil; +} + +var _cond_cmp = func(p, op) { + var left = p.getChild("property", 0, 0); + if(left != nil) { left = getprop(left.getValue()); } + else { + printlog("alert", "condition: no left value"); + dump(p); + return nil; + } + var right = p.getChild("property", 1, 0); + if(right != nil) { right = getprop(right.getValue()); } + else { + right = p.getChild("value", 0, 0); + if(right != nil) { right = right.getValue(); } + else { + printlog("alert", "condition: no right value"); + dump(p); + return nil; + } + } + if(left == nil or right == nil) { + printlog("alert", "condition: comparing with nil"); + dump(p); + return nil; + } + return op == "LT" ? left < right : op == "GT" ? left > right : left == right; +} +