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 __init__(self,
                 code,
                 save_history=True,
                 with_opt=True,
                 fast_method=False,
                 parallel=False):
        self.code = UnicodeHack(code.replace(' ', ''))
        if with_opt:
            self.code = CAM.optimize_code(self.code)
        self.term = ()
        self.stack = []

        self.recursion_stack = {}
        self.recs_count = 0

        self.history = []
        self.iteration = 0
        self.evaluated = False
        self.errors = False

        self.parallel = parallel
        self.fast_method = fast_method

        if self.fast_method:
            self.parsed_code = self._parse_code(self.code)

        self.save_history = save_history
        if self.save_history:
            self.history.append([0, (), self.code, []])
Beispiel #3
0
 def evaluate(self):
     if self.fast_method:
         while self.parsed_code:
             self._transitions[self.parsed_code.pop()](self, True)
             self.iteration += 1
     else:
         while self.code and not self.evaluated:
             self.next_step()
             if self.save_history:
                 self.history.append([
                     self.iteration, self.term,
                     UnicodeHack(self.code),
                     deepcopy(self.stack)
                 ])
     self.evaluated = True
Beispiel #4
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 #5
0
 def _get_next_token_new(code):
     for i in CAM._possible_token_len:
         if code[0:i] in CAM._valid_tokens:
             return UnicodeHack(code[0:i]), code[i:]
     raise Exception('Unknown token')
Beispiel #6
0
 def _quote(self):
     self.term = int(UnicodeHack(
         re.search(self.nums_re, self.code).group()))
     length = int(log10(abs(self.term))) + 1 if self.term != 0 else 1
     self.code = self.code[length if int(self.term) >= 0 else length + 1:]
Beispiel #7
0
def print_lib():
    i = 1
    for item in LIBRARY:
        print '%s) %s = %s' % (i, item[0], UnicodeHack(item[1]))
        i += 1
    print