diff --git a/lib/javascript/gflib.js b/lib/javascript/gflib.js index 2097e53ae..5c05259f0 100644 --- a/lib/javascript/gflib.js +++ b/lib/javascript/gflib.js @@ -1,10 +1,7 @@ /* Abstract syntax trees */ - function Fun(name) { this.name = name; - for (var i = 1; i < arguments.length; i++) { - this.setChild(i-1, arguments[i]); - } + this.children = copy_arguments(arguments, 1); } Fun.prototype.print = function () { return this.show(0); } ; Fun.prototype.show = function (prec) { @@ -31,21 +28,13 @@ Fun.prototype.show = function (prec) { } }; Fun.prototype.getChild = function (i) { - return this['arg'+i]; + return this.children[i]; }; Fun.prototype.setChild = function (i,c) { - this['arg'+i] = c; + this.children[i] = c; }; -/* Gets an array containing all the children. Modifying the array does not - change the tree, but modifying the elements of the array does. */ Fun.prototype.getChildren = function () { - var a = new Array(); - for (var i = 0; ; i++) { - var c = this.getChild(i); - if (isUndefined(c)) { break; } - a.push(c); - } - return a; + return this.children; } ; Fun.prototype.isMeta = function() { return this.name == '?'; @@ -110,22 +99,23 @@ Abstract.prototype.annotate = function(tree, type) { } else { var typ = this.types[tree.name]; var cs = tree.getChildren(); - for (var i = 0; i < cs.length; i++) { + for (var i in cs) { this.annotate(cs[i], typ.args[i]); } } return tree; } ; /* Hack to get around the fact that our SISR doesn't build real Fun objects. */ -Abstract.prototype.copyTree = function(x, type) { - return this.annotate(this.copyTree_(x), type); -}; -Abstract.prototype.copyTree_ = function(x) { +Abstract.prototype.copyTree = function(x) { var t = new Fun(x.name); - for (var i = 0; ; i++) { - var c = x['arg' + i]; - if (isUndefined(c)) { break; } - t.setChild(i, this.copyTree(c)); + if (!isUndefined(x.type)) { + t.type = x.type; + } + var cs = x.children; + if (!isUndefined(cs)) { + for (var i in cs) { + t.setChild(i, this.copyTree(cs[i])); + } } return t; } ; @@ -177,7 +167,7 @@ Concrete.prototype.linearizeToTerm = function (tree) { } } else { var cs = tree.getChildren(); - for (var i = 0; i < cs.length; i++) { + for (var i in cs) { cs[i] = this.linearizeToTerm(cs[i]); } return this.rule(tree.name, cs); @@ -203,7 +193,7 @@ function dumpObject (obj) { return obj.toString(); } else if (isArray(obj)) { var x = "["; - for (var i = 0; i < obj.length; i++) { + for (var i in obj) { x += dumpObject(obj[i]); if (i < obj.length-1) { x += ","; @@ -230,7 +220,7 @@ function copy_arguments(args, start) { function join_print(values, glue) { var str = ""; - for (var i = 0; i < values.length; i++) { + for (var i in values) { var s = values[i].print(); if (s.length > 0) { if (str.length > 0) { str += glue; } diff --git a/src/GF/Speech/SISR.hs b/src/GF/Speech/SISR.hs index b74b44076..9e926c72f 100644 --- a/src/GF/Speech/SISR.hs +++ b/src/GF/Speech/SISR.hs @@ -82,7 +82,7 @@ field x y = JS.EMember x (JS.Ident y) ass = JS.EAssign -tree n xs = obj $ [("name", JS.EStr n)] ++ [("arg"++show i, x) | (i,x) <- zip [0..] xs] +tree n xs = obj [("name", JS.EStr n), ("children", JS.EArray xs)] obj ps = JS.EObj [JS.Prop (JS.Ident x) y | (x,y) <- ps]