Changeset 804 for proto/Compiler


Ignore:
Timestamp:
Dec 10, 2010, 2:17:21 PM (9 years ago)
Author:
ksherdy
Message:

Refactor template command line argument handling to remove the notion of an implicit base directory.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/Compiler/compiler2.py

    r801 r804  
    88#
    99
    10 import optparse,os
     10import optparse, os
    1111import ast, pablo, string, sys, copy
    1212
    1313class Program:
    14     def __init__(self, out_file, template_name = "template.c"):
     14    def __init__(self, output_file, template_file = "template.c"):
    1515        pass
    16         self.templ_name = template_name
    17         self.outfile_name = out_file
     16        self.template_file = template_file
     17        self.output_file = output_file
    1818
    19     def output(self, decl, stream_stmts , block_stmts, any_carry_expr, templ):
    20         templ = string.replace(templ, '@decl', decl)
    21         templ = string.replace(templ, '@stream_stmts', stream_stmts)
    22         templ = string.replace(templ, '@block_stmts', block_stmts)
    23         templ = string.replace(templ, '@any_carry', any_carry_expr)
    24         return templ
     19    def output(self, decl, stream_stmts , block_stmts, any_carry_expr, template_string):
     20        template_string = string.replace(template_string, '@decl', decl)
     21        template_string = string.replace(template_string, '@stream_stmts', stream_stmts)
     22        template_string = string.replace(template_string, '@block_stmts', block_stmts)
     23        template_string = string.replace(template_string, '@any_carry', any_carry_expr)
     24        return template_string
    2525
    26     def read_template(self, file_path):
    27         f=open(file_path + self.templ_name)
     26    def read_template(self):
     27        f=open(self.template_file)
    2828        inp = ""
    2929        for line in f:
     
    3333
    3434    def write_final_code(self, code):
    35         f=open(self.outfile_name, 'w')
     35        f=open(self.output_file, 'w')
    3636        f.write(code)
    3737        f.close()
    3838
    39     def generate_code(self, s, file_path):
     39    def generate_code(self, input_string):
    4040        # FRONT END
    41         s = ast.parse(s)
     41        s = ast.parse(input_string)
    4242        # Analysis and Transformation
    4343        xfrmr = pablo.MainLoopTransformer(s, "carryQ")
     
    4747        xfrmr.add_loop_carryQ_adjust()
    4848        # BACK END
    49         templ = self.read_template(file_path)
    50         templ = self.output(xfrmr.getCdecls(),
     49        template_contents = self.read_template()
     50        template_contents = self.output(xfrmr.getCdecls(),
    5151                            xfrmr.getCinits(),
    5252                            xfrmr.getCstmts(),
    5353                            xfrmr.any_carry_expr(),
    54                             templ)
    55         self.write_final_code(templ)
     54                            template_contents)
     55        self.write_final_code(template_contents)
    5656        return s
    5757
     
    9090          parser.print_usage()
    9191
    92         input_longpath = args[0]
    93         input_dirname =  os.path.dirname(input_longpath) + os.path.sep
    94         input_content = readfile(input_longpath)
     92        input_file = args[0]
     93        input_string = readfile(input_file)
    9594
    9695        # Optional arguments
     
    9897        template_filename = options.template_filename
    9998
    100         s = Program(output_filename, template_filename).generate_code(input_content, input_dirname)
     99        s = Program(output_filename, template_filename).generate_code(input_string)
Note: See TracChangeset for help on using the changeset viewer.