-
Notifications
You must be signed in to change notification settings - Fork 1
/
gaot_plus_plus.py
136 lines (103 loc) · 2.86 KB
/
gaot_plus_plus.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
#!/usr/bin/env python
import re
import sys
from getch import getch # From https://gist.github.com/chao787/2652257
from stack import Stack
try:
raw_input
except NameError:
raw_input = input
class Gaot(object):
def __init__(self):
self.stack = Stack([])
self.instructions = None
self.ipointer = 0
self.move = 1
self.BAA = re.compile(r"ba{2,}")
self.BLEET = re.compile(r"ble{2,}t")
self.commands = [self._add, #2
self._subtract, #3
self._continue, #4
self._exit, #5
self._reverse, #6
self._skiptrue, #7
self._skipfalse, #8
self._printnum, #9
self._printchar, #10
self._getnum, #11
self._getchar, #12
self._dupe, #13
self._swap, #14
self._reverse_stack,#15
self._rotate_stack] #16
self.ccount = 0
def run(self, program):
self.instructions = program.split()
while self.ipointer < len(self.instructions):
c = self.instructions[self.ipointer]
if self.BAA.search(c): # is a "baa" command
self.stack.append(len(c) - 2)
elif self.BLEET.search(c): # is a "bleet" command
n = len(c) - 3
self.commands[n-2]()
self.ipointer += self.move
def get_code(self):
if "--file" in sys.argv:
code = open(sys.argv[sys.argv.index("--file") + 1]).read()
input_index = sys.argv.index("--file")
sys.argv.pop(sys.argv.index("--file") + 1)
sys.argv.pop(sys.argv.index("--file"))
elif len(sys.argv) >= 2:
code = sys.argv[1]
input_index = 2
else:
code = raw_input("Code: ")
try:
self.input = sys.argv[input_index:]
except:
self.input = []
return code
# --------------------------------------
def _add(self):
self.stack.push(self.stack.pop() + self.stack.pop())
def _subtract(self):
self.stack.push(self.stack.pop() - self.stack.pop())
def _continue(self):
self.ipointer += self.move
def _exit(self):
exit()
def _reverse(self):
self.move *= -1
def __skip(self, state):
self.ipointer += self.move*(self.stack.pop() == state)
def _skiptrue(self):
self.__skip(True)
def _skipfalse(self):
self.__skip(False)
def _printnum(self):
sys.stdout.write(unicode(self.stack.pop()))
sys.stdout.flush()
def _printchar(self):
sys.stdout.write(unicode(chr(self.stack.pop())))
sys.stdout.flush()
def _getnum(self):
self.stack.push(float(raw_input()))
def _getchar(self):
_ = ord(getch())
self.stack.append(_*(_!=4))
def _dupe(self):
self.stack.push(self.stack.peek())
def _swap(self):
x,y = self.stack.pop(), self.stack.pop()
self.stack.push(y)
self.stack.push(x)
def _reverse_stack(self):
self.stack = Stack(self.stack.get()[::-1])
def _rotate_stack(self):
_ = self.stack.get()
self.stack = Stack(x[1:]+x[0])
def main():
gaot = Gaot()
gaot.run(gaot.get_code())
if __name__ == "__main__":
main()