Skip to content

Instantly share code, notes, and snippets.

@gernst
Created May 11, 2022 15:08
Show Gist options
  • Save gernst/d88f474dc4ac293b0c924a3d7ad59d70 to your computer and use it in GitHub Desktop.
Save gernst/d88f474dc4ac293b0c924a3d7ad59d70 to your computer and use it in GitHub Desktop.
class Expr:
def eval(self, state):
pass
class Var(Expr):
def __init__(self, name):
self.name = name
def eval(self, state):
return state[name]
class BinOp(Expr):
def __init__(self, op, left, right):
self.op = op # here: a binary lambda function, but can be anything
self.left = left
self.right = right
def eval(self, state):
return op(left.eval(state), right.eval(state))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment