naringas / craftinginterpreters-plox

Lox from Crafting Interpreters but in Python
0 stars 0 forks source link

0 is not truthy, but should be #3

Open ysangkok opened 1 year ago

ysangkok commented 1 year ago

According to the book, zero should not be truthy, but when I do (0?1:2)==(1?1:2); I get False, implying that zero is special.

The book:

Lox follows Ruby’s simple rule: false and nil are falsey, and everything else is truthy. We implement that like so:

  private boolean isTruthy(Object object) {
    if (object == null) return false;
    if (object instanceof Boolean) return (boolean)object;
    return true;
  }
naringas commented 1 year ago

ah, yea I wanted to discuss this... but I'm waiting until we all get to the interpreter class (chp7) to have this conversation.

there's something... that feels silly or dumb about the interpreter. it is all falling back onto the existing python code:

the interpreter is mostly stuff like this:

            case TokenType.GREATER:
                return l > r

So I hope you see what I mean... feels pointless, I guess this is where being closer to the metal makes a difference, because at some point this definition have to match up with hardware ANDs and ORs (or loops of them); microcode or just specific assembly commands?

But the interesting thing, is how the object comparison, or this boolean comparison, that I implement like this over python changes the semantics of "lox" because I got lazy and relied on the underlying python semantics, but in the case of writing lox over java, some specifics are different.

It’s pretty straightforward. Since Lox was designed to be familiar to someone coming from Java, things like Booleans look the same in both languages. -- from chapter 7.4

plox is not lox ???