def parse(opcodes, source): template = Template('test', opcodes) s = BCDataStream() for t in source: if isinstance(t, bytes): s.write_many(push_data(t)) elif isinstance(t, int): s.write_uint8(t) else: raise ValueError() s.reset() return template.parse(tokenize(s))
def generate(self, values): source = BCDataStream() for opcode in self.opcodes: if isinstance(opcode, PUSH_SINGLE): data = values[opcode.name] source.write_many(push_data(data)) elif isinstance(opcode, PUSH_INTEGER): data = values[opcode.name] source.write_many(push_data( data.to_bytes((data.bit_length() + 7) // 8, byteorder='little') )) elif isinstance(opcode, PUSH_SUBSCRIPT): data = values[opcode.name] source.write_many(push_data(data.source)) elif isinstance(opcode, PUSH_MANY): for data in values[opcode.name]: source.write_many(push_data(data)) elif isinstance(opcode, SMALL_INTEGER): data = values[opcode.name] source.write_many(push_small_integer(data)) else: source.write_uint8(opcode) return source.get_bytes()