-
Notifications
You must be signed in to change notification settings - Fork 1
/
lost.py
151 lines (127 loc) · 3.04 KB
/
lost.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import argparse
from interpreter import Interpreter
from KleinException import KleinException
parser = argparse.ArgumentParser(description="Klein Interpreter")
parser.add_argument(
"-v",
"--version",
action = "version",
version = "%(prog)s 0.0",
help = "Prints the version number of the interpreter."
)
parser.add_argument(
"-V",
"--verify",
action = "store_true",
help = "Runs the interpreter in verification mode."
)
parser.add_argument(
"-Q",
"--quiet-verification",
action = "store_true",
help = "Runs in a quieter version of verification mode."
)
parser.add_argument(
"-d",
"--debug",
action = "store_true",
help = "Runs the interpreter in debug mode."
)
parser.add_argument(
"-a",
"--ASCII-in",
action = "store_true",
help = "Takes input as ASCII code points"
)
parser.add_argument(
"-A",
"--ASCII-out",
action = "store_true",
help = "Outputs as ASCII code points"
)
parser.add_argument(
"-c",
"--ASCII",
action = "store_true",
help = "Takes input and outputs by ASCII code points"
)
parser.add_argument(
"source",
metavar = "Source",
help = "The name of the file from which the source is read."
)
parser.add_argument(
"input",
metavar = "Input",
nargs = "*",
help = "Integer input."
)
args = parser.parse_args()
with open(args.source) as file:
source = file.read()
if args.ASCII or args.ASCII_in:
a=Interpreter(source,map(ord," ".join(args.input)))
else:
a=Interpreter(source,map(int,args.input))
if args.debug:
curselib = None
try:
import curses
curselib = curses
except ImportError:
try:
import unicurses
curselib = unicurses
except ImportError:
raise KleinException("Cannot use debug mode without a curses library. Try installing either curses or unicurses.")
screen = curselib.initscr()
curselib.start_color()
curselib.use_default_colors()
curselib.init_pair(1, curselib.COLOR_RED, -1)
while a.direction != [0,0]:
a.output(screen,0,0)
try:
screen.addstr(a.dim,0," ".join(map(str,a.memory)))
except:pass
screen.refresh()
screen.getch()
try:
screen.addstr(a.dim,0," "*len(" ".join(map(str,a.memory))))
except:pass
a.action()
a.move()
curselib.endwin()
elif args.verify or args.quiet_verification:
maxX = len(source.strip().split("\n"))
maxY = max(map(len,source.strip().split("\n")))
outputs = []
for x in range(maxX):
for y in range(maxY):
for z in [[1,0],[0,1],[-1,0],[0,-1]]:
if args.ASCII or args.ASCII_in:
a=Interpreter(source,map(ord," ".join(args.input)),x,y,z)
else:
a=Interpreter(source,map(int,args.input),x,y,z)
while a.direction != [0,0]:
a.action()
a.move()
if not args.quiet_verification:
print (x,y,z),':',
if args.ASCII or args.ASCII_out:
o = "".join(map(chr,a.memory))
else:
o = " ".join(map(str,a.memory))
outputs.append(o)
print o
if len(set(outputs)) == 1:
print "Deterministic"
else:
print "Non-deterministic"
else:
while a.direction != [0,0]:
a.action()
a.move()
if args.ASCII or args.ASCII_out:
print "".join(map(chr,a.memory))
else:
print " ".join(map(str,a.memory))