munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.75k stars 1.03k forks source link

Fix LoxArray Length return type #975

Closed ashwinath closed 3 years ago

ashwinath commented 3 years ago

Thanks for the wonderful book, still in the middle of going through it. I found a small bug in the implementation of the LoxArray length returns. This is due to Java's array.length returning an integer rather than a double. The checkNumberOperand checks for Double only. To fix this, we cast the primitive into a double.

To reproduce:

var a = Array(5);
for (var i = 0; i < a.length; i = i+1) {
  print i;
}

Will return Operands must be a number. as it errors out here: https://github.com/munificent/craftinginterpreters/blob/master/java/com/craftinginterpreters/lox/Interpreter.java#L495

munificent commented 3 years ago

Thank you!