1
0
Fork 0

replace typeof() by is<type>()

Signed-off-by: Stuart Buchanan <stuart_d_buchanan@yahoo.co.uk>
This commit is contained in:
Henning Stahlke 2020-05-29 20:04:49 +02:00 committed by Stuart Buchanan
parent 148328d793
commit b5f00cb50e
4 changed files with 34 additions and 37 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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");
}

View file

@ -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<n; i+=1) { v[i] = wrapNode(v[i]); }
@ -307,11 +306,11 @@ var createNodeObjectsFromHash = func (property_list, namespace = nil) {
if (namespace == nil) {
namespace = caller(1)[0];
}
if (typeof(namespace) != "hash") {
if (!ishash(namespace)) {
logprint(LOG_WARN, "createNodeObjectsFromHash: Error, namespace argument is not a hash.");
return nil;
}
if (typeof(property_list) != "hash") {
if (!ishash(property_list)) {
logprint(LOG_WARN, "createNodeObjectsFromHash: Error, property_list argument is not a hash.");
return nil;
}
@ -330,20 +329,19 @@ var createNodeObjectsFromHash = func (property_list, namespace = nil) {
var nodeList = func {
var list = [];
foreach(var a; arg) {
var t = typeof(a);
if(isa(a, Node))
append(list, a);
elsif(t == "scalar")
elsif(isscalar(a))
append(list, props.globals.getNode(a, 1));
elsif(t == "vector")
elsif(isvec(a))
foreach(var i; a)
list ~= nodeList(i);
elsif(t == "hash")
elsif(ishash(a))
foreach(var i; keys(a))
list ~= nodeList(a[i]);
elsif(t == "func")
elsif(isfunc(a))
list ~= nodeList(a());
elsif(t == "ghost" and ghosttype(a) == "prop")
elsif(isghost(a) and ghosttype(a) == "prop")
append(list, wrapNode(a));
else
die("nodeList: invalid nil property");
@ -501,9 +499,9 @@ var runBinding = func(node, module = nil) {
#
var UpdateManager =
{
_updateProperty : func(_property)
{
},
_updateProperty : func(_property)
{
},
FromProperty : func(_propname, _delta, _changed_method)
{
var obj = {parents : [UpdateManager] };