-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacl.py
More file actions
184 lines (143 loc) · 4.53 KB
/
Copy pathacl.py
File metadata and controls
184 lines (143 loc) · 4.53 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#Credit to JonoCode9374
#Advanced Computer Language --> Python port
import random
class ACL:
def getCode(self) -> str:
try:
print("File to interpret? ")
fileName: str = input()
file = open(fileName)
code: str = file.read()
file.close()
return code
except FileNotFoundError as e:
print("FILE FAIL")
return "F"
def toDec(self, _bin: str) -> int:
l: int = len(_bin)
j: int = l - 1
x: int = 0
dec: int = 0
while x < l:
dec += (2 ** x) * (ord(_bin[j]) - 48)
x += 1; j -= 1
return dec
def main(self):
#declares necessary variables etc.
code: str = self.getCode()
l: int = len(code)
i: int = 0 #code reader pointer
memory: list(int) = list() #cell based, binary
memory.append(int(0)) #make the memory have one cell at start
ptr: int = 0 #memory pointer
cells: int = 1 #cells in memory
bOutput: str = "" #binary output
cOutput: str = "" #character output
c: str = "" #code command
f: str = "" #for functions
#temporary variables
t: str = ""
x: int = 0
#--------------------
while i < l:
c = code[i]
#pointer movement
if c == "0":
ptr = 0
elif c == "1":
ptr += 1
if ptr == cells:
memory.append(int(0))
cells += 1
elif c == "2":
ptr -= 1
if ptr < 0:
ptr = cells - 1
#bit flip
elif c == "3":
if memory[ptr] == 1:
memory[ptr] = int(0)
else:
memory[ptr] = int(1)
elif c == "4":
bOutput += str(memory[ptr])
#if-else-endif
elif c == "5":
if memory[ptr] == 0:
x = 1
while x != 0:
i += 1
if code[i] == '5':
x += 1
elif code[i] == "7" or code[i] == "8" (x == 1 and code[i] == "6"):
x -= 1
#else
elif c == "6":
x = 1
while x != 0:
i += 1
if code[i] == "5":
x += 1
elif code[i] == "7" or code[i] == "8":
x -= 1
elif c == "7":
#endif command, does nothing on its own
pass
#loop
elif c == "8":
if memory[ptr] == 1:
x = 1
i -= 1
while x != 0:
i -= 1
if code[i] == "7" or code[i] == "8":
x += 1
elif code[i] == "5":
x -= 1
elif c == "9":
memory[ptr] = int(round(random.random()))
#input
elif c == "A":
x = int(input())
if (x != 0 and x != 1):
print(1111)
i = l
else:
memory[ptr] = int(x)
#binary output
elif c == "B":
print(bOutput)
bOutput = ""
#integer and character output
elif c == "C":
if bOutput == "":
print(cOutput)
cOutput = ""
elif memory[ptr] == 1:
cOutput += str(self.toDec(bOutput))
else:
cOutput += str(chr(self.toDec(bOutput)))
bOutput = ""
#declare function
elif c == "D":
i += 1
f = ""
while code[i] != "D":
f += code[i]
i += 1
#enter function into code
elif c == "E":
t = ""
for x in range(0, i + 1):
t += code[x]
t += f
for x in range(i + 1, l):
t += code[x]
code = t
l = len(code)
elif c == "F":
print(1111)
i = l
i += 1
interpreter = ACL()
interpreter.main()