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

drying up compileSplice

This commit is contained in:
Jeremy Ashkenas 2010-12-21 00:46:31 -05:00
parent d42f7daef7
commit f7d19f5a3a
2 changed files with 22 additions and 31 deletions

View file

@ -1179,30 +1179,24 @@
return new Op(this.context.slice(0, -1), left, new Assign(rite, this.value, '=')).compile(o);
};
Assign.prototype.compileSplice = function(o) {
var excl, fromDecl, fromRef, name, range, to, val, _ref, _ref2;
range = this.variable.properties.pop().range;
var exclusive, from, fromDecl, fromRef, name, to, val, _ref, _ref2;
_ref = this.variable.properties.pop().range, from = _ref.from, to = _ref.to, exclusive = _ref.exclusive;
name = this.variable.compile(o);
excl = range.exclusive;
if (!range.to) {
to = "" + name + ".length";
}
if (range.from) {
_ref = range.from.cache(o, LEVEL_OP), fromDecl = _ref[0], fromRef = _ref[1];
} else {
_ref2 = ['0', '0'], fromDecl = _ref2[0], fromRef = _ref2[1];
}
if (!to) {
if (range.from && range.from.isSimpleNumber() && range.to.isSimpleNumber()) {
to = +range.to.compile(o) - +fromRef;
if (!excl) {
_ref2 = from ? from.cache(o, LEVEL_OP) : ['0', '0'], fromDecl = _ref2[0], fromRef = _ref2[1];
if (to) {
if (from && from.isSimpleNumber() && to.isSimpleNumber()) {
to = +to.compile(o) - +fromRef;
if (!exclusive) {
to += 1;
}
} else {
to = range.to.compile(o) + ' - ' + fromRef;
if (!excl) {
to = to.compile(o) + ' - ' + fromRef;
if (!exclusive) {
to += ' + 1';
}
}
} else {
to = "" + name + ".length";
}
val = this.value.compile(o);
return "[].splice.apply(" + name + ", [" + fromDecl + ", " + to + "].concat(" + val + "))";

View file

@ -966,21 +966,18 @@ exports.Assign = class Assign extends Base
# Compile the assignment from an array splice literal, using JavaScript's
# `Array#splice` method.
compileSplice: (o) ->
{range} = @variable.properties.pop()
{range: {from, to, exclusive}} = @variable.properties.pop()
name = @variable.compile o
excl = range.exclusive
to = "#{name}.length" unless range.to
if range.from
[fromDecl, fromRef] = range.from.cache o, LEVEL_OP
[fromDecl, fromRef] = if from then from.cache(o, LEVEL_OP) else ['0', '0']
if to
if from and from.isSimpleNumber() and to.isSimpleNumber()
to = +to.compile(o) - +fromRef
to += 1 unless exclusive
else
[fromDecl, fromRef] = ['0', '0']
unless to
if range.from and range.from.isSimpleNumber() and range.to.isSimpleNumber()
to = +range.to.compile(o) - +fromRef
to += 1 unless excl
to = to.compile(o) + ' - ' + fromRef
to += ' + 1' unless exclusive
else
to = range.to.compile(o) + ' - ' + fromRef
to += ' + 1' unless excl
to = "#{name}.length"
val = @value.compile(o)
"[].splice.apply(#{name}, [#{fromDecl}, #{to}].concat(#{val}))"