|
3 | 3 | import os
|
4 | 4 | import sys
|
5 | 5 | import argparse
|
6 |
| -import logging as log |
| 6 | +import logging |
7 | 7 |
|
8 | 8 | import pyucode as ucode
|
9 | 9 |
|
10 |
| -def parseArguments(): |
11 |
| - """ |
12 |
| - parse all command line arguments to the program. |
13 |
| - """ |
14 |
| - parser = argparse.ArgumentParser(description=__doc__) |
15 |
| - parser.add_argument("program",help="The program to compile.") |
16 |
| - parser.add_argument("--output","-O",help="Output path", default="out.v") |
17 |
| - parser.add_argument("--gendocs","-D", help="Generate documentation", |
18 |
| - action="store_true") |
19 |
| - parser.add_argument("--instrdocs", |
20 |
| - help="Instruction documentation output file path", |
21 |
| - default = "doc-instrs.html") |
22 |
| - parser.add_argument("--progdocs", |
23 |
| - help="Program documentation output file path", |
24 |
| - default = "doc-program.html") |
25 |
| - parser.add_argument("--debug-states", help="Display the current state each\ |
26 |
| - simulation cycle.",action="store_true") |
27 |
| - parser.add_argument("--flowgraph", help="Emit a graph of program control\ |
28 |
| - flow changes.",action="store_true") |
29 |
| - parser.add_argument("--graphpath", help="Path of file created when \ |
30 |
| - --flowgraph is set.",default="flow.dot") |
31 |
| - parser.add_argument("--opt-coalesce", |
32 |
| - help="Enable coalecsing of blocks to improve performance.", |
33 |
| - action="store_true") |
34 |
| - |
35 |
| - args = parser.parse_args() |
36 |
| - return args |
37 |
| - |
38 |
| -def main(): |
39 |
| - """ |
40 |
| - Main entry point for the program |
41 |
| - """ |
42 |
| - args = parseArguments() |
43 |
| - |
44 |
| - print("---------- uCode Compiler ----------") |
45 |
| - print("> Loading sources") |
46 |
| - |
47 |
| - program = ucode.UCProgram() |
48 |
| - program.parseSource(args.program) |
49 |
| - |
50 |
| - print("> Resolving objects") |
51 |
| - |
52 |
| - resolver = ucode.UCResolver() |
53 |
| - resolver.addVariables(program.variables) |
54 |
| - resolver.addInstructions(program.instructions) |
55 |
| - resolver.addProgram(program) |
56 |
| - resolver.enable_coalescing = args.opt_coalesce |
57 |
| - resolver.resolve() |
58 |
| - |
59 |
| - print("> Rendering template to %s" % args.output) |
60 |
| - |
61 |
| - renderer = ucode.UCTemplater(resolver) |
62 |
| - renderer.debug_states = args.debug_states |
63 |
| - renderer.renderTo(args.output) |
64 |
| - |
65 |
| - # Generate per-program documentation |
66 |
| - progdocs = ucode.UCProgramDocgen(resolver) |
67 |
| - |
68 |
| - if(args.gendocs): |
69 |
| - print("> Rendering instruction documentation to %s" % args.instrdocs) |
70 |
| - dg = ucode.UCInstructionDocGen(resolver.instrs) |
71 |
| - dg.renderTo(args.instrdocs) |
| 10 | +class Program(object): |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + """ |
| 14 | + Create a new program object. |
| 15 | + """ |
| 16 | + self.log = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | + def configureLogging(self,args): |
| 20 | + """ |
| 21 | + Setup all of the logging / info message defaults. |
| 22 | + """ |
| 23 | + if(args.verbose): |
| 24 | + self.log.setLevel(logging.INFO) |
| 25 | + logging.basicConfig(level=logging.INFO) |
| 26 | + else: |
| 27 | + self.log.setLevel(logging.WARNING) |
| 28 | + logging.basicConfig(level=logging.WARNING) |
| 29 | + |
| 30 | + |
| 31 | + def parseArguments(self): |
| 32 | + """ |
| 33 | + parse all command line arguments to the program. |
| 34 | + """ |
| 35 | + parser = argparse.ArgumentParser(description=__doc__) |
| 36 | + parser.add_argument("program",help="The program to compile.") |
| 37 | + parser.add_argument("--output","-O",help="Output path", default="out.v") |
| 38 | + parser.add_argument("--gendocs","-D", help="Generate documentation", |
| 39 | + action="store_true") |
| 40 | + parser.add_argument("--instrdocs", |
| 41 | + help="Instruction documentation output file path", |
| 42 | + default = "doc-instrs.html") |
| 43 | + parser.add_argument("--progdocs", |
| 44 | + help="Program documentation output file path", |
| 45 | + default = "doc-program.html") |
| 46 | + parser.add_argument("--debug-states", help="Display the current state each\ |
| 47 | + simulation cycle.",action="store_true") |
| 48 | + parser.add_argument("--flowgraph", help="Emit a graph of program control\ |
| 49 | + flow changes.",action="store_true") |
| 50 | + parser.add_argument("--graphpath", help="Path of file created when \ |
| 51 | + --flowgraph is set.",default="flow.dot") |
| 52 | + parser.add_argument("--opt-coalesce", |
| 53 | + help="Enable coalecsing of blocks to improve performance.", |
| 54 | + action="store_true") |
| 55 | + parser.add_argument("--verbose", "-v", action="store_true", |
| 56 | + help="Be verbose when displaying messages") |
| 57 | + |
| 58 | + args = parser.parse_args() |
| 59 | + return args |
| 60 | + |
| 61 | + def main(self): |
| 62 | + """ |
| 63 | + Main entry point for the program |
| 64 | + """ |
| 65 | + args = self.parseArguments() |
| 66 | + self.configureLogging(args) |
| 67 | + |
| 68 | + self.log.info("---------- uCode Compiler ----------") |
| 69 | + self.log.info("> Loading sources") |
| 70 | + |
| 71 | + program = ucode.UCProgram() |
| 72 | + program.parseSource(args.program) |
72 | 73 |
|
73 |
| - print("> Rendering program documentation to %s" % args.progdocs) |
74 |
| - progdocs.gen_program_docs(args.progdocs) |
75 |
| - |
76 |
| - if(args.flowgraph): |
77 |
| - print("> Writing flow graph to '%s'" % args.graphpath) |
78 |
| - progdocs.gen_flow_dot_graph(args.graphpath) |
79 |
| - |
80 |
| - print("> Done") |
| 74 | + self.log.info("> Resolving objects") |
| 75 | + |
| 76 | + resolver = ucode.UCResolver() |
| 77 | + resolver.addVariables(program.variables) |
| 78 | + resolver.addInstructions(program.instructions) |
| 79 | + resolver.addProgram(program) |
| 80 | + resolver.enable_coalescing = args.opt_coalesce |
| 81 | + resolver.resolve() |
| 82 | + |
| 83 | + self.log.info("> Rendering template to %s" % args.output) |
| 84 | + |
| 85 | + renderer = ucode.UCTemplater(resolver) |
| 86 | + renderer.debug_states = args.debug_states |
| 87 | + renderer.renderTo(args.output) |
| 88 | + |
| 89 | + # Generate per-program documentation |
| 90 | + progdocs = ucode.UCProgramDocgen(resolver) |
| 91 | + |
| 92 | + if(args.gendocs): |
| 93 | + self.log.info("> Rendering instruction documentation to %s" % args.instrdocs) |
| 94 | + dg = ucode.UCInstructionDocGen(resolver.instrs) |
| 95 | + dg.renderTo(args.instrdocs) |
| 96 | + |
| 97 | + self.log.info("> Rendering program documentation to %s" % args.progdocs) |
| 98 | + progdocs.gen_program_docs(args.progdocs) |
| 99 | + |
| 100 | + if(args.flowgraph): |
| 101 | + self.log.info("> Writing flow graph to '%s'" % args.graphpath) |
| 102 | + progdocs.gen_flow_dot_graph(args.graphpath) |
| 103 | + |
| 104 | + self.log.info("> Done") |
| 105 | + return 0 |
| 106 | + |
81 | 107 |
|
82 | 108 | if(__name__ == "__main__"):
|
83 |
| - main() |
| 109 | + p = Program() |
| 110 | + sys.exit(p.main()) |
| 111 | + |
0 commit comments