nodes: re{name,formatt}ed things

This commit is contained in:
satyr
2010-10-07 09:25:00 +09:00
parent 69b901a5b6
commit f90f1ef8e0
2 changed files with 127 additions and 124 deletions

View File

@@ -1,7 +1,7 @@
(function() {
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, InNode, IndexNode, LiteralNode, NO, NUMBER, ObjectNode, OpNode, ParamNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, SIMPLENUM, Scope, SliceNode, SplatNode, SwitchNode, TAB, THIS, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, YES, _ref, compact, del, ends, flatten, include, indexOf, last, literal, merge, starts, utility;
var __extends = function(child, parent) {
var ctor = function(){};
var ctor = function() {};
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
@@ -89,13 +89,13 @@
return contains;
};
BaseNode.prototype.containsType = function(type) {
return this instanceof type || this.contains(function(n) {
return n instanceof type;
return this instanceof type || this.contains(function(node) {
return node instanceof type;
});
};
BaseNode.prototype.containsPureStatement = function() {
return this.isPureStatement() || this.contains(function(n) {
return (typeof n.isPureStatement !== "function" ? undefined : n.isPureStatement());
return this.isPureStatement() || this.contains(function(node) {
return node.isPureStatement();
});
};
BaseNode.prototype.traverse = function(block) {
@@ -222,7 +222,7 @@
var code;
code = this.compileNode(o);
if (o.scope.hasAssignments(this)) {
code = ("" + (this.tab) + "var " + (o.scope.compiledAssignments()) + ";\n" + code);
code = ("" + (this.tab) + "var " + (o.scope.compiledAssignments().replace(/\n/g, '$&' + this.tab)) + ";\n" + code);
}
if (!o.globals && o.scope.hasDeclarations(this)) {
code = ("" + (this.tab) + "var " + (o.scope.compiledDeclarations()) + ";\n" + code);
@@ -567,7 +567,7 @@
return "" + (this.superReference(o)) + ".call(this" + (args.length ? ', ' : '') + args + ")";
};
CallNode.prototype.compileSplat = function(o) {
var _i, _len, _ref2, a, arg, argvar, b, base, c, call, fun, idt, name, ref, splatargs;
var _i, _len, _ref2, arg, argvar, base, call, ctor, fun, idt, name, ref, result, splatargs;
splatargs = this.compileSplatArguments(o);
if (this.isSuper) {
return ("" + (this.superReference(o)) + ".apply(this, " + splatargs + ")");
@@ -588,8 +588,8 @@
return ("" + fun + ".apply(" + ref + ", " + splatargs + ")");
}
call = 'call(this)';
argvar = function(n) {
return n instanceof LiteralNode && n.value === 'arguments';
argvar = function(node) {
return node instanceof LiteralNode && node.value === 'arguments';
};
_ref2 = this.args;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
@@ -599,10 +599,10 @@
break;
}
}
a = o.scope.freeVariable('ctor');
b = o.scope.freeVariable('ref');
c = o.scope.freeVariable('result');
return "(function() {\n" + (idt = this.idt(1)) + "var ctor = function() {};\n" + idt + (utility('extends')) + "(ctor, " + a + " = " + (this.variable.compile(o)) + ");\n" + idt + "return typeof (" + c + " = " + a + ".apply(" + b + " = new ctor, " + splatargs + ")) === \"object\" ? " + c + " : " + b + ";\n" + (this.tab) + "})." + call;
ctor = o.scope.freeVariable('ctor');
ref = o.scope.freeVariable('ref');
result = o.scope.freeVariable('result');
return "(function() {\n" + (idt = this.idt(1)) + "var ctor = function() {};\n" + idt + (utility('extends')) + "(ctor, " + ctor + " = " + (this.variable.compile(o)) + ");\n" + idt + "return typeof (" + result + " = " + ctor + ".apply(" + ref + " = new ctor, " + splatargs + ")) === \"object\" ? " + result + " : " + ref + ";\n" + (this.tab) + "})." + call;
};
return CallNode;
})();
@@ -873,11 +873,11 @@
})();
exports.ClassNode = (function() {
ClassNode = (function() {
return function ClassNode(_arg, _arg2, _arg3) {
this.properties = _arg3;
this.parent = _arg2;
this.variable = _arg;
return function ClassNode(variable, _arg, _arg2) {
this.properties = _arg2;
this.parent = _arg;
ClassNode.__super__.constructor.call(this);
this.variable = variable === '__temp__' ? literal(variable) : variable;
this.properties || (this.properties = []);
this.returns = false;
return this;
@@ -891,15 +891,16 @@
return this;
};
ClassNode.prototype.compileNode = function(o) {
var _i, _len, _ref2, _ref3, access, applied, className, constScope, construct, constructor, extension, func, me, pname, prop, props, pvar, returns, val;
if (this.variable === '__temp__') {
this.variable = literal(o.scope.freeVariable('ctor'));
var _i, _len, _ref2, _ref3, access, applied, className, constScope, construct, constructor, extension, func, me, pname, prop, props, pvar, returns, val, variable;
variable = this.variable;
if (variable.value === '__temp__') {
variable = literal(o.scope.freeVariable('ctor'));
}
extension = this.parent && new ExtendsNode(this.variable, this.parent);
extension = this.parent && new ExtendsNode(variable, this.parent);
props = new Expressions;
o.top = true;
me = null;
className = this.variable.compile(o);
className = variable.compile(o);
constScope = null;
if (this.parent) {
applied = new ValueNode(this.parent, [new AccessorNode(literal('apply'))]);
@@ -917,8 +918,8 @@
}
func.name = className;
func.body.push(new ReturnNode(literal('this')));
this.variable = new ValueNode(this.variable);
this.variable.namespaced = include(func.name, '.');
variable = new ValueNode(variable);
variable.namespaced = include(func.name, '.');
constructor = func;
continue;
}
@@ -938,7 +939,7 @@
}
if (pvar) {
access = prop.context === 'this' ? pvar.base.properties[0] : new AccessorNode(pvar, 'prototype');
val = new ValueNode(this.variable, [access]);
val = new ValueNode(variable, [access]);
prop = new AssignNode(val, func);
}
props.push(prop);
@@ -947,12 +948,12 @@
if (me) {
constructor.body.unshift(literal("" + me + " = this"));
}
construct = this.idt() + (new AssignNode(this.variable, constructor)).compile(merge(o, {
construct = this.idt() + new AssignNode(variable, constructor).compile(merge(o, {
sharedScope: constScope
})) + ';';
props = !props.empty() ? '\n' + props.compile(o) : '';
extension = extension ? '\n' + this.idt() + extension.compile(o) + ';' : '';
returns = this.returns ? '\n' + new ReturnNode(this.variable).compile(o) : '';
returns = this.returns ? '\n' + new ReturnNode(variable).compile(o) : '';
return construct + extension + props + returns;
};
return ClassNode;
@@ -1640,8 +1641,8 @@
topLevel = del(o, 'top') && !this.returns;
range = this.source instanceof ValueNode && this.source.base instanceof RangeNode && !this.source.properties.length;
source = range ? this.source.base : this.source;
codeInBody = this.body.contains(function(n) {
return n instanceof CodeNode;
codeInBody = this.body.contains(function(node) {
return node instanceof CodeNode;
});
scope = o.scope;
name = this.name && this.name.compile(o);
@@ -1849,7 +1850,7 @@
})().join(' || ');
};
IfNode.prototype.compileNode = function(o) {
return this.isStatement(o) ? this.compileStatement(o) : this.compileTernary(o);
return this.isStatement(o) ? this.compileStatement(o) : this.compileExpression(o);
};
IfNode.prototype.makeReturn = function() {
if (this.isStatement()) {
@@ -1883,7 +1884,7 @@
})) : (" else {\n" + (this.elseBody.compile(o)) + "\n" + (this.tab) + "}");
return "" + ifPart + elsePart;
};
IfNode.prototype.compileTernary = function(o) {
IfNode.prototype.compileExpression = function(o) {
var code, elsePart, ifPart;
this.bodyNode().tags.operation = (this.condition.tags.operation = true);
if (this.elseBody) {
@@ -1896,31 +1897,23 @@
};
return IfNode;
})();
PushNode = (exports.PushNode = {
wrap: function(array, expressions) {
var expr;
expr = expressions.unwrap();
if (expr.isPureStatement() || expr.containsPureStatement()) {
PushNode = {
wrap: function(name, expressions) {
if (expressions.empty() || expressions.containsPureStatement()) {
return expressions;
}
return Expressions.wrap([new CallNode(new ValueNode(literal(array), [new AccessorNode(literal('push'))]), [expr])]);
return Expressions.wrap([new CallNode(new ValueNode(literal(name), [new AccessorNode(literal('push'))]), [expressions.unwrap()])]);
}
});
ClosureNode = (exports.ClosureNode = {
};
ClosureNode = {
wrap: function(expressions, statement) {
var args, call, func, mentionsArgs, mentionsThis, meth;
var args, call, func, mentionsArgs, meth;
if (expressions.containsPureStatement()) {
return expressions;
}
func = new ParentheticalNode(new CodeNode([], Expressions.wrap([expressions])));
args = [];
mentionsArgs = expressions.contains(function(n) {
return n instanceof LiteralNode && (n.value === 'arguments');
});
mentionsThis = expressions.contains(function(n) {
return (n instanceof LiteralNode && (n.value === 'this')) || (n instanceof CodeNode && n.bound);
});
if (mentionsArgs || mentionsThis) {
if ((mentionsArgs = expressions.contains(this.literalArgs)) || (expressions.contains(this.literalThis))) {
meth = literal(mentionsArgs ? 'apply' : 'call');
args = [literal('this')];
if (mentionsArgs) {
@@ -1930,11 +1923,17 @@
}
call = new CallNode(func, args);
return statement ? Expressions.wrap([call]) : call;
},
literalArgs: function(node) {
return node instanceof LiteralNode && node.value === 'arguments';
},
literalThis: function(node) {
return node instanceof LiteralNode && node.value === 'this' || node instanceof CodeNode && node.bound;
}
});
};
UTILITIES = {
"extends": "function(child, parent) {\n var ctor = function(){};\n ctor.prototype = parent.prototype;\n child.prototype = new ctor();\n child.prototype.constructor = child;\n if (typeof parent.extended === \"function\") parent.extended(child);\n child.__super__ = parent.prototype;\n }",
bind: "function(func, context) {\n return function(){ return func.apply(context, arguments); };\n }",
"extends": 'function(child, parent) {\n var ctor = function() {};\n ctor.prototype = parent.prototype;\n child.prototype = new ctor();\n child.prototype.constructor = child;\n if (typeof parent.extended === "function") parent.extended(child);\n child.__super__ = parent.prototype;\n}',
bind: 'function(func, context) {\n return function() { return func.apply(context, arguments); };\n}',
hasProp: 'Object.prototype.hasOwnProperty',
slice: 'Array.prototype.slice'
};