-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
90 lines (71 loc) · 2.71 KB
/
main.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
class translator:
def up(self, value=1):
return "self.memory_array[self.memory_pointer] += "+ str(value)
def down(self, value=1):
return "self.memory_array[self.memory_pointer] -= "+ str(value)
def fright(self, value=1):
self.ident += value
return "\n".join(["\t" * (self.ident*(i>0) + i - value) + "while self.memory_array[self.memory_pointer] != 0:" for i in range(value)])
def fleft(self, value=1):
self.ident -= value
return ""
def right(self, value=1):
return "self.memory_pointer += "+ str(value)
def left(self, value=1):
return "self.memory_pointer -= " + str(value)
def front(self, value=1):
return "print(chr(self.memory_array[self.memory_pointer])*" + str(value) + ", end='')"
commands_to_python = {
'👆' : up,
'👇' : down,
'🤜' : fright,
'🤛' : fleft,
'👉' : right,
'👈' : left,
'👊' : front,
}
def __init__(self, name):
self.header = \
f"""import numpy as np
class {name}:
\tdef __init__(self):
\t\tself.memory_pointer = 0
\t\tself.memory_array_lenght = 1000
\t\tself.memory_array = np.zeros(1000, dtype=np.uint8)
\tdef main(self):"""
self.finisher = \
f"""\n\n
if __name__=='__main__':
\tfoo = {name}()
\tfoo.main()
"""
self.code = ""
self.ident = 2
def __getitem__(self, key):
return "\n" + "\t"*self.ident + self.commands_to_python[key](self, self.value)
def read_input(self, filename):
with open(filename, 'r', encoding='utf8') as input_file:
self.last_command = None
for line in input_file:
for command in line:
if self.last_command is None:
self.value = 1
self.last_command = command
continue
if command == self.last_command:
self.value += 1
continue
self.code += self[self.last_command]
self.value = 1
self.last_command = command
self.code += self[self.last_command]
def save_program(self, filename):
with open(filename, 'w') as output_file:
output_file.write(self.header)
output_file.write(self.code)
output_file.write(self.finisher)
if __name__=='__main__':
name = "test2"
tr = translator(name)
tr.read_input(f"{name}.hand")
tr.save_program(f"{name}.py")