mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
made unless
less parenfull
This commit is contained in:
parent
c7157ca90c
commit
9447796d8e
7 changed files with 56 additions and 49 deletions
|
@ -12,7 +12,7 @@
|
||||||
helpers.extend(global, {
|
helpers.extend(global, {
|
||||||
task: function(name, description, action) {
|
task: function(name, description, action) {
|
||||||
var _ref;
|
var _ref;
|
||||||
if (!(action)) {
|
if (!action) {
|
||||||
_ref = [description, action], action = _ref[0], description = _ref[1];
|
_ref = [description, action], action = _ref[0], description = _ref[1];
|
||||||
}
|
}
|
||||||
return (tasks[name] = {
|
return (tasks[name] = {
|
||||||
|
@ -25,7 +25,7 @@
|
||||||
return switches.push([letter, flag, description]);
|
return switches.push([letter, flag, description]);
|
||||||
},
|
},
|
||||||
invoke: function(name) {
|
invoke: function(name) {
|
||||||
if (!(tasks[name])) {
|
if (!tasks[name]) {
|
||||||
missingTask(name);
|
missingTask(name);
|
||||||
}
|
}
|
||||||
return tasks[name].action(options);
|
return tasks[name].action(options);
|
||||||
|
@ -34,7 +34,7 @@
|
||||||
exports.run = function() {
|
exports.run = function() {
|
||||||
return path.exists('Cakefile', function(exists) {
|
return path.exists('Cakefile', function(exists) {
|
||||||
var _i, _len, _ref, _result, arg, args;
|
var _i, _len, _ref, _result, arg, args;
|
||||||
if (!(exists)) {
|
if (!exists) {
|
||||||
throw new Error("Cakefile not found in " + (process.cwd()));
|
throw new Error("Cakefile not found in " + (process.cwd()));
|
||||||
}
|
}
|
||||||
args = process.argv.slice(2, process.argv.length);
|
args = process.argv.slice(2, process.argv.length);
|
||||||
|
@ -42,7 +42,7 @@
|
||||||
fileName: 'Cakefile'
|
fileName: 'Cakefile'
|
||||||
});
|
});
|
||||||
oparse = new optparse.OptionParser(switches);
|
oparse = new optparse.OptionParser(switches);
|
||||||
if (!(args.length)) {
|
if (!args.length) {
|
||||||
return printTasks();
|
return printTasks();
|
||||||
}
|
}
|
||||||
options = oparse.parse(args);
|
options = oparse.parse(args);
|
||||||
|
|
|
@ -32,7 +32,7 @@
|
||||||
if (opts.eval) {
|
if (opts.eval) {
|
||||||
return compileScript('console', sources[0]);
|
return compileScript('console', sources[0]);
|
||||||
}
|
}
|
||||||
if (!(sources.length)) {
|
if (!sources.length) {
|
||||||
return require('./repl');
|
return require('./repl');
|
||||||
}
|
}
|
||||||
separator = sources.indexOf('--');
|
separator = sources.indexOf('--');
|
||||||
|
@ -59,7 +59,7 @@
|
||||||
base = source;
|
base = source;
|
||||||
compile = function(source, topLevel) {
|
compile = function(source, topLevel) {
|
||||||
return path.exists(source, function(exists) {
|
return path.exists(source, function(exists) {
|
||||||
if (!(exists)) {
|
if (!exists) {
|
||||||
throw new Error("File not found: " + source);
|
throw new Error("File not found: " + source);
|
||||||
}
|
}
|
||||||
return fs.stat(source, function(err, stats) {
|
return fs.stat(source, function(err, stats) {
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
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) {
|
||||||
var match;
|
var match;
|
||||||
if (!(action)) {
|
if (!action) {
|
||||||
return [patternString, '$$ = $1;', options];
|
return [patternString, '$$ = $1;', options];
|
||||||
}
|
}
|
||||||
action = (match = (action + '').match(unwrap)) ? match[1] : ("(" + action + "())");
|
action = (match = (action + '').match(unwrap)) ? match[1] : ("(" + action + "())");
|
||||||
|
@ -614,7 +614,7 @@
|
||||||
_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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
lib/lexer.js
10
lib/lexer.js
|
@ -71,7 +71,7 @@
|
||||||
this.identifierError(id);
|
this.identifierError(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!(forcedIdentifier)) {
|
if (!forcedIdentifier) {
|
||||||
if (COFFEE_ALIASES.hasOwnProperty(id)) {
|
if (COFFEE_ALIASES.hasOwnProperty(id)) {
|
||||||
tag = (id = COFFEE_ALIASES[id]);
|
tag = (id = COFFEE_ALIASES[id]);
|
||||||
}
|
}
|
||||||
|
@ -398,7 +398,7 @@
|
||||||
if (herecomment && 0 > doc.indexOf('\n')) {
|
if (herecomment && 0 > doc.indexOf('\n')) {
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
if (!(herecomment)) {
|
if (!herecomment) {
|
||||||
while (match = HEREDOC_INDENT.exec(doc)) {
|
while (match = HEREDOC_INDENT.exec(doc)) {
|
||||||
attempt = match[1];
|
attempt = match[1];
|
||||||
if (indent === null || (0 < (_ref3 = attempt.length)) && (_ref3 < indent.length)) {
|
if (indent === null || (0 < (_ref3 = attempt.length)) && (_ref3 < indent.length)) {
|
||||||
|
@ -409,7 +409,7 @@
|
||||||
if (indent) {
|
if (indent) {
|
||||||
doc = doc.replace(RegExp("\\n" + indent, "g"), '\n');
|
doc = doc.replace(RegExp("\\n" + indent, "g"), '\n');
|
||||||
}
|
}
|
||||||
if (!(herecomment)) {
|
if (!herecomment) {
|
||||||
doc = doc.replace(/^\n/, '');
|
doc = doc.replace(/^\n/, '');
|
||||||
}
|
}
|
||||||
return doc;
|
return doc;
|
||||||
|
@ -463,7 +463,7 @@
|
||||||
if (levels.length && starts(str, close, i) && last(levels) === pair) {
|
if (levels.length && starts(str, close, i) && last(levels) === pair) {
|
||||||
levels.pop();
|
levels.pop();
|
||||||
i += close.length - 1;
|
i += close.length - 1;
|
||||||
if (!(levels.length)) {
|
if (!levels.length) {
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -524,7 +524,7 @@
|
||||||
if (regex) {
|
if (regex) {
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
if (!(tokens.length)) {
|
if (!tokens.length) {
|
||||||
return this.token('STRING', '""');
|
return this.token('STRING', '""');
|
||||||
}
|
}
|
||||||
if (interpolated = tokens.length > 1) {
|
if (interpolated = tokens.length > 1) {
|
||||||
|
|
57
lib/nodes.js
57
lib/nodes.js
|
@ -43,7 +43,7 @@
|
||||||
Base.prototype.compileReference = function(o, options) {
|
Base.prototype.compileReference = function(o, options) {
|
||||||
var _len, compiled, i, node, pair, reference;
|
var _len, compiled, i, node, pair, reference;
|
||||||
pair = (function() {
|
pair = (function() {
|
||||||
if (!(this.isComplex())) {
|
if (!this.isComplex()) {
|
||||||
return [this, this];
|
return [this, this];
|
||||||
} else {
|
} else {
|
||||||
reference = new Literal(o.scope.freeVariable('ref'));
|
reference = new Literal(o.scope.freeVariable('ref'));
|
||||||
|
@ -111,7 +111,7 @@
|
||||||
};
|
};
|
||||||
Base.prototype.eachChild = function(func) {
|
Base.prototype.eachChild = function(func) {
|
||||||
var _i, _j, _len, _len2, _ref2, _ref3, _result, attr, child;
|
var _i, _j, _len, _len2, _ref2, _ref3, _result, attr, child;
|
||||||
if (!(this.children)) {
|
if (!this.children) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_result = [];
|
_result = [];
|
||||||
|
@ -362,7 +362,7 @@
|
||||||
bref = new Literal(o.scope.freeVariable('base'));
|
bref = new Literal(o.scope.freeVariable('base'));
|
||||||
base = new Value(new Parens(new Assign(bref, base)));
|
base = new Value(new Parens(new Assign(bref, base)));
|
||||||
}
|
}
|
||||||
if (!(name)) {
|
if (!name) {
|
||||||
return [base, bref];
|
return [base, bref];
|
||||||
}
|
}
|
||||||
if (name.isComplex()) {
|
if (name.isComplex()) {
|
||||||
|
@ -428,7 +428,7 @@
|
||||||
} else if (node instanceof Value) {
|
} else if (node instanceof Value) {
|
||||||
ifnode = node.unfoldSoak(o);
|
ifnode = node.unfoldSoak(o);
|
||||||
}
|
}
|
||||||
if (!(ifnode)) {
|
if (!ifnode) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
parent[name] = ifnode.body;
|
parent[name] = ifnode.body;
|
||||||
|
@ -481,11 +481,11 @@
|
||||||
Call.prototype.superReference = function(o) {
|
Call.prototype.superReference = function(o) {
|
||||||
var method, name;
|
var method, name;
|
||||||
method = o.scope.method;
|
method = o.scope.method;
|
||||||
if (!(method)) {
|
if (!method) {
|
||||||
throw Error("cannot call super outside of a function");
|
throw Error("cannot call super outside of a function");
|
||||||
}
|
}
|
||||||
name = method.name;
|
name = method.name;
|
||||||
if (!(name)) {
|
if (!name) {
|
||||||
throw Error("cannot call super on an anonymous function.");
|
throw Error("cannot call super on an anonymous function.");
|
||||||
}
|
}
|
||||||
return method.klass ? ("" + (method.klass) + ".__super__." + name) : ("" + name + ".__super__.constructor");
|
return method.klass ? ("" + (method.klass) + ".__super__." + name) : ("" + name + ".__super__.constructor");
|
||||||
|
@ -567,7 +567,7 @@
|
||||||
if (this.isSuper) {
|
if (this.isSuper) {
|
||||||
return ("" + (this.superReference(o)) + ".apply(this, " + splatargs + ")");
|
return ("" + (this.superReference(o)) + ".apply(this, " + splatargs + ")");
|
||||||
}
|
}
|
||||||
if (!(this.isNew)) {
|
if (!this.isNew) {
|
||||||
if (!((base = this.variable) instanceof Value)) {
|
if (!((base = this.variable) instanceof Value)) {
|
||||||
base = new Value(base);
|
base = new Value(base);
|
||||||
}
|
}
|
||||||
|
@ -694,7 +694,7 @@
|
||||||
Range.prototype.compileNode = function(o) {
|
Range.prototype.compileNode = function(o) {
|
||||||
var compare, idx, incr, intro, step, stepPart, vars;
|
var compare, idx, incr, intro, step, stepPart, vars;
|
||||||
this.compileVariables(o);
|
this.compileVariables(o);
|
||||||
if (!(o.index)) {
|
if (!o.index) {
|
||||||
return this.compileArray(o);
|
return this.compileArray(o);
|
||||||
}
|
}
|
||||||
if (this.fromNum && this.toNum) {
|
if (this.fromNum && this.toNum) {
|
||||||
|
@ -1059,7 +1059,7 @@
|
||||||
}
|
}
|
||||||
assigns.push(new Assign(obj, val).compile(otop));
|
assigns.push(new Assign(obj, val).compile(otop));
|
||||||
}
|
}
|
||||||
if (!(top)) {
|
if (!top) {
|
||||||
assigns.push(valVar);
|
assigns.push(valVar);
|
||||||
}
|
}
|
||||||
code = assigns.join(', ');
|
code = assigns.join(', ');
|
||||||
|
@ -1141,7 +1141,7 @@
|
||||||
}
|
}
|
||||||
return _result;
|
return _result;
|
||||||
})();
|
})();
|
||||||
if (!(empty)) {
|
if (!empty) {
|
||||||
this.body.makeReturn();
|
this.body.makeReturn();
|
||||||
}
|
}
|
||||||
for (_i = 0, _len2 = params.length; _i < _len2; _i++) {
|
for (_i = 0, _len2 = params.length; _i < _len2; _i++) {
|
||||||
|
@ -1200,7 +1200,7 @@
|
||||||
Splat = (function() {
|
Splat = (function() {
|
||||||
return function Splat(name) {
|
return function Splat(name) {
|
||||||
Splat.__super__.constructor.call(this);
|
Splat.__super__.constructor.call(this);
|
||||||
if (!(name.compile)) {
|
if (!name.compile) {
|
||||||
name = new Literal(name);
|
name = new Literal(name);
|
||||||
}
|
}
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -1301,7 +1301,7 @@
|
||||||
this.condition.parenthetical = true;
|
this.condition.parenthetical = true;
|
||||||
cond = this.condition.compile(o);
|
cond = this.condition.compile(o);
|
||||||
set = '';
|
set = '';
|
||||||
if (!(top)) {
|
if (!top) {
|
||||||
rvar = o.scope.freeVariable('result');
|
rvar = o.scope.freeVariable('result');
|
||||||
set = ("" + (this.tab) + rvar + " = [];\n");
|
set = ("" + (this.tab) + rvar + " = [];\n");
|
||||||
if (this.body) {
|
if (this.body) {
|
||||||
|
@ -1363,7 +1363,7 @@
|
||||||
};
|
};
|
||||||
Op.prototype.isInvertible = function() {
|
Op.prototype.isInvertible = function() {
|
||||||
var _ref2;
|
var _ref2;
|
||||||
return (('===' === (_ref2 = this.operator) || '!==' === _ref2)) && !(this.first instanceof Op) && !(this.second instanceof Op);
|
return ('===' === (_ref2 = this.operator) || '!==' === _ref2);
|
||||||
};
|
};
|
||||||
Op.prototype.isComplex = function() {
|
Op.prototype.isComplex = function() {
|
||||||
return this.operator !== '!' || this.first.isComplex();
|
return this.operator !== '!' || this.first.isComplex();
|
||||||
|
@ -1650,7 +1650,7 @@
|
||||||
immediate: true
|
immediate: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (!(topLevel)) {
|
if (!topLevel) {
|
||||||
rvar = scope.freeVariable('result');
|
rvar = scope.freeVariable('result');
|
||||||
}
|
}
|
||||||
ivar = range ? name : index;
|
ivar = range ? name : index;
|
||||||
|
@ -1672,7 +1672,7 @@
|
||||||
immediate: true
|
immediate: true
|
||||||
}))) {
|
}))) {
|
||||||
sourcePart = ("" + (ref = scope.freeVariable('ref')) + " = " + svar);
|
sourcePart = ("" + (ref = scope.freeVariable('ref')) + " = " + svar);
|
||||||
if (!(this.object)) {
|
if (!this.object) {
|
||||||
sourcePart = ("(" + sourcePart + ")");
|
sourcePart = ("(" + sourcePart + ")");
|
||||||
}
|
}
|
||||||
svar = ref;
|
svar = ref;
|
||||||
|
@ -1680,7 +1680,7 @@
|
||||||
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');
|
lvar = scope.freeVariable('len');
|
||||||
stepPart = this.step ? ("" + ivar + " += " + (this.step.compile(o))) : ("" + ivar + "++");
|
stepPart = this.step ? ("" + ivar + " += " + (this.step.compile(o))) : ("" + ivar + "++");
|
||||||
forPart = ("" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length; " + ivar + " < " + lvar + "; " + stepPart);
|
forPart = ("" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length; " + ivar + " < " + lvar + "; " + stepPart);
|
||||||
|
@ -1688,7 +1688,7 @@
|
||||||
}
|
}
|
||||||
resultPart = rvar ? ("" + (this.tab) + rvar + " = [];\n") : '';
|
resultPart = rvar ? ("" + (this.tab) + rvar + " = [];\n") : '';
|
||||||
returnResult = this.compileReturnValue(rvar, o);
|
returnResult = this.compileReturnValue(rvar, o);
|
||||||
if (!(topLevel)) {
|
if (!topLevel) {
|
||||||
body = Push.wrap(rvar, body);
|
body = Push.wrap(rvar, body);
|
||||||
}
|
}
|
||||||
if (this.guard) {
|
if (this.guard) {
|
||||||
|
@ -1712,7 +1712,7 @@
|
||||||
}
|
}
|
||||||
if (this.object) {
|
if (this.object) {
|
||||||
forPart = ("" + ivar + " in " + sourcePart);
|
forPart = ("" + ivar + " in " + sourcePart);
|
||||||
if (!(this.raw)) {
|
if (!this.raw) {
|
||||||
guardPart = ("\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;");
|
guardPart = ("\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1782,18 +1782,23 @@
|
||||||
})();
|
})();
|
||||||
exports.If = (function() {
|
exports.If = (function() {
|
||||||
If = (function() {
|
If = (function() {
|
||||||
return function If(_arg, _arg2, _arg3) {
|
return function If(condition, _arg, _arg2) {
|
||||||
this.tags = _arg3;
|
var op;
|
||||||
this.body = _arg2;
|
this.tags = _arg2;
|
||||||
this.condition = _arg;
|
this.body = _arg;
|
||||||
this.tags || (this.tags = {});
|
this.tags || (this.tags = {});
|
||||||
if (this.tags.invert) {
|
if (this.tags.invert) {
|
||||||
if (this.condition instanceof Op && this.condition.isInvertible()) {
|
op = condition instanceof Op;
|
||||||
this.condition.invert();
|
if (op && condition.isInvertible()) {
|
||||||
|
condition.invert();
|
||||||
} else {
|
} else {
|
||||||
this.condition = new Op('!', new Parens(this.condition));
|
if (op && !condition.isUnary()) {
|
||||||
|
condition = new Parens(condition);
|
||||||
|
}
|
||||||
|
condition = new Op('!', condition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.condition = condition;
|
||||||
this.elseBody = null;
|
this.elseBody = null;
|
||||||
this.isChain = false;
|
this.isChain = false;
|
||||||
return this;
|
return this;
|
||||||
|
@ -1863,7 +1868,7 @@
|
||||||
comDent = child ? this.idt() : '';
|
comDent = child ? this.idt() : '';
|
||||||
body = this.body.compile(o);
|
body = this.body.compile(o);
|
||||||
ifPart = ("" + ifDent + "if (" + (this.compileCondition(condO)) + ") {\n" + body + "\n" + (this.tab) + "}");
|
ifPart = ("" + ifDent + "if (" + (this.compileCondition(condO)) + ") {\n" + body + "\n" + (this.tab) + "}");
|
||||||
if (!(this.elseBody)) {
|
if (!this.elseBody) {
|
||||||
return ifPart;
|
return ifPart;
|
||||||
}
|
}
|
||||||
elsePart = this.isChain ? ' else ' + this.elseBodyNode().compile(merge(o, {
|
elsePart = this.isChain ? ' else ' + this.elseBodyNode().compile(merge(o, {
|
||||||
|
|
|
@ -265,7 +265,7 @@
|
||||||
};
|
};
|
||||||
return this.scanTokens(function(token, i) {
|
return this.scanTokens(function(token, i) {
|
||||||
var _ref, original;
|
var _ref, original;
|
||||||
if (!(('IF' === (_ref = token[0]) || 'UNLESS' === _ref))) {
|
if (!('IF' === (_ref = token[0]) || 'UNLESS' === _ref)) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
original = token;
|
original = token;
|
||||||
|
@ -326,7 +326,7 @@
|
||||||
stack.push(token);
|
stack.push(token);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (!(include(EXPRESSION_END, tag))) {
|
if (!include(EXPRESSION_END, tag)) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (debt[(inv = INVERSES[tag])] > 0) {
|
if (debt[(inv = INVERSES[tag])] > 0) {
|
||||||
|
|
|
@ -1184,8 +1184,7 @@ exports.Op = class Op extends Base
|
||||||
not @second
|
not @second
|
||||||
|
|
||||||
isInvertible: ->
|
isInvertible: ->
|
||||||
(@operator in ['===', '!==']) and
|
@operator in ['===', '!==']
|
||||||
not (@first instanceof Op) and not (@second instanceof Op)
|
|
||||||
|
|
||||||
isComplex: -> @operator isnt '!' or @first.isComplex()
|
isComplex: -> @operator isnt '!' or @first.isComplex()
|
||||||
|
|
||||||
|
@ -1516,15 +1515,18 @@ exports.If = class If extends Base
|
||||||
|
|
||||||
topSensitive: YES
|
topSensitive: YES
|
||||||
|
|
||||||
constructor: (@condition, @body, @tags) ->
|
constructor: (condition, @body, @tags) ->
|
||||||
@tags or= {}
|
@tags or= {}
|
||||||
if @tags.invert
|
if @tags.invert
|
||||||
if @condition instanceof Op and @condition.isInvertible()
|
op = condition instanceof Op
|
||||||
@condition.invert()
|
if op and condition.isInvertible()
|
||||||
|
condition.invert()
|
||||||
else
|
else
|
||||||
@condition = new Op '!', new Parens @condition
|
condition = new Parens condition if op and not condition.isUnary()
|
||||||
@elseBody = null
|
condition = new Op '!', condition
|
||||||
@isChain = false
|
@condition = condition
|
||||||
|
@elseBody = null
|
||||||
|
@isChain = false
|
||||||
|
|
||||||
bodyNode: -> @body?.unwrap()
|
bodyNode: -> @body?.unwrap()
|
||||||
elseBodyNode: -> @elseBody?.unwrap()
|
elseBodyNode: -> @elseBody?.unwrap()
|
||||||
|
|
Loading…
Add table
Reference in a new issue