Permalink
Browse files

Remove all remnants of the old ASDL metaprogramming API.

The new API is obj.ASDL_TYPE, which is an instance of a type defined in
asdl/asdl_.py.

All spec tests and unit tests pass.
  • Loading branch information...
Andy Chu
Andy Chu committed Dec 20, 2017
1 parent 12c1a58 commit 1c172398b38dc5cad956e83f2949f83b237e4428
Showing with 17 additions and 69 deletions.
  1. +3 −14 asdl/arith_ast_test.py
  2. +1 −1 asdl/encode.py
  3. +13 −54 asdl/py_meta.py
View
@@ -100,17 +100,6 @@ def testEncode(self):
self.assertEqual(b'\x63\x00\x00', e[9:12]) # 0x63 = 99
def testConstructorType(self):
print(ArithVar)
print('FIELDS', ArithVar.FIELDS)
print('DESCRIPTOR_LOOKUP', ArithVar.DESCRIPTOR_LOOKUP)
print('DESCRIPTOR', ArithVar.DESCRIPTOR)
print(ArithUnary)
print(ArithUnary.FIELDS)
print(ArithBinary)
print(ArithBinary.FIELDS)
n1 = ArithVar('x')
n2 = ArithVar(name='y')
print(n1)
@@ -144,7 +133,7 @@ def testProductType(self):
s.length = 3
print(s)
assert isinstance(s.DESCRIPTOR, asdl_.Product)
assert isinstance(s.ASDL_TYPE, asdl_.Product)
# Implementation detail for dynamic type checking
assert isinstance(s, py_meta.CompoundObj)
@@ -161,7 +150,7 @@ def testSimpleSumType(self):
assert isinstance(o, py_meta.SimpleObj)
# Implementation detail for dynamic type checking
assert isinstance(o.DESCRIPTOR, asdl_.Sum)
assert isinstance(o.ASDL_TYPE, asdl_.Sum)
def testCompoundSumType(self):
print
@@ -175,7 +164,7 @@ def testCompoundSumType(self):
assert isinstance(c, py_meta.CompoundObj)
# Implementation detail for dynamic type checking
assert isinstance(c.DESCRIPTOR, asdl_.Constructor), c.DESCRIPTOR
assert isinstance(c.ASDL_TYPE, asdl_.Constructor), c.ASDL_TYPE
def testOtherTypes(self):
c = Const(66)
View
@@ -214,7 +214,7 @@ def EncodeObj(obj, enc, out):
'%r is not a compound obj (%r)' % (obj, obj.__class__)
# Constructor objects have a tag.
if isinstance(obj.DESCRIPTOR, asdl.Constructor):
if isinstance(obj.ASDL_TYPE, asdl.Constructor):
enc.Tag(obj.tag, this_chunk)
for name, desc in obj.ASDL_TYPE.GetFields(): # encode in order
View
@@ -76,7 +76,7 @@ def _CheckType(value, expected_desc):
return isinstance(value, expected_desc.typ)
try:
actual_desc = value.__class__.DESCRIPTOR
actual_desc = value.__class__.ASDL_TYPE
except AttributeError:
return False # it's not of the right type
@@ -89,7 +89,10 @@ def _CheckType(value, expected_desc):
else:
for cons in expected_desc.types:
#print("CHECKING desc %s against %s" % (desc, cons))
# It has to be one of the alternatives
from core.util import log
#log('Checking %s against %s', actual_desc, cons)
if actual_desc is cons:
return True
return False
@@ -101,7 +104,7 @@ def _CheckType(value, expected_desc):
class Obj(object):
# NOTE: We're using CAPS for these static fields, since they are constant at
# runtime after metaprogramming.
DESCRIPTOR = None # Used for type checking
ASDL_TYPE = None # Used for type checking
class SimpleObj(Obj):
@@ -122,16 +125,13 @@ class CompoundObj(Obj):
Uses some metaprogramming.
"""
FIELDS = [] # ordered list of field names
DESCRIPTOR_LOOKUP = {} # field name: (asdl.Type | int | str)
# Always set for constructor types, which are subclasses of sum types. Never
# set for product types.
tag = None
def __init__(self, *args, **kwargs):
# The user must specify ALL required fields or NONE.
self._assigned = {f: False for f in self.FIELDS}
self._assigned = {f: False for f in self.ASDL_TYPE.GetFieldNames()}
self._SetDefaults()
if args or kwargs:
self._Init(args, kwargs)
@@ -178,7 +178,7 @@ def CheckUnassigned(self):
unassigned = []
for name in self.ASDL_TYPE.GetFieldNames():
if not self._assigned[name]:
desc = self.DESCRIPTOR_LOOKUP[name]
desc = self.ASDL_TYPE.LookupFieldType(name)
if not isinstance(desc, asdl.MaybeType):
unassigned.append(name)
if unassigned:
@@ -211,39 +211,6 @@ def __repr__(self):
return s
def _MakeFieldDescriptors(module, fields, type_lookup, add_spids=True):
"""
Args:
module: asdl.Module
It must have a types attribute for lookup.
fields: list of asdl.Field instances
app_types: {string: <app-specific type}
add_spids: TODO this should be replaced by attributes?
"""
desc_lookup = {}
for f in fields:
# Lookup order: primitive, defined in the ASDL file, passed by the app
desc = type_lookup.Get(f)
desc_lookup[f.name] = desc
field_names = [f.name for f in fields]
# Add 'int* spids' if requested.
# TODO: Use ASDL attributes for this!
if add_spids:
field_names.append('spids')
desc_lookup['spids'] = asdl.ArrayType(asdl.IntType())
# TODO: remove these
class_attr = {
'FIELDS': field_names,
'DESCRIPTOR_LOOKUP': desc_lookup,
}
return class_attr
def MakeTypes(module, root, type_lookup):
"""
Args:
@@ -258,7 +225,6 @@ def MakeTypes(module, root, type_lookup):
sum_type = typ
if asdl.is_simple(sum_type):
# An object without fields, which can be stored inline.
class_attr = {'DESCRIPTOR': sum_type} # asdl.Sum
# Create a class called foo_e. Unlike the CompoundObj case, it doesn't
# have subtypes. Instead if has attributes foo_e.Bar, which Bar is an
@@ -270,6 +236,7 @@ def MakeTypes(module, root, type_lookup):
# change. I haven't run into this problem in practice yet.
class_name = defn.name + '_e'
class_attr = {'ASDL_TYPE': sum_type} # asdl.Sum
cls = type(class_name, (SimpleObj, ), class_attr)
setattr(root, class_name, cls)
@@ -295,13 +262,10 @@ def MakeTypes(module, root, type_lookup):
tag_num[cons.name] = tag # for enum
# Add 'int* spids' to every constructor.
class_attr = _MakeFieldDescriptors(module, cons.fields, type_lookup)
class_attr['ASDL_TYPE'] = cons # asdl.Constructor
# TODO: remove these
class_attr['DESCRIPTOR'] = cons # asdl.Constructor
class_attr['tag'] = tag
class_attr = {
'ASDL_TYPE': cons, # asdl.Constructor
'tag': tag, # Does this API change?
}
cls = type(cons.name, (base_class, ), class_attr)
setattr(root, cons.name, cls)
@@ -312,12 +276,7 @@ def MakeTypes(module, root, type_lookup):
setattr(root, enum_name, tag_enum)
elif isinstance(typ, asdl.Product):
class_attr = _MakeFieldDescriptors(module, typ.fields, type_lookup)
class_attr['ASDL_TYPE'] = typ
# TODO: remove
class_attr['DESCRIPTOR'] = typ
class_attr = {'ASDL_TYPE': typ}
cls = type(defn.name, (CompoundObj, ), class_attr)
setattr(root, defn.name, cls)

0 comments on commit 1c17239

Please sign in to comment.