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

Merge pull request #1666 from geraldalewis/1643_splatted_access

#1643 splatted access
This commit is contained in:
Jeremy Ashkenas 2011-09-09 16:17:24 -07:00
commit a0d4242da4
3 changed files with 65 additions and 32 deletions

View file

@ -1044,6 +1044,7 @@
this.value = value;
this.context = context;
this.param = options && options.param;
this.subpattern = options && options.subpattern;
}
Assign.prototype.children = ['variable', 'value'];
Assign.prototype.isStatement = function(o) {
@ -1056,7 +1057,7 @@
return unfoldSoak(o, this, 'variable');
};
Assign.prototype.compileNode = function(o) {
var isValue, match, name, val, _ref2, _ref3, _ref4, _ref5;
var isValue, match, name, val, varBase, _ref2, _ref3, _ref4, _ref5;
if (isValue = this.variable instanceof Value) {
if (this.variable.isArray() || this.variable.isObject()) {
return this.compilePatternMatch(o);
@ -1067,14 +1068,16 @@
}
}
name = this.variable.compile(o, LEVEL_LIST);
if (!(this.context || this.variable.isAssignable())) {
throw SyntaxError("\"" + (this.variable.compile(o)) + "\" cannot be assigned.");
}
if (!(this.context || isValue && (this.variable.namespaced || this.variable.hasProperties()))) {
if (this.param) {
o.scope.add(name, 'var');
} else {
o.scope.find(name);
if (!this.context) {
if (!(varBase = this.variable.unwrapAll()).isAssignable()) {
throw SyntaxError("\"" + (this.variable.compile(o)) + "\" cannot be assigned.");
}
if (!(typeof varBase.hasProperties === "function" ? varBase.hasProperties() : void 0)) {
if (this.param) {
o.scope.add(name, 'var');
} else {
o.scope.find(name);
}
}
}
if (this.value instanceof Code && (match = METHOD_DEF.exec(name))) {
@ -1091,7 +1094,7 @@
}
};
Assign.prototype.compilePatternMatch = function(o) {
var acc, assigns, code, i, idx, isObject, ivar, name, obj, objects, olen, ref, rest, splat, top, val, value, vvar, _len, _ref2, _ref3, _ref4, _ref5, _ref6;
var acc, assigns, code, i, idx, isObject, ivar, name, obj, objects, olen, ref, rest, splat, top, val, value, vvar, _len, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8;
top = o.level === LEVEL_TOP;
value = this.value;
objects = this.variable.base.objects;
@ -1106,10 +1109,10 @@
isObject = this.variable.isObject();
if (top && olen === 1 && !((obj = objects[0]) instanceof Splat)) {
if (obj instanceof Assign) {
_ref2 = obj, idx = _ref2.variable.base, obj = _ref2.value;
_ref2 = obj, (_ref3 = _ref2.variable, idx = _ref3.base), obj = _ref2.value;
} else {
if (obj.base instanceof Parens) {
_ref3 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref3[0], idx = _ref3[1];
_ref4 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref4[0], idx = _ref4[1];
} else {
idx = isObject ? obj["this"] ? obj.properties[0].name : obj : new Literal(0);
}
@ -1117,7 +1120,7 @@
acc = IDENTIFIER.test(idx.unwrap().value || 0);
value = new Value(value);
value.properties.push(new (acc ? Access : Index)(idx));
if (_ref4 = obj.unwrap().value, __indexOf.call(['arguments', 'eval'].concat(RESERVED), _ref4) >= 0) {
if (_ref5 = obj.unwrap().value, __indexOf.call(['arguments', 'eval'].concat(RESERVED), _ref5) >= 0) {
throw new SyntaxError("assignment to a reserved word: " + (obj.compile(o)) + " = " + (value.compile(o)));
}
return new Assign(obj, value, null, {
@ -1136,20 +1139,18 @@
idx = i;
if (isObject) {
if (obj instanceof Assign) {
_ref5 = obj, idx = _ref5.variable.base, obj = _ref5.value;
_ref6 = obj, (_ref7 = _ref6.variable, idx = _ref7.base), obj = _ref6.value;
} else {
if (obj.base instanceof Parens) {
_ref6 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref6[0], idx = _ref6[1];
_ref8 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref8[0], idx = _ref8[1];
} else {
idx = obj["this"] ? obj.properties[0].name : obj;
}
}
}
if (!splat && obj instanceof Splat) {
if (!obj.name.unwrapAll().isAssignable()) {
throw SyntaxError("\"" + (obj.name.compile(o)) + "\" cannot be assigned.");
}
name = obj.name.unwrap().value;
obj = obj.unwrap();
val = "" + olen + " <= " + vvar + ".length ? " + (utility('slice')) + ".call(" + vvar + ", " + i;
if (rest = olen - i - 1) {
ivar = o.scope.freeVariable('i');
@ -1177,10 +1178,11 @@
throw new SyntaxError("assignment to a reserved word: " + (obj.compile(o)) + " = " + (val.compile(o)));
}
assigns.push(new Assign(obj, val, null, {
param: this.param
}).compile(o, LEVEL_TOP));
param: this.param,
subpattern: true
}).compile(o, LEVEL_LIST));
}
if (!top) assigns.push(vvar);
if (!(top || this.subpattern)) assigns.push(vvar);
code = assigns.join(', ');
if (o.level < LEVEL_LIST) {
return code;
@ -1365,6 +1367,9 @@
return this.name.compile(o);
}
};
Splat.prototype.unwrap = function() {
return this.name;
};
Splat.compileSplattedArray = function(o, list, apply) {
var args, base, code, i, index, node, _len;
index = -1;