예제 #1
0
 def test_stores_the_right_value_for_strings(self):
     ev = OpalEvaluator()
     ev.evaluate('gamma = "bon appetit"', run=False)
     str(ev.llvm_mod).should.contain('%gamma = alloca [12 x i8]*')
     str(ev.llvm_mod).should.contain(
         'store [12 x i8]* @str_2f274d89cb7f072099747f894e80986616b2e81cc8e02711ef7c78db956d1fca, [12 x i8]** %gamma'
     )
예제 #2
0
    def setup_class(cls):
        expr = f"""
        class Object
        end
        """

        evaluator = OpalEvaluator()
        evaluator.evaluate(expr, run=False)
        cls.code = str(evaluator.codegen)
예제 #3
0
    def test_works_for_strings(self):
        opal_string = 'something something complete..'

        expr = f"""'{opal_string}'
        """
        ev = OpalEvaluator()
        ev.evaluate(expr)

        global_str_constant = r'@"str_[0-9a-fA-F]+" = private unnamed_addr constant \[31 x i8\] c"%s\\00"' % opal_string

        str(ev.codegen).should.match(global_str_constant)
예제 #4
0
    def test_simple_division(self):
        """
        Wrong configuration on Lark lead to parser ambiguity errors
        """
        expr = """
        10 / 2
        """

        ev = OpalEvaluator()

        ev.evaluate(expr)
예제 #5
0
    def test_works_for_booleans(self):
        expr = f"print(true)"

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('true')
예제 #6
0
    def test_works_for_arithmetics_with_parenthesis(self):
        expr = f"print(1000 / (10 - 80) + 22)"

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('8')
예제 #7
0
    def test_works_for_floats(self):
        expr = f"print(432.108)"

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('432.108')
예제 #8
0
    def test_works_for_integers(self):
        expr = f"print(432234)"

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('432234')
예제 #9
0
 def setup_class(cls):
     expr = f"""
     class {cls.parent_class_name}
     end
     
     class {cls.class_name} < {cls.parent_class_name}
     end
     """
     evaluator = OpalEvaluator()
     evaluator.evaluate(expr, run=False)
     cls.code = str(evaluator.codegen)
예제 #10
0
    def test_should_work_for_less_than(self):
        expr = f"""
        print(2 < 1)
        """

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('false')
예제 #11
0
    def test_works_for_strings(self):
        something_complete = f"""printing string"""
        expr = f"""print('%s')
        """ % something_complete

        ev = OpalEvaluator()
        ev.evaluate(expr, run=False)

        global_str_constant = \
            fr'@"str_[0-9a-fA-F]+" = private unnamed_addr constant \[16 x i8\] c"{something_complete}\\00"'

        str(ev.codegen).should.match(global_str_constant)
        str(ev.codegen).should.contain('%".3" = call i32 @"puts"(i8* %".2")')
예제 #12
0
    def test_works_for_strings(self):
        expr = """
        gamma = "so be it"
        print(gamma)
        """

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('so be it')
예제 #13
0
    def test_works_for_integers(self):
        expr = """
        alpha = 123
        print(alpha)
        """

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('123')
예제 #14
0
    def test_works_for_floats(self):
        expr = """
        beta = 12.3
        print(beta)
        """

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('12.3')
예제 #15
0
    def test_should_work_for_both_integers_greater_and_less_than_equal(self):
        expr = f"""
        print(3 <= 10)
        print(20 <= 5)
        """

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('false')
        out.should.contain('true')
예제 #16
0
    def test_should_work_for_float_inequality(self):
        expr = f"""
        print(3.0 != 3.0)
        print(0.3 != 0.8)
        """

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('false')
        out.should.contain('true')
예제 #17
0
    def test_should_work_for_both_floats_greater_and_less_than(self):
        expr = f"""
        print(3.0 < 10.1)
        print(20.4 < 5.4)
        """

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('false')
        out.should.contain('true')
예제 #18
0
    def test_should_work_for_equality(self):
        expr = f"""
        print(3 == 3)
        print(3 == 8)
        """

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('true')
        out.should.contain('false')
예제 #19
0
    def test_works_for_multiple_strings(self):
        str1 = 'something special'
        str2 = 'something different'

        expr = f"""print('%s')
        print('%s')
        """ % (str1, str2)
        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain(str1)
        out.should.contain(str2)
예제 #20
0
 def test_fails_when_declaring(self):
     expr = f"""
     class Integer < Bogus
     end
     """
     evaluator = OpalEvaluator()
     evaluator.evaluate. \
         when.called_with(expr, run=False).should.throw(CodegenError, 'Parent class Bogus not defined')
예제 #21
0
    def test_works_for_booleans(self):
        expr = """
        delta = true
        print(delta)
        zeta = false
        print(zeta)
        """

        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('true')
        out.should.contain('false')
예제 #22
0
    def test_twice_just_creates_one_constant(self):
        str1 = f"same thing printed twice"

        expr = f"""
        print('{str1}')
        print('{str1}')
        """
        ev = OpalEvaluator()

        with pipes() as (out, _):
            ev.evaluate(expr)

        out = out.read()

        out.should.contain('same thing printed twice')

        const_string_declaration_regex = r'(@"str_[0-9a-fA-F]+" =)'
        re.findall(const_string_declaration_regex,
                   str(ev.codegen),
                   flags=re.MULTILINE).should.have.length_of(1)
예제 #23
0
def evaluator() -> OpalEvaluator:
    return OpalEvaluator()
예제 #24
0
 def test_stores_the_right_value_for_ints(self):
     ev = OpalEvaluator()
     ev.evaluate('alpha = 1', run=False)
     str(ev.llvm_mod).should.contain('%alpha = alloca i32')
     str(ev.llvm_mod).should.contain('store i32 1, i32* %alpha')
예제 #25
0
 def test_stores_the_right_value_for_floats(self):
     ev = OpalEvaluator()
     ev.evaluate('beta = 2.3', run=False)
     str(ev.llvm_mod).should.contain('%beta = alloca double')
     str(ev.llvm_mod).should.contain(
         'store double 2.300000e+00, double* %beta')
예제 #26
0
 def test_includes_int_to_c_function(self):
     ev = OpalEvaluator()
     ev.evaluate('1 / 1', run=False)
     # noinspection PyStatementEffect
     ev.llvm_mod.get_function('int_to_string').should.be.truthy
예제 #27
0
 def test_includes_printf(self):
     ev = OpalEvaluator()
     str(ev.codegen).should.contain('declare i32 @"printf"(i8* %".1", ...)')
예제 #28
0
 def test_includes_int_to_string_function(self):
     ev = OpalEvaluator()
     str(ev.codegen).should.contain(
         'declare i8* @"int_to_string"(i32 %".1", i8* %".2", i32 %".3")')
예제 #29
0
 def test_includes_puts_function(self):
     ev = OpalEvaluator()
     str(ev.codegen).should.contain('declare i32 @"puts"(i8* %".1")')
예제 #30
0
 def test_includes_free_function(self):
     ev = OpalEvaluator()
     str(ev.codegen).should.contain('declare void @"free"(i8* %".1")')