-
Notifications
You must be signed in to change notification settings - Fork 4
/
analyze.py
61 lines (45 loc) · 1.49 KB
/
analyze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import re
def loopstats(code):
depth = 0
pos = 0
leaf = False
nleaf = 0
nrepos = 0
for c in code:
if c == '[':
pos = 0
leaf = True
depth += 1
elif c == ']':
if leaf:
nleaf += 1
if pos != 0:
nrepos += 1
leaf = False
depth -= 1
elif c == '>':
pos += 1
elif c == '<':
pos -= 1
return dict(nrepos=nrepos,
nleaf=nleaf)
def main():
code = ''.join([c for c in sys.stdin.read()
if c in (',' ,'.', '[', ']', '<', '>', '+', '-')])
nall = len(code)
nclearloops = 3 * (len(code.split('[-]')) - 1 + len(code.split('[+]')) - 1)
nscanloops = 3 * (len(code.split('[>]')) - 1 + len(code.split('[<]')) - 1)
ncontract = sum(map(len, re.findall(r'(\+{2,}|-{2,}|<{2,}|>{2,})', code)))
nloops = len([c for c in code if c == '['])
d = loopstats(code)
print "clearloop", nclearloops, nall, "%.2f" % (float(nclearloops) / nall)
print "scanloop", nscanloops, nall, "%.2f" % (float(nscanloops) / nall)
print "contract", ncontract, nall, "%.2f" % (float(ncontract) / nall)
print "reposloop", d['nrepos'], nloops, "%.2f" % (float(d['nrepos']) / nloops)
print "leafloop", d['nleaf'], nloops, "%.2f" % (float(d['nleaf']) / nloops)
return 0
if __name__ == "__main__":
sys.exit(main())