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. # Turn p into props.Node (if it isn't yet), or return nil.
# #
var propify = func(p, create = 0) { var propify = func(p, create = 0) {
var type = typeof(p); if (isghost(p) and ghosttype(p) == "prop")
if (type == "ghost" and ghosttype(p) == "prop")
return props.wrapNode(p); return props.wrapNode(p);
if (type == "scalar" and num(p) == nil) if (isscalar(p) and num(p) == nil)
return props.globals.getNode(p, create); return props.globals.getNode(p, create);
if (isa(p, props.Node)) if (isa(p, props.Node))
return p; return p;
@ -233,17 +232,17 @@ var string = func(o, color=nil, ttl=5) {
if (t == "nil") { if (t == "nil") {
return _nil("null", color); return _nil("null", color);
} elsif (t == "scalar") { } elsif (isscalar(o)) {
return num(o) == nil ? _dump_string(o, color) : _num(o~"", color); return num(o) == nil ? _dump_string(o, color) : _num(o~"", color);
} elsif (t == "vector") { } elsif (isvec(o)) {
var s = ""; var s = "";
forindex (var i; o) forindex (var i; o)
s ~= (i == 0 ? "" : ", ") ~ debug.string(o[i], color, ttl - 1); s ~= (i == 0 ? "" : ", ") ~ debug.string(o[i], color, ttl - 1);
return _bracket("[", color) ~ s ~ _bracket("]", color); return _bracket("[", color) ~ s ~ _bracket("]", color);
} elsif (t == "hash") { } elsif (ishash(o)) {
if (contains(o, "parents") and typeof(o.parents) == "vector" if (contains(o, "parents") and isvec(o.parents)
and size(o.parents) == 1 and o.parents[0] == props.Node) and size(o.parents) == 1 and o.parents[0] == props.Node)
return _angle("'<", color) ~ _dump_prop(o, color) ~ _angle(">'", color); 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); s ~= (i == 0 ? "" : ", ") ~ _dump_key(k[i], color) ~ ": " ~ debug.string(o[k[i]], color, ttl - 1);
return _brace("{", color) ~ " " ~ s ~ " " ~ _brace("}", color); return _brace("{", color) ~ " " ~ s ~ " " ~ _brace("}", color);
} elsif (t == "ghost") { } elsif (isghost(o)) {
return _angle("'<", color) ~ _nil(ghosttype(o), color) ~ _angle(">'", color); return _angle("'<", color) ~ _nil(ghosttype(o), color) ~ _angle(">'", color);
} else { } else {
@ -356,7 +355,7 @@ var benchmark = func(label, fn, repeat = nil, output=nil) {
if (repeat == nil) { if (repeat == nil) {
start = systime(); start = systime();
output = fn(); output = fn();
} elsif (typeof(output) == 'vector') { } elsif (isvec(output)) {
start = systime(); start = systime();
for (var i = 0; i < repeat; i += 1) for (var i = 0; i < repeat; i += 1)
append(output, fn()); append(output, fn());
@ -375,7 +374,7 @@ var benchmark_time = func(fn, repeat = 1, output = nil) {
if (repeat == nil) { if (repeat == nil) {
start = systime(); start = systime();
output = fn(); output = fn();
} elsif (typeof(output) == 'vector') { } elsif (isvec(output)) {
start = systime(); start = systime();
for (var i = 0; i < repeat; i += 1) for (var i = 0; i < repeat; i += 1)
append(output, fn()); append(output, fn());
@ -402,14 +401,13 @@ var rank = func(list, repeat = nil) {
} }
var print_rank = func(label, list, names) { var print_rank = func(label, list, names) {
var _vec = (typeof(names) == 'vector');
print("Test results for "~label); print("Test results for "~label);
var first = 1; var first = 1;
var longest = list[-1][1]; var longest = list[-1][1];
foreach (var item; list) { foreach (var item; list) {
var (fn, time) = item; var (fn, time) = item;
var name = nil; var name = nil;
if (_vec) { if (isvec(names)) {
foreach (var l; names) { foreach (var l; names) {
if (l[1] == fn) { if (l[1] == fn) {
name = l[0]; break; name = l[0]; break;

View file

@ -41,10 +41,12 @@ var assert = func (condition, message=nil) {
# (class) object. Example: isa(someObject, props.Node) # (class) object. Example: isa(someObject, props.Node)
# #
var isa = func(obj, class) { var isa = func(obj, class) {
if(typeof(obj) == "hash" and obj["parents"] != nil) if(ishash(obj) and obj["parents"] != nil) {
foreach(var c; obj.parents) foreach(var c; obj.parents) {
if(c == class or isa(c, class)) if(c == class or isa(c, class))
return 1; return 1;
}
}
return 0; return 0;
} }
@ -57,7 +59,7 @@ var isa = func(obj, class) {
# #
var fgcommand = func(cmd, node=nil) { var fgcommand = func(cmd, node=nil) {
if(isa(node, props.Node)) node = node._g; if(isa(node, props.Node)) node = node._g;
elsif(typeof(node) == 'hash') elsif(ishash(node))
node = props.Node.new(node)._g; node = props.Node.new(node)._g;
_fgcommand(cmd, node); _fgcommand(cmd, node);
} }
@ -95,8 +97,8 @@ var abs = func(v) { return v < 0 ? -v : v }
# 0 to wrap the interpolated value properly. # 0 to wrap the interpolated value properly.
# #
var interpolate = func(node, val...) { var interpolate = func(node, val...) {
if(isa(node, props.Node)) node = node._g; if (isa(node, props.Node)) node = node._g;
elsif(typeof(node) != "scalar" and typeof(node) != "ghost") elsif (!isscalar(node) and !isghost(node))
die("bad argument to interpolate()"); die("bad argument to interpolate()");
_interpolate(node, val); _interpolate(node, val);
} }
@ -112,8 +114,8 @@ var interpolate = func(node, val...) {
# written to" (2). # written to" (2).
# #
var setlistener = func(node, fn, init = 0, runtime = 1) { var setlistener = func(node, fn, init = 0, runtime = 1) {
if(isa(node, props.Node)) node = node._g; if (isa(node, props.Node)) node = node._g;
elsif(typeof(node) != "scalar" and typeof(node) != "ghost") elsif (!isscalar(node) and !isghost(node))
die("bad argument to setlistener()"); die("bad argument to setlistener()");
var id = _setlistener(node, func(chg, lst, mode, is_child) { var id = _setlistener(node, func(chg, lst, mode, is_child) {
fn(props.wrapNode(chg), props.wrapNode(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)) if (!contains(globals, module))
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"); die("io.load_nasal(): namespace '" ~ module ~ "' already in use, but not a hash");
var code = call(func compile(readfile(file), file), nil, var err = []); 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) if (size(root) != 1)
die("writexml(): tree has more than one root node"); die("writexml(): tree has more than one root node");
} }

View file

@ -77,7 +77,7 @@ var Node = {
resolveAlias : func(p = nil) { resolveAlias : func(p = nil) {
if (p == nil) if (p == nil)
p = me; p = me;
elsif (typeof(p) == "scalar") elsif (isscalar(p))
p = globals.getNode(p); p = globals.getNode(p);
if (isa(p, Node)) { if (isa(p, Node)) {
while (p.getAttribute("alias")) { while (p.getAttribute("alias")) {
@ -121,7 +121,7 @@ var Node = {
# #
Node.new = func(values = nil) { Node.new = func(values = nil) {
var result = wrapNode(_new()); var result = wrapNode(_new());
if(typeof(values) == "hash") if(ishash(values))
result.setValues(values); result.setValues(values);
return result; return result;
} }
@ -246,10 +246,9 @@ var copy = func(src, dest, attr = 0) {
# array) into Node objects. # array) into Node objects.
# #
var wrap = func(node) { var wrap = func(node) {
var argtype = typeof(node); if(isghost(node)) {
if(argtype == "ghost") {
return wrapNode(node); return wrapNode(node);
} elsif(argtype == "vector") { } elsif(isvec(node)) {
var v = node; var v = node;
var n = size(v); var n = size(v);
for(var i=0; i<n; i+=1) { v[i] = wrapNode(v[i]); } 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) { if (namespace == nil) {
namespace = caller(1)[0]; namespace = caller(1)[0];
} }
if (typeof(namespace) != "hash") { if (!ishash(namespace)) {
logprint(LOG_WARN, "createNodeObjectsFromHash: Error, namespace argument is not a hash."); logprint(LOG_WARN, "createNodeObjectsFromHash: Error, namespace argument is not a hash.");
return nil; return nil;
} }
if (typeof(property_list) != "hash") { if (!ishash(property_list)) {
logprint(LOG_WARN, "createNodeObjectsFromHash: Error, property_list argument is not a hash."); logprint(LOG_WARN, "createNodeObjectsFromHash: Error, property_list argument is not a hash.");
return nil; return nil;
} }
@ -330,20 +329,19 @@ var createNodeObjectsFromHash = func (property_list, namespace = nil) {
var nodeList = func { var nodeList = func {
var list = []; var list = [];
foreach(var a; arg) { foreach(var a; arg) {
var t = typeof(a);
if(isa(a, Node)) if(isa(a, Node))
append(list, a); append(list, a);
elsif(t == "scalar") elsif(isscalar(a))
append(list, props.globals.getNode(a, 1)); append(list, props.globals.getNode(a, 1));
elsif(t == "vector") elsif(isvec(a))
foreach(var i; a) foreach(var i; a)
list ~= nodeList(i); list ~= nodeList(i);
elsif(t == "hash") elsif(ishash(a))
foreach(var i; keys(a)) foreach(var i; keys(a))
list ~= nodeList(a[i]); list ~= nodeList(a[i]);
elsif(t == "func") elsif(isfunc(a))
list ~= nodeList(a()); list ~= nodeList(a());
elsif(t == "ghost" and ghosttype(a) == "prop") elsif(isghost(a) and ghosttype(a) == "prop")
append(list, wrapNode(a)); append(list, wrapNode(a));
else else
die("nodeList: invalid nil property"); die("nodeList: invalid nil property");
@ -501,9 +499,9 @@ var runBinding = func(node, module = nil) {
# #
var UpdateManager = var UpdateManager =
{ {
_updateProperty : func(_property) _updateProperty : func(_property)
{ {
}, },
FromProperty : func(_propname, _delta, _changed_method) FromProperty : func(_propname, _delta, _changed_method)
{ {
var obj = {parents : [UpdateManager] }; var obj = {parents : [UpdateManager] };