-
Notifications
You must be signed in to change notification settings - Fork 0
/
bf++.py
140 lines (127 loc) · 4.66 KB
/
bf++.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
from bf import *
from sys import argv
class NewStandard(Classic):
index_now_2 = 0
jmppoint_list = []
mem_back_list = []
flag_jump = False
flag_jump_right = True
@property
def break_condition(self):
return(super().break_condition or self.flag_jump)
def break_do(self, text: str):
if self.flag_jump:
if text[self.seek_now] == ":":
self.flag_jump = False
if self.flag_jump_right:
self.seek_now += 1
else:
self.seek_now -= 1
else:
super().break_do(text)
def output_str(self):
while(self.now != 0):
self.output()
self.index_now += 1
def clear(self):
self.memory = [0]
self.mem_back_list = []
self.index_now = 0
self.index_now_2 = 0
def add_memback(self):
self.mem_back_list.append(self.index_now)
def read_memback(self):
self.index_now = self.mem_back_list[-1]
del self.mem_back_list[-1]
def swap_index(self):
self.index_now, self.index_now_2 = self.index_now_2, self.index_now
def unknown_command(self, command):
match command:
case "/":
if self.now == 0:
self.flag_jump = True
self.flag_jump_right = True
self.seek_now += 1
case "\\":
if self.now == 0:
self.flag_jump = True
self.flag_jump_right = False
self.seek_now -= 1
case "*":
self.memory[self.index_now] = 0
self.seek_now += 1
case "#":
self.clear()
self.seek_now += 1
case "{":
tmp = self.now()
self.left()
self.memory[self.index_now] = tmp
self.seek_now += 1
case "}":
tmp = self.now()
self.right()
self.memory[self.index_now] = tmp
self.seek_now += 1
case ";":
self.output_str()
self.seek_now += 1
case "!":
self.add_memback()
self.seek_now += 1
case "@":
self.read_memback()
self.seek_now += 1
case "$":
self.swap_index()
self.seek_now += 1
case '`':
if self.now == 0:
exit()
else:
self.seek_now += 1
case "~":
if self.now == 0:
self.memory[self.index_now] = 1
else:
self.memory[self.index_now] = 0
self.seek_now += 1
case "=":
if self.memory[self.index_now] == self.memory[self.index_now_2]:
self.memory[self.index_now] = 1
else:
self.memory[self.index_now] = 0
self.seek_now += 1
case "&":
self.memory[self.index_now] = self.memory[self.index_now] & self.memory[self.index_now_2]
self.seek_now += 1
case "^":
self.memory[self.index_now] = self.memory[self.index_now] ^ self.memory[self.index_now_2]
self.seek_now += 1
case "|":
self.memory[self.index_now] = self.memory[self.index_now] | self.memory[self.index_now_2]
self.seek_now += 1
case "L":
self.memory[self.index_now] = self.memory[self.index_now] << self.memory[self.index_now_2]
self.seek_now += 1
case "R":
self.memory[self.index_now] = self.memory[self.index_now] >> self.memory[self.index_now_2]
self.seek_now += 1
case "A":
self.memory[self.index_now] = self.memory[self.index_now] + self.memory[self.index_now_2]
self.seek_now += 1
case "S":
self.memory[self.index_now] = self.memory[self.index_now] - self.memory[self.index_now_2]
self.seek_now += 1
case "M":
self.memory[self.index_now] = self.memory[self.index_now] * self.memory[self.index_now_2]
self.seek_now += 1
case "D":
self.memory[self.index_now] = self.memory[self.index_now] // self.memory[self.index_now_2]
self.seek_now += 1
case x:
self.memory[self.index_now] = ord(x)
self.right()
self.seek_now += 1
if __name__ == "__main__":
interpreter(argv[1:], NewStandard)