munificent / craftinginterpreters

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

Adding number and string should work? #41

Closed tcard closed 7 years ago

tcard commented 7 years ago

According to 3.4.1:

If either operand of + is a string, then the other operand is converted to a string (if it isn’t already one) and the results are concatenated.

But both interpreter currently say:

Operands must be two numbers or two strings.

egordorichev commented 7 years ago

I created a pull-request, that fixes that in clox https://github.com/munificent/craftinginterpreters/pull/42

munificent commented 7 years ago

Oof, you're right! I wrote that chapter quite a while after implementing that behavior in Lox and I'm embarrassed to admit I misremembered how my own language works!

I think the simpler fix is to update the text to describe how Lox actually behaves. In a real language, it's probably more usable to convert either operand to a string, but doing that in Lox would add a decent amount of fairly boring code, so the simplest thing is to just leave it out and leave "+" a little less flexible.

I'll fix the prose.

munificent commented 7 years ago

Fixed by https://github.com/munificent/craftinginterpreters/commit/0ba8c367f4179609201b7fbec44fc1498ade6fd1.