def setUp(self):
     self.compiler_state = CompilerState()
     self.enable_debug(False)
     UUID_TICKETS.next_value = 0
     LABEL_TICKETS.next_value = 0
     INT_REGISTER_TICKETS.next_value = 0
     FLOAT_REGISTER_TICKETS.next_value = 0
Beispiel #2
0
def main():
    ##
    #  WRITE YOUR C PROGRAM IN THIS STRING!
    ##
    your_c_program = """
      // your program here!
      int main() {
        int my_3d_array[1][2][3][4];
        return 0;
      }
    """

    compiler_state = CompilerState()
    ast = compiler_state.parse(your_c_program)

    ast.to_3ac(include_source=True)
Beispiel #3
0
 def setUp(self):
     self.compiler_state = CompilerState()
     lexer = JSTLexer(self.compiler_state)
     self.scanner = lex.lex(module=lexer)
Beispiel #4
0
def main():
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument("source",
                            type=str,
                            help="The C program file to compile.")
    arg_parser.add_argument(
        "-o",
        "--outfile",
        type=str,
        default='STDOUT',
        help=
        "The name of the output file. MUST be a .asm file! (Default: STDOUT)")
    arg_parser.add_argument(
        "-sym",
        "--symtable",
        action='store_true',
        help="Enables the printing of symbol table in other options.")
    arg_parser.add_argument(
        "-s",
        "--scandebug",
        type=int,
        choices=[0, 1, 2, 3],
        default=0,
        help=
        "The debug level for the scanner. \n 0: No debug \n 1: Tokens \n 2: Source Code \n "
        "3: Tokens and Source Code")
    arg_parser.add_argument(
        "-p",
        "--parsedebug",
        type=int,
        choices=[0, 1, 2, 3],
        default=0,
        help=
        "The debug level for the parser. \n 0: No debug \n 1: Productions \n "
        " 2: Productions and Source Code \n 3: Productions, Source, Misc info")
    arg_parser.add_argument(
        "-ast",
        "--astree",
        action='store_true',
        help="Enables the printing of the GraphViz string after parsing.")
    arg_parser.add_argument(
        "-tac",
        "--threeac",
        type=int,
        choices=[0, 1, 2],
        default=0,
        help="The debug level for the 3AC. \n 0: No debug \n 1: 3AC \n "
        " 2: 3AC + Source")
    arg_parser.add_argument(
        "-mips",
        "--mips",
        type=int,
        choices=[0, 1, 2, 3],
        default=0,
        help="The debug level for the MIPS. \n 0: No debug \n 1: 3AC \n "
        " 2: Source \n 3: 3AC + Source")
    arg_parser.add_argument("-w",
                            "--warnings",
                            action='store_true',
                            help="Enables warnings being printed.")

    args = vars(arg_parser.parse_args())

    # Set Symbol Table flags
    print_table = args['symtable']

    # Set Scanner flags
    print_tokens, print_source_scanner = False, False
    if args['scandebug'] is 1:
        print_tokens = True
    elif args['scandebug'] is 2:
        print_source_scanner = True
    elif args['scandebug'] is 3:
        print_tokens = True
        print_source_scanner = True

    # Set Parser flags
    print_productions, print_source_parser, print_info = False, False, False
    if args['parsedebug'] is 1:
        print_productions = True
    elif args['parsedebug'] is 2:
        print_productions = True
        print_source_parser = True
    elif args['parsedebug'] is 3:
        print_productions = True
        print_source_parser = True
        print_info = True

    source_file = open(args['source'], 'r')
    data = source_file.read()
    compiler_state = CompilerState(print_table=print_table,
                                   print_tokens=print_tokens,
                                   print_source_scanner=print_source_scanner,
                                   print_productions=print_productions,
                                   print_source_parser=print_source_parser,
                                   print_info=print_info,
                                   print_warnings=args['warnings'])

    try:
        ast = compiler_state.parse(data)
        if args['astree']:
            print(ast.to_graph_viz_str())

        if args['threeac'] is 2:
            source_tac, tac_as_str = ast.to_3ac(include_source=True)
        else:
            source_tac, tac_as_str = ast.to_3ac()

        if args['mips'] == 1:
            generator = generation.MipsGenerator(compiler_state,
                                                 inject_source=False,
                                                 inject_3ac=True)
        elif args['mips'] == 2:
            generator = generation.MipsGenerator(compiler_state,
                                                 inject_source=True,
                                                 inject_3ac=False)
        elif args['mips'] == 3:
            generator = generation.MipsGenerator(compiler_state,
                                                 inject_source=True,
                                                 inject_3ac=True)
        else:
            generator = generation.MipsGenerator(compiler_state,
                                                 inject_source=False,
                                                 inject_3ac=False)

        generator.load(source_tac)
        generator.translate_tac_to_mips()

        if args['outfile'] != 'STDOUT':
            fout = open(args['outfile'], 'w')
            fout.write(generator.dumps())
            fout.close()
        else:
            print(generator.dumps())

    except CompileError as error:
        print(error)
    finally:
        compiler_state.teardown()
 def setUp(self):
     self.compiler_state = CompilerState(print_productions=False)
     self.enable_debug(False)
Beispiel #6
0
 def setUp(self):
     self.debug = True
     self.compiler_state = CompilerState()
Beispiel #7
0
    def setUp(self):
        self.compiler_state = CompilerState(print_productions=False)
        self.enable_debug(False)

        self.generator = generation.MipsGenerator(self.compiler_state, inject_source = True, inject_3ac=True)