def main(): text = "" src_file_path = "" # Try to find the file to run for i in range(len(sys.argv)): if i != 0 and os.path.isfile(sys.argv[i]): src_file_path = sys.argv[i] text = open(src_file_path).read() if src_file_path == "": raise Exception("No MPL source code file given") lexer = Lexer(text) parser = Parser(lexer) try: syntax_tree = parser.parse() scope = {} funcs = {"readln": "(<built-in function readln>)"} code_generator = CodeGenerator(syntax_tree, scope, funcs) assembly = code_generator.compile() if "--dump" in sys.argv: print(f"Variables: {scope}") print(f"Functions: {funcs}") if "--tree" in sys.argv: print(syntax_tree) ofpath = src_file_path.replace(".mpl", ".tmx") assembly.assemble(of=ofpath, preserve_asm=True) print(f"Compiled {src_file_path}") except KeyError as k: print(f"Error: Nonexistent variable {k} referenced") except Exception as e: print(e)
def main(): parser = argparse.ArgumentParser(description='Generates SQLAlchemy model code from an existing database.') parser.add_argument('url', nargs='?', help='SQLAlchemy url to the database') parser.add_argument('--version', action='store_true', help="print the version number and exit") parser.add_argument('--schema', help='load tables from an alternate schema') parser.add_argument('--tables', help='tables to process (comma-separated, default: all)') parser.add_argument('--noviews', action='store_true', help="ignore views") parser.add_argument('--noindexes', action='store_true', help='ignore indexes') parser.add_argument('--noconstraints', action='store_true', help='ignore constraints') parser.add_argument('--nojoined', action='store_true', help="don't autodetect joined table inheritance") parser.add_argument('--noinflect', action='store_true', help="don't try to convert tables names to singular form") parser.add_argument('--noclasses', action='store_true', help="don't generate classes, only tables") parser.add_argument('--nocomments', action='store_true', help="don't add comments to columns") parser.add_argument('--outfile', help='file to write output to (default: stdout)') fix_args = None args = parser.parse_args(fix_args) if not args.url: print('You must supply a url\n', file=sys.stderr) parser.print_help() return engine = create_engine(args.url) metadata = MetaData(engine) tables = args.tables.split(',') if args.tables else None metadata.reflect(engine, args.schema, not args.noviews, tables) outfile = codecs.open(args.outfile, 'w', encoding='utf-8') if args.outfile else sys.stdout generator = CodeGenerator(metadata, args.noindexes, args.noconstraints, args.nojoined, args.noinflect, args.noclasses) db_comments_map = {} if args.nocomments else get_db_doc(engine, metadata) generator.render(outfile, db_comments_map)
def main(): parser = argparse.ArgumentParser() parser.add_argument('input_file', nargs='+', help='Input files') parser.add_argument('-o', '--output-file', default='-', help='Output path') args = parser.parse_args() with utils.smart_open(args.output_file, 'w') as output_file: for input_path in args.input_file: with utils.smart_open(input_path, 'r') as input_file: parser = Parser(input_file) stmts = parser.parse() code_gen = CodeGenerator('quad') code_gen.gen(stmts)
def __init__(self, model, **kwargs): self.model = model self.variables = [] self.old_signature, self.new_signature, self.kwargs = self.process_signature() self.ode_src = StringIO() self.define_src = StringIO() self.declaration_src = StringIO() CodeGenerator.__init__(self, model.ode.func_code, newline=';\n', offset=4, ostream=self.ode_src, **kwargs) self.tpl = Template(cuda_src_template)
def on_build_item (self, event): if self.diagram.filename is not None: log.info("Building code to %s" % (self.diagram.filename + '.occ')) codegen = CodeGenerator(self.diagram.network) codegen.to_file(self.diagram.filename + '.occ') else: log.info("Can't build code - diagram not saved")
def setUp(self): self.msg = "10001100" * 10 self.coded_msg = None code_gen = CodeGenerator(8, 0.2, 0.01) with open("tests/resources/code.json", "w") as outp: presenter = JsonPresenter() presenter.write(outp, code_gen)
def generate(): """Example run.""" sys.path.append("./app") from codegen import CodeGenerator from utils import import_from_file for p in Path("./templates").iterdir(): if p.is_dir() and not p.stem.startswith("_"): sys.path.append(f"./templates/{p.stem}") dist_dir = "./tests/dist" configs = import_from_file("template_config", f"./templates/{p.stem}/_sidebar.py").get_configs() configs["setup_timer"] = True configs["test_all"] = True code_gen = CodeGenerator(dist_dir=dist_dir) [*code_gen.render_templates(p.stem, configs)] code_gen.make_and_write(p.stem, Path(dist_dir)) shutil.copy(p / "_test_internal.py", f"{dist_dir}/{p.stem}")
def generate_for_dist_spawn(): sys.path.append("./app") from codegen import CodeGenerator from utils import import_from_file for p in Path("./templates").iterdir(): if p.is_dir() and not p.stem.startswith("_"): sys.path.append(f"./templates/{p.stem}") dist_dir = "./tests/dist/spawn" configs = import_from_file("template_config", f"./templates/{p.stem}/_sidebar.py").get_configs() configs["use_distributed_training"] = True configs["use_distributed_launcher"] = False configs["setup_timer"] = True configs["test_all"] = True configs["nnodes"] = 1 code_gen = CodeGenerator(dist_dir=dist_dir) [*code_gen.render_templates(p.stem, configs)] code_gen.make_and_write(p.stem, Path(dist_dir)) shutil.copy(p / "_test_internal.py", f"{dist_dir}/{p.stem}")
def main(): parser = argparse.ArgumentParser(description='Generates SQLAlchemy model code from an existing database.') parser.add_argument('url', nargs='?', help='SQLAlchemy url to the database') parser.add_argument('--version', action='store_true', help="print the version number and exit") parser.add_argument('--schema', help='load tables from an alternate schema') parser.add_argument('--tables', help='tables to process (comma-separated, default: all)') parser.add_argument('--noviews', action='store_true', help="ignore views") parser.add_argument('--noindexes', action='store_true', help='ignore indexes') parser.add_argument('--noconstraints', action='store_true', help='ignore constraints') parser.add_argument('--nojoined', action='store_true', help="don't autodetect joined table inheritance") parser.add_argument('--noinflect', action='store_true', help="don't try to convert tables names to singular form") parser.add_argument('--noclasses', action='store_true', help="don't generate classes, only tables") parser.add_argument('--outfile', help='file to write output to (default: stdout)') parser.add_argument('--nobackrefs', action='store_true', help="don't include backrefs") parser.add_argument('--flask', action='store_true', help="use Flask-SQLAlchemy columns") parser.add_argument('--ignore-cols', help="Don't check foreign key constraints on specified columns (comma-separated)") parser.add_argument('--clone-table', action='store_true', help="clone table don't have any foreign key") args = parser.parse_args() if args.version: return if not args.url: print('You must supply a url\n', file=sys.stderr) parser.print_help() return engine = create_engine(args.url) import_dialect_specificities(engine) metadata = MetaData(engine) tables = args.tables.split(',') if args.tables else None ignore_cols = args.ignore_cols.split(',') if args.ignore_cols else None metadata.reflect(engine, args.schema, not args.noviews, tables) outfile = codecs.open(args.outfile, 'w', encoding='utf-8') if args.outfile else sys.stdout generator = CodeGenerator(metadata, args.noindexes, args.noconstraints, args.nojoined, args.noinflect, args.nobackrefs, args.flask, ignore_cols, args.noclasses, args.clone_table) generator.render(outfile) print(generator.get_table_schemas()) print(generator.get_table_backref()) print(generator.gen_table_socre())
def main(): inputfile = open(sys.argv[1], 'r').read() symTab, tac = parse(inputfile) #print symTab.table tac.addlineNumbers() print("\n#Displaying 3AC\n") tac.display_code() modify3ACforCodegen(symTab, tac, tac.code) # FB = divideToFunctions(tac.code) regAlloc = varAllocateRegister(symTab, tac) tac.mapOffset() # Map the offsets codeGen = CodeGenerator(symTab, tac, regAlloc) codeGen.setup_all() codeGen.display_code()
def main(): file = sys.argv[1] content = reader(file) #print (content) # Construct the Symbol Table ? SymTab = SymTable() ac3 = ThreeAddrCode(SymTab) ac3.addTo3AC(content) FB = divideToFunctions(ac3.code) #print (FB) regAlloc = varAllocateRegister(SymTab, ac3) # Codegen object codeGen = CodeGenerator(SymTab, ac3, regAlloc, FB) codeGen.setup_all() codeGen.display_code()
print("1. Generate new code") print("2. Encode binary string") print("3. Decode binary string") n = "" while n not in ("1", "2", "3"): n = input() if n == "1": print("Enter code block length:") N = int(input()) print("Enter coding speed:") R = float(input()) print("Enter channel error probability:") p = float(input()) codegen = CodeGenerator(N, R, p) json_presenter = JsonPresenter() path = f"output/code{randint(2**31, 2**32 - 1)}.json" with open(path, "w") as output: json_presenter.write(output, codegen) print(f"Code description can be found in {path}") print(f"Code improves {codegen.get_error_bound} mistakes.") print( f"Probability of mistake appearance is {codegen.get_error_prob}." ) elif n == "2": print("Enter path to code description:") path = input() encoder = None
def main(path, verbose, prettytree): # Given the path of a Alan++ source file to be compiled, generated code will be returned # Gotta include the emoji just because Alan said not to print(colored(f'\n{package} v{ver} 🐍', 'blue', attrs=['bold'])) source_code = getFile(path) # Remove all Comments and replace tab characters source_code = removeComments(source_code) source_code = replaceTabs(source_code) programs = source_code.split('}$') try: program = 0 errors = 0 warnings = 0 # Check if this is the last program if programs[(len(programs) - 1)] is '': programs[(len(programs) - 2)] += '}$' del programs[(len(programs) - 1)] while program <= (len(programs) - 1): code = programs[program] # Add the dollar sign back to the program if program is not (len(programs) - 1): code += '}$' # Begin Lexing(add one to the program count # cuz it I dont want it to start at 0 But the array needs # to be accessed at 0) lex = Lexer(code, verbose, program + 1) if lex.errors is not 0: print( colored( f'Skipping Parse for Program {lex.program}. Lex Failed\n', 'cyan', attrs=['bold'])) program += 1 continue tokens = lex.tokens errors += lex.errors warnings += lex.warnings # Parse the tokens parse = Parser(tokens, verbose, prettytree, program + 1) if parse.errors is not 0: print( colored( f'Skipping CST Output for Program {parse.program}. Parse Failed\n', 'magenta', attrs=['bold'])) program += 1 continue if verbose: print( colored(f'CST for Program {parse.program}.\n', 'magenta', attrs=['bold'])) print(parse.cst) errors += parse.errors warnings += parse.warnings semanticAnalyser = SemanticAnalyser(verbose, prettytree, program + 1, parse.cst) if semanticAnalyser.errors is not 0: print( colored( f'Skipping AST and Symbol Table Output for Program {semanticAnalyser.program}. Semantic Analysis Failed\n', 'white', attrs=['bold'])) program += 1 continue if verbose: print( colored(f'\nAST for Program {program+1}.', 'green', attrs=['bold'])) print(semanticAnalyser.ast) print( colored(f'Symbol Table for Program {program+1}.', 'green', attrs=['bold'])) print(semanticAnalyser.symbol_table) errors += semanticAnalyser.errors warnings += semanticAnalyser.warnings codeGenerator = CodeGenerator(verbose, program + 1, semanticAnalyser.ast, semanticAnalyser.symbol_table) if codeGenerator.errors is not 0: print( colored( f'Skipping Machine Code output for Program {codeGenerator.program}. Code Generatoration Failed\n', 'white', attrs=['bold'])) program += 1 continue print( colored(f'Machine Code for Program {program+1}.', 'blue', attrs=['bold'])) print(codeGenerator) errors += codeGenerator.errors warnings += codeGenerator.warnings print( colored( f'Program {program+1} compiled with {errors} errors and {warnings} warnings.', 'white', attrs=['bold'])) program += 1 except KeyboardInterrupt: print(colored('KeyboardInterrupt', 'red'))
from trajectory import optimal_trajectory from codegen import CodeGenerator variables = list('djavx') result = optimal_trajectory(variables) with open('output.py', 'w') as f: cg = CodeGenerator(*result) cg.write(f)
import sys import logging from lexical import lexer from syntax import parser from codegen import CodeGenerator logging.basicConfig(level=logging.DEBUG, filename='parse.log', filemode='w', format='%(filename)10s:%(lineno)4d:%(message)s') log = logging.getLogger() if __name__ == '__main__': filename = sys.argv[1] s = open(filename).read() root = parser.parse(s, lexer=lexer, debug=log) cg = CodeGenerator(root) print(cg.generateProgram())
def _post_output(self): self.newline = ';\n' CodeGenerator._post_output(self)
import ply.lex as lex import ply.yacc as yacc import llvmcodes from codegen import CodeGenerator from decls import Factor, Fundecl from symtab import Scope, SymbolTable optimization = { "constant_folding": False, "remove_deadcode": False } symtab = SymbolTable() codegen = CodeGenerator(optimization) main_fundecl = Fundecl(name='main', optimize_deadcode=optimization["remove_deadcode"]) # トークンの定義 tokens = ( 'IDENT', 'NUMBER', 'BEGIN', 'DIV', 'DO', 'ELSE', 'END', 'FOR', 'FORWARD', 'FUNCTION', 'IF', 'PROCEDURE', 'PROGRAM', 'READ', 'THEN', 'TO', 'VAR', 'WHILE', 'WRITE', 'PLUS', 'MINUS', 'MULT', 'EQ', 'NEQ', 'LE', 'LT', 'GE', 'GT', 'LPAREN', 'RPAREN', 'LBRACKET', 'RBRACKET', 'COMMA', 'SEMICOLON', 'COLON', 'INTERVAL', 'PERIOD', 'ASSIGN' ) reserved = { 'begin': 'BEGIN', 'div': 'DIV', 'do': 'DO',
def print_out(path,data): with open(path,'w') as f: f.write(data) file_name = sys.argv[1] with open(file_name,'r') as f: code = f.read() errors = False scanner = Scanner() parser = Parser() symbol_table = SymbolTable() semantic_analyzer = SemanticAnalyzer() unvectorizer = Unvectorizer() code_generator = CodeGenerator() tokens = scanner.scan(code) #print_out(file_name+'.scan.temp','\n'.join([t.as_string() for t in tokens])) parse_tree = parser.parse(tokens) errors = errors or parser.errors() #print_out(file_name+'.parse.temp',parse_tree.printable_string()) if not errors: symbol_table.populate(parse_tree) errors = errors or symbol_table.errors() #print_out(file_name+'.symbols.temp',symbol_table.printable_string()) if not errors: semantic_analyzer.analyze(parse_tree,symbol_table) errors = errors or semantic_analyzer.errors() #print_out(file_name+'.operations.temp',semantic_analyzer.printable_string()) if not errors:
#!/usr/bin/env python3 import sys from lexer import Lexer from parser import Parser from codegen import CodeGenerator with open("start.s", 'r') as rt: print(rt.read()) with open(sys.argv[1]) as f: lexer = Lexer(f) parser = Parser(lexer) parsed = parser.parse() codegen = CodeGenerator() for node in parsed: codegen.codegen(node)