def show(s, reduced=True, dot_path=None, pdf_path=None, fmt='pretty'): """Generates a graphviz diagram of the AST for the given path. Since this is mostly debug functionality, there are also options to print various significant values. :param s: The regular expression to parse :param reduced: If True, includes only productions used for generating code in the diagram. If False, includes all productions in the diagram. Defaults to True. :param dot_path: Where to write dot file, if any :param pdf_path: Where to write PDF file. Requires dot_file to also be set. :param format: 'pretty' or 'json' """ ALLOWED_FORMATS = ('pretty', 'json') if fmt not in ALLOWED_FORMATS: raise ValueError('fmt must be one of %r' % ALLOWED_FORMATS) log.info('dot path: %s' % dot_path) if fmt == 'pretty': log.info('Tokens:') lexer.lexer.input(s) for tok in iter(lexer.lexer.token, None): log.info('%r %r' % (tok.type, tok.value)) root = parser.parse(s, (not reduced)) if dot_path: visualize.ast_dot(root, dot_path) log.info("Graphviz written to %s" % dot_path) if pdf_path: pdf_path = os.path.abspath(pdf_path) try: p = subprocess.Popen( ["dot", "-Tpdf", dot_path, "-o", pdf_path], stderr=subprocess.PIPE, stdout=subprocess.PIPE) _, stderr = p.communicate() if p.returncode != 0: raise OSError(stderr) log.info("PDF written to %s" % pdf_path) except OSError: log.error("PDF could not be written. Graphviz does not appear" " to be installed or some other error occurred.") instr_list = root.generate_instructions() instr_list.append(instructions.Instruction('match')) # Print instructions after the AST is drawn in case instruction printing # fails program = instructions.serialize(instr_list) if fmt == 'json': program = [(opcode_to_cmd[inst[0]].upper(), inst[1], inst[2]) for inst in program] json.dump(program, sys.stdout) elif fmt == 'pretty': log.info("Instructions for VM:") instructions.prettyprint_program(program)
def parse(s): """ Converts a regular expression into bytecode for the VM :param s: A regular expression :return: A list of opcode tuples in the form `[(opcode, arg1, arg2)]` """ instr_list = parser.parse(s).generate_instructions() instr_list.append(instructions.Instruction('match')) return instructions.serialize(instr_list)
def test_text_init(self): """Test Instruction creation from text""" # 1. Create Instruction object from text myinst = instructions.Instruction(text="b inc 5 if a > 1") # 2. Make sure it has the specified values self.assertEqual(myinst.actreg, 'b') self.assertEqual(myinst.action, 'inc') self.assertEqual(myinst.actval, 5) self.assertEqual(myinst.cmpreg, 'a') self.assertEqual(myinst.cmpopr, '>') self.assertEqual(myinst.cmpval, 1)
def test_empty_init(self): """Test default Instruction creation""" # 1. Create default Instruction object myinst = instructions.Instruction() # 2. Make sure it has the default values self.assertEqual(myinst.actreg, None) self.assertEqual(myinst.action, 'inc') self.assertEqual(myinst.actval, 0) self.assertEqual(myinst.cmpreg, None) self.assertEqual(myinst.cmpopr, '==') self.assertEqual(myinst.cmpval, 0)
def perform(self, ops, inputs=False): outputs = [] tempVars = [] for i in range(ops.numOutputs): outputs.append(self.nextVariable()) for i in range(ops.numTempvars): tempVars.append(self.nextVariable()) if outputs == []: outputs = False if tempVars == []: tempVars = False inst = instructions.Instruction(ops, inputs, outputs, tempVars) self.instructionAppend(inst) return inst
def test_value_init(self): """Test Instruction creation with values""" # 1. Create Memory object with values # myinst = instructions.Instruction(actreg='b', action='inc', actval=5, cmpreg='a', cmpopr='>', cmpval=1) # 2. Make sure it has the specified values self.assertEqual(myinst.actreg, 'b') self.assertEqual(myinst.action, 'inc') self.assertEqual(myinst.actval, 5) self.assertEqual(myinst.cmpreg, 'a') self.assertEqual(myinst.cmpopr, '>') self.assertEqual(myinst.cmpval, 1)
def addAuxilaryInst(self, addr, operandName='') -> None: if addr not in self.addr2InstAux: self.addr2InstAux[addr] = isn.Instruction(addr, operand=operandName) self.addr2InstAux[addr].start = True self.addr2InstAux[addr].fallThrough = False
'0': clear_or_return, '1': jump_to_location, '2': gosub, '3': skip_if_equal, '4': skip_if_not_equal, '5': skip_if_register, '6': set_register_to_value, '7': add_to_register, '8': set_register_to_register, '9': skip_if_not_register, 'a': setI, 'b': jump_to_location_v0, 'c': set_register_to_random, 'd': display_n_at_x_y, 'e': skip_if_key, 'f': set_timer_or_load } check_60hz() running = True while (running): if (cpu_reg.PC >= main_memory.memory_size - 1): running = False else: codes = instructions.Instruction([main_memory[cpu_reg.PC], main_memory[cpu_reg.PC + 1]]) if (codes.op in operations): operations[codes.op](codes) else: print('***** OP NOT FOUND: ' + str(codes.op)) # time.sleep(0.01)
5, ' ') + "\t" * indentLevel + str(ins) + "\n" if ins.isBlockBegin(): indentLevel += 1 return string if __name__ == '__main__': import opcodes import operation import variable import instructions import typesData Program([ instructions.Instruction(operation.Nop()), instructions.Instruction(operation.LoadInteger(1), False, [variable.Variable(12)]) ]) Program([ instructions.Instruction(operation.LoadInteger(1), False, [variable.Variable(0)]), instructions.Instruction(operation.LoadInteger(9), False, [variable.Variable(3)]), instructions.Instruction(operation.LoadString("thisisastring"), False, [variable.Variable(1)]), instructions.Instruction(operation.LoadInteger(True), False, [variable.Variable(4)]), instructions.Instruction( operation.BeginWhile(">"),
def main(): prog = program.Program([ instructions.Instruction(operation.LoadInteger(1), False, [variable.Variable(0)]), instructions.Instruction(operation.LoadInteger(9), False, [variable.Variable(3)]), instructions.Instruction(operation.LoadString("thisisastring"), False, [variable.Variable(1)]), instructions.Instruction(operation.LoadInteger(True), False, [variable.Variable(4)]), instructions.Instruction( operation.BeginWhile(">"), [variable.Variable(0), variable.Variable(3)]), instructions.Instruction(operation.LoadInteger(1337), False, [variable.Variable(6)]), instructions.Instruction(operation.BeginIf(), [variable.Variable(2)]), instructions.Instruction(operation.LoadString("thisisastring"), False, [variable.Variable(9)]), instructions.Instruction(operation.EndIf()), instructions.Instruction( operation.BinaryOperation(">"), [variable.Variable(0), variable.Variable(3)], [variable.Variable(5)]), instructions.Instruction(operation.EndWhile()), instructions.Instruction(operation.LoadInteger(10), False, [variable.Variable(31337)]), ]) prog = program.Program([ instructions.Instruction(operation.LoadInteger(0), False, [variable.Variable(0)]), instructions.Instruction(operation.LoadInteger(1), False, [variable.Variable(1)]), instructions.Instruction(operation.LoadInteger(10), False, [variable.Variable(2)]), instructions.Instruction(operation.LoadInteger(10), False, [variable.Variable(3)]), instructions.Instruction(operation.LoadInteger(20), False, [variable.Variable(4)]), instructions.Instruction(operation.LoadInteger(5), False, [variable.Variable(5)]), instructions.Instruction(operation.Phi(), [variable.Variable(3)], [variable.Variable(6)]), instructions.Instruction(operation.Phi(), [variable.Variable(4)], [variable.Variable(7)]), instructions.Instruction(operation.BeginFor("++", "<"), [ variable.Variable(0), variable.Variable(2), variable.Variable(8) ]), instructions.Instruction( operation.BinaryOperation("<"), [variable.Variable(8), variable.Variable(5)], [variable.Variable(6)]), instructions.Instruction(operation.BeginIf(), [variable.Variable(6)], False), instructions.Instruction(operation.LoadInteger(0), False, [variable.Variable(9)]), instructions.Instruction( operation.BeginWhile("<"), [variable.Variable(9), variable.Variable(5)], False), instructions.Instruction( operation.BinaryOperation("+"), [variable.Variable(6), variable.Variable(1)], [variable.Variable(10)]), instructions.Instruction( operation.Copy(), [variable.Variable(6), variable.Variable(10)]), instructions.Instruction( operation.BinaryOperation("+"), [variable.Variable(9), variable.Variable(1)], [variable.Variable(11)]), instructions.Instruction( operation.Copy(), [variable.Variable(6), variable.Variable(10)]), instructions.Instruction( operation.BeginFunction( typesData.FunctionSignature(2, [variable.Variable(11)])), False, [variable.Variable(1)], [variable.Variable(10), variable.Variable(11)]), instructions.Instruction( operation.BinaryOperation("+"), [variable.Variable(10), variable.Variable(11)], [variable.Variable(2)]), instructions.Instruction(operation.LoadString("somestring"), False, [variable.Variable(3)]), instructions.Instruction(operation.Return(), [variable.Variable(2)]), instructions.Instruction(operation.EndFunction()), instructions.Instruction(operation.LoadInteger(1337), False, [variable.Variable(4)]), instructions.Instruction(operation.CallFunction(2), [ variable.Variable(1), variable.Variable(4), variable.Variable(0) ], [variable.Variable(6)]), instructions.Instruction(operation.EndWhile(), False, False), instructions.Instruction(operation.BeginElse(), False, False), instructions.Instruction(operation.LoadInteger(0), False, [variable.Variable(9)]), instructions.Instruction( operation.BeginWhile("<"), [variable.Variable(9), variable.Variable(5)], False), instructions.Instruction( operation.BinaryOperation("+"), [variable.Variable(7), variable.Variable(1)], [variable.Variable(10)]), instructions.Instruction( operation.Copy(), [variable.Variable(7), variable.Variable(10)]), instructions.Instruction( operation.BinaryOperation("+"), [variable.Variable(9), variable.Variable(1)], [variable.Variable(11)]), instructions.Instruction( operation.Copy(), [variable.Variable(6), variable.Variable(10)]), instructions.Instruction(operation.EndWhile(), False, False), instructions.Instruction(operation.EndIf(), False, False), instructions.Instruction(operation.EndFor(), False, False), instructions.Instruction(operation.LoadInteger(1), False, [variable.Variable(90)]), ]) program.Program([ instructions.Instruction(operation.LoadInteger(1), False, [variable.Variable(0)]), instructions.Instruction( operation.BeginFunction( typesData.FunctionSignature(2, [variable.Variable(11)])), False, [variable.Variable(1)], [variable.Variable(10), variable.Variable(11)]), instructions.Instruction( operation.BinaryOperation("+"), [variable.Variable(10), variable.Variable(11)], [variable.Variable(2)]), instructions.Instruction(operation.LoadString("somestring"), False, [variable.Variable(3)]), instructions.Instruction(operation.Return(), [variable.Variable(2)]), instructions.Instruction(operation.EndFunction()), instructions.Instruction(operation.LoadInteger(1337), False, [variable.Variable(4)]), instructions.Instruction(operation.CallFunction(2), [ variable.Variable(1), variable.Variable(4), variable.Variable(0) ], [variable.Variable(6)]), instructions.Instruction(operation.LoadInteger(1337), False, [variable.Variable(7)]), ]) print prog ctx = ContextAnalyzer(prog) ctx.doAnalyze()
def main(): prog = program.Program([ instructions.Instruction(operation.LoadInteger(10), False, [variable.Variable(0)]), instructions.Instruction(operation.LoadFloat(1.1), False, [variable.Variable(1)]), instructions.Instruction(operation.LoadBoolean(True), False, [variable.Variable(1)]), instructions.Instruction(operation.LoadString("qweqweqweqwe"), False, [variable.Variable(0)]), instructions.Instruction(operation.BeginIf(), [variable.Variable(2)]), instructions.Instruction(operation.BeginElse()), instructions.Instruction(operation.EndIf()) ]) prog = program.Program([ instructions.Instruction(operation.LoadString("phar"), False, [variable.Variable(100)]), instructions.Instruction(operation.Include(), [variable.Variable(100)]), instructions.Instruction(operation.Include(), [variable.Variable(100)]), instructions.Instruction(operation.LoadInteger(1), False, [variable.Variable(0)]), instructions.Instruction( operation.BeginFunction(typesData.FunctionSignature(2)), False, [variable.Variable(1)], [variable.Variable(10), variable.Variable(11)]), instructions.Instruction( operation.BinaryOperation("+"), [variable.Variable(10), variable.Variable(11)], [variable.Variable(2)]), instructions.Instruction(operation.LoadString("somestring"), False, [variable.Variable(3)]), instructions.Instruction(operation.Return(), [variable.Variable(2)]), instructions.Instruction(operation.EndFunction()), instructions.Instruction(operation.LoadInteger(1337), False, [variable.Variable(4)]), instructions.Instruction(operation.CallFunction(2), [ variable.Variable(1), variable.Variable(4), variable.Variable(0) ], [variable.Variable(6)]), instructions.Instruction(operation.LoadInteger(1337), False, [variable.Variable(7)]), instructions.Instruction(operation.Break()), instructions.Instruction(operation.Continue()), instructions.Instruction(operation.UnaryOperation("++"), [variable.Variable(0)], [variable.Variable(0)]), ]) prog = program.Program([ instructions.Instruction(operation.LoadString("phar"), False, [variable.Variable(100)]), instructions.Instruction(operation.Include(), [variable.Variable(100)]), instructions.Instruction(operation.Include(), [variable.Variable(100)]), instructions.Instruction(operation.LoadInteger(0), False, [variable.Variable(0)]), instructions.Instruction(operation.LoadInteger(1), False, [variable.Variable(1)]), instructions.Instruction(operation.LoadInteger(10), False, [variable.Variable(2)]), instructions.Instruction(operation.LoadInteger(10), False, [variable.Variable(3)]), instructions.Instruction(operation.LoadInteger(20), False, [variable.Variable(4)]), instructions.Instruction(operation.LoadInteger(5), False, [variable.Variable(5)]), instructions.Instruction(operation.LoadInteger(50), False, [variable.Variable(8)]), instructions.Instruction(operation.Phi(), [variable.Variable(3)], [variable.Variable(6)]), instructions.Instruction(operation.Phi(), [variable.Variable(4)], [variable.Variable(7)]), instructions.Instruction(operation.BeginFor("++", "<"), [ variable.Variable(0), variable.Variable(2), variable.Variable(8) ], False, [variable.Variable(80)]), instructions.Instruction( operation.BinaryOperation("<"), [variable.Variable(8), variable.Variable(5)], [variable.Variable(6)]), instructions.Instruction(operation.BeginIf(), [variable.Variable(6)], False), instructions.Instruction(operation.LoadInteger(0), False, [variable.Variable(9)]), instructions.Instruction( operation.BeginWhile("<"), [variable.Variable(9), variable.Variable(5)], False), instructions.Instruction( operation.BinaryOperation("+"), [variable.Variable(6), variable.Variable(1)], [variable.Variable(10)]), instructions.Instruction( operation.Copy(), [variable.Variable(6), variable.Variable(10)]), instructions.Instruction( operation.BinaryOperation("+"), [variable.Variable(9), variable.Variable(1)], [variable.Variable(11)]), instructions.Instruction( operation.Copy(), [variable.Variable(6), variable.Variable(10)]), instructions.Instruction(operation.EndWhile(), False, False), instructions.Instruction(operation.BeginElse(), False, False), instructions.Instruction(operation.LoadInteger(0), False, [variable.Variable(9)]), instructions.Instruction( operation.BeginWhile("<"), [variable.Variable(9), variable.Variable(5)], False), instructions.Instruction( operation.BinaryOperation("+"), [variable.Variable(7), variable.Variable(1)], [variable.Variable(10)]), instructions.Instruction( operation.Copy(), [variable.Variable(7), variable.Variable(10)]), instructions.Instruction( operation.BinaryOperation("+"), [variable.Variable(9), variable.Variable(1)], [variable.Variable(11)]), instructions.Instruction( operation.Copy(), [variable.Variable(6), variable.Variable(10)]), instructions.Instruction(operation.EndWhile(), False, False), instructions.Instruction(operation.EndIf(), False, False), instructions.Instruction(operation.EndFor(), False, False), instructions.Instruction(operation.LoadInteger(1), False, [variable.Variable(90)]), ]) # print prog l = Lifter(prog) l.doLifting() print l.emitter