#!/usr/bin/python3 import itertools import math import re from .lexer import lexer # new lexer stuff def tokenize(text): # feed the troll lexer.input(text) # we are not interested in line numbers and absolute positions return [(t.type, t.value) for t in lexer] operators = set([ 'FROM', 'IMPLIES', 'NOT', 'EQU', 'NEQU', 'EQ', 'NEQ', 'UNIV', 'IS', 'EQA', 'NEQA', 'LT', 'LE', 'GT', 'GE', 'LTL', 'LEL', 'GTL', 'GEL', 'PLUS', 'MINUS', 'STAR', 'DIV', 'IDIV', 'MOD', 'POW', 'SEMI' ]) def stringify(tokens): def token_str(t): if t[0] in ('PERIOD', 'COMMA'): return t[1] + ' ' elif t[0] in operators: return ' ' + t[1] + ' ' return t[1] return ''.join(map(token_str, tokens)) # return a list of lines in 'code', and a list of rule indexes def decompose(code): lines = [] rules = [] tokens = tokenize(code) tokens.append(('EOF', '')) line = [] parens = [] rule_start = 0 for t in tokens: if t[0] == 'SEMI': if line != []: lines.append(tuple(line)) line = [] lines.append((t,)) continue if not parens: if t[0] in ('PERIOD', 'FROM', 'COMMA', 'EOF'): if line != []: lines.append(tuple(line)) line = [] if t[0] in ('PERIOD', 'EOF') and rule_start < len(lines): rules.append((rule_start, len(lines))) rule_start = len(lines) continue if t[0] in ('LPAREN', 'LBRACKET', 'LBRACE'): parens.append(t[0]) elif parens: if t[0] == 'RPAREN' and parens[-1] == 'LPAREN': parens.pop() elif t[0] == 'RBRACKET' and parens[-1] == 'LBRACKET': parens.pop() elif t[0] == 'RBRACE' and parens[-1] == 'LBRACE': parens.pop() line.append(t) return tuple(lines), tuple(rules) # pretty-print a list of rules def compose(lines, rules): code = '' for start, end in rules: for i in range(start, end): line = lines[i] if i > start: code += ' ' code += stringify(line) if i == end-1: code += '.\n' elif i == start: code += ' :-\n' else: if line and line[-1][0] != 'SEMI' and lines[i+1][-1][0] != 'SEMI': code += ',' code += '\n' return code.strip() # standardize variable names in order of appearance def rename_vars(tokens, names={}): # copy names so we don't fuck it up names = {k: v for k, v in names.items()} next_id = len(names) for i in range(len(tokens)): if tokens[i][0] == 'PERIOD': names.clear() next_id = 0 elif tokens[i] == ('VARIABLE', '_'): tokens[i] = ('VARIABLE', 'A' + str(next_id)) next_id += 1 elif tokens[i][0] == 'VARIABLE': cur_name = tokens[i][1] if cur_name not in names: names[cur_name] = next_id next_id += 1 tokens[i] = ('VARIABLE', 'A' + str(names[cur_name])) return names # transformation = before → after; applied on line which is part of rule # return mapping from formal vars in before+after to actual vars in rule # line and rule should of course not be normalized def map_vars(before, after, line, rule): mapping = {} new_index = 0 for i in range(len(before)): if line[i][0] == 'VARIABLE': formal_name = before[i][1] if line[i][1] != '_': actual_name = line[i][1] else: actual_name = 'New'+str(new_index) new_index += 1 mapping[formal_name] = actual_name remaining_formal = set([t[1] for t in after if t[0] == 'VARIABLE' and t[1] not in mapping.keys()]) remaining_actual = set([t[1] for t in rule if t[0] == 'VARIABLE' and t[1] != '_' and t[1] not in mapping.values()]) #for i in range(0, len(remaining_formal)-len(remaining_actual)+2): for var in remaining_formal: remaining_actual.add('New'+str(new_index)) new_index += 1 # cthulhu-inspired horrors continue... # get all possible mappings of remaining transform var. names to actual names def match(a, b): for bc in itertools.combinations(b, min(len(a), len(b))): for bp in itertools.permutations(bc): for ac in itertools.combinations(a, len(bc)): yield {ac[i]: bp[i] for i in range(len(bc))} for more_mapping in match(remaining_formal, remaining_actual): # copy base mapping dict and add remaining vars mapping_all = {k: v for k, v in mapping.items()} mapping_all.update(more_mapping) yield mapping_all