def __init__(self, filename, binds=False): source = self.read(filename) self.macros = {} self.expression_types = PageTemplate.expression_types.copy() self.expression_types["prambanan"] = PrambananExpr self.default_expression = "prambanan" default_marker = Builtin("False") try: program = BindingProgram( source, "xml", filename, escape=True, default_marker=default_marker, binds=binds, ) module = Module("initialize", program) compiler = Compiler(self.engine, module, self._builtins(), strict=False, source=source, filename=filename) self.code = compiler.code except TemplateError as e: raise
def test_getBalance(self): micheline = Compiler(source).compile_contract() vm = VM() vm.load_contract(micheline) storage = vm.contract.storage.dummy() storage['owner'] = vm.context.sender investor = "KT1EwUrkbmGxjiRvmEAa8HLGhjJeRocqVTFi" amount = 10 initial_storage = { "owner": vm.context.sender, "total_supply": amount, "tokens": { investor: amount }, "allowances": {} } res = vm.contract.getBalance({ "owner": investor, "contract_1": None }).callback_view(storage=initial_storage) self.assertEqual(res, 10) res = vm.contract.getBalance({ "owner": vm.context.sender, "contract_1": None }).callback_view() self.assertEqual(res, 0)
def on_fs(self, instance, value): self.shaderfn = value self.time_last = os.path.getmtime(self.shaderfn) self.utime_last = os.path.getmtime('utility.glf') try: compiler = Compiler( file(value, 'r').read().decode('utf-8'), False, False) except: print 'Shaderforth failure' import traceback traceback.print_exc() return output = compiler.outcode self.build_fbos(compiler) for ep, code in output.items(): print '// !%s!' % ep print code value = header + output['main'] # set the fragment shader to our source code shader = self.canvas.shader old_value = shader.fs shader.fs = value if not shader.success: shader.fs = old_value print 'Shader compilation failed (GLSL)'
def load_scripts(self): log("Reading scripts...") for script in ['wm0.ev', 'wm2.ev', 'wm3.ev']: directory = self.directory + '/' + script functions = [] if not isdir(directory): error("Script directory not found: " + directory) exit(1) files = [] for prefix in ['system', 'model', 'mesh']: for r, d, f in walk(directory): for file in f: if prefix not in file: continue files.append(file) files.sort() offset = 1 for filename in files: with open(directory + '/' + filename) as file: compiler = Compiler(file, offset) code = compiler.compile() offset += int(len(code) / 2) functions.append((filename, code)) self.scripts.append((script, functions))
def process(src,filename=None): from parser import Parser from compiler import Compiler parser = Parser(src,filename=filename) block = parser.parse() compiler = Compiler(block) return compiler.compile()
def test_compiler(self): for i, file in enumerate(os.listdir("tests_bad")): with self.subTest(i=i): compiler = Compiler("tests_bad/{}".format(file), "out.mr") compiler.read_from_file() compiler.compile() self.assertFalse(compiler.write_to_file())
def __init__(self): self.str_res = json.load( open(os.path.join("string_res", "cz.json"), "r", encoding="utf-8")) self.main = tk.Tk() self.main.title(self.str_res["window_title"]) self.main.iconbitmap(os.path.join("img", "karel.ico")) menubar = tk.Menu(self.main) menubar.add_command(label=self.str_res["menu_choose_level"], command=self.choose_level) self.main.config(menu=menubar) self.edit_code = tk.Text(self.main, width=50) self.canvas = tk.Canvas(self.main, background="white") self.canvas.grid(row=0, column=3, rowspan=2) self.karel = robot(self) # self.choose_level() self.load(os.path.abspath("levels/basic.map")) self.edit_code.grid(row=0, column=0, columnspan=3) self.edit_code.insert("end", self.str_res["edit_code_messagge"]) self.run_button = tk.Button(self.main, text=self.str_res["run_button_label"], command=self.run) self.run_button.grid(row=1, column=1, sticky="we") self.comp = Compiler(self.karel, self.main) self.reset_button = tk.Button(self.main, text=self.str_res["reset_button_label"], command=self.karel.reset_place) self.reset_button.grid(row=1, column=0, sticky="we") self.stop_button = tk.Button(self.main, text=self.str_res["stop_button_label"], command=self.comp.stop) self.stop_button.grid(row=1, column=2, sticky="we")
def __init__(self, file_name): self.proof_steps_budget = 10000 self.max_eval_steps = 1000 self.prover_cache = {} self.compiler = Compiler() self.compiler.compile_file(file_name) self.compiler.finalize() self.syntax_predicate_name = self.compiler.rules_db.constants["term"] \ if "term" in self.compiler.rules_db.constants else None self.value_predicate_name = self.compiler.rules_db.constants["value"] \ if "value" in self.compiler.rules_db.constants else None self.eval_predicate_name = self.compiler.rules_db.constants["eval"] \ if "eval" in self.compiler.rules_db.constants else None print("%d rules loaded: %s" % (len(self.compiler.rules_db.rules_by_name), ", ".join( rn.name for rn in self.compiler.rules_db.rules_by_name.keys()))) print("%d defs loaded: %s" % (len(self.compiler.rules_db.definitions), ", ".join( rn.name for rn in self.compiler.rules_db.definitions.keys()))) print("%d outputs loaded: %s" % (len(self.compiler.expressions_to_print), ", ".join( rn.name for rn in self.compiler.expressions_to_print.keys())))
def test(): from compiler import Compiler box_id = 0 src_name = 'a.c' src_path = src_name # except meta file other are all relative path com = Compiler(cpp_lang_config['compile'], str(box_id)) print com.compile
def __init__(self, filename): self.file_interpreter = Compiler(filename).low_level_file_interpreter compiled = "\n".join(self.file_interpreter.compiled) if compiled[-1] == "\n": compiled = compiled[0:-1] compiled.replace("32221","") compiled = re.sub(r'32221 [0-9]{1,3}', '32221 50', compiled) # this will also trigger read output for X,Y in addr[71] compiled = re.sub(r'-1 9999 [0-9]{1,3};', r'-1 1 71; Writing start',A71 compiled) compiled = re.sub(r'9999 ([0-9]{1,3}) ([0-9]{1,3});', r'A71 \1 \2; Reading first 4 bits of A71 into action', compiled) # OR 9998 A43 44; --> compiled = re.sub(r'OR 9998 (A[0-9]{1,3}) [0-9]{1,3};', r'MLZ -1 \1 71; Writing X,Y into bottom 12 bits of A71 and trashing old A71', compiled) #MLZ -1 9997 44; compiled = re.sub(r'9997 ([0-9]{1,3});', r'A71 \1; Reading A71 for output to teams ', compiled) #AND 9996 4095 44;; --> #AND 9996 4095 71; #compiled = re.sub(r'9996 65520 [0-9]{1,3};', r'A71 65520 71; Clearing action but leaving XY in controller A71', compiled) cnt = 3 final_out = "" for linecnt, ln in enumerate(compiled.split("\n")): if linecnt >= 3 and linecnt <= 36: # holds space for bootloaded array final_out += str(linecnt) + ". MLZ 0 " + str(cnt) + " " + str(cnt) + "; NOP \n" cnt+= 1 else: #cnt += 1 #ln = re.sub(r'^[0-9]{1,3}.', str(cnt) + "." , ln) final_out += ln + "\n" compiled = final_out open("out.asm","w").write(compiled) print(compiled)
def parse_models(self): ''' Parse the c++ reference implementation of the custom instruction. ''' logger.info('Determine if modelpath is a folder or a single file') if os.path.isdir(self._modelpath): # restore the toolchain to its defaults self.restore() logger.info('Traverse over directory') self.treewalk(self._modelpath) else: logger.info('Single file, start parsing') model = Model(self._modelpath) self._models.append(model) # add model for read function self._models.append(Model(read=True)) # add model for write function self._models.append(Model(write=True)) self._exts = Extensions(self._models) self._compiler = Compiler(self._exts, self._regs, self._tcpath) self._gem5 = Gem5(self._exts, self._regs)
def judge(cls, language_config, src, max_cpu_time, max_memory, test_case_id, spj_version=None, spj_config=None, spj_compile_config=None, spj_src=None, output=False): # init compile_config = language_config.get("compile") run_config = language_config["run"] submission_id = uuid.uuid4().hex if spj_version and spj_config: spj_exe_path = os.path.join( SPJ_EXE_DIR, spj_config["exe_name"].format(spj_version=spj_version)) # spj src has not been compiled if not os.path.isfile(spj_exe_path): logger.warning( "%s does not exists, spj src will be recompiled") cls.compile_spj(spj_version=spj_version, src=spj_src, spj_compile_config=spj_compile_config) with InitSubmissionEnv( JUDGER_WORKSPACE_BASE, submission_id=str(submission_id)) as submission_dir: if compile_config: src_path = os.path.join(submission_dir, compile_config["src_name"]) # write source code into file with open(src_path, "w", encoding="utf-8") as f: f.write(src) # compile source code, return exe file path exe_path = Compiler().compile(compile_config=compile_config, src_path=src_path, output_dir=submission_dir) else: exe_path = os.path.join(submission_dir, run_config["exe_name"]) with open(exe_path, "w", encoding="utf-8") as f: f.write(src) judge_client = JudgeClient(run_config=language_config["run"], exe_path=exe_path, max_cpu_time=max_cpu_time, max_memory=max_memory, test_case_id=str(test_case_id), submission_dir=submission_dir, spj_version=spj_version, spj_config=spj_config, output=output) run_result = judge_client.run() return run_result
def __init__(self): self.collection_list = [] self.organized_list = [] self.testbench_list = [] self.project_path = self._get_caller_filepath() self.compiler = Compiler(self.project_path) self.args = self._parse_arguments()
def compile_spj(cls, spj_version, src, spj_compile_config): spj_compile_config["src_name"] = spj_compile_config["src_name"].format( spj_version=spj_version) spj_compile_config["exe_name"] = spj_compile_config["exe_name"].format( spj_version=spj_version) spj_src_path = os.path.join(SPJ_SRC_DIR, spj_compile_config["src_name"]) # if spj source code not found, then write it into file if not os.path.exists(spj_src_path): with open(spj_src_path, "w", encoding="utf-8") as f: f.write(src) os.chown(spj_src_path, COMPILER_USER_UID, 0) os.chmod(spj_src_path, 0o400) try: exe_path = Compiler().compile(compile_config=spj_compile_config, src_path=spj_src_path, output_dir=SPJ_EXE_DIR) os.chown(exe_path, SPJ_USER_UID, 0) os.chmod(exe_path, 0o500) # turn common CompileError into SPJCompileError except CompileError as e: raise SPJCompileError(e.message) return "success"
def compile_spj(cls, spj_version, src, spj_compile_config): # format格式化特殊评判版本,也即是源文件和可执行文件的名字,目标位置在language spj_compile_config["src_name"] = spj_compile_config["src_name"].format( spj_version=spj_version) spj_compile_config["exe_name"] = spj_compile_config["exe_name"].format( spj_version=spj_version) #拼接设置特殊评判的源文件路径,这里是compile,非run spj_src_path = os.path.join(SPJ_SRC_DIR, spj_compile_config["src_name"]) #如果特殊评判源代码不存在,然后将源代码写入到文件里面 #这个设置的是特殊评判的源代码的路径的:spj_src_path if not os.path.exists(spj_src_path): with open(spj_src_path, "w", encoding="utf-8") as f: f.write(src) # 写入之后更改源代码的所有者,使用root才可以,0表示更改的用户组 os.chown(spj_src_path, COMPILER_USER_UID, 0) #更改文件的权限,)0o400表示拥有者具有读写权限 os.chmod(spj_src_path, 0o400) try: #编译传入的特殊评判源文件,并输出编译好的可执行文件到SPJ_EXE_DIR,以供运行使用 exe_path = Compiler().compile(compile_config=spj_compile_config, src_path=spj_src_path, output_dir=SPJ_EXE_DIR) #更改执行文件的用户ID和组ID,所有者对文件的权限为0o500:拥有者具有读写和执行的权利 os.chown(exe_path, SPJ_USER_UID, 0) os.chmod(exe_path, 0o500) # 出错的话就将普通的编译错误转换成特殊评判编译错误 except CompileError as e: raise SPJCompileError(e.message) return "success"
def __init__(self, tcpath, modelpath): self._compiler = Compiler(None, None, tcpath) self._gem5 = Gem5([], None) self._exts = None self._models = [] self._regs = Registers() self._modelpath = modelpath self._tcpath = tcpath
def _GetCompiler(self, context): """The name of the compiler executable is taken from the context variable 'CompilationTest.compiler_path'.""" name = context["CompilationTest.compiler_path"] options = context.GetStringList("CompilationTest.compiler_options", []) ldflags = context.GetStringList("CompilationTest.compiler_ldflags", []) return Compiler(name, options, ldflags)
def run_prg(self, filename): self.file_interpreter = Compiler(filename).low_level_file_interpreter compiled = "\n".join(self.file_interpreter.compiled) #print(compiled) interpreter = Interpreter("") interpreter.__init__(compiled) interpreter.run() self.ram = interpreter.ram
def main(file: str): with open(file, 'r') as file_: text = file_.read() compiler = Compiler() scanner = compiler.get_scanner(text) for token in scanner: print(token) compiler.output_messages()
def test_transfer(self): micheline = Compiler(source).compile_contract() vm = VM() vm.load_contract(micheline) storage = vm.contract.storage.dummy() storage['owner'] = vm.context.sender storage['tokens'] = {vm.context.sender: 10} investor = "KT1EwUrkbmGxjiRvmEAa8HLGhjJeRocqVTFi" storage = vm.contract.transfer({ "_to": investor, "_from": vm.context.sender, "value": 4 }).interpret(storage=storage, sender=vm.context.sender).storage self.assertEqual(storage['tokens'], { vm.context.sender: 6, investor: 4 }) try: vm.contract.transfer({ "_from": vm.context.sender, "_to": investor, "value": 10 }).interpret(storage=storage, sender=vm.context.sender).storage assert 0 except MichelsonRuntimeError as e: self.assertEqual(e.format_stdout(), "FAILWITH: 'NotEnoughBalance'") try: vm.contract.transfer({ "_from": vm.context.sender, "_to": investor, "value": 10 }).interpret(storage=storage, sender=investor).storage assert 0 except MichelsonRuntimeError as e: self.assertEqual(e.format_stdout(), "FAILWITH: 'NotEnoughAllowance'") storage["allowances"] = {(vm.context.sender, investor): 10} storage = vm.contract.transfer({ "_from": vm.context.sender, "_to": investor, "value": 2 }).interpret(storage=storage, sender=investor).storage assert storage["tokens"][investor] == 6 try: vm.contract.transfer({ "_from": vm.context.sender, "_to": investor, "value": 8 }).interpret(storage=storage, sender=investor).storage assert 0 except MichelsonRuntimeError as e: self.assertEqual(e.format_stdout(), "FAILWITH: 'NotEnoughBalance'")
def compile(self, *args): if self.running: ## self.comp._quit() else: self.comp = Compiler(self, self.projectPath, self.stateEntry, self.compileBtn) self.comp.compile()
def build_modules_info(self, resources_dir, app_bin_dir): self.app_modules = [] (modules, external_child_modules) = bindings.get_all_module_bindings() compiler = Compiler(self.tiapp, resources_dir, self.java, app_bin_dir, os.path.dirname(app_bin_dir)) compiler.compile(compile_bytecode=False, info_message=None) for module in compiler.modules: module_bindings = [] # TODO: we should also detect module properties for method in compiler.module_methods: if method.lower().startswith(module+'.') and '.' not in method: module_bindings.append(method[len(module)+1:]) module_class = None module_apiName = None for m in modules.keys(): if modules[m]['fullAPIName'].lower() == module: module_class = m module_apiName = modules[m]['fullAPIName'] break if module_apiName == None: continue # module wasn't found if '.' not in module: ext_modules = [] if module_class in external_child_modules: for child_module in external_child_modules[module_class]: if child_module['fullAPIName'].lower() in compiler.modules: ext_modules.append(child_module) self.app_modules.append({ 'api_name': module_apiName, 'class_name': module_class, 'bindings': module_bindings, 'external_child_modules': ext_modules }) # discover app modules detector = ModuleDetector(self.project_dir) missing, detected_modules = detector.find_app_modules(self.tiapp, 'android') for missing_module in missing: print '[WARN] Couldn\'t find app module: %s' % missing_module['id'] self.custom_modules = [] for module in detected_modules: if module.jar == None: continue module_jar = zipfile.ZipFile(module.jar) module_bindings = bindings.get_module_bindings(module_jar) if module_bindings is None: continue for module_class in module_bindings['modules'].keys(): module_id = module_bindings['proxies'][module_class]['proxyAttrs']['id'] print '[DEBUG] module_id = %s' % module_id if module_id == module.manifest.moduleid: print '[DEBUG] appending module: %s' % module_class self.custom_modules.append({ 'class_name': module_class, 'manifest': module.manifest })
class Client: # Instantiating SocketIO Client __sio = socketio.AsyncClient() __compiler = Compiler() def __init__(self, PORT): self.PORT = PORT asyncio.run(self.start()) async def start(self): await self.__sio.connect(f'http://localhost:{self.PORT}') await self.__sio.wait() # SocketIO events # -------------------------------------------------------------- @__sio.event def connect(): logger.info( f'Connection to NodeJS established UID: {Client.__sio.sid}') @__sio.event def disconnect(): logger.info(f'Backend disconnected from NodeJS') @__sio.event async def submission(data): # Deconstructing data id, lang, code = data.values() # Send an acknowledge packet await Client.__sio.emit('submissionAck', {'id': id}) p = multiprocessing.Process( name=f'Submission-{id}', target=Client.handleSubmission, args=(id, lang, code)) p.daemon = True p.start() @__sio.event async def getPoolSize(): await Client.__sio.emit('getPoolSize', len(multiprocessing.active_children())) def handleSubmission(id, lang, code): # Maximum time try: with tlim(5): logger.info(f'[PROCESSING] {multiprocessing.current_process().name}') # Get compiler c = Client.__compiler.getCompiler(lang) s = Submission(id, lang, code, c) s.start() except TimeoutException: return finally: pass
def compile_grammar(grammar_txt, grammar_fname, compiled_parser_class_name, package, inline_base): g_parser = HandRolledGrammarParser(grammar_txt, grammar_fname) g_ast, err = g_parser.parse() if err: return None, err g, _ = Analyzer(g_ast).analyze() compiler = Compiler(g, compiled_parser_class_name, package, inline_base) return compiler.walk()
def __init__(self, scanner_type: Callable, text: str, T: Enum, NT: Enum, table: dict, axiom: Enum): self.compiler = Compiler(scanner_type) self.scanner = self.compiler.get_scanner(text) self.text = text self.T = T self.NT = NT self.table = table self.axiom = axiom self.root = NonTerminalNode(self.axiom)
def run_spp(compile_only, filename, godel, input_list, verbose): interpreter = Compiler(filename, verbose) interpreter.save('%s.s' % filename[:-4]) interpreter.save('%s.express.s' % filename[:-4], True) if not compile_only: interpreter.run(input_list) if godel: g = Godel(verbose) g.save('%s.godel' % filename[:-4], interpreter.code) return interpreter
def run_godel(compile_only, filename, input_list, verbose): g = Godel(verbose) g.open(filename) interpreter = Compiler(verbosity=verbose) interpreter.code = g.code interpreter.compile() interpreter.save('%s.s' % filename[:-6]) interpreter.save('%s.express.s' % filename[:-6], True) if not compile_only: interpreter.run(input_list) return interpreter
def judge(self, language_config, src, max_cpu_time, max_memory, test_case_id, spj_version=None, spj_config=None, spj_compile_config=None, spj_src=None, output=False): # init compile_config = language_config.get("compile") run_config = language_config["run"] submission_id = str(uuid.uuid4()) if spj_version: self.compile_spj(spj_version=spj_version, src=spj_src, spj_compile_config=spj_compile_config, test_case_id=test_case_id) with InitSubmissionEnv( JUDGER_WORKSPACE_BASE, submission_id=str(submission_id)) as submission_dir: if compile_config: src_path = os.path.join(submission_dir, compile_config["src_name"]) # write source code into file with open(src_path, "w") as f: f.write(src.encode("utf-8")) # compile source code, return exe file path exe_path = Compiler().compile(compile_config=compile_config, src_path=src_path, output_dir=submission_dir) else: exe_path = os.path.join(submission_dir, run_config["exe_name"]) with open(exe_path, "w") as f: f.write(src.encode("utf-8")) judge_client = JudgeClient(run_config=language_config["run"], exe_path=exe_path, max_cpu_time=max_cpu_time, max_memory=max_memory, test_case_id=str(test_case_id), submission_dir=submission_dir, spj_version=spj_version, spj_config=spj_config, output=output) run_result = judge_client.run() return run_result
def main(file: str): with open(file, 'r') as file_: text = file_.read() compiler = Compiler() scanner = compiler.get_scanner(text) for token in scanner: print( token, compiler.get_name(token.code if hasattr(token, 'code') else None)) compiler.output_messages()
def release(input_file, output_dir): "Compile a cablelang script in release mode" try: mkdir(output_dir) except: pass copy_std_to_dir(output_dir) c = Compiler(input_file, output_dir + '/main.cpp') c.compile() c.write() build_release(output_dir)