summaryrefslogtreecommitdiff
path: root/prolog/util.py
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@araneo.org>2014-04-24 15:56:40 +0200
committerAleš Smodiš <aless@guru.si>2015-08-11 14:26:00 +0200
commitcd572b9b2de73eea8e83b028c7b4c3906ab60ab2 (patch)
treebc79a7ac57c9552602c716cf71732c2b58afbb67 /prolog/util.py
parentc580b4d7f4a91432f967d0ada459cd925f604caf (diff)
Optimize prolog.util.stringify (~15% faster)
Diffstat (limited to 'prolog/util.py')
-rw-r--r--prolog/util.py24
1 files changed, 7 insertions, 17 deletions
diff --git a/prolog/util.py b/prolog/util.py
index fc243f2..eb026fe 100644
--- a/prolog/util.py
+++ b/prolog/util.py
@@ -19,24 +19,14 @@ operators = set([
'PLUS', 'MINUS', 'STAR', 'DIV', 'IDIV', 'MOD',
'POW', 'SEMI'
])
-def stringify(tokens, indent=''):
- s = indent
- for t in tokens:
- if t[0] in operators:
- s += ' '
-
- if t[0] == 'FROM':
- s += ':-\n ' + indent
- elif t[0] == 'PERIOD':
- s += '.\n' + indent
- elif t[0] == 'COMMA':
- s += ', '
+def stringify(tokens):
+ def token_str(t):
+ if t[0] in ('PERIOD', 'COMMA'):
+ return t[1] + ' '
elif t[0] in operators:
- s += t[1] + ' '
- else:
- s += t[1]
-
- return s.strip().replace('\n', ' ').replace('\t', ' ')
+ 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):