Skip to content

Commit fb31d89

Browse files
committedNov 4, 2015
Web-executor exposes cycles counter
1 parent 51c117a commit fb31d89

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed
 

‎i4004web.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,41 @@ def c_3e0(self):
4646
sys.stdout.write(chr(v))
4747

4848
def loadSource():
49-
text = sys.stdin.read().splitlines()
49+
texts = sys.stdin.read()
50+
text = texts.splitlines()
5051
if len(text) < 3:
5152
raise Exception('improper script invocation')
5253
inputCount = int(text[0].strip())
5354
return (text[inputCount + 1:], text[1 : inputCount + 1])
5455

5556
def main():
57+
from StringIO import StringIO
58+
import sys
5659
print("Content-Type: text/plain")
57-
headerSent = False
5860

5961
try:
6062
src, inputData = loadSource()
6163
prg = translator.translate(src)
6264
print("Program-Size: " + str(prg['_top']))
63-
print('')
64-
headerSent = True
6565
cpu = EnhancedExecutor()
6666
if len(inputData) == 1:
6767
if cpu.fetchState(inputData[0]):
6868
inputData = []
6969
cpu.inputData = '\n'.join(inputData)
70+
old_stdout = sys.stdout
71+
my_stdout = StringIO()
72+
sys.stdout = my_stdout
7073
cpu.run(prg)
74+
sys.stdout = old_stdout
75+
print("Program-Cycles: " + str(cpu.cycles))
76+
print('')
77+
result = my_stdout.getvalue()
78+
sys.stdout.write(result)
7179
if len(inputData) == 0:
7280
cpu.printRegs()
7381
except Exception as e:
74-
if not headerSent:
75-
print('')
82+
sys.stdout = old_stdout
83+
print('')
7684
print("Error: %s\n" % e)
7785

7886
main()

0 commit comments

Comments
 (0)
Please sign in to comment.