michaelficarra / CoffeeScriptRedux

:sweat: rewrite of the CoffeeScript compiler with proper compiler design principles and a focus on robustness and extensibility
https://michaelficarra.github.com/CoffeeScriptRedux/
BSD 3-Clause "New" or "Revised" License
1.84k stars 110 forks source link

Negative loop step to iterate backwards #324

Open lydell opened 10 years ago

lydell commented 10 years ago
$ echo "for a in b by -1 then c" | bin/coffee --js --no-optimise
// Generated by CoffeeScript 2.0.0-beta9-dev
void function () {
  var a;
  for (var i$ = 0, length$ = b.length; i$ < length$; i$ += -1) {
    a = b[i$];
    c;
  }
}.call(this);
$ echo "for a in b by -1 then c" | coffee -sc
// Generated by CoffeeScript 1.7.1
(function() {
  var a, _i;

  for (_i = b.length - 1; _i >= 0; _i += -1) {
    a = b[_i];
    c;
  }

}).call(this);

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/3447901-negative-loop-step-to-iterate-backwards?utm_campaign=plugin&utm_content=tracker%2F33145&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F33145&utm_medium=issues&utm_source=github).
michaelficarra commented 10 years ago

Fixed by https://github.com/michaelficarra/CoffeeScriptRedux/commit/5272d3c9c3f17f62c23663a5c7c92bf2c87e9c02.

lydell commented 10 years ago

Nope.

~/CoffeeScriptRedux [master|✔] 
17:44 $ git checkout  5272d3c -q
~/CoffeeScriptRedux [:5272d3c|✔] 
17:44 $ echo "for a in b by -1 then c" | bin/coffee --js --no-optimise
// Generated by CoffeeScript 2.0.0-beta9-dev
void function () {
  var a;
  for (var i$ = 0, length$ = b.length; i$ < length$; i$ += -1) {
    a = b[i$];
    c;
  }
}.call(this);

Which is exactly the same output as in my original post.

This example might be clearer:

~/CoffeeScriptRedux [master|✔] 
17:53 $ bin/coffee -e --cli "for i in [1,2,3] by -1 then console.log i.toString 2"
1

(cli):10
    console.log(i.toString(2));
                  ^
TypeError: Cannot call method 'toString' of undefined
  at Object.<anonymous> ((cli):1:43, <js>:10:19)
  at Object.<anonymous> ((cli) <js>:12:3)
  at Module._compile (module.js:456:26)
  at runModule (/home/lydell/CoffeeScriptRedux/lib/run.js:100:17)
  at runMain (/home/lydell/CoffeeScriptRedux/lib/run.js:93:10)
  at processInput (/home/lydell/CoffeeScriptRedux/lib/cli.js:276:7)
  at Object.<anonymous> (/home/lydell/CoffeeScriptRedux/lib/cli.js:297:5)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)

~/CoffeeScriptRedux [master|✔] 
17:53 $ coffee -e "for i in [1,2,3] by -1 then console.log i.toString 2"
11
10
1
vendethiel commented 10 years ago

@lydell and congrats on that collaborator badge :-).

michaelficarra commented 10 years ago

Ah I see, the test is not changed for negative step loops. Okay, I understand now.