Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better (or at least correct) compilations for conditional assignments #733

Closed
satyr opened this issue Sep 29, 2010 · 1 comment
Closed

Comments

@satyr
Copy link
Collaborator

satyr commented Sep 29, 2010

$ coffee --no-wrap -pe 'x[++i][++i][++i] ||= 1'
var _ref, _ref2, _ref3;
x[(_ref = ++i)][(_ref2 = ++i)][(_ref3 = ++i)] || (x[_ref][_ref2][_ref3] = 1);

This shouldn't need more than two temporary variables. The better way is:
var _ref, _ref2;
(_ref = x[++i][++i])[_ref2 = ++i] || (_ref[_ref2] = 1);
Also this:
$ coffee --no-wrap -pe 'x[++i] ?= 1'
var _ref;
x[(_ref = ++i)] = (typeof x[_ref] !== "undefined" && x[_ref] !== null) ? x[_ref] : 1;
should be:
var _ref, _ref2;
(_ref = x[_ref2 = ++i]) == null ? x[_ref2] = 1 : _ref;
And this:
$ coffee --no-wrap -pe 'a.b?.c||=1'
(a.b == null ? undefined : a.b.c) || (a.b == null ? undefined : a.b.c = 1);
should be:
(_ref = a.b) == null ? undefined : _ref.c || (_ref.c = 1)
Then this (which isn't even correct):
$ coffee --no-wrap -pe 'a.b?.c?()'
var _ref, _ref2, _ref3;
(typeof (typeof (_ref3 = ((_ref2 = (_ref = a.b)))) === "undefined" || _ref3 === null) ? undefined : _ref3.c === "function" ? _ref == null ? undefined : _ref.c() : undefined);
should be:
var _ref;
(_ref = a.b) == null ? undefined : typeof _ref.c !== "function" ? undefined : _ref.c()
(hmm, aren't they horrible...)

Patch to come.

@satyr
Copy link
Collaborator Author

satyr commented Oct 1, 2010

http://github.com/satyr/coffee-script/tree/733

ValueNode with soak now delegates its compilation to IfNode by turning a?.b into if a? then a.b.
Similarly, a ?= b now delegates to a ? a = b.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant