mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-17 19:11:22 -05:00
merging the range-killer to master
This commit is contained in:
4
Cakefile
4
Cakefile
@@ -99,8 +99,8 @@ task 'loc', 'count the lines of source code in the CoffeeScript compiler', ->
|
|||||||
runTests = (CoffeeScript) ->
|
runTests = (CoffeeScript) ->
|
||||||
startTime = Date.now()
|
startTime = Date.now()
|
||||||
passedTests = failedTests = 0
|
passedTests = failedTests = 0
|
||||||
for all name, func of require 'assert'
|
wrap = (name, func) -> global[name] = -> ++passedTests; func arguments...
|
||||||
global[name] = -> ++passedTests; func arguments...
|
wrap name, func for all name, func of require 'assert'
|
||||||
global.eq = global.strictEqual
|
global.eq = global.strictEqual
|
||||||
global.CoffeeScript = CoffeeScript
|
global.CoffeeScript = CoffeeScript
|
||||||
process.on 'exit', ->
|
process.on 'exit', ->
|
||||||
|
|||||||
@@ -25,16 +25,16 @@
|
|||||||
return xhr.send(null);
|
return xhr.send(null);
|
||||||
};
|
};
|
||||||
runScripts = function() {
|
runScripts = function() {
|
||||||
var _i, _j, _len, _ref, script;
|
var _i, _len, _ref, script;
|
||||||
for (_i = 0, _len = (_ref = document.getElementsByTagName('script')).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref = document.getElementsByTagName('script')).length; _i < _len; ++_i) {
|
||||||
(function() {
|
script = _ref[_i];
|
||||||
var script = _ref[_i];
|
if (script.type === 'text/coffeescript') {
|
||||||
_j = script;
|
if (script.src) {
|
||||||
return script.type === 'text/coffeescript' ? script.src ? CoffeeScript.load(script.src) : setTimeout(function() {
|
CoffeeScript.load(script.src);
|
||||||
return CoffeeScript.run(script.innerHTML);
|
} else {
|
||||||
}) : undefined;
|
CoffeeScript.run(script.innerHTML);
|
||||||
})();
|
}
|
||||||
script = _j;
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -47,7 +47,7 @@
|
|||||||
}
|
}
|
||||||
options = oparse.parse(args);
|
options = oparse.parse(args);
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_i = 0, _len = (_ref = options.arguments).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref = options.arguments).length; _i < _len; ++_i) {
|
||||||
arg = _ref[_i];
|
arg = _ref[_i];
|
||||||
_result.push(invoke(arg));
|
_result.push(invoke(arg));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,43 +48,39 @@
|
|||||||
return compileScripts();
|
return compileScripts();
|
||||||
};
|
};
|
||||||
compileScripts = function() {
|
compileScripts = function() {
|
||||||
var _i, _j, _len, _ref2, _result, base, compile, source;
|
var _i, _len, _ref2, _result, base, compile, source;
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_i = 0, _len = (_ref2 = sources).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = sources).length; _i < _len; ++_i) {
|
||||||
(function() {
|
source = _ref2[_i];
|
||||||
var source = _ref2[_i];
|
_result.push((function() {
|
||||||
_j = source;
|
base = path.join(source);
|
||||||
return _result.push((function() {
|
compile = function(source, topLevel) {
|
||||||
base = path.join(source);
|
return path.exists(source, function(exists) {
|
||||||
compile = function(source, topLevel) {
|
if (!exists) {
|
||||||
return path.exists(source, function(exists) {
|
throw new Error("File not found: " + source);
|
||||||
if (!exists) {
|
}
|
||||||
throw new Error("File not found: " + source);
|
return fs.stat(source, function(err, stats) {
|
||||||
|
if (stats.isDirectory()) {
|
||||||
|
return fs.readdir(source, function(err, files) {
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
} else if (topLevel || path.extname(source) === '.coffee') {
|
||||||
|
fs.readFile(source, function(err, code) {
|
||||||
|
return compileScript(source, code.toString(), base);
|
||||||
|
});
|
||||||
|
return opts.watch ? watch(source, base) : undefined;
|
||||||
}
|
}
|
||||||
return fs.stat(source, function(err, stats) {
|
|
||||||
if (stats.isDirectory()) {
|
|
||||||
return fs.readdir(source, function(err, files) {
|
|
||||||
var _k, _len2, _result2, file;
|
|
||||||
_result2 = [];
|
|
||||||
for (_k = 0, _len2 = files.length; _k < _len2; _k++) {
|
|
||||||
file = files[_k];
|
|
||||||
_result2.push(compile(path.join(source, file)));
|
|
||||||
}
|
|
||||||
return _result2;
|
|
||||||
});
|
|
||||||
} else if (topLevel || path.extname(source) === '.coffee') {
|
|
||||||
fs.readFile(source, function(err, code) {
|
|
||||||
return compileScript(source, code.toString(), base);
|
|
||||||
});
|
|
||||||
return opts.watch ? watch(source, base) : undefined;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
});
|
||||||
return compile(source, true);
|
};
|
||||||
})());
|
return compile(source, true);
|
||||||
})();
|
})());
|
||||||
source = _j;
|
|
||||||
}
|
}
|
||||||
return _result;
|
return _result;
|
||||||
};
|
};
|
||||||
@@ -93,7 +89,7 @@
|
|||||||
o = opts;
|
o = opts;
|
||||||
options = compileOptions(file);
|
options = compileOptions(file);
|
||||||
if (o.require) {
|
if (o.require) {
|
||||||
for (_i = 0, _len = (_ref2 = o.require).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = o.require).length; _i < _len; ++_i) {
|
||||||
req = _ref2[_i];
|
req = _ref2[_i];
|
||||||
require(helpers.starts(req, '.') ? fs.realpathSync(req) : req);
|
require(helpers.starts(req, '.') ? fs.realpathSync(req) : req);
|
||||||
}
|
}
|
||||||
@@ -190,7 +186,7 @@
|
|||||||
var _i, _len, _ref2, _result, strings, tag, token, value;
|
var _i, _len, _ref2, _result, strings, tag, token, value;
|
||||||
strings = (function() {
|
strings = (function() {
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_i = 0, _len = tokens.length; _i < _len; _i++) {
|
for (_i = 0, _len = tokens.length; _i < _len; ++_i) {
|
||||||
token = tokens[_i];
|
token = tokens[_i];
|
||||||
_result.push((function() {
|
_result.push((function() {
|
||||||
_ref2 = [token[0], token[1].toString().replace(/\n/, '\\n')], tag = _ref2[0], value = _ref2[1];
|
_ref2 = [token[0], token[1].toString().replace(/\n/, '\\n')], tag = _ref2[0], value = _ref2[1];
|
||||||
|
|||||||
201
lib/grammar.js
201
lib/grammar.js
@@ -1,6 +1,5 @@
|
|||||||
(function() {
|
(function() {
|
||||||
var Parser, _i, _j, _len, _len2, _ref, _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;
|
Parser = require('jison').Parser;
|
||||||
unwrap = /^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/;
|
unwrap = /^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/;
|
||||||
o = function(patternString, action, options) {
|
o = function(patternString, action, options) {
|
||||||
@@ -11,7 +10,7 @@
|
|||||||
}
|
}
|
||||||
action = (match = unwrap.exec(action)) ? match[1] : "(" + action + "())";
|
action = (match = unwrap.exec(action)) ? match[1] : "(" + action + "())";
|
||||||
action = action.replace(/\bnew /g, '$&yy.');
|
action = action.replace(/\bnew /g, '$&yy.');
|
||||||
action = action.replace(/\bExpressions\.wrap\b/g, 'yy.$&');
|
action = action.replace(/\b(?:Expressions\.wrap|extend)\b/g, 'yy.$&');
|
||||||
return [patternString, "$$ = " + action + ";", options];
|
return [patternString, "$$ = " + action + ";", options];
|
||||||
};
|
};
|
||||||
grammar = {
|
grammar = {
|
||||||
@@ -78,17 +77,17 @@
|
|||||||
})
|
})
|
||||||
],
|
],
|
||||||
AssignObj: [
|
AssignObj: [
|
||||||
o("Identifier", function() {
|
o('Identifier', function() {
|
||||||
return new Value($1);
|
return new Value($1);
|
||||||
}), o("AlphaNumeric"), o("ThisProperty"), o("Identifier : Expression", function() {
|
}), o('AlphaNumeric'), o('ThisProperty'), o('Identifier : Expression', function() {
|
||||||
return new Assign(new Value($1), $3, 'object');
|
return new Assign(new Value($1), $3, 'object');
|
||||||
}), o("AlphaNumeric : Expression", function() {
|
}), o('AlphaNumeric : Expression', function() {
|
||||||
return new Assign(new Value($1), $3, 'object');
|
return new Assign(new Value($1), $3, 'object');
|
||||||
}), o("Identifier : INDENT Expression OUTDENT", function() {
|
}), o('Identifier : INDENT Expression OUTDENT', function() {
|
||||||
return new Assign(new Value($1), $4, 'object');
|
return new Assign(new Value($1), $4, 'object');
|
||||||
}), o("AlphaNumeric : INDENT Expression OUTDENT", function() {
|
}), o('AlphaNumeric : INDENT Expression OUTDENT', function() {
|
||||||
return new Assign(new Value($1), $4, 'object');
|
return new Assign(new Value($1), $4, 'object');
|
||||||
}), o("Comment")
|
}), o('Comment')
|
||||||
],
|
],
|
||||||
Return: [
|
Return: [
|
||||||
o("RETURN Expression", function() {
|
o("RETURN Expression", function() {
|
||||||
@@ -168,8 +167,6 @@
|
|||||||
return new Value($1);
|
return new Value($1);
|
||||||
}), o("Parenthetical", function() {
|
}), o("Parenthetical", function() {
|
||||||
return new Value($1);
|
return new Value($1);
|
||||||
}), o("Range", function() {
|
|
||||||
return new Value($1);
|
|
||||||
}), o("This")
|
}), o("This")
|
||||||
],
|
],
|
||||||
Accessor: [
|
Accessor: [
|
||||||
@@ -188,12 +185,14 @@
|
|||||||
Index: [
|
Index: [
|
||||||
o("INDEX_START Expression INDEX_END", function() {
|
o("INDEX_START Expression INDEX_END", function() {
|
||||||
return new Index($2);
|
return new Index($2);
|
||||||
}), o("INDEX_SOAK Index", function() {
|
}), o("INDEX_SOAK Index", function() {
|
||||||
$2.soakNode = true;
|
return extend($2, {
|
||||||
return $2;
|
soakNode: true
|
||||||
|
});
|
||||||
}), o("INDEX_PROTO Index", function() {
|
}), o("INDEX_PROTO Index", function() {
|
||||||
$2.proto = true;
|
return extend($2, {
|
||||||
return $2;
|
proto: true
|
||||||
|
});
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
Object: [
|
Object: [
|
||||||
@@ -290,32 +289,11 @@
|
|||||||
return new Value(new Literal('this'));
|
return new Value(new Literal('this'));
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
RangeDots: [
|
|
||||||
o("..", function() {
|
|
||||||
return 'inclusive';
|
|
||||||
}), o("...", function() {
|
|
||||||
return 'exclusive';
|
|
||||||
})
|
|
||||||
],
|
|
||||||
ThisProperty: [
|
ThisProperty: [
|
||||||
o("@ Identifier", function() {
|
o("@ Identifier", function() {
|
||||||
return new Value(new Literal('this'), [new Accessor($2)], 'this');
|
return new Value(new Literal('this'), [new Accessor($2)], 'this');
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
Range: [
|
|
||||||
o("[ Expression RangeDots Expression ]", function() {
|
|
||||||
return new Range($2, $4, $3);
|
|
||||||
})
|
|
||||||
],
|
|
||||||
Slice: [
|
|
||||||
o("INDEX_START Expression RangeDots Expression INDEX_END", function() {
|
|
||||||
return new Range($2, $4, $3);
|
|
||||||
}), o("INDEX_START Expression RangeDots INDEX_END", function() {
|
|
||||||
return new Range($2, null, $3);
|
|
||||||
}), o("INDEX_START RangeDots Expression INDEX_END", function() {
|
|
||||||
return new Range(null, $3, $2);
|
|
||||||
})
|
|
||||||
],
|
|
||||||
Array: [
|
Array: [
|
||||||
o("[ ]", function() {
|
o("[ ]", function() {
|
||||||
return new ArrayLiteral([]);
|
return new ArrayLiteral([]);
|
||||||
@@ -405,81 +383,37 @@
|
|||||||
})
|
})
|
||||||
],
|
],
|
||||||
For: [
|
For: [
|
||||||
o("Statement ForBody", function() {
|
o('Statement ForBody', function() {
|
||||||
return new For($1, $2, $2.vars[0], $2.vars[1]);
|
return new For($1, $2);
|
||||||
}), o("Expression ForBody", function() {
|
}), o('Expression ForBody', function() {
|
||||||
return new For($1, $2, $2.vars[0], $2.vars[1]);
|
return new For($1, $2);
|
||||||
}), o("ForBody Block", function() {
|
}), o('ForBody Block', function() {
|
||||||
return new For($2, $1, $1.vars[0], $1.vars[1]);
|
return new For($2, $1);
|
||||||
})
|
|
||||||
],
|
|
||||||
ForBody: [
|
|
||||||
o("FOR Range", function() {
|
|
||||||
return {
|
|
||||||
source: new Value($2),
|
|
||||||
vars: []
|
|
||||||
};
|
|
||||||
}), o("ForStart ForSource", function() {
|
|
||||||
$2.raw = $1.raw;
|
|
||||||
$2.vars = $1;
|
|
||||||
return $2;
|
|
||||||
})
|
|
||||||
],
|
|
||||||
ForStart: [
|
|
||||||
o("FOR ForVariables", function() {
|
|
||||||
return $2;
|
|
||||||
}), o("FOR ALL ForVariables", function() {
|
|
||||||
$3.raw = true;
|
|
||||||
return $3;
|
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
ForValue: [
|
ForValue: [
|
||||||
o("Identifier"), o("Array", function() {
|
o('Identifier'), o('Array', function() {
|
||||||
return new Value($1);
|
return new Value($1);
|
||||||
}), o("Object", function() {
|
}), o('Object', function() {
|
||||||
return new Value($1);
|
return new Value($1);
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
ForVariables: [
|
ForIn: [
|
||||||
o("ForValue", function() {
|
o('FORIN Expression', function() {
|
||||||
return [$1];
|
|
||||||
}), o("ForValue , ForValue", function() {
|
|
||||||
return [$1, $3];
|
|
||||||
})
|
|
||||||
],
|
|
||||||
ForSource: [
|
|
||||||
o("FORIN Expression", function() {
|
|
||||||
return {
|
return {
|
||||||
source: $2
|
source: $2
|
||||||
};
|
};
|
||||||
}), o("FOROF Expression", function() {
|
}), o('FORIN Expression WHEN Expression', function() {
|
||||||
return {
|
|
||||||
source: $2,
|
|
||||||
object: true
|
|
||||||
};
|
|
||||||
}), o("FORIN Expression WHEN Expression", function() {
|
|
||||||
return {
|
return {
|
||||||
source: $2,
|
source: $2,
|
||||||
guard: $4
|
guard: $4
|
||||||
};
|
};
|
||||||
}), o("FOROF Expression WHEN Expression", function() {
|
}), o('FORIN Expression BY Expression', function() {
|
||||||
return {
|
|
||||||
source: $2,
|
|
||||||
guard: $4,
|
|
||||||
object: true
|
|
||||||
};
|
|
||||||
}), o("FORIN Expression BY Expression", function() {
|
|
||||||
return {
|
return {
|
||||||
source: $2,
|
source: $2,
|
||||||
step: $4
|
step: $4
|
||||||
};
|
};
|
||||||
}), o("FORIN Expression WHEN Expression BY Expression", function() {
|
}), o('FORIN Expression BY Expression WHEN Expression', function() {
|
||||||
return {
|
|
||||||
source: $2,
|
|
||||||
guard: $4,
|
|
||||||
step: $6
|
|
||||||
};
|
|
||||||
}), o("FORIN Expression BY Expression WHEN Expression", function() {
|
|
||||||
return {
|
return {
|
||||||
source: $2,
|
source: $2,
|
||||||
step: $4,
|
step: $4,
|
||||||
@@ -487,6 +421,80 @@
|
|||||||
};
|
};
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
|
ForOf: [
|
||||||
|
o('FOROF Expression', function() {
|
||||||
|
return {
|
||||||
|
object: true,
|
||||||
|
source: $2
|
||||||
|
};
|
||||||
|
}), o('FOROF Expression WHEN Expression', function() {
|
||||||
|
return {
|
||||||
|
object: true,
|
||||||
|
source: $2,
|
||||||
|
guard: $4
|
||||||
|
};
|
||||||
|
})
|
||||||
|
],
|
||||||
|
ForTo: [
|
||||||
|
o('TO Expression', function() {
|
||||||
|
return {
|
||||||
|
to: $2
|
||||||
|
};
|
||||||
|
}), o('TO Expression WHEN Expression', function() {
|
||||||
|
return {
|
||||||
|
to: $2,
|
||||||
|
guard: $4
|
||||||
|
};
|
||||||
|
}), o('TO Expression BY Expression', function() {
|
||||||
|
return {
|
||||||
|
to: $2,
|
||||||
|
step: $4
|
||||||
|
};
|
||||||
|
}), o('TO Expression BY Expression WHEN Expression', function() {
|
||||||
|
return {
|
||||||
|
to: $2,
|
||||||
|
step: $4,
|
||||||
|
guard: $6
|
||||||
|
};
|
||||||
|
})
|
||||||
|
],
|
||||||
|
ForBody: [
|
||||||
|
o('FOR ForValue ForIn', function() {
|
||||||
|
return extend($3, {
|
||||||
|
name: $2
|
||||||
|
});
|
||||||
|
}), o('FOR ForValue , Identifier ForIn', function() {
|
||||||
|
return extend($5, {
|
||||||
|
name: $2,
|
||||||
|
index: $4
|
||||||
|
});
|
||||||
|
}), o('FOR Identifier ForOf', function() {
|
||||||
|
return extend($3, {
|
||||||
|
index: $2
|
||||||
|
});
|
||||||
|
}), o('FOR ForValue , ForValue ForOf', function() {
|
||||||
|
return extend($5, {
|
||||||
|
index: $2,
|
||||||
|
name: $4
|
||||||
|
});
|
||||||
|
}), o('FOR ALL Identifier ForOf', function() {
|
||||||
|
return extend($4, {
|
||||||
|
raw: true,
|
||||||
|
index: $3
|
||||||
|
});
|
||||||
|
}), o('FOR ALL Identifier , ForValue ForOf', function() {
|
||||||
|
return extend($6, {
|
||||||
|
raw: true,
|
||||||
|
index: $3,
|
||||||
|
name: $5
|
||||||
|
});
|
||||||
|
}), o('FOR Identifier FROM Expression ForTo', function() {
|
||||||
|
return extend($5, {
|
||||||
|
index: $2,
|
||||||
|
from: $4
|
||||||
|
});
|
||||||
|
})
|
||||||
|
],
|
||||||
Switch: [
|
Switch: [
|
||||||
o("SWITCH Expression INDENT Whens OUTDENT", function() {
|
o("SWITCH Expression INDENT Whens OUTDENT", function() {
|
||||||
return new Switch($2, $4);
|
return new Switch($2, $4);
|
||||||
@@ -588,17 +596,16 @@
|
|||||||
})
|
})
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
operators = [["left", 'CALL_START', 'CALL_END'], ["nonassoc", '++', '--'], ["left", '?'], ["right", 'UNARY'], ["left", 'MATH'], ["left", '+', '-'], ["left", 'SHIFT'], ["left", 'COMPARE'], ["left", 'RELATION'], ["left", '==', '!='], ["left", 'LOGIC'], ["left", '.'], ["nonassoc", 'INDENT', 'OUTDENT'], ["right", 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'BY', 'THROW'], ["right", 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'EXTENDS'], ["right", '=', ':', 'COMPOUND_ASSIGN', 'RETURN'], ["right", '->', '=>', 'UNLESS', 'POST_IF', 'POST_UNLESS']];
|
operators = [["left", 'CALL_START', 'CALL_END'], ["nonassoc", '++', '--'], ["left", '?'], ["right", 'UNARY'], ["left", 'MATH'], ["left", '+', '-'], ["left", 'SHIFT'], ["left", 'COMPARE'], ["left", 'RELATION'], ["left", '==', '!='], ["left", 'LOGIC'], ["left", '.'], ["nonassoc", 'INDENT', 'OUTDENT'], ["right", 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'FROM', 'TO', 'BY', 'THROW'], ["right", 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'EXTENDS'], ["right", '=', ':', 'COMPOUND_ASSIGN', 'RETURN'], ["right", '->', '=>', 'UNLESS', 'POST_IF', 'POST_UNLESS']];
|
||||||
tokens = [];
|
tokens = [];
|
||||||
for (name in grammar) {
|
for (name in grammar) {
|
||||||
if (!__hasProp.call(grammar, name)) continue;
|
|
||||||
alternatives = grammar[name];
|
alternatives = grammar[name];
|
||||||
grammar[name] = (function() {
|
grammar[name] = (function() {
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_i = 0, _len = alternatives.length; _i < _len; _i++) {
|
for (_i = 0, _len = alternatives.length; _i < _len; ++_i) {
|
||||||
alt = alternatives[_i];
|
alt = alternatives[_i];
|
||||||
_result.push((function() {
|
_result.push((function() {
|
||||||
for (_j = 0, _len2 = (_ref = alt[0].split(' ')).length; _j < _len2; _j++) {
|
for (_j = 0, _len2 = (_ref = alt[0].split(' ')).length; _j < _len2; ++_j) {
|
||||||
token = _ref[_j];
|
token = _ref[_j];
|
||||||
if (!grammar[token]) {
|
if (!grammar[token]) {
|
||||||
tokens.push(token);
|
tokens.push(token);
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
exports.compact = function(array) {
|
exports.compact = function(array) {
|
||||||
var _i, _len, _result, item;
|
var _i, _len, _result, item;
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_i = 0, _len = array.length; _i < _len; _i++) {
|
for (_i = 0, _len = array.length; _i < _len; ++_i) {
|
||||||
item = array[_i];
|
item = array[_i];
|
||||||
if (item) {
|
if (item) {
|
||||||
_result.push(item);
|
_result.push(item);
|
||||||
@@ -41,7 +41,7 @@
|
|||||||
exports.flatten = flatten = function(array) {
|
exports.flatten = flatten = function(array) {
|
||||||
var _i, _len, element, flattened;
|
var _i, _len, element, flattened;
|
||||||
flattened = [];
|
flattened = [];
|
||||||
for (_i = 0, _len = array.length; _i < _len; _i++) {
|
for (_i = 0, _len = array.length; _i < _len; ++_i) {
|
||||||
element = array[_i];
|
element = array[_i];
|
||||||
if (element instanceof Array) {
|
if (element instanceof Array) {
|
||||||
flattened = flattened.concat(flatten(element));
|
flattened = flattened.concat(flatten(element));
|
||||||
|
|||||||
25
lib/lexer.js
25
lib/lexer.js
@@ -23,9 +23,9 @@
|
|||||||
this.indent = 0;
|
this.indent = 0;
|
||||||
this.indebt = 0;
|
this.indebt = 0;
|
||||||
this.outdebt = 0;
|
this.outdebt = 0;
|
||||||
this.seenFor = false;
|
|
||||||
this.indents = [];
|
this.indents = [];
|
||||||
this.tokens = [];
|
this.tokens = [];
|
||||||
|
this.seenFor = this.seenFrom = false;
|
||||||
while (this.chunk = code.slice(this.i)) {
|
while (this.chunk = code.slice(this.i)) {
|
||||||
this.identifierToken() || this.commentToken() || this.whitespaceToken() || this.lineToken() || this.heredocToken() || this.stringToken() || this.numberToken() || this.regexToken() || this.jsToken() || this.literalToken();
|
this.identifierToken() || this.commentToken() || this.whitespaceToken() || this.lineToken() || this.heredocToken() || this.stringToken() || this.numberToken() || this.regexToken() || this.jsToken() || this.literalToken();
|
||||||
}
|
}
|
||||||
@@ -46,6 +46,17 @@
|
|||||||
this.token('ALL', id);
|
this.token('ALL', id);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (id === 'from' && this.tag(1) === 'FOR') {
|
||||||
|
this.seenFor = false;
|
||||||
|
this.seenFrom = true;
|
||||||
|
this.token('FROM', id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (id === 'to' && this.seenFrom) {
|
||||||
|
this.seenFrom = false;
|
||||||
|
this.token('TO', id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
forcedIdentifier = colon || this.tagAccessor();
|
forcedIdentifier = colon || this.tagAccessor();
|
||||||
tag = 'IDENTIFIER';
|
tag = 'IDENTIFIER';
|
||||||
if (__indexOf.call(JS_KEYWORDS, id) >= 0 || !forcedIdentifier && __indexOf.call(COFFEE_KEYWORDS, id) >= 0) {
|
if (__indexOf.call(JS_KEYWORDS, id) >= 0 || !forcedIdentifier && __indexOf.call(COFFEE_KEYWORDS, id) >= 0) {
|
||||||
@@ -217,7 +228,7 @@
|
|||||||
tokens = [];
|
tokens = [];
|
||||||
for (_i = 0, _len = (_ref2 = this.interpolateString(body, {
|
for (_i = 0, _len = (_ref2 = this.interpolateString(body, {
|
||||||
regex: true
|
regex: true
|
||||||
})).length; _i < _len; _i++) {
|
})).length; _i < _len; ++_i) {
|
||||||
_ref3 = _ref2[_i], tag = _ref3[0], value = _ref3[1];
|
_ref3 = _ref2[_i], tag = _ref3[0], value = _ref3[1];
|
||||||
if (tag === 'TOKENS') {
|
if (tag === 'TOKENS') {
|
||||||
tokens.push.apply(tokens, value);
|
tokens.push.apply(tokens, value);
|
||||||
@@ -469,7 +480,7 @@
|
|||||||
if (levels.length && str.charAt(i) === '\\') {
|
if (levels.length && str.charAt(i) === '\\') {
|
||||||
i += 1;
|
i += 1;
|
||||||
} else {
|
} else {
|
||||||
for (_i = 0, _len = delimited.length; _i < _len; _i++) {
|
for (_i = 0, _len = delimited.length; _i < _len; ++_i) {
|
||||||
pair = delimited[_i];
|
pair = delimited[_i];
|
||||||
open = pair[0], close = pair[1];
|
open = pair[0], close = pair[1];
|
||||||
if (levels.length && starts(str, close, i) && last(levels) === pair) {
|
if (levels.length && starts(str, close, i) && last(levels) === pair) {
|
||||||
@@ -495,7 +506,7 @@
|
|||||||
if (levels.length) {
|
if (levels.length) {
|
||||||
throw SyntaxError("Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1));
|
throw SyntaxError("Unterminated " + (levels.pop()[0]) + " starting on line " + (this.line + 1));
|
||||||
}
|
}
|
||||||
return !i ? false : str.slice(0, i);
|
return i && str.slice(0, i);
|
||||||
};
|
};
|
||||||
Lexer.prototype.interpolateString = function(str, options) {
|
Lexer.prototype.interpolateString = function(str, options) {
|
||||||
var _len, _ref2, _ref3, _this, expr, heredoc, i, inner, interpolated, letter, nested, pi, regex, tag, tokens, value;
|
var _len, _ref2, _ref3, _this, expr, heredoc, i, inner, interpolated, letter, nested, pi, regex, tag, tokens, value;
|
||||||
@@ -545,7 +556,7 @@
|
|||||||
if (interpolated = tokens.length > 1) {
|
if (interpolated = tokens.length > 1) {
|
||||||
this.token('(', '(');
|
this.token('(', '(');
|
||||||
}
|
}
|
||||||
for (i = 0, _len = tokens.length; i < _len; i++) {
|
for (i = 0, _len = tokens.length; i < _len; ++i) {
|
||||||
_ref3 = tokens[i], tag = _ref3[0], value = _ref3[1];
|
_ref3 = tokens[i], tag = _ref3[0], value = _ref3[1];
|
||||||
if (i) {
|
if (i) {
|
||||||
this.token('+', '+');
|
this.token('+', '+');
|
||||||
@@ -611,7 +622,7 @@
|
|||||||
IDENTIFIER = /^([$A-Za-z_][$\w]*)([^\n\S]*:(?!:))?/;
|
IDENTIFIER = /^([$A-Za-z_][$\w]*)([^\n\S]*:(?!:))?/;
|
||||||
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i;
|
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i;
|
||||||
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[ \t]*)?\1/;
|
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[ \t]*)?\1/;
|
||||||
OPERATOR = /^(?:-[-=>]?|\+[+=]?|\.\.\.?|[*&|\/%=<>^:!?]+)/;
|
OPERATOR = /^(?:-[-=>]?|\+[+=]?|\.{3}|[*&|\/%=<>^:!?]+)/;
|
||||||
WHITESPACE = /^[ \t]+/;
|
WHITESPACE = /^[ \t]+/;
|
||||||
COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/;
|
COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/;
|
||||||
CODE = /^[-=]>/;
|
CODE = /^[-=]>/;
|
||||||
@@ -625,7 +636,7 @@
|
|||||||
HEREDOC_INDENT = /\n+([ \t]*)/g;
|
HEREDOC_INDENT = /\n+([ \t]*)/g;
|
||||||
ASSIGNED = /^\s*@?[$A-Za-z_][$\w]*[ \t]*?[:=][^:=>]/;
|
ASSIGNED = /^\s*@?[$A-Za-z_][$\w]*[ \t]*?[:=][^:=>]/;
|
||||||
NEXT_CHARACTER = /^\s*(\S?)/;
|
NEXT_CHARACTER = /^\s*(\S?)/;
|
||||||
NEXT_ELLIPSIS = /^\s*\.\.\.?/;
|
NEXT_ELLIPSIS = /^\s*\.{3}/;
|
||||||
LEADING_SPACES = /^\s+/;
|
LEADING_SPACES = /^\s+/;
|
||||||
TRAILING_SPACES = /\s+$/;
|
TRAILING_SPACES = /\s+$/;
|
||||||
NO_NEWLINE = /^(?:[-+*&|\/%=<>!.\\][<>=&|]*|and|or|is(?:nt)?|n(?:ot|ew)|delete|typeof|instanceof)$/;
|
NO_NEWLINE = /^(?:[-+*&|\/%=<>!.\\][<>=&|]*|and|or|is(?:nt)?|n(?:ot|ew)|delete|typeof|instanceof)$/;
|
||||||
|
|||||||
367
lib/nodes.js
367
lib/nodes.js
@@ -1,5 +1,5 @@
|
|||||||
(function() {
|
(function() {
|
||||||
var Accessor, ArrayLiteral, Assign, Base, Call, Class, Closure, Code, Comment, Existence, Expressions, Extends, For, IDENTIFIER, IS_STRING, If, In, Index, Literal, NO, NUMBER, ObjectLiteral, Op, Param, Parens, Push, Range, Return, SIMPLENUM, Scope, Slice, Splat, Switch, TAB, THIS, TRAILING_WHITESPACE, Throw, Try, UTILITIES, Value, While, YES, _ref, compact, del, ends, flatten, last, merge, starts, utility;
|
var Accessor, ArrayLiteral, Assign, Base, Call, Class, Closure, Code, Comment, Existence, Expressions, Extends, For, IDENTIFIER, IS_STRING, If, In, Index, Literal, NO, NUMBER, ObjectLiteral, Op, Param, Parens, Push, Return, SIMPLENUM, Scope, Splat, Switch, TAB, THIS, TRAILING_WHITESPACE, Throw, Try, UTILITIES, Value, While, YES, _ref, compact, del, ends, extend, flatten, last, merge, starts, utility;
|
||||||
var __extends = function(child, parent) {
|
var __extends = function(child, parent) {
|
||||||
function ctor() { this.constructor = child; }
|
function ctor() { this.constructor = child; }
|
||||||
ctor.prototype = parent.prototype;
|
ctor.prototype = parent.prototype;
|
||||||
@@ -11,7 +11,8 @@
|
|||||||
return -1;
|
return -1;
|
||||||
};
|
};
|
||||||
Scope = require('./scope').Scope;
|
Scope = require('./scope').Scope;
|
||||||
_ref = require('./helpers'), compact = _ref.compact, flatten = _ref.flatten, merge = _ref.merge, del = _ref.del, starts = _ref.starts, ends = _ref.ends, last = _ref.last;
|
_ref = require('./helpers'), compact = _ref.compact, flatten = _ref.flatten, extend = _ref.extend, merge = _ref.merge, del = _ref.del, starts = _ref.starts, ends = _ref.ends, last = _ref.last;
|
||||||
|
exports.extend = extend;
|
||||||
YES = function() {
|
YES = function() {
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
@@ -57,7 +58,7 @@
|
|||||||
}
|
}
|
||||||
}).call(this);
|
}).call(this);
|
||||||
if ((options != null) ? options.precompile : undefined) {
|
if ((options != null) ? options.precompile : undefined) {
|
||||||
for (i = 0, _len = pair.length; i < _len; i++) {
|
for (i = 0, _len = pair.length; i < _len; ++i) {
|
||||||
node = pair[i];
|
node = pair[i];
|
||||||
(pair[i] = node.compile(o));
|
(pair[i] = node.compile(o));
|
||||||
}
|
}
|
||||||
@@ -68,6 +69,16 @@
|
|||||||
this.parenthetical = true;
|
this.parenthetical = true;
|
||||||
return this.compile(o);
|
return this.compile(o);
|
||||||
};
|
};
|
||||||
|
Base.prototype.compileLoopReference = function(o, name) {
|
||||||
|
var src, tmp;
|
||||||
|
src = tmp = this.compile(o);
|
||||||
|
if (!(NUMBER.test(src) || IDENTIFIER.test(src) && o.scope.check(src, {
|
||||||
|
immediate: true
|
||||||
|
}))) {
|
||||||
|
src = "" + (tmp = o.scope.freeVariable(name)) + " = " + src;
|
||||||
|
}
|
||||||
|
return [src, tmp];
|
||||||
|
};
|
||||||
Base.prototype.idt = function(tabs) {
|
Base.prototype.idt = function(tabs) {
|
||||||
return (this.tab || '') + Array((tabs || 0) + 1).join(TAB);
|
return (this.tab || '') + Array((tabs || 0) + 1).join(TAB);
|
||||||
};
|
};
|
||||||
@@ -103,7 +114,7 @@
|
|||||||
idt || (idt = '');
|
idt || (idt = '');
|
||||||
children = (function() {
|
children = (function() {
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_i = 0, _len = (_ref2 = this.collectChildren()).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.collectChildren()).length; _i < _len; ++_i) {
|
||||||
child = _ref2[_i];
|
child = _ref2[_i];
|
||||||
_result.push(child.toString(idt + TAB));
|
_result.push(child.toString(idt + TAB));
|
||||||
}
|
}
|
||||||
@@ -117,10 +128,10 @@
|
|||||||
if (!this.children) {
|
if (!this.children) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (_i = 0, _len = (_ref2 = this.children).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.children).length; _i < _len; ++_i) {
|
||||||
attr = _ref2[_i];
|
attr = _ref2[_i];
|
||||||
if (this[attr]) {
|
if (this[attr]) {
|
||||||
for (_j = 0, _len2 = (_ref3 = flatten([this[attr]])).length; _j < _len2; _j++) {
|
for (_j = 0, _len2 = (_ref3 = flatten([this[attr]])).length; _j < _len2; ++_j) {
|
||||||
child = _ref3[_j];
|
child = _ref3[_j];
|
||||||
if (func(child) === false) {
|
if (func(child) === false) {
|
||||||
return;
|
return;
|
||||||
@@ -205,7 +216,7 @@
|
|||||||
var _i, _len, _ref2, _result, node;
|
var _i, _len, _ref2, _result, node;
|
||||||
return (function() {
|
return (function() {
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_i = 0, _len = (_ref2 = this.expressions).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.expressions).length; _i < _len; ++_i) {
|
||||||
node = _ref2[_i];
|
node = _ref2[_i];
|
||||||
_result.push(this.compileExpression(node, merge(o)));
|
_result.push(this.compileExpression(node, merge(o)));
|
||||||
}
|
}
|
||||||
@@ -348,15 +359,12 @@
|
|||||||
Value.prototype.isObject = function() {
|
Value.prototype.isObject = function() {
|
||||||
return this.base instanceof ObjectLiteral && !this.properties.length;
|
return this.base instanceof ObjectLiteral && !this.properties.length;
|
||||||
};
|
};
|
||||||
Value.prototype.isSplice = function() {
|
|
||||||
return last(this.properties) instanceof Slice;
|
|
||||||
};
|
|
||||||
Value.prototype.isComplex = function() {
|
Value.prototype.isComplex = function() {
|
||||||
return this.base.isComplex() || this.hasProperties();
|
return this.base.isComplex() || this.hasProperties();
|
||||||
};
|
};
|
||||||
Value.prototype.isAtomic = function() {
|
Value.prototype.isAtomic = function() {
|
||||||
var _i, _len, _ref2, node;
|
var _i, _len, _ref2, node;
|
||||||
for (_i = 0, _len = (_ref2 = this.properties.concat(this.base)).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.properties.concat(this.base)).length; _i < _len; ++_i) {
|
||||||
node = _ref2[_i];
|
node = _ref2[_i];
|
||||||
if (node.soakNode || node instanceof Call) {
|
if (node.soakNode || node instanceof Call) {
|
||||||
return false;
|
return false;
|
||||||
@@ -417,7 +425,7 @@
|
|||||||
if (props[0] instanceof Accessor && this.isSimpleNumber()) {
|
if (props[0] instanceof Accessor && this.isSimpleNumber()) {
|
||||||
code = "(" + code + ")";
|
code = "(" + code + ")";
|
||||||
}
|
}
|
||||||
for (_i = 0, _len = props.length; _i < _len; _i++) {
|
for (_i = 0, _len = props.length; _i < _len; ++_i) {
|
||||||
prop = props[_i];
|
prop = props[_i];
|
||||||
(code += prop.compile(o));
|
(code += prop.compile(o));
|
||||||
}
|
}
|
||||||
@@ -429,7 +437,7 @@
|
|||||||
Array.prototype.push.apply(ifn.body.properties, this.properties);
|
Array.prototype.push.apply(ifn.body.properties, this.properties);
|
||||||
return ifn;
|
return ifn;
|
||||||
}
|
}
|
||||||
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; i++) {
|
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; ++i) {
|
||||||
prop = _ref2[i];
|
prop = _ref2[i];
|
||||||
if (prop.soakNode) {
|
if (prop.soakNode) {
|
||||||
prop.soakNode = false;
|
prop.soakNode = false;
|
||||||
@@ -544,7 +552,7 @@
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (_i = 0, _len = (_ref3 = list.reverse()).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref3 = list.reverse()).length; _i < _len; ++_i) {
|
||||||
call = _ref3[_i];
|
call = _ref3[_i];
|
||||||
if (ifn) {
|
if (ifn) {
|
||||||
if (call.variable instanceof Call) {
|
if (call.variable instanceof Call) {
|
||||||
@@ -563,7 +571,7 @@
|
|||||||
return ifn.compile(o);
|
return ifn.compile(o);
|
||||||
}
|
}
|
||||||
((_ref2 = this.variable) != null) ? _ref2.tags.front = this.tags.front : undefined;
|
((_ref2 = this.variable) != null) ? _ref2.tags.front = this.tags.front : undefined;
|
||||||
for (_i = 0, _len = (_ref3 = this.args).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref3 = this.args).length; _i < _len; ++_i) {
|
||||||
arg = _ref3[_i];
|
arg = _ref3[_i];
|
||||||
if (arg instanceof Splat) {
|
if (arg instanceof Splat) {
|
||||||
return this.compileSplat(o);
|
return this.compileSplat(o);
|
||||||
@@ -571,7 +579,7 @@
|
|||||||
}
|
}
|
||||||
args = (function() {
|
args = (function() {
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_j = 0, _len2 = (_ref4 = this.args).length; _j < _len2; _j++) {
|
for (_j = 0, _len2 = (_ref4 = this.args).length; _j < _len2; ++_j) {
|
||||||
arg = _ref4[_j];
|
arg = _ref4[_j];
|
||||||
_result.push(arg.compileBare(o));
|
_result.push(arg.compileBare(o));
|
||||||
}
|
}
|
||||||
@@ -663,117 +671,6 @@
|
|||||||
};
|
};
|
||||||
return Index;
|
return Index;
|
||||||
})();
|
})();
|
||||||
exports.Range = (function() {
|
|
||||||
Range = (function() {
|
|
||||||
function Range(_arg, _arg2, tag) {
|
|
||||||
this.to = _arg2;
|
|
||||||
this.from = _arg;
|
|
||||||
Range.__super__.constructor.call(this);
|
|
||||||
this.exclusive = tag === 'exclusive';
|
|
||||||
this.equals = this.exclusive ? '' : '=';
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
return Range;
|
|
||||||
})();
|
|
||||||
__extends(Range, Base);
|
|
||||||
Range.prototype.children = ['from', 'to'];
|
|
||||||
Range.prototype.compileVariables = function(o) {
|
|
||||||
var _ref2, _ref3, _ref4, parts;
|
|
||||||
o = merge(o, {
|
|
||||||
top: true
|
|
||||||
});
|
|
||||||
_ref2 = this.from.compileReference(o, {
|
|
||||||
precompile: true
|
|
||||||
}), this.from = _ref2[0], this.fromVar = _ref2[1];
|
|
||||||
_ref3 = this.to.compileReference(o, {
|
|
||||||
precompile: true
|
|
||||||
}), this.to = _ref3[0], this.toVar = _ref3[1];
|
|
||||||
_ref4 = [this.fromVar.match(SIMPLENUM), this.toVar.match(SIMPLENUM)], this.fromNum = _ref4[0], this.toNum = _ref4[1];
|
|
||||||
parts = [];
|
|
||||||
if (this.from !== this.fromVar) {
|
|
||||||
parts.push(this.from);
|
|
||||||
}
|
|
||||||
return this.to !== this.toVar ? parts.push(this.to) : undefined;
|
|
||||||
};
|
|
||||||
Range.prototype.compileNode = function(o) {
|
|
||||||
var compare, idx, incr, intro, step, stepPart, vars;
|
|
||||||
this.compileVariables(o);
|
|
||||||
if (!o.index) {
|
|
||||||
return this.compileArray(o);
|
|
||||||
}
|
|
||||||
if (this.fromNum && this.toNum) {
|
|
||||||
return this.compileSimple(o);
|
|
||||||
}
|
|
||||||
idx = del(o, 'index');
|
|
||||||
step = del(o, 'step');
|
|
||||||
vars = ("" + idx + " = " + this.from) + (this.to !== this.toVar ? ", " + this.to : '');
|
|
||||||
intro = "(" + this.fromVar + " <= " + this.toVar + " ? " + idx;
|
|
||||||
compare = "" + intro + " <" + this.equals + " " + this.toVar + " : " + idx + " >" + this.equals + " " + this.toVar + ")";
|
|
||||||
stepPart = step ? step.compile(o) : '1';
|
|
||||||
incr = step ? "" + idx + " += " + stepPart : "" + intro + " += " + stepPart + " : " + idx + " -= " + stepPart + ")";
|
|
||||||
return "" + vars + "; " + compare + "; " + incr;
|
|
||||||
};
|
|
||||||
Range.prototype.compileSimple = function(o) {
|
|
||||||
var _ref2, from, idx, step, to;
|
|
||||||
_ref2 = [+this.fromNum, +this.toNum], from = _ref2[0], to = _ref2[1];
|
|
||||||
idx = del(o, 'index');
|
|
||||||
step = del(o, 'step');
|
|
||||||
step && (step = "" + idx + " += " + (step.compile(o)));
|
|
||||||
return from <= to ? "" + idx + " = " + from + "; " + idx + " <" + this.equals + " " + to + "; " + (step || ("" + idx + "++")) : "" + idx + " = " + from + "; " + idx + " >" + this.equals + " " + to + "; " + (step || ("" + idx + "--"));
|
|
||||||
};
|
|
||||||
Range.prototype.compileArray = function(o) {
|
|
||||||
var _i, _ref2, _ref3, _result, body, clause, i, idt, post, pre, range, result, vars;
|
|
||||||
if (this.fromNum && this.toNum && Math.abs(this.fromNum - this.toNum) <= 20) {
|
|
||||||
range = (function() {
|
|
||||||
_result = [];
|
|
||||||
for (var _i = _ref2 = +this.fromNum, _ref3 = +this.toNum; _ref2 <= _ref3 ? _i <= _ref3 : _i >= _ref3; _ref2 <= _ref3 ? _i += 1 : _i -= 1){ _result.push(_i); }
|
|
||||||
return _result;
|
|
||||||
}).call(this);
|
|
||||||
if (this.exclusive) {
|
|
||||||
range.pop();
|
|
||||||
}
|
|
||||||
return "[" + (range.join(', ')) + "]";
|
|
||||||
}
|
|
||||||
idt = this.idt(1);
|
|
||||||
i = o.scope.freeVariable('i');
|
|
||||||
result = o.scope.freeVariable('result');
|
|
||||||
pre = "\n" + idt + result + " = [];";
|
|
||||||
if (this.fromNum && this.toNum) {
|
|
||||||
o.index = i;
|
|
||||||
body = this.compileSimple(o);
|
|
||||||
} else {
|
|
||||||
vars = ("" + i + " = " + this.from) + (this.to !== this.toVar ? ", " + this.to : '');
|
|
||||||
clause = "" + this.fromVar + " <= " + this.toVar + " ?";
|
|
||||||
body = "var " + vars + "; " + clause + " " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + clause + " " + i + " += 1 : " + i + " -= 1";
|
|
||||||
}
|
|
||||||
post = "{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + o.indent;
|
|
||||||
return "(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).call(this)";
|
|
||||||
};
|
|
||||||
return Range;
|
|
||||||
})();
|
|
||||||
exports.Slice = (function() {
|
|
||||||
Slice = (function() {
|
|
||||||
function Slice(_arg) {
|
|
||||||
this.range = _arg;
|
|
||||||
Slice.__super__.constructor.call(this);
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
return Slice;
|
|
||||||
})();
|
|
||||||
__extends(Slice, Base);
|
|
||||||
Slice.prototype.children = ['range'];
|
|
||||||
Slice.prototype.compileNode = function(o) {
|
|
||||||
var from, to;
|
|
||||||
from = this.range.from ? this.range.from.compile(o) : '0';
|
|
||||||
to = this.range.to ? this.range.to.compile(o) : '';
|
|
||||||
to += !to || this.range.exclusive ? '' : ' + 1';
|
|
||||||
if (to) {
|
|
||||||
to = ', ' + to;
|
|
||||||
}
|
|
||||||
return ".slice(" + from + to + ")";
|
|
||||||
};
|
|
||||||
return Slice;
|
|
||||||
})();
|
|
||||||
exports.ObjectLiteral = (function() {
|
exports.ObjectLiteral = (function() {
|
||||||
ObjectLiteral = (function() {
|
ObjectLiteral = (function() {
|
||||||
function ObjectLiteral(props) {
|
function ObjectLiteral(props) {
|
||||||
@@ -791,7 +688,7 @@
|
|||||||
o.indent = this.idt(1);
|
o.indent = this.idt(1);
|
||||||
nonComments = (function() {
|
nonComments = (function() {
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; ++_i) {
|
||||||
prop = _ref2[_i];
|
prop = _ref2[_i];
|
||||||
if (!(prop instanceof Comment)) {
|
if (!(prop instanceof Comment)) {
|
||||||
_result.push(prop);
|
_result.push(prop);
|
||||||
@@ -802,7 +699,7 @@
|
|||||||
lastNoncom = last(nonComments);
|
lastNoncom = last(nonComments);
|
||||||
props = (function() {
|
props = (function() {
|
||||||
_result = [];
|
_result = [];
|
||||||
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; i++) {
|
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; ++i) {
|
||||||
prop = _ref2[i];
|
prop = _ref2[i];
|
||||||
_result.push((function() {
|
_result.push((function() {
|
||||||
join = i === this.properties.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
|
join = i === this.properties.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
|
||||||
@@ -823,7 +720,7 @@
|
|||||||
};
|
};
|
||||||
ObjectLiteral.prototype.assigns = function(name) {
|
ObjectLiteral.prototype.assigns = function(name) {
|
||||||
var _i, _len, _ref2, prop;
|
var _i, _len, _ref2, prop;
|
||||||
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; ++_i) {
|
||||||
prop = _ref2[_i];
|
prop = _ref2[_i];
|
||||||
if (prop.assigns(name)) {
|
if (prop.assigns(name)) {
|
||||||
return true;
|
return true;
|
||||||
@@ -850,14 +747,14 @@
|
|||||||
ArrayLiteral.prototype.compileNode = function(o) {
|
ArrayLiteral.prototype.compileNode = function(o) {
|
||||||
var _i, _len, _len2, _ref2, _ref3, code, i, obj, objects;
|
var _i, _len, _len2, _ref2, _ref3, code, i, obj, objects;
|
||||||
o.indent = this.idt(1);
|
o.indent = this.idt(1);
|
||||||
for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; ++_i) {
|
||||||
obj = _ref2[_i];
|
obj = _ref2[_i];
|
||||||
if (obj instanceof Splat) {
|
if (obj instanceof Splat) {
|
||||||
return this.compileSplatLiteral(o);
|
return this.compileSplatLiteral(o);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
objects = [];
|
objects = [];
|
||||||
for (i = 0, _len2 = (_ref3 = this.objects).length; i < _len2; i++) {
|
for (i = 0, _len2 = (_ref3 = this.objects).length; i < _len2; ++i) {
|
||||||
obj = _ref3[i];
|
obj = _ref3[i];
|
||||||
code = obj.compileBare(o);
|
code = obj.compileBare(o);
|
||||||
objects.push(obj instanceof Comment ? "\n" + code + "\n" + o.indent : i === this.objects.length - 1 ? code : code + ', ');
|
objects.push(obj instanceof Comment ? "\n" + code + "\n" + o.indent : i === this.objects.length - 1 ? code : code + ', ');
|
||||||
@@ -867,7 +764,7 @@
|
|||||||
};
|
};
|
||||||
ArrayLiteral.prototype.assigns = function(name) {
|
ArrayLiteral.prototype.assigns = function(name) {
|
||||||
var _i, _len, _ref2, obj;
|
var _i, _len, _ref2, obj;
|
||||||
for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; ++_i) {
|
||||||
obj = _ref2[_i];
|
obj = _ref2[_i];
|
||||||
if (obj.assigns(name)) {
|
if (obj.assigns(name)) {
|
||||||
return true;
|
return true;
|
||||||
@@ -914,7 +811,7 @@
|
|||||||
} else {
|
} else {
|
||||||
constructor = new Code([], new Expressions([new Return(new Literal('this'))]));
|
constructor = new Code([], new Expressions([new Return(new Literal('this'))]));
|
||||||
}
|
}
|
||||||
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; ++_i) {
|
||||||
prop = _ref2[_i];
|
prop = _ref2[_i];
|
||||||
pvar = prop.variable, func = prop.value;
|
pvar = prop.variable, func = prop.value;
|
||||||
if (pvar && pvar.base.value === 'constructor') {
|
if (pvar && pvar.base.value === 'constructor') {
|
||||||
@@ -996,9 +893,6 @@
|
|||||||
if (this.variable.isArray() || this.variable.isObject()) {
|
if (this.variable.isArray() || this.variable.isObject()) {
|
||||||
return this.compilePatternMatch(o);
|
return this.compilePatternMatch(o);
|
||||||
}
|
}
|
||||||
if (this.variable.isSplice()) {
|
|
||||||
return this.compileSplice(o);
|
|
||||||
}
|
|
||||||
if (ifn = If.unfoldSoak(o, this, 'variable')) {
|
if (ifn = If.unfoldSoak(o, this, 'variable')) {
|
||||||
delete o.top;
|
delete o.top;
|
||||||
return ifn.compile(o);
|
return ifn.compile(o);
|
||||||
@@ -1058,7 +952,7 @@
|
|||||||
assigns.push("" + (ref = o.scope.freeVariable('ref')) + " = " + valVar);
|
assigns.push("" + (ref = o.scope.freeVariable('ref')) + " = " + valVar);
|
||||||
valVar = ref;
|
valVar = ref;
|
||||||
}
|
}
|
||||||
for (i = 0, _len = objects.length; i < _len; i++) {
|
for (i = 0, _len = objects.length; i < _len; ++i) {
|
||||||
obj = objects[i];
|
obj = objects[i];
|
||||||
idx = i;
|
idx = i;
|
||||||
if (isObject) {
|
if (isObject) {
|
||||||
@@ -1089,17 +983,6 @@
|
|||||||
code = assigns.join(', ');
|
code = assigns.join(', ');
|
||||||
return top || this.parenthetical ? code : "(" + code + ")";
|
return top || this.parenthetical ? code : "(" + code + ")";
|
||||||
};
|
};
|
||||||
Assign.prototype.compileSplice = function(o) {
|
|
||||||
var from, name, plus, range, ref, to, val;
|
|
||||||
range = this.variable.properties.pop().range;
|
|
||||||
name = this.variable.compile(o);
|
|
||||||
plus = range.exclusive ? '' : ' + 1';
|
|
||||||
from = range.from ? range.from.compile(o) : '0';
|
|
||||||
to = range.to ? range.to.compile(o) + ' - ' + from + plus : "" + name + ".length";
|
|
||||||
ref = o.scope.freeVariable('ref');
|
|
||||||
val = this.value.compile(o);
|
|
||||||
return "([].splice.apply(" + name + ", [" + from + ", " + to + "].concat(" + ref + " = " + val + ")), " + ref + ")";
|
|
||||||
};
|
|
||||||
Assign.prototype.compileConditional = function(o) {
|
Assign.prototype.compileConditional = function(o) {
|
||||||
var _ref2, left, rite;
|
var _ref2, left, rite;
|
||||||
_ref2 = this.variable.cacheReference(o), left = _ref2[0], rite = _ref2[1];
|
_ref2 = this.variable.cacheReference(o), left = _ref2[0], rite = _ref2[1];
|
||||||
@@ -1140,7 +1023,7 @@
|
|||||||
delete o.globals;
|
delete o.globals;
|
||||||
splat = undefined;
|
splat = undefined;
|
||||||
params = [];
|
params = [];
|
||||||
for (i = 0, _len = (_ref2 = this.params).length; i < _len; i++) {
|
for (i = 0, _len = (_ref2 = this.params).length; i < _len; ++i) {
|
||||||
param = _ref2[i];
|
param = _ref2[i];
|
||||||
if (splat) {
|
if (splat) {
|
||||||
if (param.attach) {
|
if (param.attach) {
|
||||||
@@ -1168,7 +1051,7 @@
|
|||||||
o.scope.startLevel();
|
o.scope.startLevel();
|
||||||
params = (function() {
|
params = (function() {
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_i = 0, _len2 = params.length; _i < _len2; _i++) {
|
for (_i = 0, _len2 = params.length; _i < _len2; ++_i) {
|
||||||
param = params[_i];
|
param = params[_i];
|
||||||
_result.push(param.compile(o));
|
_result.push(param.compile(o));
|
||||||
}
|
}
|
||||||
@@ -1177,7 +1060,7 @@
|
|||||||
if (!(empty || this.noReturn)) {
|
if (!(empty || this.noReturn)) {
|
||||||
this.body.makeReturn();
|
this.body.makeReturn();
|
||||||
}
|
}
|
||||||
for (_i = 0, _len2 = params.length; _i < _len2; _i++) {
|
for (_i = 0, _len2 = params.length; _i < _len2; ++_i) {
|
||||||
param = params[_i];
|
param = params[_i];
|
||||||
o.scope.parameter(param);
|
o.scope.parameter(param);
|
||||||
}
|
}
|
||||||
@@ -1258,7 +1141,7 @@
|
|||||||
variadic = o.scope.freeVariable('result');
|
variadic = o.scope.freeVariable('result');
|
||||||
o.scope.assign(variadic, len + ' >= ' + this.arglength);
|
o.scope.assign(variadic, len + ' >= ' + this.arglength);
|
||||||
end = this.trailings.length ? ", " + len + " - " + this.trailings.length : undefined;
|
end = this.trailings.length ? ", " + len + " - " + this.trailings.length : undefined;
|
||||||
for (idx = 0, _len = (_ref2 = this.trailings).length; idx < _len; idx++) {
|
for (idx = 0, _len = (_ref2 = this.trailings).length; idx < _len; ++idx) {
|
||||||
trailing = _ref2[idx];
|
trailing = _ref2[idx];
|
||||||
if (trailing.attach) {
|
if (trailing.attach) {
|
||||||
assign = trailing.assign;
|
assign = trailing.assign;
|
||||||
@@ -1280,7 +1163,7 @@
|
|||||||
var _len, arg, args, code, end, i, prev;
|
var _len, arg, args, code, end, i, prev;
|
||||||
args = [];
|
args = [];
|
||||||
end = -1;
|
end = -1;
|
||||||
for (i = 0, _len = list.length; i < _len; i++) {
|
for (i = 0, _len = list.length; i < _len; ++i) {
|
||||||
arg = list[i];
|
arg = list[i];
|
||||||
code = arg.compile(o);
|
code = arg.compile(o);
|
||||||
prev = args[end];
|
prev = args[end];
|
||||||
@@ -1480,7 +1363,7 @@
|
|||||||
_ref3 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref3[0], cnj = _ref3[1];
|
_ref3 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref3[0], cnj = _ref3[1];
|
||||||
tests = (function() {
|
tests = (function() {
|
||||||
_result = [];
|
_result = [];
|
||||||
for (i = 0, _len = (_ref4 = this.array.base.objects).length; i < _len; i++) {
|
for (i = 0, _len = (_ref4 = this.array.base.objects).length; i < _len; ++i) {
|
||||||
item = _ref4[i];
|
item = _ref4[i];
|
||||||
_result.push((i ? ref : sub) + cmp + item.compile(o));
|
_result.push((i ? ref : sub) + cmp + item.compile(o));
|
||||||
}
|
}
|
||||||
@@ -1616,22 +1499,16 @@
|
|||||||
})();
|
})();
|
||||||
exports.For = (function() {
|
exports.For = (function() {
|
||||||
For = (function() {
|
For = (function() {
|
||||||
function For(_arg, source, _arg2, _arg3) {
|
function For(_arg, head) {
|
||||||
var _ref2;
|
|
||||||
this.index = _arg3;
|
|
||||||
this.name = _arg2;
|
|
||||||
this.body = _arg;
|
this.body = _arg;
|
||||||
For.__super__.constructor.call(this);
|
if (head.index instanceof Value) {
|
||||||
this.source = source.source, this.guard = source.guard, this.step = source.step;
|
|
||||||
this.raw = !!source.raw;
|
|
||||||
this.object = !!source.object;
|
|
||||||
if (this.object) {
|
|
||||||
_ref2 = [this.index, this.name], this.name = _ref2[0], this.index = _ref2[1];
|
|
||||||
}
|
|
||||||
if (this.index instanceof Value) {
|
|
||||||
throw SyntaxError('index cannot be a pattern matching expression');
|
throw SyntaxError('index cannot be a pattern matching expression');
|
||||||
}
|
}
|
||||||
this.range = this.source instanceof Value && this.source.base instanceof Range && !this.source.properties.length;
|
For.__super__.constructor.call(this);
|
||||||
|
extend(this, head);
|
||||||
|
if (!this.object) {
|
||||||
|
this.step || (this.step = new Literal(1));
|
||||||
|
}
|
||||||
this.pattern = this.name instanceof Value;
|
this.pattern = this.name instanceof Value;
|
||||||
if (this.range && this.pattern) {
|
if (this.range && this.pattern) {
|
||||||
throw SyntaxError('cannot pattern match a range loop');
|
throw SyntaxError('cannot pattern match a range loop');
|
||||||
@@ -1642,7 +1519,7 @@
|
|||||||
return For;
|
return For;
|
||||||
})();
|
})();
|
||||||
__extends(For, Base);
|
__extends(For, Base);
|
||||||
For.prototype.children = ['body', 'source', 'guard'];
|
For.prototype.children = ['body', 'source', 'guard', 'step', 'from', 'to'];
|
||||||
For.prototype.topSensitive = YES;
|
For.prototype.topSensitive = YES;
|
||||||
For.prototype.isStatement = YES;
|
For.prototype.isStatement = YES;
|
||||||
For.prototype.makeReturn = function() {
|
For.prototype.makeReturn = function() {
|
||||||
@@ -1659,16 +1536,16 @@
|
|||||||
return '';
|
return '';
|
||||||
};
|
};
|
||||||
For.prototype.compileNode = function(o) {
|
For.prototype.compileNode = function(o) {
|
||||||
var body, codeInBody, forPart, guardPart, idt1, index, ivar, lastLine, lvar, name, namePart, nvar, ref, resultPart, returnResult, rvar, scope, source, sourcePart, stepPart, svar, topLevel, unstepPart, varPart, vars;
|
var _ref2, _ref3, _ref4, _ref5, _ref6, body, cond, forPart, guardPart, idt, index, ivar, lvar, name, namePart, pvar, resultDef, resultRet, rvar, scope, sourcePart, step, svar, tail, top, tvar, varPart, vars;
|
||||||
topLevel = del(o, 'top') && !this.returns;
|
|
||||||
source = this.range ? this.source.base : this.source;
|
|
||||||
codeInBody = !this.body.containsPureStatement() && this.body.contains(function(node) {
|
|
||||||
return node instanceof Code;
|
|
||||||
});
|
|
||||||
scope = o.scope;
|
scope = o.scope;
|
||||||
name = this.name && this.name.compile(o);
|
top = del(o, 'top') && !this.returns;
|
||||||
index = this.index && this.index.compile(o);
|
name = !this.pattern && (((_ref2 = this.name) != null) ? _ref2.compile(o) : undefined);
|
||||||
if (name && !this.pattern && (this.range || !codeInBody)) {
|
index = ((_ref3 = this.index) != null) ? _ref3.compile(o) : undefined;
|
||||||
|
ivar = !index ? scope.freeVariable('i') : index;
|
||||||
|
varPart = '';
|
||||||
|
body = Expressions.wrap([this.body]);
|
||||||
|
idt = this.idt(1);
|
||||||
|
if (name) {
|
||||||
scope.find(name, {
|
scope.find(name, {
|
||||||
immediate: true
|
immediate: true
|
||||||
});
|
});
|
||||||
@@ -1678,101 +1555,73 @@
|
|||||||
immediate: true
|
immediate: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (!topLevel) {
|
if (this.step) {
|
||||||
rvar = scope.freeVariable('result');
|
_ref4 = this.step.compileLoopReference(o, 'step'), step = _ref4[0], pvar = _ref4[1];
|
||||||
}
|
}
|
||||||
ivar = this.range ? name : index;
|
if (this.from) {
|
||||||
if (!ivar || codeInBody) {
|
_ref5 = this.to.compileLoopReference(o, 'to'), tail = _ref5[0], tvar = _ref5[1];
|
||||||
ivar = scope.freeVariable('i');
|
vars = "" + ivar + " = " + (this.from.compile(o));
|
||||||
}
|
if (tail !== tvar) {
|
||||||
if (name && !this.range && codeInBody) {
|
vars += ", " + tail;
|
||||||
nvar = scope.freeVariable('i');
|
}
|
||||||
}
|
cond = +pvar ? "" + ivar + " " + (pvar < 0 ? '>' : '<') + "= " + tvar : "" + pvar + " < 0 ? " + ivar + " >= " + tvar + " : " + ivar + " <= " + tvar;
|
||||||
varPart = '';
|
|
||||||
guardPart = '';
|
|
||||||
unstepPart = '';
|
|
||||||
body = Expressions.wrap([this.body]);
|
|
||||||
idt1 = this.idt(1);
|
|
||||||
if (this.range) {
|
|
||||||
forPart = source.compile(merge(o, {
|
|
||||||
index: ivar,
|
|
||||||
step: this.step
|
|
||||||
}));
|
|
||||||
} else {
|
} else {
|
||||||
svar = sourcePart = this.source.compile(o);
|
if (name || !this.raw) {
|
||||||
if ((name || !this.raw) && !(IDENTIFIER.test(svar) && scope.check(svar, {
|
_ref6 = this.source.compileLoopReference(o, 'ref'), sourcePart = _ref6[0], svar = _ref6[1];
|
||||||
immediate: true
|
if (!(sourcePart === svar || this.object)) {
|
||||||
}))) {
|
|
||||||
sourcePart = "" + (ref = scope.freeVariable('ref')) + " = " + svar;
|
|
||||||
if (!this.object) {
|
|
||||||
sourcePart = "(" + sourcePart + ")";
|
sourcePart = "(" + sourcePart + ")";
|
||||||
}
|
}
|
||||||
svar = ref;
|
} else {
|
||||||
|
sourcePart = svar = this.source.compile(o);
|
||||||
}
|
}
|
||||||
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(merge(o, {
|
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(merge(o, {
|
||||||
top: true
|
top: true
|
||||||
})) : name ? "" + name + " = " + svar + "[" + ivar + "]" : undefined;
|
})) : name ? "" + name + " = " + svar + "[" + ivar + "]" : undefined;
|
||||||
if (!this.object) {
|
if (!this.object) {
|
||||||
lvar = scope.freeVariable('len');
|
if (0 > pvar && (pvar | 0) === +pvar) {
|
||||||
stepPart = this.step ? "" + ivar + " += " + (this.step.compile(o)) : "" + ivar + "++";
|
vars = "" + ivar + " = " + sourcePart + ".length - 1";
|
||||||
forPart = "" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length; " + ivar + " < " + lvar + "; " + stepPart;
|
cond = "" + ivar + " >= 0";
|
||||||
|
} else {
|
||||||
|
lvar = scope.freeVariable('len');
|
||||||
|
vars = "" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length";
|
||||||
|
cond = "" + ivar + " < " + lvar;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resultPart = rvar ? "" + this.tab + rvar + " = [];\n" : '';
|
if (this.object) {
|
||||||
returnResult = this.compileReturnValue(rvar, o);
|
forPart = "" + ivar + " in " + sourcePart;
|
||||||
if (!topLevel) {
|
guardPart = !this.raw && ("" + idt + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;\n");
|
||||||
|
} else {
|
||||||
|
if (step !== pvar) {
|
||||||
|
vars += ", " + step;
|
||||||
|
}
|
||||||
|
forPart = ("" + vars + "; " + cond + "; ") + (function() {
|
||||||
|
switch (+pvar) {
|
||||||
|
case 1:
|
||||||
|
return '++' + ivar;
|
||||||
|
case -1:
|
||||||
|
return '--' + ivar;
|
||||||
|
default:
|
||||||
|
return ivar + (pvar < 0 ? ' -= ' + pvar.slice(1) : ' += ' + pvar);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
if (!top) {
|
||||||
|
rvar = scope.freeVariable('result');
|
||||||
|
resultDef = "" + this.tab + rvar + " = [];\n";
|
||||||
|
resultRet = this.compileReturnValue(rvar, o);
|
||||||
body = Push.wrap(rvar, body);
|
body = Push.wrap(rvar, body);
|
||||||
}
|
}
|
||||||
if (this.guard) {
|
if (this.guard) {
|
||||||
body = Expressions.wrap([new If(this.guard, body)]);
|
body = Expressions.wrap([new If(this.guard, body)]);
|
||||||
}
|
}
|
||||||
if (codeInBody) {
|
if (namePart) {
|
||||||
if (this.range) {
|
varPart = "" + idt + namePart + ";\n";
|
||||||
body.unshift(new Literal("var " + name + " = " + ivar));
|
|
||||||
}
|
|
||||||
if (namePart) {
|
|
||||||
body.unshift(new Literal("var " + namePart));
|
|
||||||
}
|
|
||||||
if (index) {
|
|
||||||
body.unshift(new Literal("var " + index + " = " + ivar));
|
|
||||||
}
|
|
||||||
lastLine = body.expressions.pop();
|
|
||||||
if (index) {
|
|
||||||
body.push(new Assign(new Literal(ivar), new Literal(index)));
|
|
||||||
}
|
|
||||||
if (nvar) {
|
|
||||||
body.push(new Assign(new Literal(nvar), new Literal(name)));
|
|
||||||
}
|
|
||||||
body.push(lastLine);
|
|
||||||
o.indent = this.idt(1);
|
|
||||||
body = Expressions.wrap([new Literal(body.compile(o))]);
|
|
||||||
if (index) {
|
|
||||||
body.push(new Assign(this.index, new Literal(ivar)));
|
|
||||||
}
|
|
||||||
if (name) {
|
|
||||||
body.push(new Assign(this.name, new Literal(nvar || ivar)));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (namePart) {
|
|
||||||
varPart = "" + idt1 + namePart + ";\n";
|
|
||||||
}
|
|
||||||
if (forPart && name === ivar) {
|
|
||||||
unstepPart = this.step ? "" + name + " -= " + (this.step.compile(o)) + ";" : "" + name + "--;";
|
|
||||||
unstepPart = ("\n" + this.tab) + unstepPart;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (this.object) {
|
return "" + (resultDef || '') + this.tab + "for (" + forPart + ") {\n" + (guardPart || '') + varPart + (body.compile(merge(o, {
|
||||||
forPart = "" + ivar + " in " + sourcePart;
|
indent: idt,
|
||||||
if (!this.raw) {
|
|
||||||
guardPart = "\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
body = body.compile(merge(o, {
|
|
||||||
indent: idt1,
|
|
||||||
top: true
|
top: true
|
||||||
}));
|
}))) + "\n" + this.tab + "}" + (resultRet || '');
|
||||||
vars = this.range ? name : "" + name + ", " + ivar;
|
|
||||||
return "" + resultPart + this.tab + "for (" + forPart + ") {" + guardPart + "\n" + varPart + body + "\n" + this.tab + "}" + unstepPart + returnResult;
|
|
||||||
};
|
};
|
||||||
return For;
|
return For;
|
||||||
})();
|
})();
|
||||||
@@ -1792,7 +1641,7 @@
|
|||||||
Switch.prototype.isStatement = YES;
|
Switch.prototype.isStatement = YES;
|
||||||
Switch.prototype.makeReturn = function() {
|
Switch.prototype.makeReturn = function() {
|
||||||
var _i, _len, _ref2, pair;
|
var _i, _len, _ref2, pair;
|
||||||
for (_i = 0, _len = (_ref2 = this.cases).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.cases).length; _i < _len; ++_i) {
|
||||||
pair = _ref2[_i];
|
pair = _ref2[_i];
|
||||||
pair[1].makeReturn();
|
pair[1].makeReturn();
|
||||||
}
|
}
|
||||||
@@ -1807,9 +1656,9 @@
|
|||||||
idt2 = o.indent = this.idt(2);
|
idt2 = o.indent = this.idt(2);
|
||||||
o.top = true;
|
o.top = true;
|
||||||
code = "" + this.tab + "switch (" + ((((_ref2 = this.subject) != null) ? _ref2.compile(o) : undefined) || true) + ") {";
|
code = "" + this.tab + "switch (" + ((((_ref2 = this.subject) != null) ? _ref2.compile(o) : undefined) || true) + ") {";
|
||||||
for (_i = 0, _len = (_ref3 = this.cases).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref3 = this.cases).length; _i < _len; ++_i) {
|
||||||
_ref4 = _ref3[_i], conditions = _ref4[0], block = _ref4[1];
|
_ref4 = _ref3[_i], conditions = _ref4[0], block = _ref4[1];
|
||||||
for (_j = 0, _len2 = (_ref5 = flatten([conditions])).length; _j < _len2; _j++) {
|
for (_j = 0, _len2 = (_ref5 = flatten([conditions])).length; _j < _len2; ++_j) {
|
||||||
condition = _ref5[_j];
|
condition = _ref5[_j];
|
||||||
if (!this.subject) {
|
if (!this.subject) {
|
||||||
condition = condition.invert().invert();
|
condition = condition.invert().invert();
|
||||||
@@ -1968,7 +1817,7 @@
|
|||||||
TAB = ' ';
|
TAB = ' ';
|
||||||
TRAILING_WHITESPACE = /[ \t]+$/gm;
|
TRAILING_WHITESPACE = /[ \t]+$/gm;
|
||||||
IDENTIFIER = /^[$A-Za-z_][$\w]*$/;
|
IDENTIFIER = /^[$A-Za-z_][$\w]*$/;
|
||||||
NUMBER = /^0x[\da-f]+$|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?$/i;
|
NUMBER = /^-?(?:0x[\da-f]+|(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?)$/i;
|
||||||
SIMPLENUM = /^[+-]?\d+$/;
|
SIMPLENUM = /^[+-]?\d+$/;
|
||||||
IS_STRING = /^['"]/;
|
IS_STRING = /^['"]/;
|
||||||
utility = function(name) {
|
utility = function(name) {
|
||||||
|
|||||||
@@ -15,11 +15,11 @@
|
|||||||
arguments: []
|
arguments: []
|
||||||
};
|
};
|
||||||
args = normalizeArguments(args);
|
args = normalizeArguments(args);
|
||||||
for (i = 0, _len = args.length; i < _len; i++) {
|
for (i = 0, _len = args.length; i < _len; ++i) {
|
||||||
arg = args[i];
|
arg = args[i];
|
||||||
isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
||||||
matchedRule = false;
|
matchedRule = false;
|
||||||
for (_i = 0, _len2 = (_ref = this.rules).length; _i < _len2; _i++) {
|
for (_i = 0, _len2 = (_ref = this.rules).length; _i < _len2; ++_i) {
|
||||||
rule = _ref[_i];
|
rule = _ref[_i];
|
||||||
if (rule.shortFlag === arg || rule.longFlag === arg) {
|
if (rule.shortFlag === arg || rule.longFlag === arg) {
|
||||||
value = rule.hasArgument ? args[i += 1] : true;
|
value = rule.hasArgument ? args[i += 1] : true;
|
||||||
@@ -44,7 +44,7 @@
|
|||||||
if (this.banner) {
|
if (this.banner) {
|
||||||
lines.unshift("" + this.banner + "\n");
|
lines.unshift("" + this.banner + "\n");
|
||||||
}
|
}
|
||||||
for (_i = 0, _len = (_ref = this.rules).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref = this.rules).length; _i < _len; ++_i) {
|
||||||
rule = _ref[_i];
|
rule = _ref[_i];
|
||||||
spaces = 15 - rule.longFlag.length;
|
spaces = 15 - rule.longFlag.length;
|
||||||
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
|
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
|
||||||
@@ -62,7 +62,7 @@
|
|||||||
buildRules = function(rules) {
|
buildRules = function(rules) {
|
||||||
var _i, _len, _result, tuple;
|
var _i, _len, _result, tuple;
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_i = 0, _len = rules.length; _i < _len; _i++) {
|
for (_i = 0, _len = rules.length; _i < _len; ++_i) {
|
||||||
tuple = rules[_i];
|
tuple = rules[_i];
|
||||||
_result.push((function() {
|
_result.push((function() {
|
||||||
if (tuple.length < 3) {
|
if (tuple.length < 3) {
|
||||||
@@ -91,10 +91,10 @@
|
|||||||
var _i, _j, _len, _len2, _ref, arg, l, match, result;
|
var _i, _j, _len, _len2, _ref, arg, l, match, result;
|
||||||
args = args.slice(0);
|
args = args.slice(0);
|
||||||
result = [];
|
result = [];
|
||||||
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
for (_i = 0, _len = args.length; _i < _len; ++_i) {
|
||||||
arg = args[_i];
|
arg = args[_i];
|
||||||
if (match = arg.match(MULTI_FLAG)) {
|
if (match = arg.match(MULTI_FLAG)) {
|
||||||
for (_j = 0, _len2 = (_ref = match[1].split('')).length; _j < _len2; _j++) {
|
for (_j = 0, _len2 = (_ref = match[1].split('')).length; _j < _len2; ++_j) {
|
||||||
l = _ref[_j];
|
l = _ref[_j];
|
||||||
result.push('-' + l);
|
result.push('-' + l);
|
||||||
}
|
}
|
||||||
|
|||||||
386
lib/parser.js
386
lib/parser.js
File diff suppressed because one or more lines are too long
@@ -87,7 +87,7 @@
|
|||||||
};
|
};
|
||||||
exports.Rewriter.prototype.removeLeadingNewlines = function() {
|
exports.Rewriter.prototype.removeLeadingNewlines = function() {
|
||||||
var _len, _ref, i, tag;
|
var _len, _ref, i, tag;
|
||||||
for (i = 0, _len = (_ref = this.tokens).length; i < _len; i++) {
|
for (i = 0, _len = (_ref = this.tokens).length; i < _len; ++i) {
|
||||||
tag = _ref[i][0];
|
tag = _ref[i][0];
|
||||||
if (tag !== 'TERMINATOR') {
|
if (tag !== 'TERMINATOR') {
|
||||||
break;
|
break;
|
||||||
@@ -286,7 +286,7 @@
|
|||||||
this.scanTokens(function(token, i) {
|
this.scanTokens(function(token, i) {
|
||||||
var _i, _len, _ref, _ref2, close, open, tag;
|
var _i, _len, _ref, _ref2, close, open, tag;
|
||||||
tag = token[0];
|
tag = token[0];
|
||||||
for (_i = 0, _len = (_ref = pairs).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref = pairs).length; _i < _len; ++_i) {
|
||||||
_ref2 = _ref[_i], open = _ref2[0], close = _ref2[1];
|
_ref2 = _ref[_i], open = _ref2[0], close = _ref2[1];
|
||||||
levels[open] |= 0;
|
levels[open] |= 0;
|
||||||
if (tag === open) {
|
if (tag === open) {
|
||||||
@@ -366,7 +366,7 @@
|
|||||||
INVERSES = {};
|
INVERSES = {};
|
||||||
EXPRESSION_START = [];
|
EXPRESSION_START = [];
|
||||||
EXPRESSION_END = [];
|
EXPRESSION_END = [];
|
||||||
for (_i = 0, _len = BALANCED_PAIRS.length; _i < _len; _i++) {
|
for (_i = 0, _len = BALANCED_PAIRS.length; _i < _len; ++_i) {
|
||||||
_ref = BALANCED_PAIRS[_i], left = _ref[0], rite = _ref[1];
|
_ref = BALANCED_PAIRS[_i], left = _ref[0], rite = _ref[1];
|
||||||
EXPRESSION_START.push(INVERSES[rite] = left);
|
EXPRESSION_START.push(INVERSES[rite] = left);
|
||||||
EXPRESSION_END.push(INVERSES[left] = rite);
|
EXPRESSION_END.push(INVERSES[left] = rite);
|
||||||
|
|||||||
10
lib/scope.js
10
lib/scope.js
@@ -42,7 +42,7 @@
|
|||||||
};
|
};
|
||||||
Scope.prototype.endLevel = function() {
|
Scope.prototype.endLevel = function() {
|
||||||
var _i, _len, _ref2, name;
|
var _i, _len, _ref2, name;
|
||||||
for (_i = 0, _len = (_ref2 = this.garbage.pop()).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.garbage.pop()).length; _i < _len; ++_i) {
|
||||||
name = _ref2[_i];
|
name = _ref2[_i];
|
||||||
if (this.type(name) === 'var') {
|
if (this.type(name) === 'var') {
|
||||||
this.add(name, 'reuse');
|
this.add(name, 'reuse');
|
||||||
@@ -59,7 +59,7 @@
|
|||||||
};
|
};
|
||||||
Scope.prototype.any = function(fn) {
|
Scope.prototype.any = function(fn) {
|
||||||
var _i, _len, _ref2, v;
|
var _i, _len, _ref2, v;
|
||||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) {
|
||||||
v = _ref2[_i];
|
v = _ref2[_i];
|
||||||
if (fn(v)) {
|
if (fn(v)) {
|
||||||
return true;
|
return true;
|
||||||
@@ -83,7 +83,7 @@
|
|||||||
};
|
};
|
||||||
Scope.prototype.type = function(name) {
|
Scope.prototype.type = function(name) {
|
||||||
var _i, _len, _ref2, v;
|
var _i, _len, _ref2, v;
|
||||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) {
|
||||||
v = _ref2[_i];
|
v = _ref2[_i];
|
||||||
if (v.name === name) {
|
if (v.name === name) {
|
||||||
return v.type;
|
return v.type;
|
||||||
@@ -122,7 +122,7 @@
|
|||||||
var _i, _len, _ref2, _ref3, _result, v;
|
var _i, _len, _ref2, _ref3, _result, v;
|
||||||
return (function() {
|
return (function() {
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) {
|
||||||
v = _ref2[_i];
|
v = _ref2[_i];
|
||||||
if ((_ref3 = v.type) === 'var' || _ref3 === 'reuse') {
|
if ((_ref3 = v.type) === 'var' || _ref3 === 'reuse') {
|
||||||
_result.push(v.name);
|
_result.push(v.name);
|
||||||
@@ -134,7 +134,7 @@
|
|||||||
Scope.prototype.assignedVariables = function() {
|
Scope.prototype.assignedVariables = function() {
|
||||||
var _i, _len, _ref2, _result, v;
|
var _i, _len, _ref2, _result, v;
|
||||||
_result = [];
|
_result = [];
|
||||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; _i++) {
|
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) {
|
||||||
v = _ref2[_i];
|
v = _ref2[_i];
|
||||||
if (v.type.assigned) {
|
if (v.type.assigned) {
|
||||||
_result.push("" + v.name + " = " + v.type.value);
|
_result.push("" + v.name + " = " + v.type.value);
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ runScripts = ->
|
|||||||
if script.src
|
if script.src
|
||||||
CoffeeScript.load script.src
|
CoffeeScript.load script.src
|
||||||
else
|
else
|
||||||
setTimeout -> CoffeeScript.run script.innerHTML
|
CoffeeScript.run script.innerHTML
|
||||||
null
|
null
|
||||||
if window.addEventListener
|
if window.addEventListener
|
||||||
addEventListener 'DOMContentLoaded', runScripts, no
|
addEventListener 'DOMContentLoaded', runScripts, no
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
# from our rules and saves it into `lib/parser.js`.
|
# from our rules and saves it into `lib/parser.js`.
|
||||||
|
|
||||||
# The only dependency is on the **Jison.Parser**.
|
# The only dependency is on the **Jison.Parser**.
|
||||||
Parser = require('jison').Parser
|
{Parser} = require 'jison'
|
||||||
|
|
||||||
# Jison DSL
|
# Jison DSL
|
||||||
# ---------
|
# ---------
|
||||||
@@ -35,7 +35,7 @@ o = (patternString, action, options) ->
|
|||||||
return [patternString, '$$ = $1;', options] unless action
|
return [patternString, '$$ = $1;', options] unless action
|
||||||
action = if match = unwrap.exec action then match[1] else "(#{action}())"
|
action = if match = unwrap.exec action then match[1] else "(#{action}())"
|
||||||
action = action.replace /\bnew /g, '$&yy.'
|
action = action.replace /\bnew /g, '$&yy.'
|
||||||
action = action.replace /\bExpressions\.wrap\b/g, 'yy.$&'
|
action = action.replace /\b(?:Expressions\.wrap|extend)\b/g, 'yy.$&'
|
||||||
[patternString, "$$ = #{action};", options]
|
[patternString, "$$ = #{action};", options]
|
||||||
|
|
||||||
# Grammatical Rules
|
# Grammatical Rules
|
||||||
@@ -144,14 +144,14 @@ grammar =
|
|||||||
# Assignment when it happens within an object literal. The difference from
|
# Assignment when it happens within an object literal. The difference from
|
||||||
# the ordinary **Assign** is that these allow numbers and strings as keys.
|
# the ordinary **Assign** is that these allow numbers and strings as keys.
|
||||||
AssignObj: [
|
AssignObj: [
|
||||||
o "Identifier", -> new Value $1
|
o 'Identifier', -> new Value $1
|
||||||
o "AlphaNumeric"
|
o 'AlphaNumeric'
|
||||||
o "ThisProperty"
|
o 'ThisProperty'
|
||||||
o "Identifier : Expression", -> new Assign new Value($1), $3, 'object'
|
o 'Identifier : Expression', -> new Assign new Value($1), $3, 'object'
|
||||||
o "AlphaNumeric : Expression", -> new Assign new Value($1), $3, 'object'
|
o 'AlphaNumeric : Expression', -> new Assign new Value($1), $3, 'object'
|
||||||
o "Identifier : INDENT Expression OUTDENT", -> new Assign new Value($1), $4, 'object'
|
o 'Identifier : INDENT Expression OUTDENT', -> new Assign new Value($1), $4, 'object'
|
||||||
o "AlphaNumeric : INDENT Expression OUTDENT", -> new Assign new Value($1), $4, 'object'
|
o 'AlphaNumeric : INDENT Expression OUTDENT', -> new Assign new Value($1), $4, 'object'
|
||||||
o "Comment"
|
o 'Comment'
|
||||||
]
|
]
|
||||||
|
|
||||||
# A return statement from a function body.
|
# A return statement from a function body.
|
||||||
@@ -233,7 +233,6 @@ grammar =
|
|||||||
o "Assignable"
|
o "Assignable"
|
||||||
o "Literal", -> new Value $1
|
o "Literal", -> new Value $1
|
||||||
o "Parenthetical", -> new Value $1
|
o "Parenthetical", -> new Value $1
|
||||||
o "Range", -> new Value $1
|
|
||||||
o "This"
|
o "This"
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -251,8 +250,8 @@ grammar =
|
|||||||
# Indexing into an object or array using bracket notation.
|
# Indexing into an object or array using bracket notation.
|
||||||
Index: [
|
Index: [
|
||||||
o "INDEX_START Expression INDEX_END", -> new Index $2
|
o "INDEX_START Expression INDEX_END", -> new Index $2
|
||||||
o "INDEX_SOAK Index", -> $2.soakNode = yes; $2
|
o "INDEX_SOAK Index", -> extend $2, soakNode: yes
|
||||||
o "INDEX_PROTO Index", -> $2.proto = yes; $2
|
o "INDEX_PROTO Index", -> extend $2, proto: yes
|
||||||
]
|
]
|
||||||
|
|
||||||
# In CoffeeScript, an object literal is simply a list of assignments.
|
# In CoffeeScript, an object literal is simply a list of assignments.
|
||||||
@@ -330,28 +329,11 @@ grammar =
|
|||||||
o "@", -> new Value new Literal 'this'
|
o "@", -> new Value new Literal 'this'
|
||||||
]
|
]
|
||||||
|
|
||||||
RangeDots: [
|
|
||||||
o "..", -> 'inclusive'
|
|
||||||
o "...", -> 'exclusive'
|
|
||||||
]
|
|
||||||
|
|
||||||
# A reference to a property on *this*.
|
# A reference to a property on *this*.
|
||||||
ThisProperty: [
|
ThisProperty: [
|
||||||
o "@ Identifier", -> new Value new Literal('this'), [new Accessor($2)], 'this'
|
o "@ Identifier", -> new Value new Literal('this'), [new Accessor($2)], 'this'
|
||||||
]
|
]
|
||||||
|
|
||||||
# The CoffeeScript range literal.
|
|
||||||
Range: [
|
|
||||||
o "[ Expression RangeDots Expression ]", -> new Range $2, $4, $3
|
|
||||||
]
|
|
||||||
|
|
||||||
# The slice literal.
|
|
||||||
Slice: [
|
|
||||||
o "INDEX_START Expression RangeDots Expression INDEX_END", -> new Range $2, $4, $3
|
|
||||||
o "INDEX_START Expression RangeDots INDEX_END", -> new Range $2, null, $3
|
|
||||||
o "INDEX_START RangeDots Expression INDEX_END", -> new Range null, $3, $2
|
|
||||||
]
|
|
||||||
|
|
||||||
# The array literal.
|
# The array literal.
|
||||||
Array: [
|
Array: [
|
||||||
o "[ ]", -> new ArrayLiteral []
|
o "[ ]", -> new ArrayLiteral []
|
||||||
@@ -435,48 +417,49 @@ grammar =
|
|||||||
# Comprehensions can either be normal, with a block of expressions to execute,
|
# Comprehensions can either be normal, with a block of expressions to execute,
|
||||||
# or postfix, with a single expression.
|
# or postfix, with a single expression.
|
||||||
For: [
|
For: [
|
||||||
o "Statement ForBody", -> new For $1, $2, $2.vars[0], $2.vars[1]
|
o 'Statement ForBody', -> new For $1, $2
|
||||||
o "Expression ForBody", -> new For $1, $2, $2.vars[0], $2.vars[1]
|
o 'Expression ForBody', -> new For $1, $2
|
||||||
o "ForBody Block", -> new For $2, $1, $1.vars[0], $1.vars[1]
|
o 'ForBody Block', -> new For $2, $1
|
||||||
]
|
|
||||||
|
|
||||||
ForBody: [
|
|
||||||
o "FOR Range", -> source: new Value($2), vars: []
|
|
||||||
o "ForStart ForSource", -> $2.raw = $1.raw; $2.vars = $1; $2
|
|
||||||
]
|
|
||||||
|
|
||||||
ForStart: [
|
|
||||||
o "FOR ForVariables", -> $2
|
|
||||||
o "FOR ALL ForVariables", -> $3.raw = true; $3
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# An array of all accepted values for a variable inside the loop. This
|
# An array of all accepted values for a variable inside the loop. This
|
||||||
# enables support for pattern matching.
|
# enables support for pattern matching.
|
||||||
ForValue: [
|
ForValue: [
|
||||||
o "Identifier"
|
o 'Identifier'
|
||||||
o "Array", -> new Value $1
|
o 'Array', -> new Value $1
|
||||||
o "Object", -> new Value $1
|
o 'Object', -> new Value $1
|
||||||
]
|
]
|
||||||
|
|
||||||
# An array or range comprehension has variables for the current element and
|
ForIn: [
|
||||||
# (optional) reference to the current index. Or, *key, value*, in the case
|
o 'FORIN Expression', -> source: $2
|
||||||
# of object comprehensions.
|
o 'FORIN Expression WHEN Expression', -> source: $2, guard: $4
|
||||||
ForVariables: [
|
o 'FORIN Expression BY Expression', -> source: $2, step: $4
|
||||||
o "ForValue", -> [$1]
|
o 'FORIN Expression BY Expression WHEN Expression', -> source: $2, step: $4, guard: $6
|
||||||
o "ForValue , ForValue", -> [$1, $3]
|
]
|
||||||
|
|
||||||
|
ForOf: [
|
||||||
|
o 'FOROF Expression', -> object: on, source: $2
|
||||||
|
o 'FOROF Expression WHEN Expression', -> object: on, source: $2, guard: $4
|
||||||
|
]
|
||||||
|
|
||||||
|
ForTo: [
|
||||||
|
o 'TO Expression', -> to: $2
|
||||||
|
o 'TO Expression WHEN Expression', -> to: $2, guard: $4
|
||||||
|
o 'TO Expression BY Expression', -> to: $2, step: $4
|
||||||
|
o 'TO Expression BY Expression WHEN Expression', -> to: $2, step: $4, guard: $6
|
||||||
]
|
]
|
||||||
|
|
||||||
# The source of a comprehension is an array or object with an optional guard
|
# The source of a comprehension is an array or object with an optional guard
|
||||||
# clause. If it's an array comprehension, you can also choose to step through
|
# clause. If it's an array comprehension, you can also choose to step through
|
||||||
# in fixed-size increments.
|
# in fixed-size increments.
|
||||||
ForSource: [
|
ForBody: [
|
||||||
o "FORIN Expression", -> source: $2
|
o 'FOR ForValue ForIn', -> extend $3, name: $2
|
||||||
o "FOROF Expression", -> source: $2, object: true
|
o 'FOR ForValue , Identifier ForIn', -> extend $5, name: $2, index: $4
|
||||||
o "FORIN Expression WHEN Expression", -> source: $2, guard: $4
|
o 'FOR Identifier ForOf', -> extend $3, index: $2
|
||||||
o "FOROF Expression WHEN Expression", -> source: $2, guard: $4, object: true
|
o 'FOR ForValue , ForValue ForOf', -> extend $5, index: $2, name: $4
|
||||||
o "FORIN Expression BY Expression", -> source: $2, step: $4
|
o 'FOR ALL Identifier ForOf', -> extend $4, raw: on, index: $3
|
||||||
o "FORIN Expression WHEN Expression BY Expression", -> source: $2, guard: $4, step: $6
|
o 'FOR ALL Identifier , ForValue ForOf', -> extend $6, raw: on, index: $3, name: $5
|
||||||
o "FORIN Expression BY Expression WHEN Expression", -> source: $2, step: $4, guard: $6
|
o 'FOR Identifier FROM Expression ForTo', -> extend $5, index: $2, from: $4
|
||||||
]
|
]
|
||||||
|
|
||||||
Switch: [
|
Switch: [
|
||||||
@@ -578,7 +561,7 @@ operators = [
|
|||||||
["left", 'LOGIC']
|
["left", 'LOGIC']
|
||||||
["left", '.']
|
["left", '.']
|
||||||
["nonassoc", 'INDENT', 'OUTDENT']
|
["nonassoc", 'INDENT', 'OUTDENT']
|
||||||
["right", 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'BY', 'THROW']
|
["right", 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'FROM', 'TO', 'BY', 'THROW']
|
||||||
["right", 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'EXTENDS']
|
["right", 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'EXTENDS']
|
||||||
["right", '=', ':', 'COMPOUND_ASSIGN', 'RETURN']
|
["right", '=', ':', 'COMPOUND_ASSIGN', 'RETURN']
|
||||||
["right", '->', '=>', 'UNLESS', 'POST_IF', 'POST_UNLESS']
|
["right", '->', '=>', 'UNLESS', 'POST_IF', 'POST_UNLESS']
|
||||||
@@ -592,7 +575,7 @@ operators = [
|
|||||||
# terminals (every symbol which does not appear as the name of a rule above)
|
# terminals (every symbol which does not appear as the name of a rule above)
|
||||||
# as "tokens".
|
# as "tokens".
|
||||||
tokens = []
|
tokens = []
|
||||||
for name, alternatives of grammar
|
for all name, alternatives of grammar
|
||||||
grammar[name] = for alt in alternatives
|
grammar[name] = for alt in alternatives
|
||||||
for token in alt[0].split ' '
|
for token in alt[0].split ' '
|
||||||
tokens.push token unless grammar[token]
|
tokens.push token unless grammar[token]
|
||||||
|
|||||||
@@ -41,9 +41,10 @@ exports.Lexer = class Lexer
|
|||||||
@indent = 0 # The current indentation level.
|
@indent = 0 # The current indentation level.
|
||||||
@indebt = 0 # The over-indentation at the current level.
|
@indebt = 0 # The over-indentation at the current level.
|
||||||
@outdebt = 0 # The under-outdentation at the current level.
|
@outdebt = 0 # The under-outdentation at the current level.
|
||||||
@seenFor = no # The flag for distinguishing FORIN/FOROF from IN/OF.
|
|
||||||
@indents = [] # The stack of all current indentation levels.
|
@indents = [] # The stack of all current indentation levels.
|
||||||
@tokens = [] # Stream of parsed tokens in the form ['TYPE', value, line]
|
@tokens = [] # Stream of parsed tokens in the form ['TYPE', value, line]
|
||||||
|
# Flags for distinguishing FORIN/FOROF/FROM/TO.
|
||||||
|
@seenFor = @seenFrom = no
|
||||||
# At every position, run through this list of attempted matches,
|
# At every position, run through this list of attempted matches,
|
||||||
# short-circuiting if any of them succeed. Their order determines precedence:
|
# short-circuiting if any of them succeed. Their order determines precedence:
|
||||||
# `@literalToken` is the fallback catch-all.
|
# `@literalToken` is the fallback catch-all.
|
||||||
@@ -78,6 +79,15 @@ exports.Lexer = class Lexer
|
|||||||
if id is 'all' and @tag() is 'FOR'
|
if id is 'all' and @tag() is 'FOR'
|
||||||
@token 'ALL', id
|
@token 'ALL', id
|
||||||
return true
|
return true
|
||||||
|
if id is 'from' and @tag(1) is 'FOR'
|
||||||
|
@seenFor = no
|
||||||
|
@seenFrom = yes
|
||||||
|
@token 'FROM', id
|
||||||
|
return true
|
||||||
|
if id is 'to' and @seenFrom
|
||||||
|
@seenFrom = no
|
||||||
|
@token 'TO', id
|
||||||
|
return true
|
||||||
forcedIdentifier = colon or @tagAccessor()
|
forcedIdentifier = colon or @tagAccessor()
|
||||||
tag = 'IDENTIFIER'
|
tag = 'IDENTIFIER'
|
||||||
if id in JS_KEYWORDS or
|
if id in JS_KEYWORDS or
|
||||||
@@ -426,7 +436,7 @@ exports.Lexer = class Lexer
|
|||||||
i += 1
|
i += 1
|
||||||
if levels.length
|
if levels.length
|
||||||
throw SyntaxError "Unterminated #{levels.pop()[0]} starting on line #{@line + 1}"
|
throw SyntaxError "Unterminated #{levels.pop()[0]} starting on line #{@line + 1}"
|
||||||
if not i then false else str[0...i]
|
i and str.slice 0, i
|
||||||
|
|
||||||
# Expand variables and expressions inside double-quoted strings using
|
# Expand variables and expressions inside double-quoted strings using
|
||||||
# Ruby-like notation for substitution of arbitrary expressions.
|
# Ruby-like notation for substitution of arbitrary expressions.
|
||||||
@@ -549,7 +559,7 @@ IDENTIFIER = /// ^
|
|||||||
///
|
///
|
||||||
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i
|
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?/i
|
||||||
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[ \t]*)?\1/
|
HEREDOC = /^("""|''')([\s\S]*?)(?:\n[ \t]*)?\1/
|
||||||
OPERATOR = /// ^ (?: -[-=>]? | \+[+=]? | \.\.\.? | [*&|/%=<>^:!?]+ ) ///
|
OPERATOR = /// ^ (?: -[-=>]? | \+[+=]? | \.{3} | [*&|/%=<>^:!?]+ ) ///
|
||||||
WHITESPACE = /^[ \t]+/
|
WHITESPACE = /^[ \t]+/
|
||||||
COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/
|
COMMENT = /^###([^#][\s\S]*?)(?:###[ \t]*\n|(?:###)?$)|^(?:\s*#(?!##[^#]).*)+/
|
||||||
CODE = /^[-=]>/
|
CODE = /^[-=]>/
|
||||||
@@ -579,7 +589,7 @@ MULTILINER = /\n/g
|
|||||||
HEREDOC_INDENT = /\n+([ \t]*)/g
|
HEREDOC_INDENT = /\n+([ \t]*)/g
|
||||||
ASSIGNED = /^\s*@?[$A-Za-z_][$\w]*[ \t]*?[:=][^:=>]/
|
ASSIGNED = /^\s*@?[$A-Za-z_][$\w]*[ \t]*?[:=][^:=>]/
|
||||||
NEXT_CHARACTER = /^\s*(\S?)/
|
NEXT_CHARACTER = /^\s*(\S?)/
|
||||||
NEXT_ELLIPSIS = /^\s*\.\.\.?/
|
NEXT_ELLIPSIS = /^\s*\.{3}/
|
||||||
LEADING_SPACES = /^\s+/
|
LEADING_SPACES = /^\s+/
|
||||||
TRAILING_SPACES = /\s+$/
|
TRAILING_SPACES = /\s+$/
|
||||||
NO_NEWLINE = /// ^
|
NO_NEWLINE = /// ^
|
||||||
|
|||||||
239
src/nodes.coffee
239
src/nodes.coffee
@@ -6,7 +6,9 @@
|
|||||||
{Scope} = require './scope'
|
{Scope} = require './scope'
|
||||||
|
|
||||||
# Import the helpers we plan to use.
|
# Import the helpers we plan to use.
|
||||||
{compact, flatten, merge, del, starts, ends, last} = require './helpers'
|
{compact, flatten, extend, merge, del, starts, ends, last} = require './helpers'
|
||||||
|
|
||||||
|
exports.extend = extend # for parser
|
||||||
|
|
||||||
# Constant functions for nodes that don't need customization.
|
# Constant functions for nodes that don't need customization.
|
||||||
YES = -> yes
|
YES = -> yes
|
||||||
@@ -73,6 +75,14 @@ exports.Base = class Base
|
|||||||
@parenthetical = on
|
@parenthetical = on
|
||||||
@compile o
|
@compile o
|
||||||
|
|
||||||
|
# Compile to a source/variable pair suitable for looping.
|
||||||
|
compileLoopReference: (o, name) ->
|
||||||
|
src = tmp = @compile o
|
||||||
|
unless NUMBER.test(src) or
|
||||||
|
IDENTIFIER.test(src) and o.scope.check(src, immediate: on)
|
||||||
|
src = "#{ tmp = o.scope.freeVariable name } = #{src}"
|
||||||
|
[src, tmp]
|
||||||
|
|
||||||
# Convenience method to grab the current indentation level, plus tabbing in.
|
# Convenience method to grab the current indentation level, plus tabbing in.
|
||||||
idt: (tabs) ->
|
idt: (tabs) ->
|
||||||
(@tab or '') + Array((tabs or 0) + 1).join TAB
|
(@tab or '') + Array((tabs or 0) + 1).join TAB
|
||||||
@@ -329,9 +339,6 @@ exports.Value = class Value extends Base
|
|||||||
isObject: ->
|
isObject: ->
|
||||||
@base instanceof ObjectLiteral and not @properties.length
|
@base instanceof ObjectLiteral and not @properties.length
|
||||||
|
|
||||||
isSplice: ->
|
|
||||||
last(@properties) instanceof Slice
|
|
||||||
|
|
||||||
isComplex: ->
|
isComplex: ->
|
||||||
@base.isComplex() or @hasProperties()
|
@base.isComplex() or @hasProperties()
|
||||||
|
|
||||||
@@ -590,96 +597,6 @@ exports.Index = class Index extends Base
|
|||||||
|
|
||||||
isComplex: -> @index.isComplex()
|
isComplex: -> @index.isComplex()
|
||||||
|
|
||||||
#### Range
|
|
||||||
|
|
||||||
# A range literal. Ranges can be used to extract portions (slices) of arrays,
|
|
||||||
# to specify a range for comprehensions, or as a value, to be expanded into the
|
|
||||||
# corresponding array of integers at runtime.
|
|
||||||
exports.Range = class Range extends Base
|
|
||||||
|
|
||||||
children: ['from', 'to']
|
|
||||||
|
|
||||||
constructor: (@from, @to, tag) ->
|
|
||||||
super()
|
|
||||||
@exclusive = tag is 'exclusive'
|
|
||||||
@equals = if @exclusive then '' else '='
|
|
||||||
|
|
||||||
# Compiles the range's source variables -- where it starts and where it ends.
|
|
||||||
# But only if they need to be cached to avoid double evaluation.
|
|
||||||
compileVariables: (o) ->
|
|
||||||
o = merge(o, top: true)
|
|
||||||
[@from, @fromVar] = @from.compileReference o, precompile: yes
|
|
||||||
[@to, @toVar] = @to.compileReference o, precompile: yes
|
|
||||||
[@fromNum, @toNum] = [@fromVar.match(SIMPLENUM), @toVar.match(SIMPLENUM)]
|
|
||||||
parts = []
|
|
||||||
parts.push @from if @from isnt @fromVar
|
|
||||||
parts.push @to if @to isnt @toVar
|
|
||||||
|
|
||||||
# When compiled normally, the range returns the contents of the *for loop*
|
|
||||||
# needed to iterate over the values in the range. Used by comprehensions.
|
|
||||||
compileNode: (o) ->
|
|
||||||
@compileVariables o
|
|
||||||
return @compileArray(o) unless o.index
|
|
||||||
return @compileSimple(o) if @fromNum and @toNum
|
|
||||||
idx = del o, 'index'
|
|
||||||
step = del o, 'step'
|
|
||||||
vars = "#{idx} = #{@from}" + if @to isnt @toVar then ", #{@to}" else ''
|
|
||||||
intro = "(#{@fromVar} <= #{@toVar} ? #{idx}"
|
|
||||||
compare = "#{intro} <#{@equals} #{@toVar} : #{idx} >#{@equals} #{@toVar})"
|
|
||||||
stepPart = if step then step.compile(o) else '1'
|
|
||||||
incr = if step then "#{idx} += #{stepPart}" else "#{intro} += #{stepPart} : #{idx} -= #{stepPart})"
|
|
||||||
"#{vars}; #{compare}; #{incr}"
|
|
||||||
|
|
||||||
# Compile a simple range comprehension, with integers.
|
|
||||||
compileSimple: (o) ->
|
|
||||||
[from, to] = [+@fromNum, +@toNum]
|
|
||||||
idx = del o, 'index'
|
|
||||||
step = del o, 'step'
|
|
||||||
step and= "#{idx} += #{step.compile(o)}"
|
|
||||||
if from <= to
|
|
||||||
"#{idx} = #{from}; #{idx} <#{@equals} #{to}; #{step or "#{idx}++"}"
|
|
||||||
else
|
|
||||||
"#{idx} = #{from}; #{idx} >#{@equals} #{to}; #{step or "#{idx}--"}"
|
|
||||||
|
|
||||||
# When used as a value, expand the range into the equivalent array.
|
|
||||||
compileArray: (o) ->
|
|
||||||
if @fromNum and @toNum and Math.abs(@fromNum - @toNum) <= 20
|
|
||||||
range = [+@fromNum..+@toNum]
|
|
||||||
range.pop() if @exclusive
|
|
||||||
return "[#{ range.join(', ') }]"
|
|
||||||
idt = @idt 1
|
|
||||||
i = o.scope.freeVariable 'i'
|
|
||||||
result = o.scope.freeVariable 'result'
|
|
||||||
pre = "\n#{idt}#{result} = [];"
|
|
||||||
if @fromNum and @toNum
|
|
||||||
o.index = i
|
|
||||||
body = @compileSimple o
|
|
||||||
else
|
|
||||||
vars = "#{i} = #{@from}" + if @to isnt @toVar then ", #{@to}" else ''
|
|
||||||
clause = "#{@fromVar} <= #{@toVar} ?"
|
|
||||||
body = "var #{vars}; #{clause} #{i} <#{@equals} #{@toVar} : #{i} >#{@equals} #{@toVar}; #{clause} #{i} += 1 : #{i} -= 1"
|
|
||||||
post = "{ #{result}.push(#{i}); }\n#{idt}return #{result};\n#{o.indent}"
|
|
||||||
"(function() {#{pre}\n#{idt}for (#{body})#{post}}).call(this)"
|
|
||||||
|
|
||||||
#### Slice
|
|
||||||
|
|
||||||
# An array slice literal. Unlike JavaScript's `Array#slice`, the second parameter
|
|
||||||
# specifies the index of the end of the slice, just as the first parameter
|
|
||||||
# is the index of the beginning.
|
|
||||||
exports.Slice = class Slice extends Base
|
|
||||||
|
|
||||||
children: ['range']
|
|
||||||
|
|
||||||
constructor: (@range) ->
|
|
||||||
super()
|
|
||||||
|
|
||||||
compileNode: (o) ->
|
|
||||||
from = if @range.from then @range.from.compile(o) else '0'
|
|
||||||
to = if @range.to then @range.to.compile(o) else ''
|
|
||||||
to += if not to or @range.exclusive then '' else ' + 1'
|
|
||||||
to = ', ' + to if to
|
|
||||||
".slice(#{from}#{to})"
|
|
||||||
|
|
||||||
#### ObjectLiteral
|
#### ObjectLiteral
|
||||||
|
|
||||||
# An object literal, nothing fancy.
|
# An object literal, nothing fancy.
|
||||||
@@ -863,7 +780,6 @@ exports.Assign = class Assign extends Base
|
|||||||
compileNode: (o) ->
|
compileNode: (o) ->
|
||||||
if isValue = @variable instanceof Value
|
if isValue = @variable instanceof Value
|
||||||
return @compilePatternMatch(o) if @variable.isArray() or @variable.isObject()
|
return @compilePatternMatch(o) if @variable.isArray() or @variable.isObject()
|
||||||
return @compileSplice(o) if @variable.isSplice()
|
|
||||||
if ifn = If.unfoldSoak o, this, 'variable'
|
if ifn = If.unfoldSoak o, this, 'variable'
|
||||||
delete o.top
|
delete o.top
|
||||||
return ifn.compile o
|
return ifn.compile o
|
||||||
@@ -933,18 +849,6 @@ exports.Assign = class Assign extends Base
|
|||||||
code = assigns.join ', '
|
code = assigns.join ', '
|
||||||
if top or @parenthetical then code else "(#{code})"
|
if top or @parenthetical then code else "(#{code})"
|
||||||
|
|
||||||
# Compile the assignment from an array splice literal, using JavaScript's
|
|
||||||
# `Array#splice` method.
|
|
||||||
compileSplice: (o) ->
|
|
||||||
{range} = @variable.properties.pop()
|
|
||||||
name = @variable.compile o
|
|
||||||
plus = if range.exclusive then '' else ' + 1'
|
|
||||||
from = if range.from then range.from.compile(o) else '0'
|
|
||||||
to = if range.to then range.to.compile(o) + ' - ' + from + plus else "#{name}.length"
|
|
||||||
ref = o.scope.freeVariable 'ref'
|
|
||||||
val = @value.compile(o)
|
|
||||||
"([].splice.apply(#{name}, [#{from}, #{to}].concat(#{ref} = #{val})), #{ref})"
|
|
||||||
|
|
||||||
# When compiling a conditional assignment, take care to ensure that the
|
# When compiling a conditional assignment, take care to ensure that the
|
||||||
# operands are only evaluated once, even though we have to reference them
|
# operands are only evaluated once, even though we have to reference them
|
||||||
# more than once.
|
# more than once.
|
||||||
@@ -1399,19 +1303,17 @@ exports.Parens = class Parens extends Base
|
|||||||
# you can map and filter in a single pass.
|
# you can map and filter in a single pass.
|
||||||
exports.For = class For extends Base
|
exports.For = class For extends Base
|
||||||
|
|
||||||
children: ['body', 'source', 'guard']
|
children: ['body', 'source', 'guard', 'step', 'from', 'to']
|
||||||
|
|
||||||
topSensitive: YES
|
topSensitive: YES
|
||||||
isStatement : YES
|
isStatement : YES
|
||||||
|
|
||||||
constructor: (@body, source, @name, @index) ->
|
constructor: (@body, head) ->
|
||||||
|
if head.index instanceof Value
|
||||||
|
throw SyntaxError 'index cannot be a pattern matching expression'
|
||||||
super()
|
super()
|
||||||
{@source, @guard, @step} = source
|
extend this, head
|
||||||
@raw = !!source.raw
|
@step or= new Literal 1 unless @object
|
||||||
@object = !!source.object
|
|
||||||
[@name, @index] = [@index, @name] if @object
|
|
||||||
throw SyntaxError 'index cannot be a pattern matching expression' if @index instanceof Value
|
|
||||||
@range = @source instanceof Value and @source.base instanceof Range and not @source.properties.length
|
|
||||||
@pattern = @name instanceof Value
|
@pattern = @name instanceof Value
|
||||||
throw SyntaxError 'cannot pattern match a range loop' if @range and @pattern
|
throw SyntaxError 'cannot pattern match a range loop' if @range and @pattern
|
||||||
@returns = false
|
@returns = false
|
||||||
@@ -1430,71 +1332,64 @@ exports.For = class For extends Base
|
|||||||
# comprehensions. Some of the generated code can be shared in common, and
|
# comprehensions. Some of the generated code can be shared in common, and
|
||||||
# some cannot.
|
# some cannot.
|
||||||
compileNode: (o) ->
|
compileNode: (o) ->
|
||||||
topLevel = del(o, 'top') and not @returns
|
{scope} = o
|
||||||
source = if @range then @source.base else @source
|
top = del(o, 'top') and not @returns
|
||||||
codeInBody = not @body.containsPureStatement() and @body.contains (node) -> node instanceof Code
|
name = not @pattern and @name?.compile o
|
||||||
scope = o.scope
|
index = @index?.compile o
|
||||||
name = @name and @name.compile o
|
ivar = if not index then scope.freeVariable 'i' else index
|
||||||
index = @index and @index.compile o
|
varPart = ''
|
||||||
scope.find(name, immediate: yes) if name and not @pattern and (@range or not codeInBody)
|
body = Expressions.wrap [@body]
|
||||||
|
idt = @idt 1
|
||||||
|
scope.find(name, immediate: yes) if name
|
||||||
scope.find(index, immediate: yes) if index
|
scope.find(index, immediate: yes) if index
|
||||||
rvar = scope.freeVariable 'result' unless topLevel
|
[step, pvar] = @step.compileLoopReference o, 'step' if @step
|
||||||
ivar = if @range then name else index
|
if @from
|
||||||
ivar = scope.freeVariable 'i' if not ivar or codeInBody
|
[tail, tvar] = @to.compileLoopReference o, 'to'
|
||||||
nvar = scope.freeVariable 'i' if name and not @range and codeInBody
|
vars = "#{ivar} = #{ @from.compile o }"
|
||||||
varPart = ''
|
vars += ", #{tail}" if tail isnt tvar
|
||||||
guardPart = ''
|
cond = if +pvar
|
||||||
unstepPart = ''
|
"#{ivar} #{ if pvar < 0 then '>' else '<' }= #{tvar}"
|
||||||
body = Expressions.wrap [@body]
|
else
|
||||||
idt1 = @idt 1
|
"#{pvar} < 0 ? #{ivar} >= #{tvar} : #{ivar} <= #{tvar}"
|
||||||
if @range
|
|
||||||
forPart = source.compile merge o, {index: ivar, @step}
|
|
||||||
else
|
else
|
||||||
svar = sourcePart = @source.compile o
|
if name or not @raw
|
||||||
if (name or not @raw) and
|
[sourcePart, svar] = @source.compileLoopReference o, 'ref'
|
||||||
not (IDENTIFIER.test(svar) and scope.check svar, immediate: on)
|
sourcePart = "(#{sourcePart})" unless sourcePart is svar or @object
|
||||||
sourcePart = "#{ref = scope.freeVariable 'ref'} = #{svar}"
|
else
|
||||||
sourcePart = "(#{sourcePart})" unless @object
|
sourcePart = svar = @source.compile o
|
||||||
svar = ref
|
|
||||||
namePart = if @pattern
|
namePart = if @pattern
|
||||||
new Assign(@name, new Literal "#{svar}[#{ivar}]").compile merge o, top: on
|
new Assign(@name, new Literal "#{svar}[#{ivar}]").compile merge o, top: on
|
||||||
else if name
|
else if name
|
||||||
"#{name} = #{svar}[#{ivar}]"
|
"#{name} = #{svar}[#{ivar}]"
|
||||||
unless @object
|
unless @object
|
||||||
lvar = scope.freeVariable 'len'
|
if 0 > pvar and (pvar | 0) is +pvar # negative int
|
||||||
stepPart = if @step then "#{ivar} += #{ @step.compile(o) }" else "#{ivar}++"
|
vars = "#{ivar} = #{sourcePart}.length - 1"
|
||||||
forPart = "#{ivar} = 0, #{lvar} = #{sourcePart}.length; #{ivar} < #{lvar}; #{stepPart}"
|
cond = "#{ivar} >= 0"
|
||||||
resultPart = if rvar then "#{@tab}#{rvar} = [];\n" else ''
|
else
|
||||||
returnResult = @compileReturnValue rvar, o
|
lvar = scope.freeVariable 'len'
|
||||||
body = Push.wrap rvar, body unless topLevel
|
vars = "#{ivar} = 0, #{lvar} = #{sourcePart}.length"
|
||||||
if @guard
|
cond = "#{ivar} < #{lvar}"
|
||||||
body = Expressions.wrap [new If @guard, body]
|
|
||||||
if codeInBody
|
|
||||||
body.unshift new Literal "var #{name} = #{ivar}" if @range
|
|
||||||
body.unshift new Literal "var #{namePart}" if namePart
|
|
||||||
body.unshift new Literal "var #{index} = #{ivar}" if index
|
|
||||||
lastLine = body.expressions.pop()
|
|
||||||
body.push new Assign new Literal(ivar), new Literal index if index
|
|
||||||
body.push new Assign new Literal(nvar), new Literal name if nvar
|
|
||||||
body.push lastLine
|
|
||||||
o.indent = @idt 1
|
|
||||||
body = Expressions.wrap [new Literal body.compile o]
|
|
||||||
body.push new Assign @index, new Literal ivar if index
|
|
||||||
body.push new Assign @name, new Literal nvar or ivar if name
|
|
||||||
else
|
|
||||||
varPart = "#{idt1}#{namePart};\n" if namePart
|
|
||||||
if forPart and name is ivar
|
|
||||||
unstepPart = if @step then "#{name} -= #{ @step.compile(o) };" else "#{name}--;"
|
|
||||||
unstepPart = "\n#{@tab}" + unstepPart
|
|
||||||
if @object
|
if @object
|
||||||
forPart = "#{ivar} in #{sourcePart}"
|
forPart = "#{ivar} in #{sourcePart}"
|
||||||
guardPart = "\n#{idt1}if (!#{utility('hasProp')}.call(#{svar}, #{ivar})) continue;" unless @raw
|
guardPart = not @raw and
|
||||||
body = body.compile merge o, indent: idt1, top: true
|
"#{idt}if (!#{ utility 'hasProp' }.call(#{svar}, #{ivar})) continue;\n"
|
||||||
vars = if @range then name else "#{name}, #{ivar}"
|
else
|
||||||
|
vars += ", #{step}" if step isnt pvar
|
||||||
|
forPart = "#{vars}; #{cond}; " + switch +pvar
|
||||||
|
when 1 then '++' + ivar
|
||||||
|
when -1 then '--' + ivar
|
||||||
|
else ivar + if pvar < 0 then ' -= ' + pvar.slice 1 else ' += ' + pvar
|
||||||
|
unless top
|
||||||
|
rvar = scope.freeVariable 'result'
|
||||||
|
resultDef = "#{@tab}#{rvar} = [];\n"
|
||||||
|
resultRet = @compileReturnValue rvar, o
|
||||||
|
body = Push.wrap rvar, body
|
||||||
|
body = Expressions.wrap [new If @guard, body] if @guard
|
||||||
|
varPart = "#{idt}#{namePart};\n" if namePart
|
||||||
"""
|
"""
|
||||||
#{resultPart}#{@tab}for (#{forPart}) {#{guardPart}
|
#{ resultDef or '' }#{@tab}for (#{forPart}) {
|
||||||
#{varPart}#{body}
|
#{ guardPart or '' }#{varPart}#{ body.compile merge o, indent: idt, top: on }
|
||||||
#{@tab}}#{unstepPart}#{returnResult}
|
#{@tab}}#{ resultRet or '' }
|
||||||
"""
|
"""
|
||||||
|
|
||||||
#### Switch
|
#### Switch
|
||||||
@@ -1703,7 +1598,7 @@ TAB = ' '
|
|||||||
TRAILING_WHITESPACE = /[ \t]+$/gm
|
TRAILING_WHITESPACE = /[ \t]+$/gm
|
||||||
|
|
||||||
IDENTIFIER = /^[$A-Za-z_][$\w]*$/
|
IDENTIFIER = /^[$A-Za-z_][$\w]*$/
|
||||||
NUMBER = /^0x[\da-f]+$|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?$/i
|
NUMBER = /// ^ -? (?: 0x[\da-f]+ | (?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)? ) $ ///i
|
||||||
SIMPLENUM = /^[+-]?\d+$/
|
SIMPLENUM = /^[+-]?\d+$/
|
||||||
|
|
||||||
# Is a literal value a string?
|
# Is a literal value a string?
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ ok obj.func().name is 'Dog'
|
|||||||
class Mini
|
class Mini
|
||||||
num: 10
|
num: 10
|
||||||
generate: =>
|
generate: =>
|
||||||
for i in [1..3]
|
for i from 1 to 3
|
||||||
=>
|
=>
|
||||||
@num
|
@num
|
||||||
|
|
||||||
|
|||||||
@@ -15,37 +15,15 @@ ok odds.join(' ') is "one! three!"
|
|||||||
|
|
||||||
|
|
||||||
# Basic range comprehensions.
|
# Basic range comprehensions.
|
||||||
nums = i * 3 for i in [1..3]
|
nums = i * 3 for i from 1 to 3
|
||||||
|
negs = x for x from -20 to -5*2
|
||||||
negs = x for x in [-20..-5*2]
|
eq nums.concat(negs.slice 0, 3).join(' '), '3 6 9 -20 -19 -18'
|
||||||
negs = negs[0..2]
|
|
||||||
|
|
||||||
result = nums.concat(negs).join(', ')
|
|
||||||
|
|
||||||
ok result is '3, 6, 9, -20, -19, -18'
|
|
||||||
ok i is 3
|
|
||||||
ok x is -10
|
|
||||||
|
|
||||||
|
|
||||||
# With range comprehensions, you can loop in steps.
|
# With range comprehensions, you can loop in steps.
|
||||||
results = x for x in [0...15] by 5
|
eq "#{ x for x from 0 to 9 by 3 }", '0,3,6,9'
|
||||||
ok results.join(' ') is '0 5 10'
|
eq "#{ x for x from 9 to 0 by -3 }", '9,6,3,0'
|
||||||
|
eq "#{ x for x from 3*3 to 0*0 by 0-3 }", '9,6,3,0'
|
||||||
results = x for x in [0..100] by 10
|
|
||||||
ok results.join(' ') is '0 10 20 30 40 50 60 70 80 90 100'
|
|
||||||
|
|
||||||
|
|
||||||
# And can loop downwards, with a negative step.
|
|
||||||
results = x for x in [5..1]
|
|
||||||
|
|
||||||
ok results.join(' ') is '5 4 3 2 1'
|
|
||||||
ok results.join(' ') is [(10-5)..(-2+3)].join(' ')
|
|
||||||
|
|
||||||
results = x for x in [10..1]
|
|
||||||
ok results.join(' ') is [10..1].join(' ')
|
|
||||||
|
|
||||||
results = x for x in [10...0] by -2
|
|
||||||
ok results.join(' ') is [10, 8, 6, 4, 2].join(' ')
|
|
||||||
|
|
||||||
|
|
||||||
# Multiline array comprehension with filter.
|
# Multiline array comprehension with filter.
|
||||||
@@ -53,77 +31,29 @@ evens = for num in [1, 2, 3, 4, 5, 6] when num % 2 is 0
|
|||||||
num *= -1
|
num *= -1
|
||||||
num -= 2
|
num -= 2
|
||||||
num * -1
|
num * -1
|
||||||
|
eq evens + '', '4,6,8'
|
||||||
|
|
||||||
ok evens.join(', ') is '4, 6, 8'
|
|
||||||
|
# Backward traversing.
|
||||||
|
odds = num for num in [0, 1, 2, 3, 4, 5] by -2
|
||||||
|
eq odds + '', '5,3,1'
|
||||||
|
|
||||||
|
|
||||||
# The in operator still works, standalone.
|
# The in operator still works, standalone.
|
||||||
ok 2 of evens
|
ok 2 of evens
|
||||||
|
|
||||||
|
# all/from/to aren't reserved.
|
||||||
# Ensure that the closure wrapper preserves local variables.
|
all = from = to = 1
|
||||||
obj = {}
|
|
||||||
|
|
||||||
for method in ['one', 'two', 'three']
|
|
||||||
obj[method] = ->
|
|
||||||
"I'm " + method
|
|
||||||
|
|
||||||
ok obj.one() is "I'm one"
|
|
||||||
ok obj.two() is "I'm two"
|
|
||||||
ok obj.three() is "I'm three"
|
|
||||||
|
|
||||||
i = 0
|
|
||||||
for i in [1..3]
|
|
||||||
-> 'func'
|
|
||||||
break if false
|
|
||||||
ok i is 3
|
|
||||||
|
|
||||||
|
|
||||||
# Ensure that local variables are closed over for range comprehensions.
|
|
||||||
funcs = for i in [1..3]
|
|
||||||
-> -i
|
|
||||||
|
|
||||||
ok (func() for func in funcs).join(' ') is '-1 -2 -3'
|
|
||||||
ok i is 3
|
|
||||||
|
|
||||||
|
|
||||||
# Ensure that closing over local variables doesn't break scoping laws.
|
|
||||||
for i in [0]
|
|
||||||
count = 0
|
|
||||||
i = 50
|
|
||||||
->
|
|
||||||
ok count is 0
|
|
||||||
ok i is 50
|
|
||||||
|
|
||||||
for [a, b] in [[0, 1]] then ->
|
|
||||||
ok a is 0
|
|
||||||
ok b is 1
|
|
||||||
|
|
||||||
|
|
||||||
# Even when referenced in the filter.
|
|
||||||
list = ['one', 'two', 'three']
|
|
||||||
|
|
||||||
methods = for num, i in list when num isnt 'two' and i isnt 1
|
|
||||||
-> num + ' ' + i
|
|
||||||
|
|
||||||
ok methods.length is 2
|
|
||||||
ok methods[0]() is 'one 0'
|
|
||||||
ok methods[1]() is 'three 2'
|
|
||||||
|
|
||||||
|
|
||||||
# Naked ranges are expanded into arrays.
|
|
||||||
array = [0..10]
|
|
||||||
ok(num % 2 is 0 for num in array by 2)
|
|
||||||
|
|
||||||
|
|
||||||
# Nested comprehensions.
|
# Nested comprehensions.
|
||||||
multiLiner =
|
multiLiner =
|
||||||
for x in [3..5]
|
for x from 3 to 5
|
||||||
for y in [3..5]
|
for y from 3 to 5
|
||||||
[x, y]
|
[x, y]
|
||||||
|
|
||||||
singleLiner =
|
singleLiner =
|
||||||
[x, y] for y in [3..5] for x in [3..5]
|
[x, y] for y from 3 to 5 for x from 3 to 5
|
||||||
|
|
||||||
ok multiLiner.length is singleLiner.length
|
ok multiLiner.length is singleLiner.length
|
||||||
ok 5 is multiLiner[2][2][1]
|
ok 5 is multiLiner[2][2][1]
|
||||||
@@ -159,11 +89,6 @@ ok own.join(' ') is 'Whiskers'
|
|||||||
ok all.sort().join(' ') is 'Whiskers cream tabby'
|
ok all.sort().join(' ') is 'Whiskers cream tabby'
|
||||||
|
|
||||||
|
|
||||||
# Optimized range comprehensions.
|
|
||||||
exxes = 'x' for [0...10]
|
|
||||||
ok exxes.join(' ') is 'x x x x x x x x x x'
|
|
||||||
|
|
||||||
|
|
||||||
# Comprehensions safely redeclare parameters if they're not present in closest
|
# Comprehensions safely redeclare parameters if they're not present in closest
|
||||||
# scope.
|
# scope.
|
||||||
rule = (x) -> x
|
rule = (x) -> x
|
||||||
@@ -173,4 +98,4 @@ learn = ->
|
|||||||
|
|
||||||
ok learn().join(' ') is '1 2 3'
|
ok learn().join(' ') is '1 2 3'
|
||||||
|
|
||||||
ok rule(101) is 101
|
ok rule(101) is 101
|
||||||
|
|||||||
@@ -62,11 +62,6 @@ ok Math.AnonymousAdd(10, 10) is 20
|
|||||||
ok Math.FastAdd(20, 20) is 40
|
ok Math.FastAdd(20, 20) is 40
|
||||||
|
|
||||||
|
|
||||||
# Parens are optional on simple function calls.
|
|
||||||
ok 100 > 1 if 1 > 0
|
|
||||||
ok true unless false
|
|
||||||
ok true for i in [1..3]
|
|
||||||
|
|
||||||
okFunc = (f) -> ok(f())
|
okFunc = (f) -> ok(f())
|
||||||
okFunc -> true
|
okFunc -> true
|
||||||
|
|
||||||
@@ -122,7 +117,7 @@ mult = (x, mids..., y) ->
|
|||||||
|
|
||||||
ok mult(1, 2,) is 2
|
ok mult(1, 2,) is 2
|
||||||
ok mult(1, 2, 3,) is 6
|
ok mult(1, 2, 3,) is 6
|
||||||
ok mult(10,[1..6]...,) is 7200
|
ok mult(10, (i for i from 1 to 6)...) is 7200
|
||||||
|
|
||||||
|
|
||||||
# Test for inline functions with parentheses and implicit calls.
|
# Test for inline functions with parentheses and implicit calls.
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{starts, ends, compact, count, merge, extend, flatten, del, last} = CoffeeScript.helpers
|
{starts, ends, compact, count, merge, extend, flatten, del, last} = CoffeeScript.helpers
|
||||||
|
|
||||||
array = [0..4]
|
array = [0, 1, 2, 3, 4]
|
||||||
string = array.join ''
|
string = array.join ''
|
||||||
object = {}
|
object = {}
|
||||||
|
|
||||||
|
|||||||
@@ -14,10 +14,6 @@ ok value is 1
|
|||||||
value = 0.0 + -.25 - -.75 + 0.0
|
value = 0.0 + -.25 - -.75 + 0.0
|
||||||
ok value is 0.5
|
ok value is 0.5
|
||||||
|
|
||||||
# Decimals don't interfere with ranges.
|
|
||||||
ok [0..10].join(' ') is '0 1 2 3 4 5 6 7 8 9 10'
|
|
||||||
ok [0...10].join(' ') is '0 1 2 3 4 5 6 7 8 9'
|
|
||||||
|
|
||||||
|
|
||||||
# Can call methods directly on numbers.
|
# Can call methods directly on numbers.
|
||||||
4.valueOf() is 4
|
4.valueOf() is 4
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
# Slice.
|
|
||||||
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
||||||
|
|
||||||
a = array[7..9]
|
|
||||||
b = array[2...4]
|
|
||||||
|
|
||||||
result = a.concat(b).join(' ')
|
|
||||||
|
|
||||||
ok result is "7 8 9 2 3"
|
|
||||||
|
|
||||||
a = [0, 1, 2, 3, 4, 5, 6, 7]
|
|
||||||
eq a[2...6].join(' '), '2 3 4 5'
|
|
||||||
|
|
||||||
|
|
||||||
# Ranges.
|
|
||||||
countdown = [10..1].join(' ')
|
|
||||||
ok countdown is "10 9 8 7 6 5 4 3 2 1"
|
|
||||||
|
|
||||||
a = 1
|
|
||||||
b = 5
|
|
||||||
nums = [a...b]
|
|
||||||
ok nums.join(' ') is '1 2 3 4'
|
|
||||||
|
|
||||||
b = -5
|
|
||||||
nums = [a..b]
|
|
||||||
ok nums.join(' ') is '1 0 -1 -2 -3 -4 -5'
|
|
||||||
|
|
||||||
|
|
||||||
# Expression-based range.
|
|
||||||
array = [(1+5)..1+9]
|
|
||||||
ok array.join(' ') is "6 7 8 9 10"
|
|
||||||
|
|
||||||
array = [5..1]
|
|
||||||
ok array.join(' ') is '5 4 3 2 1'
|
|
||||||
|
|
||||||
array = [30...0]
|
|
||||||
ok (len = array.length) is 30
|
|
||||||
ok array[len - 1] is 1
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# String slicing (at least on Node).
|
|
||||||
hello = "Hello World"
|
|
||||||
|
|
||||||
ok hello[1...1] is ""
|
|
||||||
ok hello[1..1] is "e"
|
|
||||||
ok hello[1...5] is "ello"
|
|
||||||
ok hello[0..4] is "Hello"
|
|
||||||
|
|
||||||
|
|
||||||
# Splice literals.
|
|
||||||
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
||||||
|
|
||||||
array[5..10] = [0, 0, 0]
|
|
||||||
|
|
||||||
ok array.join(' ') is '0 1 2 3 4 0 0 0'
|
|
||||||
|
|
||||||
|
|
||||||
# Slices and splices that omit their beginning or end.
|
|
||||||
array = [0..10]
|
|
||||||
|
|
||||||
ok array[7..].join(' ') is '7 8 9 10'
|
|
||||||
ok array[-2..].join(' ') is '9 10'
|
|
||||||
|
|
||||||
ok array[...3].join(' ') is '0 1 2'
|
|
||||||
ok array[..-5].join(' ') is '0 1 2 3 4 5 6'
|
|
||||||
|
|
||||||
array[3..] = [9, 8, 7]
|
|
||||||
|
|
||||||
ok array.join(' ') is '0 1 2 9 8 7'
|
|
||||||
|
|
||||||
array[...3] = [7, 8, 9]
|
|
||||||
|
|
||||||
ok array.join(' ') is '7 8 9 9 8 7'
|
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ ok last is "Usain Bolt"
|
|||||||
ok theField.length is 8
|
ok theField.length is 8
|
||||||
|
|
||||||
contenders.reverse()
|
contenders.reverse()
|
||||||
medalists contenders[0...2]..., "Mighty Mouse", contenders[2...contenders.length]...
|
medalists contenders.slice(0, 2)..., "Mighty Mouse", contenders.slice(2)...
|
||||||
|
|
||||||
ok gold is "Usain Bolt"
|
ok gold is "Usain Bolt"
|
||||||
ok silver is "Asafa Powell"
|
ok silver is "Asafa Powell"
|
||||||
@@ -69,7 +69,7 @@ crowd = [
|
|||||||
|
|
||||||
bests = [
|
bests = [
|
||||||
"Mighty Mouse"
|
"Mighty Mouse"
|
||||||
contenders[0..3]...
|
contenders.slice(0, 4)...
|
||||||
]
|
]
|
||||||
|
|
||||||
ok crowd[0] is contenders[0]
|
ok crowd[0] is contenders[0]
|
||||||
|
|||||||
Reference in New Issue
Block a user