munificent / craftinginterpreters

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

In 18.4.2, we attempt negation on VAL_NIL, which the code does not support #991

Open dellis23 opened 3 years ago

dellis23 commented 3 years ago

The last clox expression in the chapter the book instructs us to run is this:

!(5 - 4 > 3 * 2 == !nil)

This fails, however, with the error:

Operand must be a number

At this point in the chapter, I believe we have the following logic for the negate operation:

      case OP_NEGATE:
        if (!IS_NUMBER(peek(0))) {
          runtimeError("Operand must be a number.");
          return INTERPRET_RUNTIME_ERROR;
        }
        push(NUMBER_VAL(-AS_NUMBER(pop())));
        break;

Thus, negating nil is not supported.

heinthanth commented 2 years ago

It's not negating nil. It's "NOT"ing the boolean presentation of nil!