From b5f00cb50e01c32e78315fe866ef6e9477323ca6 Mon Sep 17 00:00:00 2001 From: Henning Stahlke Date: Fri, 29 May 2020 20:04:49 +0200 Subject: [PATCH] replace typeof() by is() Signed-off-by: Stuart Buchanan --- Nasal/debug.nas | 22 ++++++++++------------ Nasal/globals.nas | 16 +++++++++------- Nasal/io.nas | 3 +-- Nasal/props.nas | 30 ++++++++++++++---------------- 4 files changed, 34 insertions(+), 37 deletions(-) diff --git a/Nasal/debug.nas b/Nasal/debug.nas index 1c3a5e2ef..778ae1795 100644 --- a/Nasal/debug.nas +++ b/Nasal/debug.nas @@ -102,10 +102,9 @@ var _varname = func(s, color=nil) s; # Turn p into props.Node (if it isn't yet), or return nil. # var propify = func(p, create = 0) { - var type = typeof(p); - if (type == "ghost" and ghosttype(p) == "prop") + if (isghost(p) and ghosttype(p) == "prop") return props.wrapNode(p); - if (type == "scalar" and num(p) == nil) + if (isscalar(p) and num(p) == nil) return props.globals.getNode(p, create); if (isa(p, props.Node)) return p; @@ -233,17 +232,17 @@ var string = func(o, color=nil, ttl=5) { if (t == "nil") { return _nil("null", color); - } elsif (t == "scalar") { + } elsif (isscalar(o)) { return num(o) == nil ? _dump_string(o, color) : _num(o~"", color); - } elsif (t == "vector") { + } elsif (isvec(o)) { var s = ""; forindex (var i; o) s ~= (i == 0 ? "" : ", ") ~ debug.string(o[i], color, ttl - 1); return _bracket("[", color) ~ s ~ _bracket("]", color); - } elsif (t == "hash") { - if (contains(o, "parents") and typeof(o.parents) == "vector" + } elsif (ishash(o)) { + if (contains(o, "parents") and isvec(o.parents) and size(o.parents) == 1 and o.parents[0] == props.Node) return _angle("'<", color) ~ _dump_prop(o, color) ~ _angle(">'", color); @@ -253,7 +252,7 @@ var string = func(o, color=nil, ttl=5) { s ~= (i == 0 ? "" : ", ") ~ _dump_key(k[i], color) ~ ": " ~ debug.string(o[k[i]], color, ttl - 1); return _brace("{", color) ~ " " ~ s ~ " " ~ _brace("}", color); - } elsif (t == "ghost") { + } elsif (isghost(o)) { return _angle("'<", color) ~ _nil(ghosttype(o), color) ~ _angle(">'", color); } else { @@ -356,7 +355,7 @@ var benchmark = func(label, fn, repeat = nil, output=nil) { if (repeat == nil) { start = systime(); output = fn(); - } elsif (typeof(output) == 'vector') { + } elsif (isvec(output)) { start = systime(); for (var i = 0; i < repeat; i += 1) append(output, fn()); @@ -375,7 +374,7 @@ var benchmark_time = func(fn, repeat = 1, output = nil) { if (repeat == nil) { start = systime(); output = fn(); - } elsif (typeof(output) == 'vector') { + } elsif (isvec(output)) { start = systime(); for (var i = 0; i < repeat; i += 1) append(output, fn()); @@ -402,14 +401,13 @@ var rank = func(list, repeat = nil) { } var print_rank = func(label, list, names) { - var _vec = (typeof(names) == 'vector'); print("Test results for "~label); var first = 1; var longest = list[-1][1]; foreach (var item; list) { var (fn, time) = item; var name = nil; - if (_vec) { + if (isvec(names)) { foreach (var l; names) { if (l[1] == fn) { name = l[0]; break; diff --git a/Nasal/globals.nas b/Nasal/globals.nas index e83cc0809..895156ecc 100644 --- a/Nasal/globals.nas +++ b/Nasal/globals.nas @@ -41,10 +41,12 @@ var assert = func (condition, message=nil) { # (class) object. Example: isa(someObject, props.Node) # var isa = func(obj, class) { - if(typeof(obj) == "hash" and obj["parents"] != nil) - foreach(var c; obj.parents) + if(ishash(obj) and obj["parents"] != nil) { + foreach(var c; obj.parents) { if(c == class or isa(c, class)) return 1; + } + } return 0; } @@ -57,7 +59,7 @@ var isa = func(obj, class) { # var fgcommand = func(cmd, node=nil) { if(isa(node, props.Node)) node = node._g; - elsif(typeof(node) == 'hash') + elsif(ishash(node)) node = props.Node.new(node)._g; _fgcommand(cmd, node); } @@ -95,8 +97,8 @@ var abs = func(v) { return v < 0 ? -v : v } # 0 to wrap the interpolated value properly. # var interpolate = func(node, val...) { - if(isa(node, props.Node)) node = node._g; - elsif(typeof(node) != "scalar" and typeof(node) != "ghost") + if (isa(node, props.Node)) node = node._g; + elsif (!isscalar(node) and !isghost(node)) die("bad argument to interpolate()"); _interpolate(node, val); } @@ -112,8 +114,8 @@ var interpolate = func(node, val...) { # written to" (2). # var setlistener = func(node, fn, init = 0, runtime = 1) { - if(isa(node, props.Node)) node = node._g; - elsif(typeof(node) != "scalar" and typeof(node) != "ghost") + if (isa(node, props.Node)) node = node._g; + elsif (!isscalar(node) and !isghost(node)) die("bad argument to setlistener()"); var id = _setlistener(node, func(chg, lst, mode, is_child) { fn(props.wrapNode(chg), props.wrapNode(lst), mode, is_child); diff --git a/Nasal/io.nas b/Nasal/io.nas index 01674dd49..60f541ff2 100644 --- a/Nasal/io.nas +++ b/Nasal/io.nas @@ -105,7 +105,7 @@ var load_nasal = func(file, module = nil) { if (!contains(globals, module)) globals[module] = {}; - elsif (typeof(globals[module]) != "hash") + elsif (!ishash(globals[module])) die("io.load_nasal(): namespace '" ~ module ~ "' already in use, but not a hash"); var code = call(func compile(readfile(file), file), nil, var err = []); @@ -303,4 +303,3 @@ var writexml = func(path, node, indent = "\t", prefix = "___") { if (size(root) != 1) die("writexml(): tree has more than one root node"); } - diff --git a/Nasal/props.nas b/Nasal/props.nas index 385826c4b..652976de8 100644 --- a/Nasal/props.nas +++ b/Nasal/props.nas @@ -77,7 +77,7 @@ var Node = { resolveAlias : func(p = nil) { if (p == nil) p = me; - elsif (typeof(p) == "scalar") + elsif (isscalar(p)) p = globals.getNode(p); if (isa(p, Node)) { while (p.getAttribute("alias")) { @@ -121,7 +121,7 @@ var Node = { # Node.new = func(values = nil) { var result = wrapNode(_new()); - if(typeof(values) == "hash") + if(ishash(values)) result.setValues(values); return result; } @@ -246,10 +246,9 @@ var copy = func(src, dest, attr = 0) { # array) into Node objects. # var wrap = func(node) { - var argtype = typeof(node); - if(argtype == "ghost") { + if(isghost(node)) { return wrapNode(node); - } elsif(argtype == "vector") { + } elsif(isvec(node)) { var v = node; var n = size(v); for(var i=0; i