-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchexcomp.py
91 lines (84 loc) · 2.34 KB
/
chexcomp.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
import os
import sys
args = sys.argv
def wb(byte):
try:
f = open("out.chexc", "ab")
f.write(byte)
f.close()
except:
print("something went wrong during the writing process")
sys.exit(1)
def ewerr(err, errcode, line):
print("CHex compile error on line " + str(line) + ":" + err)
sys.exit(errcode)
def create_file(filename):
f = open(filename, "w")
f.close()
try:
filler = args[1]
except:
print("No args given")
sys.exit(1)
try:
f = open(args[1], "r")
lines = f.readlines()
f.close()
except:
ewerr("Missing file", 2, 0)
linenum = 0
create_file("out.chexc")
wb(b'hi')
for line in lines:
linenum += 1
split = line.split()
# comments
if line == "" or line.startswith(";"):
pass
# print
elif line.startswith("pr "):
if not len(split) == 2:
ewerr("Wrong args", 5, linenum)
try:
hext = b'\x01' + bytes([int(split[1])])
except:
ewerr("Can't convert decimal to hex", 4, linenum)
wb(hext)
# jump
elif line.startswith("jmp "):
if not len(split) == 2:
ewerr("Wrong args", 5, linenum)
try:
hext = b'\x02' + bytes([int(split[1])])
except:
ewerr("Can't convert decimal to hex", 4, linenum)
wb(hext)
# store
elif line.startswith("str "):
if not len(split) == 3:
ewerr("Wrong args", 5, linenum)
try:
hext = b'\x03' + bytes([int(split[1])]) + bytes([int(split[2])])
except:
ewerr("Can't convert decimal to hex", 4, linenum)
wb(hext)
# jump to place in stroed memory
elif line.startswith("jmpmem "):
if not len(split) == 2:
ewerr("Wrong args", 5, linenum)
try:
hext = b'\x04' + bytes([int(split[1])])
except:
ewerr("Can't convert decimal to hex", 4, linenum)
wb(hext)
# jump if equal
elif line.startswith("jmpeq "):
if not len(split) == 4:
ewerr("Wrong args", 5, linenum)
try:
hext = b'\x05' + bytes([int(split[1])]) + bytes([int(split[2])]) + bytes([int(split[3])])
except:
ewerr("Can't convert decimal to hex", 4, linenum)
wb(hext)
else:
ewerr("Unkown command", 10, linenum)