In this post we are adding comparison functions to our interpreter:
class Interpreter:
# .... previous code ....
def Lt(self,xs):
return self.eval(xs[1]) < self.eval(xs[2])
def Gt(self,xs):
return self.eval(xs[1]) > self.eval(xs[2])
def Lte(self,xs):
return not self.Gt(xs)
def Gte(self,xs):
return not self.Lt(xs)
def Eq(self,xs):
return self.eval(xs[1]) == self.eval(xs[2])
def NotEq(self,xs):
return not self.Eq(xs)
code=[
["If",["Lte",3,5],
["Print","3 <= 5"],
["Print","3 > 5"]
],
["If",["NotEq",3,5],
["Print","3 != 5"],
["Print","3 == 5"]
]
]
interpreter=Interpreter()
interpreter.run(code)
Output:
3 <= 5
3 != 5
Top comments (0)