Exemple #1
0
 def test_if_false(self):
     st = If(Variable("x"), Assign("y", Number(1)), DoNothing())
     en = {"x": Boolean(False)}
     while st.reducible:
         st, en = st.reduce(en)
     self.assertEqual(st, DoNothing())
     self.assertEqual(en["x"], Boolean(False))
Exemple #2
0
 def evaluate(self, environment):
     """Optimize tail recursion"""
     while True:
         if self.condition.evaluate(environment) == Boolean(False):
             return environment
         elif self.condition.evaluate(environment) == Boolean(True):
             environment = self.body.evaluate(environment)
Exemple #3
0
def test_if_false():
    st = If(Variable("x"), Assign("y", Number(1)), DoNothing())
    en = {"x": Boolean(False)}
    while st.reducible:
        st, en = st.reduce(en)
    assert st == DoNothing()
    assert en["x"] == Boolean(False)
Exemple #4
0
 def test_if_true(self):
     st = If(Variable("x"), Assign("y", Number(1)), Assign("y", Number(2)))
     self.assertEqual(str(st), "if (x) { y = 1 } else { y = 2 }")
     en = {"x": Boolean(True)}
     while st.reducible:
         st, en = st.reduce(en)
     self.assertEqual(en["y"], Number(1))
     self.assertEqual(en["x"], Boolean(True))
Exemple #5
0
def test_if_true():
    st = If(Variable("x"), Assign("y", Number(1)), Assign("y", Number(2)))
    assert str(st) == "if (x) { y = 1 } else { y = 2 }"
    en = {"x": Boolean(True)}
    while st.reducible:
        st, en = st.reduce(en)
    assert en["y"] == Number(1)
    assert en["x"] == Boolean(True)
Exemple #6
0
 def reduce(self, environment):
     if self.condition.reducible:
         return (
             If(
                 self.condition.reduce(environment),
                 self.consequence,
                 self.alternative,
             ),
             environment,
         )
     else:
         if self.condition == Boolean(True):
             return self.consequence, environment
         elif self.condition == Boolean(False):
             return self.alternative, environment
Exemple #7
0
    def test_type(self):
        n1 = Number(5)
        b1 = Boolean(False)
        v1 = Variable("x")

        self.assertEqual(eval(n1.to_python)({}), 5)
        self.assertEqual(eval(b1.to_python)({}), False)
        self.assertEqual(eval(v1.to_python)({"x": 7}), 7)
Exemple #8
0
 def test_type(self):
     self.assertEqual(Number(23).evaluate({}), Number(23))
     self.assertEqual(Variable("x").evaluate({"x": Number(23)}), Number(23))
     self.assertEqual(
         LessThan(Add(Variable("x"), Number(2)), Variable("y")).evaluate({
             "x":
             Number(2),
             "y":
             Number(5)
         }),
         Boolean(True),
     )
def test_type():
    assert Number(23).evaluate({}) == Number(23)
    assert Variable("x").evaluate({"x": Number(23)}) == Number(23)
    assert LessThan(Add(Variable("x"), Number(2)), Variable("y")).evaluate({
        "x":
        Number(2),
        "y":
        Number(5)
    })

    n1 = Number(5)
    b1 = Boolean(False)
    v1 = Variable("x")

    assert eval(n1.to_python)({}) == 5
    assert not eval(b1.to_python)({})
    assert eval(v1.to_python)({"x": 7}) == 7
def test_less():
    expr = LessThan(Variable("x"), Variable("y"))
    en = {"x": Number(1), "y": Number(3)}
    while expr.reducible:
        expr = expr.reduce(en)
    assert expr == Boolean(True)
def test_boolean():
    assert Boolean(True) and Boolean(True)
    assert Boolean(True), Boolean(True)
    assert Boolean(False), Boolean(False)
    assert Boolean(True) != Boolean(False)
Exemple #12
0
 def evaluate(self, environment):
     if self.condition.evaluate(environment) == Boolean(True):
         return self.consequence.evaluate(environment)
     elif self.condition.evaluate(environment) == Boolean(False):
         return self.alternative.evaluate(environment)
Exemple #13
0
 def evaluate_with_recursion(self, environment):
     if self.condition.evaluate(environment) == Boolean(True):
         return self.evaluate_with_recursion(
             self.body.evaluate(environment))
     elif self.condition.evaluate(environment) == Boolean(False):
         return environment
Exemple #14
0
 def test_less(self):
     expr = LessThan(Variable("x"), Variable("y"))
     en = {"x": Number(1), "y": Number(3)}
     while expr.reducible:
         expr = expr.reduce(en)
     self.assertEqual(expr, Boolean(True))
Exemple #15
0
 def test_boolean(self):
     self.assertTrue(Boolean(True) and Boolean(True))
     self.assertEqual(Boolean(True), Boolean(True))
     self.assertEqual(Boolean(False), Boolean(False))
     self.assertNotEqual(Boolean(True), Boolean(False))