Negation

Step 3 - Negation

Add the unary Not expression.

  • Not(expr).interpret(context) returns the logical negation of the child result.

With And, Or and Not you now have a complete boolean algebra over the context.

Hints
Starter code
class Var:
    def __init__(self, name):
        self.name = name

    def interpret(self, context):
        return bool(context.get(self.name, False))


class Const:
    def __init__(self, value):
        self.value = value

    def interpret(self, context):
        return self.value


class And:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def interpret(self, context):
        return self.a.interpret(context) and self.b.interpret(context)


class Or:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def interpret(self, context):
        return self.a.interpret(context) or self.b.interpret(context)


class Not:
    def __init__(self, expr):
        self.expr = expr

    def interpret(self, context):
        """Return the logical negation of the child result."""
        # TODO: return not self.expr.interpret(context)
        pass
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.