-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.py
47 lines (35 loc) · 1.34 KB
/
interpreter.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
import hrminterp
import argparse
def main():
args = get_args()
image = tuple(map(hrminterp.Value.parse, args.memory.read().split(",")))
io_in, io_out = hrminterp.io.ConsoleInput(), hrminterp.io.ConsoleOutput()
if args.input is not None:
io_in = hrminterp.io.FileInput(args.input)
if args.output is not None:
io_out = hrminterp.io.FileOutput(args.output)
parsed = hrminterp.parse_program(args.program)
program = hrminterp.assemble_bytecode(parsed)
machine = hrminterp.Machine(io_in, io_out, len(image))
machine.fill_memory(image)
machine.set_program(program)
machine.run()
def get_args():
parser = argparse.ArgumentParser(
description="program to interpret programs from Human Resource Machine"
)
parser.add_argument(
'-p', '--program', type=argparse.FileType(), help="path to the program source", required=True
)
parser.add_argument(
'-m', '--memory', type=argparse.FileType(), help="path to the memory image", required=True
)
parser.add_argument(
'-i', '--input', type=argparse.FileType(), help="optional file that specifies input stream"
)
parser.add_argument(
'-o', '--output', type=argparse.FileType('w'), help="optional file to write output to"
)
return parser.parse_args()
if __name__ == "__main__":
main()