示例#1
0
 def expression_without_precedence(self):
     if self.__accept_and_consume(KeywordToken('nil')):
         return NilValue()
     elif self.__accept_type(NumberToken):
         token = self.__next()
         return IntegerValue.from_string(token.value)
     elif self.__accept_and_consume(SymbolToken('-')):
         token = self.__next()
         return IntegerValue.from_string('-' + token.value)
     elif self.__accept_type(StringToken):
         token = self.__next()
         return StringValue(token.value)
     elif self.__accept(SymbolToken('(')):
         return self.sequence()
     elif self.__accept_type(IdentifierToken):
         return self.id_started()
     elif self.__accept(KeywordToken('if')):
         return self.if_then()
     elif self.__accept(KeywordToken('while')):
         return self.while_do()
     elif self.__accept(KeywordToken('for')):
         return self.for_do()
     elif self.__accept_and_consume(KeywordToken('break')):
         return Break()
     elif self.__accept(KeywordToken('let')):
         return self.let()
     elif self.__accept(KeywordToken('type')):
         return self.type_declaration()
     elif self.__accept(KeywordToken('var')):
         return self.variable_declaration()
     elif self.__accept(KeywordToken('function')):
         return self.function_declaration()
     else:
         return None
        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
            def evaluate(self, env):
                env.push()
                start_value = self.start.evaluate(env)
                assert isinstance(start_value, IntegerValue)
                end_value = self.end.evaluate(env)
                assert isinstance(end_value, IntegerValue)

                iterator = IntegerValue(start_value.integer)
                for i in range(iterator.integer, end_value.integer + 1):
                    iterator.integer = i
                    env.set_current_level(self.var, iterator)
                    try:
                        result = loop(self, self.body, env)
                        assert result is None
                    except BreakException:
                        break

                env.pop()
            def evaluate(self, env):
                env.push()
                start_value = self.start.evaluate(env)
                assert isinstance(start_value, IntegerValue)
                end_value = self.end.evaluate(env)
                assert isinstance(end_value, IntegerValue)

                iterator = IntegerValue(start_value.integer)
                for i in range(iterator.integer, end_value.integer + 1):
                    jitdriver.jit_merge_point(code=self, expression=self.body, environment=env)
                    iterator.integer = i
                    env.set_current_level(self.var, iterator)
                    try:
                        result = self.body.evaluate(env)
                        assert result is None
                    except BreakException:
                        break

                env.pop()
示例#6
0
def tiger_stop_timer():
    """Native function to stop the timer timer, printing out the number of ticks; see tiger_start_timer()"""
    end_timestamp = read_timestamp()
    total_time = end_timestamp - start_timestamp.value
    try:
        if int(os.environ['DEBUG']):
            os.write(STDERR_FD, "ticks=%d\n" % total_time)
    except KeyError:
        # sure would like to avoid this try-catch
        pass
    return IntegerValue(total_time)
        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
            def evaluate(self, env):
                env.push()
                start_value = self.start.evaluate(env)
                assert isinstance(start_value, IntegerValue)
                end_value = self.end.evaluate(env)
                assert isinstance(end_value, IntegerValue)

                iterator = IntegerValue(start_value.integer)
                result = None
                while iterator.integer < end_value.integer + 1:
                    jitdriver.jit_merge_point(code=self, expression=self.body, environment=env, iterator=iterator,
                                              end_value=end_value, level=env.local_variables)
                    promote(end_value)
                    env.set_current_level(self.var, iterator)

                    try:
                        result = self.body.evaluate(env)
                        assert result is None
                    except BreakException:
                        break

                    iterator.integer += 1

                env.pop()
示例#9
0
def tiger_start_timer():
    """Native function to start a timer; in RPython this will measure the CPU ticks with RDTSC, see
    genop_math_read_timestamp in pypy/rpython/jit/backend/x86/assembler.py"""
    start_timestamp.value = read_timestamp()
    return IntegerValue(start_timestamp.value)
        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