-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
329 lines (298 loc) · 14.2 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# encoding: utf-8
"""
A Python implementation of an AHHH language interpreter originally concocted by Kyle Morgenstein, for some reason.
I wrote this because I don't know C++ or know how to compile C++ but I still need to scream.
Requires Python 3.6 or above.
I honestly have no idea if this even meets the original spec, I spent maybe 6 hours on this. I've also extended the
interpreter to include an "input" mode, where instead of entering inputs manually into the console it will read from
a provided input file. :)
Find the original AHHH here: https://github.com/KyleM73/AHHH
If you've come across this file outside of Github then find my repo here: https://github.com/CDWimmer/PyAHHH
Like the original, feel free to edit/use as you like, but please do not sell this nonsense or its derivatives.
(This is predominantly life advice rather than a legal disclaimer but I suppose both can be true)
If you use this for some reason then attribution would be pretty cool. Link back to the Github repository maybe.
"""
import argparse
import os
from sys import stderr, exit
__author__ = "Charles D Wimmer"
__copyright__ = "Copyright 2021, Charles D Wimmer"
__credits__ = "Charles D Wimmer"
__licence__ = "GPL-3.0 Licence"
__version__ = "27102021"
__twitter__ = "@CharlesDWimmer"
__email__ = __twitter__ # yup
__status__ = "Stage 3: Bargaining"
REGISTER_1 = None # "None" indicates that we have no value yet set to a register.
REGISTER_2 = None
PROGRAM_REGISTER = 0 # keeping track of which program instruction we're on.
POINTER_POS = 0
MEMORY_TAPE = [0] # I don't know if this should start as totally empty or with an [0] = 0 but this made less errors
PROGRAM = []
MODE = 0 # 0 - interactive, request from stdin for input. 1 - read from file for input (no idea if it works right)
INPUT = ""
CODES = {
"hhhh": 0, "hhhH": 1, "hhHh": 2, "hhHH": 3, "hHhh": 4, "hHhH": 5, "hHHh": 6, "hHHH": 7, "Hhhh": 8, "HhhH": 9,
"HhHh": 10, "HhHH": 11, "HHhh": 12, "HHhH": 13, "HHHh": 14, "HHHH": 15, "AHHH": 16, "hhh!": 17, "WHY?": 18
}
def quit_ahhh(exit_code):
if debug:
print("== EXECUTION FINISHED. ==\n"
f"Exit code: {exit_code}\n"
f"MEMORY TAPE: {MEMORY_TAPE}\n"
f"MEMORY POINTER: {POINTER_POS}\n"
f"REGISTER 1: {REGISTER_1}\n"
f"REGISTER 2: {REGISTER_2}\n"
f"PROGRAM REGISTER: {PROGRAM_REGISTER}\n")
exit(exit_code)
def throw_error(message):
"""This is probably a horrible way to do this"""
print(f"Error: {message}", file=stderr)
quit_ahhh(1)
def read_input(message):
if MODE == 0:
return input(message) or 0 # return whatever was entered or zero on blank
elif MODE == 1:
try:
return INPUT.pop(0)
except IndexError:
# input is empty/EOF, return zero
return 0
def read_program(filepath):
"""Reads in the program line by line and converts it to instruction codes in the `PROGRAM` list."""
if not os.path.exists(filepath):
parser.error(f"The file {filepath} does not exist!")
else:
with open(filepath, 'r') as f:
# If I understand the commenting rules correctly then a comment is anything after a space on a line.
# So if we just cut off everything after a space on each line then we are left with just instructions
lines = [line.split(' ')[0] for line in f.readlines()]
# iterate through each line, break it down into chunks of 4 characters and freak out if it's not a multiple
# of 4 long:
for i, line in enumerate(lines):
line = line.strip() # no newlines >:( - stripping empty lines or lines without comments
if len(line) % 4 != 0:
throw_error(f"Instructions of wrong length (len % 4 != 0) on line {i + 1}")
elif len(line) == 0:
continue # blank lines are allowed we just ignore them :) Reading in the lines above cuts off the
# CRLFs so this is works.
else:
# https://stackoverflow.com/questions/9475241/split-string-every-nth-character
n = 4 # instructions are length 4.
instructions = [line[i:i + n] for i in range(0, len(line), n)]
for instruction in instructions:
if instruction not in CODES.keys():
throw_error(f"Instruction `{instruction}` not recognised on line {i + 1}.")
else:
# actually start turning it into codes!
PROGRAM.append(CODES[instruction])
def run():
"""All AHHH program execution happens in here"""
global POINTER_POS # I don't understand why it wants this but the language of snakes must feed on tears
global PROGRAM_REGISTER
global MEMORY_TAPE
global REGISTER_1, REGISTER_2
if len(PROGRAM) == 0 or PROGRAM[0] != 16:
throw_error("Program is empty or does not start with 'AHHH'")
while True:
try:
instruction = PROGRAM[PROGRAM_REGISTER]
except IndexError:
# instruction counter is out of range of the program. We're finished executing.
break
if debug:
print(f"== INSTRUCTION: {instruction} ==")
# hhhh
if instruction == 0:
"""Searching in reverse, skip the first preceding command, and then jump back to matching HHHH command and
begin execution from that HHHH command (end loop)."""
level = 1
PROGRAM_REGISTER -= 1
while level > 0:
if PROGRAM_REGISTER == 1:
throw_error("Cannot end loop as first instruction!")
PROGRAM_REGISTER -= 1
if PROGRAM[PROGRAM_REGISTER] == 0:
level += 1
elif PROGRAM[PROGRAM_REGISTER] == 15:
level -= 1
if level != 0:
throw_error("Loop error")
continue # avoid incrementing the program register at the end of the main run() while.
# hhhH
elif instruction == 1:
"""Move the pointer right one cell."""
POINTER_POS += 1
if POINTER_POS > len(MEMORY_TAPE) - 1:
MEMORY_TAPE.append(0)
# hhHh
elif instruction == 2:
"""Move the pointer left one cell."""
POINTER_POS -= 1
if POINTER_POS < 0:
throw_error(f"Memory pointer position set to less than zero! Instruction number: {PROGRAM_REGISTER}")
# hhHH
elif instruction == 3:
"""Print the current memory cell as an integer."""
if debug:
print(f"== PRINT INT FROM {POINTER_POS}: {MEMORY_TAPE[POINTER_POS]}==")
print(str(MEMORY_TAPE[POINTER_POS]), end='', flush=True)
# hHhh
elif instruction == 4:
"""If Register 1 is empty, copy the current memory cell to the register. Otherwise, write the Register 1
value to the current memory cell."""
if REGISTER_1 is None:
REGISTER_1 = MEMORY_TAPE[POINTER_POS]
else: # register has value
MEMORY_TAPE[POINTER_POS] = REGISTER_1
REGISTER_1 = None
# hHhH
elif instruction == 5:
"""If Register 2 is empty, copy the current memory cell to the register. Otherwise, write the Register 2
value to the current memory cell."""
if REGISTER_2 is None:
REGISTER_2 = MEMORY_TAPE[POINTER_POS]
else:
MEMORY_TAPE[POINTER_POS] = REGISTER_2
REGISTER_2 = None
# hHHh
elif instruction == 6:
"""Add the current memory cell to the value of Register 1 and store the sum in Register 1. The memory cell
is unchanged."""
REGISTER_1 = REGISTER_1 + MEMORY_TAPE[POINTER_POS]
# hHHH
elif instruction == 7:
"""Add the current memory cell to the value of Register 2 and store the sum in Register 2. The memory cell
is unchanged."""
REGISTER_2 = REGISTER_2 + MEMORY_TAPE[POINTER_POS]
# Hhhh
elif instruction == 8:
"""If the current memory cell is nonzero, print the cell as an ASCII character. Otherwise, read in an
ASCII character from the console."""
if MEMORY_TAPE[POINTER_POS] > 0: # not zero and not less than zero. - negs are invalid for ascii!
if debug:
print(f"== PRINT CHAR FROM {POINTER_POS}: {MEMORY_TAPE[POINTER_POS]} -> "
f"{chr(MEMORY_TAPE[POINTER_POS])} ==")
print(chr(MEMORY_TAPE[POINTER_POS]), end='', flush=True)
else:
if debug:
print(f"== INPUTTING ASCII CHAR TO MEMORY POSITION {POINTER_POS} ==")
inputting = True
while inputting:
try:
if (user_input := read_input("ASCII character input: ")) == 0:
# if its been set to zero on blank input
# walruses are so cool :=
MEMORY_TAPE[POINTER_POS] = 0
else:
MEMORY_TAPE[POINTER_POS] = ord(user_input)
except TypeError:
print("Please enter a single valid ASCII character. ")
else:
inputting = False
# HhhH
elif instruction == 9:
"""Increment the current memory cell by one."""
MEMORY_TAPE[POINTER_POS] += 1
# HhHh
elif instruction == 10:
"""Decrement the current memory cell by one."""
MEMORY_TAPE[POINTER_POS] -= 1
# HhHH
elif instruction == 11:
"""Read in an integer from the console and store it in the current memory cell (overwrites current cell)."""
if MEMORY_TAPE[POINTER_POS] != 0:
print(chr(MEMORY_TAPE[POINTER_POS]), end='', flush=True)
else:
if debug:
print(f"== INPUTTING INT TO MEMORY POSITION {POINTER_POS} ==")
inputting = True
while inputting:
try:
MEMORY_TAPE[POINTER_POS] = int(read_input("Integer input: "))
except TypeError:
print("Please enter a valid integer. ")
else:
inputting = False
# HHhh
elif instruction == 12:
"""Set the current memory cell to zero."""
MEMORY_TAPE[POINTER_POS] = 0
# HHhH
elif instruction == 13:
"""Double the value of the current memory cell and store it in the current memory cell."""
MEMORY_TAPE[POINTER_POS] = MEMORY_TAPE[POINTER_POS] * 2
# HHHh
elif instruction == 14:
"""Square the value of the current memory cell and store it in the current memory cell."""
MEMORY_TAPE[POINTER_POS] = MEMORY_TAPE[POINTER_POS] ** 2
# HHHH
elif instruction == 15:
if MEMORY_TAPE[POINTER_POS] == 0: # if non-zero then we skip the loop, otherwise do nothing.
level = 1
prev = 0
PROGRAM_REGISTER += 1
if PROGRAM_REGISTER >= len(PROGRAM) - 1:
throw_error("Loop error, EOF while searching for end of loop.")
while level > 0:
prev = PROGRAM[PROGRAM_REGISTER]
PROGRAM_REGISTER += 1
if PROGRAM_REGISTER == len(PROGRAM) - 1:
break
elif PROGRAM[PROGRAM_REGISTER] == 15:
level += 1
elif PROGRAM[PROGRAM_REGISTER] == 0:
# found a hhhh
level -= 1
if prev == 15:
level -= 1
if level != 0:
throw_error("Loop error, EOF while searching for end of loop.")
# AHHH
elif instruction == 16:
"""Start program."""
pass # not sure what to do if this is encountered in the wrong place? Ignoring for now
# hhh!
elif instruction == 17:
"""Print new line (useful after printing ASCII characters, which otherwise don't print a new line)."""
print("") # prints newline automatically, probably.
elif instruction == 18:
"""Do nothing. WHY? is for comment lines/REMarks."""
pass
if debug:
print(f"-> MEM: {MEMORY_TAPE}\tREG1: {REGISTER_1}\tREG2: {REGISTER_2}\t MPOS: {POINTER_POS}")
PROGRAM_REGISTER += 1
# end of while
quit_ahhh(0)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='An AHHH interpreter written in Python.', epilog='Ahhhhhhh!')
parser.add_argument('filepath', type=str, help='filepath for a .ahhh script.')
parser.add_argument('-i', '--input', help='Input file. Optional. If not present then inputs will be requested from '
'console.', required=False)
parser.add_argument('-d', '--debug', help='Debug mode flag.', action='store_true')
args = parser.parse_args()
debug = False
if args.debug:
print("== DEBUG MODE IS ON. ==")
debug = True
if args.input:
if debug:
print("== This input mode is experimental, if it's horribly broken please leave an issue on the GitHub! ==")
print("== LOADING INPUT FILE TO MEMORY ==")
MODE = 1 # file input mode
try:
with open(args.input, 'r') as in_file:
INPUT = list(''.join(in_file.readlines()))
if INPUT == "":
throw_error(f"Provided input file ({args.input}) is empty!")
if debug:
print(f"== INPUT FILE PROVIDED: {args.input} ==")
print(f"INPUT CONTENT: {''.join(INPUT)}")
except FileNotFoundError as e:
print(f"File {args.input} does not seem to exist!")
# read program
read_program(args.filepath)
if debug:
print(f"PROGRAM CODE:\n{', '.join(str(x) for x in PROGRAM)}")
# RUN
run()