def run(self, src, kwargs): src_text = StringIO() self._apply_options(src_text, kwargs) src_text.write(src) imports = kwargs.get('imports', {}) imports['<string>'] = src_text.getvalue() result = CodegenTestResult() result.parse_tree = blobc.parse_file( '<string>', handle_imports=True, import_handler=InMemoryImportHandler(imports)) result.tsys = blobc.compile_types(result.parse_tree) out_fh = StringIO() aux_fh = StringIO() gen = self._gen_cls(out_fh, 'input.blob', aux_fh, 'output.h') gen.generate_code(result.parse_tree, result.tsys) result.output = out_fh.getvalue() result.aux_output = aux_fh.getvalue() if not kwargs.get('keep_ws', False): result.output = compress_c(result.output) result.aux_output = compress_c(result.aux_output) return result
parser.add_argument('-I', metavar='<path>', dest='import_paths', action='append', help='Specify import search directories', default=['.']) parser.add_argument('-m', '--merge-imports', dest='merge_imports', action='store_true', help='Merge all imports together and produce stand-alone output suitable for distribution') parser.add_argument('-o', metavar='<file>', dest='output_fn', help='Output source file') parser.add_argument('-a', metavar='<file>', dest='aux_fn', help='Auxiliary output source file') parser.add_argument('-l', metavar='<language>', dest='lang', required=True, choices=languages.keys().sort(), help='Specify language to generate code for (%s)' % (', '.join(languages))) args = parser.parse_args() try: parse_tree = blobc.parse_file(args.input_fn, import_dirs=args.import_paths, handle_imports=True) type_system = blobc.compile_types(parse_tree) fh = sys.stdout aux_fh = None try: if args.output_fn is not None: fh = open(args.output_fn, 'w') if fh and args.aux_fn is not None: aux_fh = open(args.aux_fn, 'w') # create generator gencls = languages[args.lang] gen = gencls(fh, args.input_fn, aux_fh, args.output_fn)