The Interpreter pattern represents a grammar as a tree of expression objects. Every expression exposes one method: interpret(context), where context is a dict acting as the environment.
Implement the two terminal (leaf) expressions:
These leaves are the foundation the rest of the grammar builds on.
class Var:
def __init__(self, name):
self.name = name
def interpret(self, context):
"""Return the boolean value stored under name, or False if missing."""
# TODO: look up self.name in context, defaulting to False, return a bool
pass
class Const:
def __init__(self, value):
self.value = value
def interpret(self, context):
"""Return the fixed value, ignoring the context."""
# TODO: return self.value
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
The Interpreter pattern represents a grammar as a tree of expression objects. Every expression exposes one method: interpret(context), where context is a dict acting as the environment.
Implement the two terminal (leaf) expressions:
These leaves are the foundation the rest of the grammar builds on.
Press Run Tests or ββ© to check your solution.