mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
finished up the CoffeeScript version of the Scope object
This commit is contained in:
parent
69808ba523
commit
b8d22bc572
8 changed files with 244 additions and 143 deletions
|
@ -1,6 +1,7 @@
|
|||
(function(){
|
||||
var Expressions, LiteralNode, Node, TAB, TRAILING_WHITESPACE, compact, del, dup, flatten, inherit, merge, statement;
|
||||
var __hasProp = Object.prototype.hasOwnProperty;
|
||||
process.mixin(require('./scope'));
|
||||
// The abstract base class for all CoffeeScript nodes.
|
||||
// All nodes are implement a "compile_node" method, which performs the
|
||||
// code generation for that node. To compile a node, call the "compile"
|
||||
|
@ -271,7 +272,7 @@
|
|||
};
|
||||
// Quickie inheritance convenience wrapper to reduce typing.
|
||||
inherit = function inherit(parent, props) {
|
||||
var __a, __b, __c, klass, name, prop;
|
||||
var __a, __b, klass, name, prop;
|
||||
klass = props.constructor;
|
||||
delete props.constructor;
|
||||
__a = function(){};
|
||||
|
@ -279,16 +280,13 @@
|
|||
klass.__superClass__ = parent.prototype;
|
||||
klass.prototype = new __a();
|
||||
klass.prototype.constructor = klass;
|
||||
klass.prototype[name] = (function() {
|
||||
__b = []; __c = props;
|
||||
for (name in __c) {
|
||||
prop = __c[name];
|
||||
if (__hasProp.call(__c, name)) {
|
||||
__b.push(prop);
|
||||
}
|
||||
__b = props;
|
||||
for (name in __b) {
|
||||
prop = __b[name];
|
||||
if (__hasProp.call(__b, name)) {
|
||||
((klass.prototype[name] = prop));
|
||||
}
|
||||
return __b;
|
||||
}).call(this);
|
||||
}
|
||||
return klass;
|
||||
};
|
||||
// # Provide a quick implementation of a children method.
|
||||
|
@ -378,20 +376,10 @@
|
|||
};
|
||||
// A collection of nodes, each one representing an expression.
|
||||
Expressions = (exports.Expressions = inherit(Node, {
|
||||
constructor: function constructor() {
|
||||
var nodes;
|
||||
nodes = Array.prototype.slice.call(arguments, 0);
|
||||
constructor: function constructor(nodes) {
|
||||
this.expressions = flatten(nodes);
|
||||
return this.children = this.expressions;
|
||||
},
|
||||
// Wrap up a node as an Expressions, unless it already is.
|
||||
wrap: function wrap() {
|
||||
var nodes;
|
||||
nodes = Array.prototype.slice.call(arguments, 0);
|
||||
if (nodes.length === 1 && nodes[0] instanceof Expressions) {
|
||||
return nodes[0];
|
||||
}
|
||||
return new Expressions.apply(this, nodes);
|
||||
this.children = this.expressions;
|
||||
return this;
|
||||
},
|
||||
// Tack an expression on to the end of this expression list.
|
||||
push: function push(node) {
|
||||
|
@ -483,28 +471,40 @@
|
|||
}));
|
||||
}
|
||||
// If it's not part of a constructor, we can just return the value of the expression.
|
||||
if (!((o.scope.function == undefined ? undefined : o.scope.function.is_constructor()))) {
|
||||
if (!((o.scope.method == undefined ? undefined : o.scope.method.is_constructor()))) {
|
||||
return this.idt() + 'return ' + node.compile(o);
|
||||
}
|
||||
// It's the last line of a constructor, add a safety check.
|
||||
temp = o.scope.free_variable();
|
||||
return this.idt() + temp + ' = ' + node.compile(o) + ";\n" + this.idt() + "return " + o.scope.function.name + ' === this.constructor ? this : ' + temp + ';';
|
||||
return this.idt() + temp + ' = ' + node.compile(o) + ";\n" + this.idt() + "return " + o.scope.method.name + ' === this.constructor ? this : ' + temp + ';';
|
||||
}
|
||||
}));
|
||||
// Wrap up a node as an Expressions, unless it already is one.
|
||||
Expressions.wrap = function wrap(nodes) {
|
||||
if (nodes.length === 1 && nodes[0] instanceof Expressions) {
|
||||
return nodes[0];
|
||||
}
|
||||
return new Expressions(nodes);
|
||||
};
|
||||
statement(Expressions);
|
||||
// Literals are static values that can be passed through directly into
|
||||
// JavaScript without translation, eg.: strings, numbers, true, false, null...
|
||||
LiteralNode = (exports.LiteralNode = function LiteralNode(value) {
|
||||
var __a;
|
||||
this.value = value;
|
||||
__a = this.children = [value];
|
||||
return LiteralNode === this.constructor ? this : __a;
|
||||
});
|
||||
// Break and continue must be treated as statements -- they lose their meaning
|
||||
// when wrapped in a closure.
|
||||
LiteralNode.prototype.is_statement = function is_statement() {
|
||||
return this.value === 'break' || this.value === 'continue';
|
||||
};
|
||||
LiteralNode = (exports.LiteralNode = inherit(Node, {
|
||||
constructor: function constructor(value) {
|
||||
this.value = value;
|
||||
return this.children = [value];
|
||||
},
|
||||
// Break and continue must be treated as statements -- they lose their meaning
|
||||
// when wrapped in a closure.
|
||||
is_statement: function is_statement() {
|
||||
return this.value === 'break' || this.value === 'continue';
|
||||
},
|
||||
compile_node: function compile_node(o) {
|
||||
var end, idt;
|
||||
idt = this.is_statement() ? this.idt() : '';
|
||||
end = this.is_statement() ? ';' : '';
|
||||
return idt + this.value + end;
|
||||
}
|
||||
}));
|
||||
LiteralNode.prototype.is_statement_only = LiteralNode.prototype.is_statement;
|
||||
LiteralNode.prototype.compile_node = function compile_node(o) { };
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue