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 --- prolog/util.py | 39 +++++---------------------------------- 1 file changed, 5 insertions(+), 34 deletions(-) (limited to 'prolog') 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