Beispiel #1
0
    def optimize_code(code):
        code = code.replace(u'Λ', '\\').replace(u'ε', 'Eps')
        was_optimized = True
        while was_optimized:
            was_optimized = False
            for item in re.finditer(CAM.betta_opt_rule, code):
                token, rest_code = get_term_in_brackets(item.group(),
                                                        br='<>',
                                                        remove_brackets=False)
                if rest_code[:3] != 'Eps':
                    continue
                arg1, arg2 = parse_args_in_brackets(token, br='<>')
                lambda_arg, _ = get_term_in_brackets(arg1[1:])
                code = code.replace('<%s,%s>Eps' % (arg1, arg2),
                                    '<%s>%s' % (arg2, lambda_arg))
                was_optimized = True
            for item in re.finditer(CAM.operation_opt_rule, code):
                arg, rest_code = get_term_in_brackets(item.group(),
                                                      br='<>',
                                                      exception=False)
                if not arg or rest_code[:3] != 'Snd':
                    continue
                operation = rest_code[-1]
                code = code.replace('<%s>Snd%s' % (arg, operation),
                                    '%s%s' % (arg, operation))
                was_optimized = True

        code = UnicodeHack(code.replace('\\', u'Λ').replace('Eps', u'ε'))
        logging.info('Optimized code: %s' % code)
        return code
Beispiel #2
0
    def optimize_code(code):
        logging.info('Code before optimization: %s' % code)
        code = code.replace(u'Λ', '\\').replace(u'ε', 'Eps')
        was_optimized = True
        while was_optimized:
            was_optimized = False
            for item in re.finditer(CAM.betta_opt_rule, code):
                token, rest_code = get_term_in_brackets(item.group(), br='<>', remove_brackets=False)
                if rest_code[:3] != 'Eps':
                    continue
                arg1, arg2 = parse_args_in_brackets(token, br='<>')
                lambda_arg, _ = get_term_in_brackets(arg1[1:])
                code = code.replace('<%s,%s>Eps' % (arg1, arg2), '<%s>%s' % (arg2, lambda_arg))
                was_optimized = True
            for item in re.finditer(CAM.operation_opt_rule, code):
                arg, rest_code = get_term_in_brackets(item.group(), br='<>', exception=False)
                if not arg or rest_code[:3] != 'Snd':
                    continue
                operation = rest_code[-1]
                code = code.replace('<%s>Snd%s' % (arg, operation), '%s%s' % (arg, operation))
                was_optimized = True

        code = UnicodeHack(code.replace('\\', u'Λ').replace('Eps', u'ε'))
        logging.info('Optimized code: %s' % code)
        return code
Beispiel #3
0
 def _push(self, parallel):
     if parallel:
         try:
             term, tmp = get_term_in_brackets('<' + self.code, br='<>', remove_brackets=False)
             args = parse_args_in_brackets(term, br='<>')
             self.code = tmp  # self.code[len(term) - 1:]
             if args:
                 t1 = pool.apply_async(CAM._calc_part, (self, args[0]))
                 t2 = pool.apply_async(CAM._calc_part, (self, args[1]))
                 self.term = (t1.get(), t2.get())
             else:
                 self.term = CAM._calc_part(self, term[1:-1])
         except TermException:
             self.stack.append(self.term)
     else:
         self.stack.append(self.term)
Beispiel #4
0
 def _push(self, parallel):
     if parallel:
         try:
             term, tmp = get_term_in_brackets('<' + self.code,
                                              br='<>',
                                              remove_brackets=False)
             args = parse_args_in_brackets(term, br='<>')
             self.code = tmp  # self.code[len(term) - 1:]
             if args:
                 t1 = pool.apply_async(CAM._calc_part, (self, args[0]))
                 t2 = pool.apply_async(CAM._calc_part, (self, args[1]))
                 self.term = (t1.get(), t2.get())
             else:
                 self.term = CAM._calc_part(self, term[1:-1])
         except TermException:
             self.stack.append(self.term)
     else:
         self.stack.append(self.term)
Beispiel #5
0
 def _parse_code(code):
     parsed_code = []
     while len(code):
         next_token, code = CAM._get_next_token_new(code)
         parsed_code.append(next_token)
         if next_token == u'Λ' or next_token == 'Y':
             arg, code = get_term_in_brackets(code)
             parsed_code.append(CAM._parse_code(arg))
         elif next_token == '\'':
             arg = int(UnicodeHack(re.search(CAM.nums_re, code).group()))
             parsed_code.append([arg])
             length = int(log10(abs(arg))) + 1 if arg != 0 else 1
             code = code[length if arg >= 0 else length + 1:]
         elif next_token == 'br':
             args, code = get_term_in_brackets(code, remove_brackets=False)
             arg1, arg2 = parse_args_in_brackets(args)
             parsed_code.append([CAM._parse_code(arg1), CAM._parse_code(arg2)])
     return parsed_code[::-1]
Beispiel #6
0
 def _parse_code(code):
     parsed_code = []
     while len(code):
         next_token, code = CAM._get_next_token_new(code)
         parsed_code.append(next_token)
         if next_token == u'Λ' or next_token == 'Y':
             arg, code = get_term_in_brackets(code)
             parsed_code.append(CAM._parse_code(arg))
         elif next_token == '\'':
             arg = int(UnicodeHack(re.search(CAM.nums_re, code).group()))
             parsed_code.append([arg])
             length = int(log10(abs(arg))) + 1 if arg != 0 else 1
             code = code[length if arg >= 0 else length + 1:]
         elif next_token == 'br':
             args, code = get_term_in_brackets(code, remove_brackets=False)
             arg1, arg2 = parse_args_in_brackets(args)
             parsed_code.append(
                 [CAM._parse_code(arg1),
                  CAM._parse_code(arg2)])
     return parsed_code[::-1]
Beispiel #7
0
 def _branch(self):
     args, code = get_term_in_brackets(self.code, remove_brackets=False)
     arg1, arg2 = parse_args_in_brackets(args)
     self.code = (arg1 if self.term else arg2) + code
     self.term = self.stack.pop()
Beispiel #8
0
 def _branch(self):
     args, code = get_term_in_brackets(self.code, remove_brackets=False)
     arg1, arg2 = parse_args_in_brackets(args)
     self.code = (arg1 if self.term else arg2) + code
     self.term = self.stack.pop()