rewrite the constructor for ast

This commit is contained in:
yimi 2017-09-18 06:47:05 +00:00
parent aceb0af36b
commit 6546aaa704

View File

@ -47,25 +47,28 @@ function DEFNODE(type, props, methods, base) {
if (arguments.length < 4) base = AST_Node; if (arguments.length < 4) base = AST_Node;
if (!props) props = []; if (!props) props = [];
else props = props.split(/\s+/); else props = props.split(/\s+/);
var self_props = props; var ctor = function (props) {
if (base && base.PROPS) if (props) {
props = props.concat(base.PROPS); var ctor = this.CTOR;
var code = "return function AST_" + type + "(props){ if (props) { "; do {
for (var i = props.length; --i >= 0;) { var self_props = ctor.SELF_PROPS;
code += "this." + props[i] + " = props." + props[i] + ";"; for (var i = self_props.length; i;) {
var k = self_props[--i];
this[k] = props[k];
}
} while (ctor = ctor.BASE)
if (this.initialize) {
this.initialize();
}
}
} }
var proto = base && new base; var proto = base && new base;
if (proto && proto.initialize || (methods && methods.initialize))
code += "this.initialize();";
code += "}}";
var ctor = new Function(code)();
if (proto) { if (proto) {
ctor.prototype = proto; ctor.prototype = proto;
ctor.BASE = base; ctor.BASE = base;
} }
if (base) base.SUBCLASSES.push(ctor); if (base) base.SUBCLASSES.push(ctor);
ctor.prototype.CTOR = ctor; ctor.prototype.CTOR = ctor;
ctor.PROPS = props || null;
ctor.SELF_PROPS = self_props; ctor.SELF_PROPS = self_props;
ctor.SUBCLASSES = []; ctor.SUBCLASSES = [];
if (type) { if (type) {