CoffeeScript-in-CoffeeScript just had it's first self-compiled snippet.

This commit is contained in:
Jeremy Ashkenas
2010-02-08 22:22:59 -05:00
parent d5c98165ea
commit 0b5b6113ee
7 changed files with 312 additions and 109 deletions

View File

@@ -1,5 +1,5 @@
(function(){
var Expressions, LiteralNode, Node, TAB, TRAILING_WHITESPACE, compact, del, dup, flatten, inherit, merge, statement;
var CommentNode, Expressions, LiteralNode, Node, ReturnNode, TAB, TRAILING_WHITESPACE, ValueNode, compact, del, dup, flatten, inherit, merge, statement;
var __hasProp = Object.prototype.hasOwnProperty;
process.mixin(require('./scope'));
// The abstract base class for all CoffeeScript nodes.
@@ -86,6 +86,13 @@
__a = this.values = arguments;
return SliceNode === this.constructor ? this : __a;
};
exports.ThisNode = function ThisNode() {
var __a;
var arguments = Array.prototype.slice.call(arguments, 0);
this.name = this.constructor.name;
__a = this.values = arguments;
return ThisNode === this.constructor ? this : __a;
};
exports.AssignNode = function AssignNode() {
var __a;
var arguments = Array.prototype.slice.call(arguments, 0);
@@ -200,18 +207,18 @@
TRAILING_WHITESPACE = /\s+$/g;
// Flatten nested arrays recursively.
flatten = function flatten(list) {
var __a, __b, __c, item, memo;
var __a, __b, item, memo;
memo = [];
__a = []; __b = list;
for (__c = 0; __c < __b.length; __c++) {
item = __b[__c];
__a = list;
for (__b = 0; __b < __a.length; __b++) {
item = __a[__b];
if (item instanceof Array) {
return memo.concat(flatten(item));
}
memo.push(item);
memo;
}
return __a;
return memo;
};
// Remove all null values from an array.
compact = function compact(input) {
@@ -273,8 +280,7 @@
// Quickie inheritance convenience wrapper to reduce typing.
inherit = function inherit(parent, props) {
var __a, __b, klass, name, prop;
klass = props.constructor;
delete props.constructor;
klass = del(props, 'constructor');
__a = function(){};
__a.prototype = parent.prototype;
klass.__superClass__ = parent.prototype;
@@ -299,11 +305,11 @@
klass.prototype.is_statement = function is_statement() {
return true;
};
return klass.prototype.is_statement_only = function is_statement_only() {
if (only) {
if (only) {
return ((klass.prototype.is_statement_only = function is_statement_only() {
return true;
}
};
}));
}
};
// The abstract base class for all CoffeeScript nodes.
// All nodes are implement a "compile_node" method, which performs the
@@ -322,7 +328,7 @@
this.options = dup(o || {
});
this.indent = o.indent;
top = this.top_sensitive() ? o.top : del(obj('top'));
top = this.top_sensitive() ? o.top : del(o, 'top');
closure = this.is_statement() && !this.is_statement_only() && !top && !o.returns && !this instanceof CommentNode && !this.contains(function(node) {
return node.is_statement_only();
});
@@ -338,7 +344,7 @@
// Quick short method for the current indentation level, plus tabbing in.
Node.prototype.idt = function idt(tabs) {
var __a, __b, __c, __d, i, idt;
idt = this.indent;
idt = (this.indent || '');
__c = 0; __d = (tabs || 0);
for (__b=0, i=__c; (__c <= __d ? i <= __d : i >= __d); (__c <= __d ? i += 1 : i -= 1), __b++) {
idt += TAB;
@@ -436,7 +442,7 @@
var args, argv, code;
code = this.compile_node(o);
args = this.contains(function(node) {
return node instanceof ValueNode && node.arguments();
return node instanceof ValueNode && node.is_arguments();
});
argv = args && o.scope.check('arguments') ? '' : 'var ';
if (args) {
@@ -456,8 +462,7 @@
this.indent = o.indent;
stmt = node.is_statement();
// We need to return the result if this is the last node in the expressions body.
returns = o.returns && this.is_last(node) && !node.is_statement_only();
delete o.returns;
returns = del(o, 'returns') && this.is_last(node) && !node.is_statement_only();
// Return the regular compile of the node, unless we need to return the result.
if (!(returns)) {
return (stmt ? '' : this.idt()) + node.compile(merge(o, {
@@ -492,7 +497,8 @@
LiteralNode = (exports.LiteralNode = inherit(Node, {
constructor: function constructor(value) {
this.value = value;
return this.children = [value];
this.children = [value];
return this;
},
// Break and continue must be treated as statements -- they lose their meaning
// when wrapped in a closure.
@@ -507,4 +513,103 @@
}
}));
LiteralNode.prototype.is_statement_only = LiteralNode.prototype.is_statement;
// Return an expression, or wrap it in a closure and return it.
ReturnNode = (exports.ReturnNode = inherit(Node, {
constructor: function constructor(expression) {
this.expression = expression;
this.children = [expression];
return this;
},
compile_node: function compile_node(o) {
if (this.expression.is_statement()) {
return this.expression.compile(merge(o, {
returns: true
}));
}
return this.idt() + 'return ' + this.expression.compile(o) + ';';
}
}));
statement(ReturnNode, true);
// A value, indexed or dotted into, or vanilla.
ValueNode = (exports.ValueNode = inherit(Node, {
SOAK: " == undefined ? undefined : ",
constructor: function constructor(base, properties) {
this.base = base;
this.properties = flatten(properties || []);
this.children = flatten(this.base, this.properties);
return this;
},
push: function push(prop) {
this.properties.push(prop);
return this.children.push(prop);
},
has_properties: function has_properties() {
return this.properties.length || this.base instanceof ThisNode;
},
is_array: function is_array() {
return this.base instanceof ArrayNode && !this.has_properties();
},
is_object: function is_object() {
return this.base instanceof ObjectNode && !this.has_properties();
},
is_splice: function is_splice() {
return this.has_properties() && this.properties[this.properties.length - 1] instanceof SliceNode;
},
is_arguments: function is_arguments() {
return this.base === 'arguments';
},
unwrap: function unwrap() {
return this.properties.length ? this : this.base;
},
// Values are statements if their base is a statement.
is_statement: function is_statement() {
return this.base.is_statement && this.base.is_statement() && !this.has_properties();
},
compile_node: function compile_node(o) {
var __a, __b, baseline, code, only, part, parts, prop, props, soaked, temp;
soaked = false;
only = del(o, 'only_first');
props = only ? this.properties.slice(0, this.properties.length) : this.properties;
baseline = this.base.compile(o);
parts = [baseline];
__a = props;
for (__b = 0; __b < __a.length; __b++) {
prop = __a[__b];
if (prop instanceof AccessorNode && prop.soak) {
soaked = true;
if (this.base instanceof CallNode && prop === props[0]) {
temp = o.scope.free_variable();
parts[parts.length - 1] = '(' + temp + ' = ' + baseline + ')' + this.SOAK + ((baseline = temp + prop.compile(o)));
} else {
parts[parts.length - 1] = this.SOAK + (baseline += prop.compile(o));
}
} else {
part = prop.compile(o);
baseline += part;
parts.push(part);
}
}
this.last = parts[parts.length - 1];
this.source = parts.length > 1 ? parts.slice(0, parts.length).join('') : null;
code = parts.join('').replace(/\)\(\)\)/, '()))');
if (!(soaked)) {
return code;
}
return '(' + code + ')';
}
}));
// Pass through CoffeeScript comments into JavaScript comments at the
// same position.
CommentNode = (exports.CommentNode = inherit(Node, {
constructor: function constructor(lines) {
this.lines = lines;
return this;
},
compile_node: function compile_node(o) {
var delimiter;
delimiter = "\n" + this.idt() + '//';
return delimiter + this.lines.join(delimiter);
}
}));
statement(CommentNode);
})();