-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
52 lines (39 loc) · 1.18 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
import argparse
import os
from lc3asm import process_source
def parse_cmd_arguments() -> argparse.Namespace:
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
'-p', '--path',
help='Path to assembly source',
type=str,
default='./examples/hello_world.asm'
)
arg_parser.add_argument(
'-o', '--out',
help='Path of output file',
type=str,
default='./output_programms/hello_world.raw'
)
args = arg_parser.parse_args()
if not args.path:
raise AttributeError('Missed required parameter --path')
if not args.out:
raise AttributeError('Missed required parameter --out')
if not os.path.exists(args.path):
raise FileExistsError(f'File \'{args.path}\' does not exists')
return args
def main() -> None:
args = parse_cmd_arguments()
with open(args.path, 'r') as file:
program = file.read()
memory = process_source(program)
byte_memory = bytearray([
e
for m in memory
for e in (m & 0xff, (m & 0xff00) >> 8)
])
with open(args.out, 'wb') as file:
file.write(byte_memory)
if __name__ == '__main__':
main()