From 45f82a1acf465c8420690a578192cdabf4bc7c04 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Fri, 2 Dec 2011 15:06:40 +0000 Subject: Add WIP for supporting existing asm files --- pp2cc.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'pp2cc.py') diff --git a/pp2cc.py b/pp2cc.py index 95809e3..6370613 100755 --- a/pp2cc.py +++ b/pp2cc.py @@ -17,6 +17,7 @@ from Registers import Registers from LinkedNode import LinkedNode from Function import Function from Variables import Variables, GlobalVariables +from AsmParser import AsmParser __author__ = "Peter Wu" __copyright__ = "Copyright 2011, Peter Wu" @@ -91,7 +92,21 @@ class Parse(object): self.registers = Registers() def loadFile(self, filename, use_cpp=True, cpp_args=[]): """Loads a file and parses the contents of it""" - self.node = parse_file(filename, use_cpp=use_cpp, cpp_args=cpp_args); + # reset state + self.asm_node = self.node = None + + ext = os.path.splitext(filename)[1].lower() + if ext: + ext = ext[1:] + if ext == "i": + # C source code which should not be preprocessed + self.node = parse_file(filename, use_cpp=False) + elif ext in ("c", "h"): + self.node = parse_file(filename, use_cpp=use_cpp, cpp_args=cpp_args) + elif ext == "asm": + self.asm_node = AsmParser(filename) + else: + self.logger.error("Unrecognized file extension '{}'".format(ext)) def show(self): self.node.show(showcoord=True) def uniqLbl(self, labelname): @@ -111,6 +126,15 @@ class Parse(object): self.logger.error("Redefinition of label '{}'".format(labelname)) self.labels.add(labelname) def compile(self): + """Processes a loaded file and adds it to the assembly output""" + if self.node is not None: + self.compileAST() + elif self.asm_node is not None: + self.compileASM() + else: + self.logger.error("No AST node nor assembly lines found to be" + " processed") + def compileAST(self): """Loops through the nodes in an AST syntax tree and generates ASM""" root_node = LinkedNode(self.node) variables = GlobalVariables(self.varNames) @@ -123,6 +147,9 @@ class Parse(object): " node, but also found " + str(thing), linked_node=linked_node) self.codeSegment += self.parseStatement(thing, root_node) + def compileASM(self): + """Processes lines of assembly and merge it into the output""" + pass def getSource(self): """Retrieves the ASM source. You need to compile it first""" output = [] -- cgit v1.2.1