Support for @-prefixed splats.

This commit is contained in:
Timothy Jones
2010-07-28 18:17:50 +12:00
parent c9421cbfcd
commit 9026069f79
3 changed files with 37 additions and 33 deletions

View File

@@ -958,7 +958,7 @@
CodeNode.prototype['class'] = 'CodeNode';
CodeNode.prototype.children = ['params', 'body'];
CodeNode.prototype.compileNode = function(o) {
var _b, _c, _d, _e, _f, _g, _h, _i, _j, code, func, i, name, param, params, sharedScope, splat, top;
var _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, code, func, i, param, params, sharedScope, splat, top, value;
sharedScope = del(o, 'sharedScope');
top = del(o, 'top');
o.scope = sharedScope || new Scope(o.scope, this.body, this);
@@ -973,18 +973,21 @@
param = _b[i];
if (typeof splat !== "undefined" && splat !== null) {
if (param.attach) {
param.assign = new AssignNode(new ValueNode(literal('this'), [new AccessorNode(param.name)]));
param.assign = new AssignNode(new ValueNode(literal('this'), [new AccessorNode(param.value)]));
this.body.expressions.splice(splat.index + 1, 0, param.assign);
}
splat.trailings.push(param);
} else {
if (param.attach) {
name = param.name;
param = literal(o.scope.freeVariable());
this.body.unshift(new AssignNode(new ValueNode(literal('this'), [new AccessorNode(name)]), param));
_d = param;
value = _d.value;
_e = [literal(o.scope.freeVariable()), param.splat];
param = _e[0];
param.splat = _e[1];
this.body.unshift(new AssignNode(new ValueNode(literal('this'), [new AccessorNode(value)]), param));
}
if (param.splat) {
splat = new SplatNode(param.name);
splat = new SplatNode(param.value);
splat.index = i;
splat.trailings = [];
splat.arglength = this.params.length;
@@ -995,17 +998,17 @@
}
}
params = (function() {
_d = []; _f = params;
for (_e = 0, _g = _f.length; _e < _g; _e++) {
param = _f[_e];
_d.push(param.compile(o));
_f = []; _h = params;
for (_g = 0, _i = _h.length; _g < _i; _g++) {
param = _h[_g];
_f.push(param.compile(o));
}
return _d;
return _f;
})();
this.body.makeReturn();
_i = params;
for (_h = 0, _j = _i.length; _h < _j; _h++) {
param = _i[_h];
_k = params;
for (_j = 0, _l = _k.length; _j < _l; _j++) {
param = _k[_j];
(o.scope.parameter(param));
}
code = this.body.expressions.length ? ("\n" + (this.body.compileWithDeclarations(o)) + "\n") : '';
@@ -1039,20 +1042,21 @@
return CodeNode;
})();
exports.ParamNode = (function() {
ParamNode = function(name, attach, splat) {
this.name = literal(name);
this.attach = attach;
this.splat = splat;
ParamNode = function(_b, _c, _d) {
this.splat = _d;
this.attach = _c;
this.name = _b;
this.value = literal(this.name);
return this;
};
__extends(ParamNode, BaseNode);
ParamNode.prototype['class'] = 'ParamNode';
ParamNode.prototype.children = ['name'];
ParamNode.prototype.compileNode = function(o) {
return this.name.compile(o);
return this.value.compile(o);
};
ParamNode.prototype.toString = function(idt) {
return this.type === 'this' ? (literal("@" + name)).toString(idt) : this.name.toString(idt);
return this.attach ? (literal("@" + this.name)).toString(idt) : this.value.toString(idt);
};
return ParamNode;
})();

View File

@@ -876,22 +876,22 @@ exports.CodeNode = class CodeNode extends BaseNode
for param, i in @params
if splat?
if param.attach
param.assign = new AssignNode new ValueNode literal('this'), [new AccessorNode param.name]
param.assign = new AssignNode new ValueNode literal('this'), [new AccessorNode param.value]
@body.expressions.splice splat.index + 1, 0, param.assign
splat.trailings.push param
else
if param.attach
name = param.name
param = literal o.scope.freeVariable()
@body.unshift new AssignNode new ValueNode(literal('this'), [new AccessorNode name]), param
{value} = param
[param, param.splat] = [literal(o.scope.freeVariable()), param.splat]
@body.unshift new AssignNode new ValueNode(literal('this'), [new AccessorNode value]), param
if param.splat
splat = new SplatNode param.name
splat = new SplatNode param.value
splat.index = i
splat.trailings = []
splat.arglength = @params.length
@body.unshift(splat)
else
params.push(param)
params.push param
params = (param.compile(o) for param in params)
@body.makeReturn()
(o.scope.parameter(param)) for param in params
@@ -922,15 +922,12 @@ exports.ParamNode = class ParamNode extends BaseNode
class: 'ParamNode'
children: ['name']
constructor: (name, attach, splat) ->
@name = literal name
@attach = attach
@splat = splat
constructor: (@name, @attach, @splat) -> @value = literal @name
compileNode: (o) -> @name.compile o
compileNode: (o) -> @value.compile o
toString: (idt) ->
if @type is 'this' then (literal "@#name").toString idt else @name.toString idt
if @attach then (literal "@#@name").toString idt else @value.toString idt
#### SplatNode

View File

@@ -29,4 +29,7 @@ ok sumOfArgs(1, 2, 3, 4, 5) is 15
ok context.arg is 1
((splat..., @arg) ->).call context, 1, 2, 3
ok context.arg is 3
ok context.arg is 3
((@arg...) ->).call context, 1, 2, 3
ok context.arg.join ' ' is '1 2 3'