diff options
author | Timotej Lazar <timotej.lazar@araneo.org> | 2014-04-24 15:56:40 +0200 |
---|---|---|
committer | Aleš Smodiš <aless@guru.si> | 2015-08-11 14:26:00 +0200 |
commit | cd572b9b2de73eea8e83b028c7b4c3906ab60ab2 (patch) | |
tree | bc79a7ac57c9552602c716cf71732c2b58afbb67 /prolog | |
parent | c580b4d7f4a91432f967d0ada459cd925f604caf (diff) |
Optimize prolog.util.stringify (~15% faster)
Diffstat (limited to 'prolog')
-rw-r--r-- | prolog/util.py | 24 |
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): |