def get_block(self): start = None end = None for xx, ll in enumerate(self.text): if not start: if util.real_strip(ll) == '<$': start = xx if not end: if util.real_strip(ll) == '$>': end = xx if start: return start, self.text[start + 1:end] else: return None, None
def add_line(self, line): self.lines_consumed += 1 if util.real_strip(line) == 'if': self.nested_ifs += 1 if util.real_strip(line) == 'fi': if self.nested_ifs == 0: return False if self.state == 'if': self.state = 'top' return True if self.state == 'top': if util.real_strip(line) == 'else': if self.nested_ifs == 0: self.state = 'bottom' return True self.top.append(util.real_strip(line)) if util.real_strip(line) == 'fi': self.nested_ifs -= 1 return True if self.state == 'bottom': self.bottom.append(util.real_strip(line)) if util.real_strip(line) == 'fi': self.nested_ifs -= 1 return True if util.real_strip(line) == 'fi': self.nested_ifs -= 1 return True
def parse(self): sre = re.match(r'(.*)\((.*)\)', util.real_strip(self.call)) fn_name = sre.group(1) args = sre.group(2) if args == "": args = "0" return ['LDF @' + fn_name + '@', 'AP ' + args]
def loadMacros(self): if not self.macrosFile: text = "" else: with open(self.macrosFile, 'r') as ff: text = ff.readlines() macro = [] for ll in text: mm = re.match(r'^DEFINE (.*)$', ll) if mm: if len(macro) > 0: self.macros.append(macro) macro = [util.real_strip(mm.group(1))] else: stripped = util.real_strip(ll) if stripped != '': macro.append(stripped) if len(macro) > 0: self.macros.append(macro)
def replace(self, name, lines): offset = 0 fn_addr = -1 for xx, ll in enumerate(lines): if ll == '@' + name + '@': fn_addr = xx - offset if util.real_strip(ll) == '' or re.match(r'^@.*', ll): offset += 1 lines.remove('@' + name + '@') for xx, ll in enumerate(lines): lines[xx] = ll.replace('@' + name + '@', str(fn_addr)) return lines
def main(): args = parse() # strip comments #text = Commenter(load(args.file)).comments_removed() text = load(args.file) if args.macrosFile: text = Macroer(text, args.macrosFile).replaceMacros() else: text = Macroer(text, None).replaceMacros() text2 = [] for ll in text: if util.real_strip(ll) != '': text2.append(util.real_strip(ll)) text = text2 text = Linenamer(text).linesnames_changed() #text = util.filter_lines(text) # replace if statements iffer = Iffer(text) ifs = 0 iffer.parse() text = iffer.lines # replace functions functioneer = Functioneer(text) functioneer.get_functions() outer = Outputer(functioneer, iffer) print outer.out()
def linesnames_changed(self): out = [] for ll in self.lines: if ll[0:8] == 'LINENAME': ll = util.real_strip(ll) mm = re.match(r'^LINENAME (.*)', ll) self.changed.append(mm.group(1)) out.append(mm.group(1)) # this gets replaced by @ + name + @ else: out.append(ll) self.lines = out self.changed.sort(key=len, reverse=True) for nn in self.changed: self.replace_all(nn) nn2 = nn.replace('(', '___').replace(')', '---') if nn2 != nn: for ll in self.lines: ll.replace(nn, nn2) return self.lines
def is_fn_call(self, line): line = util.real_strip(line) return re.match(r'(.*)\((.*)\)', line) is not None