def test():
            # adding this line creates more jitcodes in /tmp/usession-exploration-abrown/jitcodes which reduces the number of operations
            unused = Parser(
                'let var a := 0 in (while a < 100 do a := a + 1) end').parse()

            program = Let(
                declarations=[
                    VariableDeclaration(name='a',
                                        type_id=None,
                                        expression=IntegerValue(0))
                ],
                expressions=[
                    Sequence(expressions=[
                        ModifiedWhile(condition=LessThan(
                            left=LValue('a'), right=IntegerValue(100)),
                                      body=Assign(lvalue=LValue(name='a'),
                                                  expression=Add(
                                                      left=LValue(name='a'),
                                                      right=IntegerValue(1)))),
                        LValue(name='a', next_lvalue=None)
                    ])
                ])

            environment = create_environment_with_natives(
            )  # apparently RPython barfs if we just use Environment() here because NativeFunctionDeclaration.__init__ is never called so the flowspace does not know about the 'function' field
            result = program.evaluate(environment)
            assert isinstance(result, IntegerValue)
            return result.integer
 def test():
     # output of printing the parsed version: Let(declarations=[VariableDeclaration(name=a, type=None, exp=IntegerValue(0))], expressions=[Sequence(expressions=[For(var=i, start=IntegerValue(1), end=IntegerValue(9), body=Assign(lvalue=LValue(name=a, next=None), expression=Subtract(left=LValue(name=a, next=None), right=LValue(name=i, next=None)))), LValue(name=a, next=None)])])
     program = Let(declarations=[VariableDeclaration(name='a', type_id=None, expression=IntegerValue(0))],
                   expressions=[Sequence(
                       expressions=[For(var='i', start=IntegerValue(1), end=IntegerValue(9),
                                        body=Assign(lvalue=LValue(name='a', next_lvalue=None),
                                                    expression=Subtract(
                                                        left=LValue(name='a', next_lvalue=None),
                                                        right=LValue(name='i',
                                                                     next_lvalue=None)))),
                                    LValue(name='a', next_lvalue=None)])])
     program = promote(program)
     environment = create_environment_with_natives()  # apparently RPython barfs if we just use Environment() here because NativeFunctionDeclaration.__init__ is never called so the flowspace does not know about the 'function' field
     result = program.evaluate(environment)
     assert isinstance(result, IntegerValue)
     return result.integer
Esempio n. 3
0
 def visit(self, node: ast.Let, scope: Scope):
     new_scope = Scope(scope.classname, scope)
     for decl in node.declarations:
         self.visit(decl, new_scope)
     b_type = self.visit(node.body, new_scope)
     node.static_type = b_type
     return b_type
        def test():
            # adding this line creates more jitcodes in /tmp/usession-exploration-abrown/jitcodes which reduces the number of operations
            Parser('let var a := 0 in a end').parse()

            program = Let(declarations=[VariableDeclaration(name='a', type_id=None, expression=IntegerValue(0))],
                          expressions=[Sequence(
                              expressions=[InternalMergePointFor(var='i', start=IntegerValue(1), end=IntegerValue(9),
                                                                 body=Assign(lvalue=LValue(name='a', next_lvalue=None),
                                                                             expression=Subtract(
                                                                                 left=LValue(name='a', next_lvalue=None),
                                                                                 right=LValue(name='i',
                                                                                              next_lvalue=None)))),
                                           LValue(name='a', next_lvalue=None)])])
            environment = create_environment_with_natives()  # apparently RPython barfs if we just use Environment() here because NativeFunctionDeclaration.__init__ is never called so the flowspace does not know about the 'function' field
            result = program.evaluate(Environment.empty())
            assert isinstance(result, IntegerValue)
            return result.integer
Esempio n. 5
0
 def let(self):
     self.__expect(KeywordToken('let'))
     decs = self.declarations()
     self.__expect(KeywordToken('in'))
     if not self.__accept(KeywordToken('end')):
         exps = self.expressions()
     else:
         exps = []
     self.__expect(KeywordToken('end'))
     return Let(decs, exps)
    def test(self):
        native_types = Let([
            TypeDeclaration('string', TypeId('string')),
            TypeDeclaration('int', TypeId('int'))
        ], [])
        stdout = OutputContainer()
        capture_stdout_function = NativeOneArgumentFunctionDeclaration(
            'print', [FunctionParameter('s', TypeId('str'))], None,
            stdout.capture)
        program = parse_file(path, [native_types, capture_stdout_function])
        env = Environment.empty()
        program.evaluate(env)

        expected = read_file(path.replace('.tig', '.out.bak'))
        self.assertEqual(expected, stdout.get_captured())
Esempio n. 7
0
def create_native_functions():
    """Convenience method to add all native functions to the environment"""
    string_type = TypeId('string')
    integer_type = TypeId('int')
    native_types = Let([
        TypeDeclaration('string', string_type),
        TypeDeclaration('int', integer_type)
    ], [])
    print_function = NativeOneArgumentFunctionDeclaration(
        'print', [FunctionParameter('message', string_type)], None,
        tiger_print)
    time_go_function = NativeNoArgumentFunctionDeclaration(
        'timeGo', integer_type, tiger_start_timer)
    time_stop_function = NativeNoArgumentFunctionDeclaration(
        'timeStop', integer_type, tiger_stop_timer)
    return [native_types, print_function, time_go_function, time_stop_function]
        def test():
            # this is equivalent to what is constructed below
            unused = Parser("""
            let var a := 0 var b := 0 in 
                (while a < 50 do
                   (a := a + 1;
                   while b < 100 do
                       (b := b + 1);
                    b := 0
                   );
                a)
            end""").parse()

            program = Let(
                declarations=[
                    VariableDeclaration(name='a',
                                        type_id=None,
                                        expression=IntegerValue(0)),
                    VariableDeclaration(name='b',
                                        type_id=None,
                                        expression=IntegerValue(0))
                ],
                expressions=[
                    Sequence(expressions=[
                        ModifiedWhile(
                            condition=LessThan(left=LValue(name='a',
                                                           next_lvalue=None),
                                               right=IntegerValue(50)),
                            body=Sequence(expressions=[
                                Assign(lvalue=LValue(name='a',
                                                     next_lvalue=None),
                                       expression=Add(left=LValue(
                                           name='a', next_lvalue=None),
                                                      right=IntegerValue(1))),
                                ModifiedWhile(
                                    condition=LessThan(
                                        left=LValue(name='b',
                                                    next_lvalue=None),
                                        right=IntegerValue(100)),
                                    body=Sequence(expressions=[
                                        Assign(lvalue=LValue(name='b',
                                                             next_lvalue=None),
                                               expression=Add(
                                                   left=LValue(
                                                       name='b',
                                                       next_lvalue=None),
                                                   right=IntegerValue(1)))
                                    ])),
                                Assign(lvalue=LValue(name='b',
                                                     next_lvalue=None),
                                       expression=IntegerValue(0))
                            ])),
                        LValue(name='a', next_lvalue=None)
                    ])
                ])

            environment = create_environment_with_natives(
            )  # apparently RPython barfs if we just use Environment() here because NativeFunctionDeclaration.__init__ is never called so the flowspace does not know about the 'function' field
            result = program.evaluate(environment)
            assert isinstance(result, IntegerValue)
            return result.integer