re-write fixs

This commit is contained in:
Kelvin Sherlock 2024-10-30 16:48:35 -04:00
parent 0466fe1791
commit 1a1b36e98b

View File

@ -1,61 +1,117 @@
import sublime import sublime
import sublime_plugin import sublime_plugin
SPACE = [" ", "\t"]
QUOTES = ["'", '"']
TABS = (10, 15, 28)
STROPS = set((b'ASC', b'DCI', b'FLS', b'INV', b'REV' b'STR', b'STRL'))
XDIGITS = set("0123456789ABCDEFabcdef")
DQ = '"'
SQ = "'"
WS = " \t"
QUOTES = "\"'"
def tab_to(line, ts):
pos = TABS[ts-1]
line.append(0x20)
while len(line) < pos: line.append(0x20)
#
# need to do something clever for ; comments to put in comment field?
#
def fixs(s): def fixs(s):
q = 0
st = 0
opc = bytearray()
rv = bytearray()
f = 0
q = None
sp = False
comment = False
rv = ''
if not len(s): return s
if s[0] == "*": comment = True
if s[0] == ";": comment = True ; rv = "\t\t\t"
for c in s: for c in s:
if c == "\t": c = " "
if comment: cc = ord(c)
if c == "\t": c = " " if st == 0:
rv += c if c == " ":
st = 2
continue
if c == "*":
st = 7
elif c == ";":
st = 7
tab_to(rv, 3)
else:
st += 1
rv.append(cc)
continue continue
if q: if st == 1:
rv += c # label
if c == q: q = None if c == " ":
st += 1
continue
rv.append(cc)
if st == 2:
# label ws
if c == " ": continue
if c == "*" and len(rv) == 0:
st = 7
elif c == ";":
st = 7
tab_to(rv, 3)
else:
tab_to(rv, 1)
st += 1
opc.append(cc & ~0x20)
rv.append(cc)
continue continue
if c in QUOTES: if st == 3:
rv += c # opcode
q = c if c == " ":
sp = False st += 1;
continue
rv.append(cc)
opc.append(cc & ~0x20)
continue continue
if c in SPACE: if st == 4:
if sp: continue # opcode ws
f += 1 if c == " ": continue
c = "\t" if c == ";":
sp = True st = 7
rv += c tab_to(rv, 3)
else:
st += 1
if bytes(opc) in STROPS and c not in XDIGITS: q = c
if c in QUOTES: q = c
tab_to(rv, 2)
rv.append(cc)
continue continue
if sp: if st == 5:
if c == ';': # operand
comment = True if q:
rv += "\t" * (3-f) rv.append(cc)
elif f == 3: if q == cc: q = 0
comment = True continue
rv += "; " if c == " ":
sp = False st += 1
rv += c continue
rv.append(cc)
if c in QUOTES: q = cc
continue
if st == 6:
# operand ws
if c == " ": continue
# insert ; ????
st += 1
tab_to(rv, 3)
rv.append(cc)
continue
if st == 7:
# comment
rv.append(cc)
#
return rv.decode('ascii')
return rv;
class MerlinFixs(sublime_plugin.TextCommand): class MerlinFixs(sublime_plugin.TextCommand):