1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

made ForNode avoid needless cachings

This commit is contained in:
satyr 2010-10-02 07:26:37 +09:00
parent 341f511bbd
commit 2ea549ce5f
8 changed files with 112 additions and 114 deletions

View file

@ -65,10 +65,10 @@
return fs.stat(source, function(err, stats) {
if (stats.isDirectory()) {
return fs.readdir(source, function(err, files) {
var _j, _len2, _ref3, _result2, file;
_result2 = []; _ref3 = files;
for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) {
file = _ref3[_j];
var _j, _len2, _result2, file;
_result2 = [];
for (_j = 0, _len2 = files.length; _j < _len2; _j++) {
file = files[_j];
_result2.push(compile(path.join(source, file)));
}
return _result2;
@ -188,13 +188,13 @@
return jsl.stdin.end();
};
printTokens = function(tokens) {
var _i, _len, _ref2, _ref3, _result, strings, tag, token, value;
var _i, _len, _ref2, _result, strings, tag, token, value;
strings = (function() {
_result = []; _ref2 = tokens;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
token = _ref2[_i];
_result = [];
for (_i = 0, _len = tokens.length; _i < _len; _i++) {
token = tokens[_i];
_result.push((function() {
_ref3 = [token[0], token[1].toString().replace(/\n/, '\\n')], tag = _ref3[0], value = _ref3[1];
_ref2 = [token[0], token[1].toString().replace(/\n/, '\\n')], tag = _ref2[0], value = _ref2[1];
return "[" + (tag) + " " + (value) + "]";
})());
}

View file

@ -1,5 +1,5 @@
(function() {
var Parser, _i, _j, _len, _len2, _ref, _ref2, _ref3, _result, alt, alternatives, grammar, name, o, operators, token, tokens, unwrap;
var Parser, _i, _j, _len, _len2, _ref, _result, alt, alternatives, grammar, name, o, operators, token, tokens, unwrap;
var __hasProp = Object.prototype.hasOwnProperty;
Parser = require('jison').Parser;
unwrap = /function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/;
@ -612,18 +612,17 @@
};
operators = [["right", '?'], ["left", 'CALL_START', 'CALL_END'], ["nonassoc", '++', '--'], ["right", 'UNARY'], ["left", 'MATH'], ["left", '+', '-'], ["left", 'SHIFT'], ["left", 'COMPARE'], ["left", 'INSTANCEOF'], ["left", '==', '!='], ["left", 'LOGIC'], ["right", 'COMPOUND_ASSIGN'], ["left", '.'], ["nonassoc", 'INDENT', 'OUTDENT'], ["right", 'WHEN', 'LEADING_WHEN', 'IN', 'OF', 'BY', 'THROW'], ["right", 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'EXTENDS'], ["right", '=', ':', 'RETURN'], ["right", '->', '=>', 'UNLESS', 'POST_IF', 'POST_UNLESS']];
tokens = [];
_ref = grammar;
for (name in _ref) {
if (!__hasProp.call(_ref, name)) continue;
alternatives = _ref[name];
for (name in grammar) {
if (!__hasProp.call(grammar, name)) continue;
alternatives = grammar[name];
grammar[name] = (function() {
_result = []; _ref2 = alternatives;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
alt = _ref2[_i];
_result = [];
for (_i = 0, _len = alternatives.length; _i < _len; _i++) {
alt = alternatives[_i];
_result.push((function() {
_ref3 = alt[0].split(' ');
for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) {
token = _ref3[_j];
_ref = alt[0].split(' ');
for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
token = _ref[_j];
if (!(grammar[token])) {
tokens.push(token);
}

View file

@ -3,10 +3,9 @@
indexOf = (exports.indexOf = Array.indexOf || (Array.prototype.indexOf ? function(array, item, from) {
return array.indexOf(item, from);
} : function(array, item, from) {
var _len, _ref, index, other;
_ref = array;
for (index = 0, _len = _ref.length; index < _len; index++) {
other = _ref[index];
var _len, index, other;
for (index = 0, _len = array.length; index < _len; index++) {
other = array[index];
if (other === item && (!from || (from <= index))) {
return index;
}
@ -25,10 +24,10 @@
return literal === string.substr(string.length - len - (back || 0), len);
};
exports.compact = function(array) {
var _i, _len, _ref, _result, item;
_result = []; _ref = array;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
var _i, _len, _result, item;
_result = [];
for (_i = 0, _len = array.length; _i < _len; _i++) {
item = array[_i];
if (item) {
_result.push(item);
}
@ -47,20 +46,18 @@
return extend(extend({}, options), overrides);
};
extend = (exports.extend = function(object, properties) {
var _ref, key, val;
_ref = properties;
for (key in _ref) {
val = _ref[key];
var key, val;
for (key in properties) {
val = properties[key];
object[key] = val;
}
return object;
});
exports.flatten = (flatten = function(array) {
var _i, _len, _ref, element, flattened;
var _i, _len, element, flattened;
flattened = [];
_ref = array;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
element = _ref[_i];
for (_i = 0, _len = array.length; _i < _len; _i++) {
element = array[_i];
if (element instanceof Array) {
flattened = flattened.concat(flatten(element));
} else {

View file

@ -421,7 +421,7 @@
throw new Error("SyntaxError: Reserved word \"" + (this.value()) + "\" on line " + (this.line + 1) + " can't be assigned");
};
Lexer.prototype.balancedString = function(str, delimited, options) {
var _i, _len, _ref2, _ref3, close, i, levels, open, pair, slash, slen;
var _i, _len, _ref2, close, i, levels, open, pair, slash, slen;
options || (options = {});
slash = delimited[0][0] === '/';
levels = [];
@ -431,10 +431,9 @@
if (levels.length && str.charAt(i) === '\\') {
i += 1;
} else {
_ref2 = delimited;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
pair = _ref2[_i];
_ref3 = pair, open = _ref3[0], close = _ref3[1];
for (_i = 0, _len = delimited.length; _i < _len; _i++) {
pair = delimited[_i];
_ref2 = pair, open = _ref2[0], close = _ref2[1];
if (levels.length && starts(str, close, i) && last(levels) === pair) {
levels.pop();
i += close.length - 1;
@ -463,7 +462,7 @@
return !i ? false : str.slice(0, i);
};
Lexer.prototype.interpolateString = function(str, options) {
var _len, _ref2, _ref3, end, escapeQuotes, escaped, expr, heredoc, i, idx, inner, interpolated, lexer, nested, pi, push, quote, s, tag, tok, token, tokens, value;
var _len, _ref2, end, escapeQuotes, escaped, expr, heredoc, i, idx, inner, interpolated, lexer, nested, pi, push, quote, s, tag, tok, token, tokens, value;
_ref2 = options || {}, heredoc = _ref2.heredoc, escapeQuotes = _ref2.escapeQuotes;
quote = str.charAt(0);
if (quote !== '"' || str.length < 3) {
@ -489,9 +488,8 @@
nested = lexer.tokenize("(" + (inner) + ")", {
line: this.line
});
_ref2 = nested;
for (idx = 0, _len = _ref2.length; idx < _len; idx++) {
tok = _ref2[idx];
for (idx = 0, _len = nested.length; idx < _len; idx++) {
tok = nested[idx];
if (tok[0] === 'CALL_END') {
(tok[0] = ')');
}
@ -518,10 +516,9 @@
this.token('(', '(');
}
push = tokens.push;
_ref2 = tokens;
for (i = 0, _len = _ref2.length; i < _len; i++) {
token = _ref2[i];
_ref3 = token, tag = _ref3[0], value = _ref3[1];
for (i = 0, _len = tokens.length; i < _len; i++) {
token = tokens[i];
_ref2 = token, tag = _ref2[0], value = _ref2[1];
if (tag === 'TOKENS') {
push.apply(this.tokens, value);
} else if (tag === 'STRING' && escapeQuotes) {

View file

@ -48,7 +48,7 @@
return ClosureNode.wrap(this).compile(o);
};
BaseNode.prototype.compileReference = function(o, options) {
var _len, _ref2, compiled, i, node, pair, reference;
var _len, compiled, i, node, pair, reference;
pair = (function() {
if (!(this.isComplex())) {
return [this, this];
@ -59,9 +59,8 @@
}
}).call(this);
if (((options != null) ? options.precompile : null)) {
_ref2 = pair;
for (i = 0, _len = _ref2.length; i < _len; i++) {
node = _ref2[i];
for (i = 0, _len = pair.length; i < _len; i++) {
node = pair[i];
(pair[i] = node.compile(o));
}
}
@ -370,7 +369,7 @@
return !o.top || this.properties.length ? ValueNode.__super__.compile.call(this, o) : this.base.compile(o);
};
ValueNode.prototype.compileNode = function(o) {
var _i, _len, _ref2, code, ex, prop, props;
var _i, _len, code, ex, prop, props;
if (ex = this.unfoldSoak(o)) {
return ex.compile(o);
}
@ -383,9 +382,8 @@
if (props[0] instanceof AccessorNode && this.isNumber() || o.top && this.base instanceof ObjectNode) {
code = ("(" + (code) + ")");
}
_ref2 = props;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
prop = _ref2[_i];
for (_i = 0, _len = props.length; _i < _len; _i++) {
prop = props[_i];
(code += prop.compile(o));
}
return code;
@ -991,7 +989,7 @@
return top || this.parenthetical ? val : ("(" + (val) + ")");
};
AssignNode.prototype.compilePatternMatch = function(o) {
var _len, _ref2, _ref3, accessClass, assigns, code, i, idx, isObject, obj, objects, oindex, olength, otop, splat, top, val, valVar, value;
var _len, _ref2, accessClass, assigns, code, i, idx, isObject, obj, objects, oindex, olength, otop, splat, top, val, valVar, value;
if ((value = this.value).isStatement(o)) {
value = ClosureNode.wrap(value);
}
@ -1019,13 +1017,12 @@
valVar = o.scope.freeVariable('ref');
assigns = [("" + (valVar) + " = " + (value.compile(o)))];
splat = false;
_ref2 = objects;
for (i = 0, _len = _ref2.length; i < _len; i++) {
obj = _ref2[i];
for (i = 0, _len = objects.length; i < _len; i++) {
obj = objects[i];
idx = i;
if (isObject) {
if (obj instanceof AssignNode) {
_ref3 = [obj.value, obj.variable.base], obj = _ref3[0], idx = _ref3[1];
_ref2 = [obj.value, obj.variable.base], obj = _ref2[0], idx = _ref2[1];
} else {
idx = obj;
}
@ -1119,9 +1116,9 @@
}
}
params = (function() {
_result = []; _ref2 = params;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
param = _ref2[_i];
_result = [];
for (_i = 0, _len = params.length; _i < _len; _i++) {
param = params[_i];
_result.push(param.compile(o));
}
return _result;
@ -1129,9 +1126,8 @@
if (!(empty)) {
this.body.makeReturn();
}
_ref2 = params;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
param = _ref2[_i];
for (_i = 0, _len = params.length; _i < _len; _i++) {
param = params[_i];
(o.scope.parameter(param));
}
code = this.body.expressions.length ? ("\n" + (this.body.compileWithDeclarations(o)) + "\n") : '';
@ -1221,12 +1217,11 @@
return "" + (utility('slice')) + ".call(" + (name) + ", " + (index) + (trail) + ")";
};
SplatNode.compileSplattedArray = function(list, o) {
var _len, _ref2, arg, args, code, end, i, prev;
var _len, arg, args, code, end, i, prev;
args = [];
end = -1;
_ref2 = list;
for (i = 0, _len = _ref2.length; i < _len; i++) {
arg = _ref2[i];
for (i = 0, _len = list.length; i < _len; i++) {
arg = list[i];
code = arg.compile(o);
prev = args[end];
if (!(arg instanceof SplatNode)) {
@ -1619,7 +1614,7 @@
return '';
};
ForNode.prototype.compileNode = function(o) {
var body, codeInBody, forPart, guardPart, index, ivar, lvar, name, namePart, range, returnResult, rvar, scope, source, sourcePart, stepPart, svar, topLevel, varPart, vars;
var body, codeInBody, forPart, guardPart, index, ivar, lvar, name, namePart, range, ref, returnResult, rvar, scope, source, sourcePart, stepPart, svar, topLevel, varPart, vars;
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;
@ -1656,8 +1651,16 @@
step: this.step
}));
} else {
svar = scope.freeVariable('ref');
sourcePart = ("" + (svar) + " = " + (this.source.compile(o)) + ";");
svar = this.source.compile(o);
if (IDENTIFIER.test(svar) && scope.check(svar, {
immediate: true
})) {
sourcePart = '';
} else {
ref = scope.freeVariable('ref');
sourcePart = ("" + (ref) + " = " + (svar) + ";");
svar = ref;
}
if (this.pattern) {
namePart = new AssignNode(this.name, literal("" + (svar) + "[" + (ivar) + "]")).compile(merge(o, {
indent: this.idt(1),
@ -1812,15 +1815,15 @@
return this.statement || (this.statement = !!((o && o.top) || this.bodyNode().isStatement(o) || (this.elseBody && this.elseBodyNode().isStatement(o))));
};
IfNode.prototype.compileCondition = function(o) {
var _i, _len, _ref2, _result, cond, conditions;
var _i, _len, _result, cond, conditions;
conditions = flatten([this.condition]);
if (conditions.length === 1) {
conditions[0].parenthetical = true;
}
return (function() {
_result = []; _ref2 = conditions;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
cond = _ref2[_i];
_result = [];
for (_i = 0, _len = conditions.length; _i < _len; _i++) {
cond = conditions[_i];
_result.push(cond.compile(o));
}
return _result;

View file

@ -7,19 +7,18 @@
return this;
};
OptionParser.prototype.parse = function(args) {
var _i, _len, _len2, _ref, _ref2, arg, i, isOption, matchedRule, options, rule, value;
var _i, _len, _len2, _ref, arg, i, isOption, matchedRule, options, rule, value;
options = {
arguments: []
};
args = normalizeArguments(args);
_ref = args;
for (i = 0, _len = _ref.length; i < _len; i++) {
arg = _ref[i];
for (i = 0, _len = args.length; i < _len; i++) {
arg = args[i];
isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
matchedRule = false;
_ref2 = this.rules;
for (_i = 0, _len2 = _ref2.length; _i < _len2; _i++) {
rule = _ref2[_i];
_ref = this.rules;
for (_i = 0, _len2 = _ref.length; _i < _len2; _i++) {
rule = _ref[_i];
if (rule.shortFlag === arg || rule.longFlag === arg) {
value = rule.hasArgument ? args[i += 1] : true;
options[rule.name] = rule.isList ? (options[rule.name] || []).concat(value) : value;
@ -60,10 +59,10 @@
MULTI_FLAG = /^-(\w{2,})/;
OPTIONAL = /\[(\w+(\*?))\]/;
buildRules = function(rules) {
var _i, _len, _ref, _result, tuple;
_result = []; _ref = rules;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
tuple = _ref[_i];
var _i, _len, _result, tuple;
_result = [];
for (_i = 0, _len = rules.length; _i < _len; _i++) {
tuple = rules[_i];
_result.push((function() {
if (tuple.length < 3) {
tuple.unshift(null);
@ -88,16 +87,15 @@
};
};
normalizeArguments = function(args) {
var _i, _j, _len, _len2, _ref, _ref2, arg, l, match, result;
var _i, _j, _len, _len2, _ref, arg, l, match, result;
args = args.slice(0);
result = [];
_ref = args;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
arg = _ref[_i];
for (_i = 0, _len = args.length; _i < _len; _i++) {
arg = args[_i];
if (match = arg.match(MULTI_FLAG)) {
_ref2 = match[1].split('');
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
l = _ref2[_j];
_ref = match[1].split('');
for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
l = _ref[_j];
result.push('-' + l);
}
} else {

View file

@ -1,5 +1,5 @@
(function() {
var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, LINEBREAKS, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _i, _len, _ref, _result, include, pair;
var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, LINEBREAKS, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _i, _len, _result, include, pair;
var __hasProp = Object.prototype.hasOwnProperty;
include = require('./helpers').include;
exports.Rewriter = (function() {
@ -272,7 +272,7 @@
});
};
Rewriter.prototype.ensureBalance = function(pairs) {
var _ref, _result, key, levels, line, open, openLine, unclosed, value;
var _result, key, levels, line, open, openLine, unclosed, value;
levels = {};
openLine = {};
this.scanTokens(function(token, i) {
@ -298,10 +298,10 @@
return 1;
});
unclosed = (function() {
_result = []; _ref = levels;
for (key in _ref) {
if (!__hasProp.call(_ref, key)) continue;
value = _ref[key];
_result = [];
for (key in levels) {
if (!__hasProp.call(levels, key)) continue;
value = levels[key];
if (value > 0) {
_result.push(key);
}
@ -368,24 +368,23 @@
})();
BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['PARAM_START', 'PARAM_END'], ['CALL_START', 'CALL_END'], ['INDEX_START', 'INDEX_END']];
INVERSES = {};
_ref = BALANCED_PAIRS;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
pair = _ref[_i];
for (_i = 0, _len = BALANCED_PAIRS.length; _i < _len; _i++) {
pair = BALANCED_PAIRS[_i];
INVERSES[pair[0]] = pair[1];
INVERSES[pair[1]] = pair[0];
}
EXPRESSION_START = (function() {
_result = []; _ref = BALANCED_PAIRS;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
pair = _ref[_i];
_result = [];
for (_i = 0, _len = BALANCED_PAIRS.length; _i < _len; _i++) {
pair = BALANCED_PAIRS[_i];
_result.push(pair[0]);
}
return _result;
})();
EXPRESSION_END = (function() {
_result = []; _ref = BALANCED_PAIRS;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
pair = _ref[_i];
_result = [];
for (_i = 0, _len = BALANCED_PAIRS.length; _i < _len; _i++) {
pair = BALANCED_PAIRS[_i];
_result.push(pair[1]);
}
return _result;

View file

@ -1440,8 +1440,13 @@ exports.ForNode = class ForNode extends BaseNode
sourcePart = source.compileVariables(o)
forPart = source.compile merge o, index: ivar, step: @step
else
svar = scope.freeVariable 'ref'
sourcePart = "#{svar} = #{ @source.compile(o) };"
svar = @source.compile o
if IDENTIFIER.test(svar) and scope.check(svar, immediate: on)
sourcePart = ''
else
ref = scope.freeVariable 'ref'
sourcePart = "#{ref} = #{svar};"
svar = ref
if @pattern
namePart = new AssignNode(@name, literal("#{svar}[#{ivar}]")).compile(merge o, {indent: @idt(1), top: true, keepLevel: yes}) + '\n'
else