The moment of truth: CoffeeScript has now compiled itself ten times over.

This commit is contained in:
Jeremy Ashkenas
2010-02-13 01:13:08 -05:00
parent 126f6c2d88
commit b965fcf32d
6 changed files with 24 additions and 35 deletions

View File

@@ -27,8 +27,7 @@
exports.compile = function compile(script, source) { exports.compile = function compile(script, source) {
var options; var options;
source = source || 'error'; source = source || 'error';
options = { options = {};
};
if (this.options.no_wrap) { if (this.options.no_wrap) {
options.no_wrap = true; options.no_wrap = true;
} }
@@ -94,8 +93,7 @@
// Use OptionParser for all the options. // Use OptionParser for all the options.
exports.parse_options = function parse_options() { exports.parse_options = function parse_options() {
var oparser, opts, paths; var oparser, opts, paths;
opts = (this.options = { opts = (this.options = {});
});
oparser = (this.option_parser = new optparse.OptionParser(SWITCHES)); oparser = (this.option_parser = new optparse.OptionParser(SWITCHES));
oparser.add = oparser['on']; oparser.add = oparser['on'];
oparser.add('interactive', function() { oparser.add('interactive', function() {

View File

@@ -519,8 +519,7 @@
}; };
// Helpers ============================================================== // Helpers ==============================================================
// Make the Jison parser. // Make the Jison parser.
bnf = { bnf = {};
};
tokens = []; tokens = [];
__a = grammar; __a = grammar;
for (name in __a) { for (name in __a) {

View File

@@ -5,7 +5,7 @@
// Some helper functions // Some helper functions
// Tabs are two spaces for pretty printing. // Tabs are two spaces for pretty printing.
TAB = ' '; TAB = ' ';
TRAILING_WHITESPACE = /\s+$/g; TRAILING_WHITESPACE = /\s+$/gm;
// Keep the identifier regex in sync with the Lexer. // Keep the identifier regex in sync with the Lexer.
IDENTIFIER = /^[a-zA-Z$_](\w|\$)*$/; IDENTIFIER = /^[a-zA-Z$_](\w|\$)*$/;
// Flatten nested arrays recursively. // Flatten nested arrays recursively.
@@ -46,8 +46,7 @@
} }
return __a; return __a;
} else { } else {
output = { output = {};
};
__d = input; __d = input;
for (key in __d) { for (key in __d) {
val = __d[key]; val = __d[key];
@@ -61,8 +60,7 @@
// Merge objects. // Merge objects.
merge = function merge(src, dest) { merge = function merge(src, dest) {
var __a, __b, fresh, key, val; var __a, __b, fresh, key, val;
fresh = { fresh = {};
};
__a = src; __a = src;
for (key in __a) { for (key in __a) {
val = __a[key]; val = __a[key];
@@ -144,8 +142,7 @@
// already been asked to return the result. // already been asked to return the result.
Node.prototype.compile = function compile(o) { Node.prototype.compile = function compile(o) {
var closure, top; var closure, top;
this.options = dup(o || { this.options = dup(o || {});
});
this.indent = o.indent; this.indent = o.indent;
top = this.top_sensitive() ? this.options.top : del(this.options, 'top'); top = this.top_sensitive() ? this.options.top : del(this.options, 'top');
closure = this.is_statement() && !this.is_statement_only() && !top && !this.options.returns && !(this instanceof CommentNode) && !this.contains(function(node) { closure = this.is_statement() && !this.is_statement_only() && !top && !this.options.returns && !(this instanceof CommentNode) && !this.contains(function(node) {
@@ -245,8 +242,7 @@
return node === this.expressions[l - last_index]; return node === this.expressions[l - last_index];
}, },
compile: function compile(o) { compile: function compile(o) {
o = o || { o = o || {};
};
return o.scope ? Node.prototype.compile.call(this, o) : this.compile_root(o); return o.scope ? Node.prototype.compile.call(this, o) : this.compile_root(o);
}, },
// Compile each expression in the Expressions body. // Compile each expression in the Expressions body.
@@ -525,11 +521,11 @@
}, },
// Hooking one constructor into another's prototype chain. // Hooking one constructor into another's prototype chain.
compile_node: function compile_node(o) { compile_node: function compile_node(o) {
var child, constructor, parent; var child, construct, parent;
constructor = o.scope.free_variable(); construct = o.scope.free_variable();
child = this.child.compile(o); child = this.child.compile(o);
parent = this.parent.compile(o); parent = this.parent.compile(o);
return this.idt() + constructor + ' = function(){};\n' + this.idt() + constructor + '.prototype = ' + parent + ".prototype;\n" + this.idt() + child + '.__superClass__ = ' + parent + ".prototype;\n" + this.idt() + child + '.prototype = new ' + constructor + "();\n" + this.idt() + child + '.prototype.constructor = ' + child + ';'; return this.idt() + construct + ' = function(){};\n' + this.idt() + construct + '.prototype = ' + parent + ".prototype;\n" + this.idt() + child + '.__superClass__ = ' + parent + ".prototype;\n" + this.idt() + child + '.prototype = new ' + construct + "();\n" + this.idt() + child + '.prototype.constructor = ' + child + ';';
} }
})); }));
statement(ExtendsNode); statement(ExtendsNode);
@@ -1215,8 +1211,7 @@
this.body = body && body.unwrap(); this.body = body && body.unwrap();
this.else_body = else_body && else_body.unwrap(); this.else_body = else_body && else_body.unwrap();
this.children = compact([this.condition, this.body, this.else_body]); this.children = compact([this.condition, this.body, this.else_body]);
this.tags = tags || { this.tags = tags || {};
};
if (this.condition instanceof Array) { if (this.condition instanceof Array) {
this.multiple = true; this.multiple = true;
} }

View File

@@ -32,8 +32,7 @@
IMPLICIT_END = ['IF', 'UNLESS', 'FOR', 'WHILE', 'TERMINATOR', 'OUTDENT']; IMPLICIT_END = ['IF', 'UNLESS', 'FOR', 'WHILE', 'TERMINATOR', 'OUTDENT'];
IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'TRY', 'DELETE', 'TYPEOF', 'SWITCH', 'TRUE', 'FALSE', 'YES', 'NO', 'ON', 'OFF', '!', '!!', 'NOT', '@', '->', '=>', '[', '(', '{']; IMPLICIT_CALL = ['IDENTIFIER', 'NUMBER', 'STRING', 'JS', 'REGEX', 'NEW', 'PARAM_START', 'TRY', 'DELETE', 'TYPEOF', 'SWITCH', 'TRUE', 'FALSE', 'YES', 'NO', 'ON', 'OFF', '!', '!!', 'NOT', '@', '->', '=>', '[', '(', '{'];
// The inverse mappings of token pairs we're trying to fix up. // The inverse mappings of token pairs we're trying to fix up.
INVERSES = { INVERSES = {};
};
__g = BALANCED_PAIRS; __g = BALANCED_PAIRS;
for (__h = 0; __h < __g.length; __h++) { for (__h = 0; __h < __g.length; __h++) {
pair = __g[__h]; pair = __g[__h];
@@ -266,8 +265,7 @@
// the course of the token stream. // the course of the token stream.
re.prototype.ensure_balance = function ensure_balance(pairs) { re.prototype.ensure_balance = function ensure_balance(pairs) {
var __i, __j, key, levels, unclosed, value; var __i, __j, key, levels, unclosed, value;
levels = { levels = {};
};
this.scan_tokens((function(__this) { this.scan_tokens((function(__this) {
var __func = function(prev, token, post, i) { var __func = function(prev, token, post, i) {
var __i, __j, __k, close, open; var __i, __j, __k, close, open;
@@ -322,11 +320,11 @@
// it with the inverse of what we've just popped. // it with the inverse of what we've just popped.
// 3. Keep track of "debt" for tokens that we fake, to make sure we end // 3. Keep track of "debt" for tokens that we fake, to make sure we end
// up balanced in the end. // up balanced in the end.
//
re.prototype.rewrite_closing_parens = function rewrite_closing_parens() { re.prototype.rewrite_closing_parens = function rewrite_closing_parens() {
var __i, debt, key, stack, val; var __i, debt, key, stack, val;
stack = []; stack = [];
debt = { debt = {};
};
__i = INVERSES; __i = INVERSES;
for (key in __i) { for (key in __i) {
val = __i[key]; val = __i[key];

View File

@@ -12,8 +12,7 @@
this.parent = parent; this.parent = parent;
this.expressions = expressions; this.expressions = expressions;
this.method = method; this.method = method;
this.variables = { this.variables = {};
};
this.temp_variable = this.parent ? this.parent.temp_variable : '__a'; this.temp_variable = this.parent ? this.parent.temp_variable : '__a';
return this; return this;
}); });

View File

@@ -4,7 +4,7 @@ process.mixin require './scope'
# Tabs are two spaces for pretty printing. # Tabs are two spaces for pretty printing.
TAB: ' ' TAB: ' '
TRAILING_WHITESPACE: /\s+$/g TRAILING_WHITESPACE: /\s+$/gm
# Keep the identifier regex in sync with the Lexer. # Keep the identifier regex in sync with the Lexer.
IDENTIFIER: /^[a-zA-Z$_](\w|\$)*$/ IDENTIFIER: /^[a-zA-Z$_](\w|\$)*$/
@@ -382,13 +382,13 @@ ExtendsNode: exports.ExtendsNode: inherit Node, {
# Hooking one constructor into another's prototype chain. # Hooking one constructor into another's prototype chain.
compile_node: (o) -> compile_node: (o) ->
constructor: o.scope.free_variable() construct: o.scope.free_variable()
child: @child.compile(o) child: @child.compile(o)
parent: @parent.compile(o) parent: @parent.compile(o)
@idt() + constructor + ' = function(){};\n' + @idt() + @idt() + construct + ' = function(){};\n' + @idt() +
constructor + '.prototype = ' + parent + ".prototype;\n" + @idt() + construct + '.prototype = ' + parent + ".prototype;\n" + @idt() +
child + '.__superClass__ = ' + parent + ".prototype;\n" + @idt() + child + '.__superClass__ = ' + parent + ".prototype;\n" + @idt() +
child + '.prototype = new ' + constructor + "();\n" + @idt() + child + '.prototype = new ' + construct + "();\n" + @idt() +
child + '.prototype.constructor = ' + child + ';' child + '.prototype.constructor = ' + child + ';'
} }