Permalink
Browse files

pyassem: Extract method to ArgEncoder.

  • Loading branch information...
Andy Chu
Andy Chu committed Mar 20, 2018
1 parent 6e8cd3f commit 9a867fe63b6ac271cfded486c5150a954020ec91
Showing with 16 additions and 14 deletions.
  1. +16 −14 opy/compiler2/pyassem.py
View
@@ -398,17 +398,10 @@ def MakeCodeObject(self):
# LOAD_CLOSURE/LOAD_DEREF refer to both kinds of variables.
closure = cellvars + self.freevars
# Convert arguments from symbolic to concrete form. Mutates
# self.consts, self.names, etc.
# Convert arguments from symbolic to concrete form.
enc = ArgEncoder(self.klass, consts, names, self.varnames, closure)
for i, t in enumerate(insts):
if len(t) == 2:
opname, oparg = t
method = enc.Get(opname)
if method:
arg_index = method(enc, oparg)
insts[i] = opname, arg_index
# Mutates not only insts, but also self.consts, self.names, etc.
enc.Run(insts)
if self.flags & CO_NEWLOCALS:
nlocals = len(self.varnames)
@@ -436,7 +429,7 @@ def MakeCodeObject(self):
class ArgEncoder(object):
""" TODO: This should just be a simple switch ."""
"""Replaces arg objects with indices."""
def __init__(self, klass, consts, names, varnames, closure):
"""
@@ -449,6 +442,18 @@ def __init__(self, klass, consts, names, varnames, closure):
self.varnames = varnames
self.closure = closure
def Run(self, insts):
"""Mutates insts."""
for i, t in enumerate(insts):
if len(t) == 2:
opname, oparg = t
method = self._converters.get(opname, None)
if method:
arg_index = method(self, oparg)
insts[i] = opname, arg_index
# TODO: This should just be a simple switch
def _convert_LOAD_CONST(self, arg):
from . import pycodegen
if isinstance(arg, pycodegen.CodeGenerator):
@@ -506,9 +511,6 @@ def _convert_COMPARE_OP(self, arg):
_converters[opname] = obj
del name, obj, opname
def Get(self, opname):
return self._converters.get(opname, None)
class Assembler(object):
"""Builds co_code and lnotab.

0 comments on commit 9a867fe

Please sign in to comment.