better asm output newline logic

This commit is contained in:
Bryan Bishop 2012-04-25 16:11:38 -05:00
parent ff4c37eb2b
commit 759c55d3f2

View File

@ -4662,45 +4662,52 @@ class Asm:
fh = open(filename, "w") fh = open(filename, "w")
newlines_before_next_obj_requested = 0 newlines_before_next_obj_requested = 0
newlines_before_next_obj_given = 0 newlines_before_next_obj_given = 0
current_requested_newlines_before = 0
current_requested_newlines_after = 0
previous_requested_newlines_before = 0
previous_requested_newlines_after = 0
written_newlines = 0
write_something = False
first = True
last = None
for each in self.parts: for each in self.parts:
asm = "" asm = ""
if hasattr(each, "to_asm"): previous_requested_newlines_after = current_requested_newlines_after
#get the asm content to insert into the file current_requested_newlines_before = current_requested_newlines_after
if isinstance(each, AsmSection) or isinstance(each, Incbin) or isinstance(each, AsmLine):
write_something = True
if (isinstance(each, str) and each == "") or (isinstance(each, AsmLine) and each.line == ""):
current_requested_newlines_before = 0
if current_requested_newlines_after < 2:
current_requested_newlines_after += 1
write_something = False
elif (isinstance(each, str) and each != "") or (isinstance(each, AsmLine) and each.line != ""):
if isinstance(each, AsmLine):
asm = each.to_asm() asm = each.to_asm()
else: #like: MapHeader, SecondMapHeader, MapEventHeader, MapScriptHeader, .. elif isinstance(each, str):
asm = to_asm(each) asm = each
current_requested_newlines_before = 0
current_requested_newlines_after = 1
elif isinstance(each, AsmSection) or isinstance(each, Incbin) or hasattr(each, "to_asm"):
asm = each.to_asm()
current_requested_newlines_before = 2
current_requested_newlines_after = 2
else:
raise Exception, "dunno what to do with("+str(each)+") in Asm.parts"
#calculate how many newlines should be inserted if write_something:
if isinstance(each, AsmSection) or isinstance(each, Incbin): if not first:
newlines_before_next_obj_requested = 2 newlines_before = max([current_requested_newlines_before, previous_requested_newlines_after])
elif isinstance(each, AsmLine) and each.line != "": while written_newlines < newlines_before:
newlines_before_next_obj_requested = 1 fh.write("\n")
written_newlines += 1
else: else:
newlines_before_next_obj_requested = 2 first = False
elif isinstance(each, str):
asm = each
newlines_before_next_obj_requested = 1
else:
raise Exception, "dunno what to do with ("+str(each)+") in Asm.parts"
#blank lines are newlines because main.asm was parsed with split("\n")
#and split("\n") doesn't leave \n characters at the end of each line
if asm == "" and newlines_before_next_obj_given < newlines_before_next_obj_requested:
fh.write("\n")
newlines_before_next_obj_given += 1
else:
fh.write(asm) fh.write(asm)
written_newlines = 0
#write the requested newlines if it hasn't happened yet last = each
if len(asm) > 0 and asm[-1] != "\n":
while newlines_before_next_obj_given < newlines_before_next_obj_requested:
fh.write("\n")
newlines_before_next_obj_given += 1
newlines_before_next_obj_given = 0
newlines_before_next_obj_requested = 0
elif len(asm) > 1 and asm[-1] == "\n":
raise Exception, "last character is newline in asm, but asm has other content?"
def index(seq, f): def index(seq, f):
"""return the index of the first item in seq """return the index of the first item in seq