JavaSscript and SISR abstract syntax now uses an array for the node children, instead of cheesy argN fields.

This commit is contained in:
bringert
2007-01-07 17:18:50 +00:00
parent 133491fd8b
commit d549ce6256
2 changed files with 18 additions and 28 deletions

View File

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

View File

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