summaryrefslogtreecommitdiff
path: root/prolog
diff options
context:
space:
mode:
Diffstat (limited to 'prolog')
-rw-r--r--prolog/util.py39
1 files changed, 5 insertions, 34 deletions
diff --git a/prolog/util.py b/prolog/util.py
index e69d4d5..c0f1ef5 100644
--- a/prolog/util.py
+++ b/prolog/util.py
@@ -79,31 +79,6 @@ def stringify(obj):
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:
names = {}
@@ -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