forked from mchubby-3rdparty/Bgi_script_tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
asdis.py
122 lines (104 loc) · 2.92 KB
/
asdis.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
"""
Common assembler/disassembler routines.
This module is meant to be imported.
"""
import re
comma_replace = '@@@z@@Q@@@'
quote_replace = '$$$q$$H$$$'
backslash_replace = '###g##V###'
re_label = re.compile(r'([A-Za-z_][A-Za-z0-9_]+):$')
re_instr = re.compile(r'([A-Za-z_][A-Za-z0-9_:]*)\((.*)\);$')
re_header = re.compile(r'#header\s+"(.+)"')
re_define = re.compile(r'#define\s+([A-Za-z0-9_]+)\s+([A-Za-z0-9_]+)')
class QuoteMismatch(Exception):
"""
Raised when a closing quote was not found
"""
pass
class InvalidInstructionFormat(Exception):
"""
Raised when an instruction is misformed
"""
pass
class InvalidFunction(Exception):
"""
Raised when an instruction contains an unknown function
"""
pass
def escape(text):
"""
Escape text when writing to file
Returns: str
"""
text = text.replace('\\', '\\\\')
text = text.replace('\a', '\\a')
text = text.replace('\b', '\\b')
text = text.replace('\t', '\\t')
text = text.replace('\n', '\\n')
text = text.replace('\v', '\\v')
text = text.replace('\f', '\\f')
text = text.replace('\r', '\\r')
text = text.replace('"', '\\"')
return text
def unescape(text):
"""
Reverse operation of escape()
Returns: str
"""
text = text.replace('\\\\', backslash_replace)
text = text.replace('\\a', '\a')
text = text.replace('\\b', '\b')
text = text.replace('\\t', '\t')
text = text.replace('\\n', '\n')
text = text.replace('\\v', '\v')
text = text.replace('\\f', '\f')
text = text.replace('\\r', '\r')
text = text.replace('\\"', '"')
text = text.replace(backslash_replace, '\\')
return text
def remove_comment(line):
"""
Strips any trailing whitespace and comments
Returns: str
"""
cpos = 0
while True:
cpos = line.find('//', cpos)
if cpos == -1:
return line.rstrip()
qcount = line[:cpos].count('"') - line[:cpos].count('\\"')
if qcount % 2 == 0:
break
cpos += 1
line = line[:cpos]
return line.rstrip()
def get_quotes(line):
"""
Finds all occurrences of quote character
Returns: array of integers
"""
pos = 0
quotes = []
while True:
pos = line.find('"', pos)
if pos == -1:
break
quotes.append(pos)
pos += 1
return quotes
def replace_quote_commas(line, quotes):
pos = 0
commas = []
while True:
pos = line.find(',', pos)
if pos == -1:
break
for squote, equote in zip(quotes[::2], quotes[1::2]):
if squote < pos < equote:
commas.append(pos)
break
pos += 1
commas.reverse()
for pos in commas:
line = line[:pos] + comma_replace + line[pos + 1:]
return line