From 4da3793985d1d177a95d67e78dd2f4f5f52e2eab Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Tue, 12 Jan 2016 13:57:12 +0100 Subject: Replace prolog.util.rename_vars with rename_vars_list everywhere --- monkey/__init__.py | 4 ++-- monkey/edits.py | 8 ++++---- prolog/util.py | 39 +++++---------------------------------- 3 files changed, 11 insertions(+), 40 deletions(-) diff --git a/monkey/__init__.py b/monkey/__init__.py index fbb4535..79dfe2d 100644 --- a/monkey/__init__.py +++ b/monkey/__init__.py @@ -16,7 +16,7 @@ import time -from prolog.util import tokenize, rename_vars, stringify, parse, interesting_ranges, rename_vars_list +from prolog.util import tokenize, stringify, parse, interesting_ranges, rename_vars_list from .util import PQueue # Starting from [code], find a sequence of edits that transforms it into a @@ -33,7 +33,7 @@ def fix(code, edits, test, timeout=30, debug=False): for (path, a, b), (p, uids) in edits.items(): if path == range_path and a == part_normal: reverse_names = {v: k for k, v in names.items()} - b_real = tuple(rename_vars(b, reverse_names)) + b_real = tuple(rename_vars_list(b, reverse_names)) new_tokens = [] idx = None for i, t in enumerate(tokens): diff --git a/monkey/edits.py b/monkey/edits.py index 73f692d..82e5066 100644 --- a/monkey/edits.py +++ b/monkey/edits.py @@ -18,7 +18,7 @@ import collections import math from .action import expand, parse -from prolog.util import normalized, parse as prolog_parse, rename_vars, rename_vars_ast, rename_vars_list, interesting_ranges, stringify, tokenize +from prolog.util import normalized, parse as prolog_parse, rename_vars_ast, rename_vars_list, interesting_ranges, stringify, tokenize from .util import avg, logistic def get_edits_from_trace(trace, test, id): @@ -142,10 +142,10 @@ def get_edits_from_solutions(solutions, test): # Update submission/query counters (use normalized variables). for code, correct in trace_submissions: - code = stringify(rename_vars(tokenize(code))) + code = stringify(rename_vars_list(tokenize(code))) submissions[(code, correct)].add(uid) for query in trace_queries: - code = stringify(rename_vars(tokenize(query))) + code = stringify(rename_vars_list(tokenize(query))) queries[code] += 1 # Discard edits that only occur in one trace. @@ -232,7 +232,7 @@ if __name__ == '__main__': if p.identifier in used_predicate_identifiers]) # Check for cached results. - normal_code = stringify(rename_vars(tokenize(code))) + normal_code = stringify(rename_vars_list(tokenize(code))) code_key = (normal_code, tuple(dependencies)) if code_key not in test_results[pid]: aux_code = '\n' + solutions_for_problems(problem.language, dependencies) + '\n' + facts diff --git a/prolog/util.py b/prolog/util.py index e69d4d5..c0f1ef5 100644 --- a/prolog/util.py +++ b/prolog/util.py @@ -78,31 +78,6 @@ def stringify(obj): return ''.join([stringify(child) for child in obj]) + '\n' return ''.join([stringify(child) for child in obj]) -# Rename variables in [tokens] to A0, A1, A2,… in order of appearance. -def rename_vars(tokens, names=None): - if names is None: - names = {} - next_id = len(names) - - # Return a new list. - tokens = list(tokens) - for i, t in enumerate(tokens): - if t.type == 'PERIOD': - pass -# names.clear() -# next_id = 0 - elif t.type == 'VARIABLE': - if t.val.startswith('_'): - tokens[i] = t.clone(val='A{}'.format(next_id)) - next_id += 1 - else: - cur_name = t.val - if cur_name not in names: - names[cur_name] = 'A{}'.format(next_id) - next_id += 1 - tokens[i] = t.clone(val=names[cur_name]) - return tokens - # Rename variables in [tokens] to A0, A1, A2,… in order of appearance. def rename_vars_list(tokens, names=None): if names is None: @@ -112,16 +87,12 @@ def rename_vars_list(tokens, names=None): # Return a new list. tokens = list(tokens) for i, t in enumerate(tokens): - if t.type == 'VARIABLE': - if t.val.startswith('_'): - tokens[i] = t.clone(val='A{}'.format(next_id)) + if t.type == 'VARIABLE' and t.val != '_': + cur_name = t.val + if cur_name not in names: + names[cur_name] = 'A{}'.format(next_id) next_id += 1 - else: - cur_name = t.val - if cur_name not in names: - names[cur_name] = 'A{}'.format(next_id) - next_id += 1 - tokens[i] = t.clone(val=names[cur_name]) + tokens[i] = t.clone(val=names[cur_name]) return tokens # Rename variables in AST rooted at [root] to A0, A1, A2,… in order of -- cgit v1.2.1