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):
asm = each.to_asm()
else: #like: MapHeader, SecondMapHeader, MapEventHeader, MapScriptHeader, ..
asm = to_asm(each)
#calculate how many newlines should be inserted write_something = True
if isinstance(each, AsmSection) or isinstance(each, Incbin): if (isinstance(each, str) and each == "") or (isinstance(each, AsmLine) and each.line == ""):
newlines_before_next_obj_requested = 2 current_requested_newlines_before = 0
elif isinstance(each, AsmLine) and each.line != "": if current_requested_newlines_after < 2:
newlines_before_next_obj_requested = 1 current_requested_newlines_after += 1
else: write_something = False
newlines_before_next_obj_requested = 2 elif (isinstance(each, str) and each != "") or (isinstance(each, AsmLine) and each.line != ""):
if isinstance(each, AsmLine):
asm = each.to_asm()
elif isinstance(each, str): elif isinstance(each, str):
asm = each asm = each
newlines_before_next_obj_requested = 1 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: else:
raise Exception, "dunno what to do with("+str(each)+") in Asm.parts" raise Exception, "dunno what to do with("+str(each)+") in Asm.parts"
#blank lines are newlines because main.asm was parsed with split("\n") if write_something:
#and split("\n") doesn't leave \n characters at the end of each line if not first:
if asm == "" and newlines_before_next_obj_given < newlines_before_next_obj_requested: newlines_before = max([current_requested_newlines_before, previous_requested_newlines_after])
while written_newlines < newlines_before:
fh.write("\n") fh.write("\n")
newlines_before_next_obj_given += 1 written_newlines += 1
else: else:
first = False
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