I’m working through the Interpreter challenge and ran into a serious issue: one of the test cases seems flat-out wrong.
The code being tested is:
if ("" and "bar")
print("bar")
According to the test, this is supposed to output “bar”, but that doesn’t make any sense. In any sane interpreter, “” is falsy, so the condition (“” and “bar”) should evaluate to “”, and the print should not run. I confirmed this behavior in a real PHP interpreter, and the output is nothing, as expected.
The problem is that your test expects “bar” to be printed, which causes my implementation to fail even though it’s correct. This isn’t just confusing — it’s blocking progress entirely.
Please fix this test case.
@danilovict2 do you have a reference for where the book or Lox language specify that an empty string is falsy? We follow the book and official test cases as far as possible, and our tests pass against the official implementation too.
There are many languages where an empty string is treated as truthy, Ruby is an example.
You’re right—I don’t have a direct reference. It just didn’t make sense to me. I believe this statement is incorrect:
our tests pass against the official implementation too.
From what I understand, the official implementation doesn’t support else-if statements. So I assumed you were deviating from the book and saying an empty string is truthy. My bad!
@danilovict2 The book has quite a few chapters, so it’s easy to overlook something.
Yeah sorry about the empty strings thing, that was my bad. But there still isn’t any mention of else-ifs.
@danilovict2 No worries about the empty strings!
As shown in the grammar, “else” accepts a full statement
, which can itself be another ifStmt
. That’s how else-if chains work in Lox:
Lox Playground: