Permalink
Browse files

Remove TypeLookup wrapper; just use a dictionary.

  • Loading branch information...
Andy Chu
Andy Chu committed Aug 19, 2018
1 parent 4fa0a80 commit 3bb4bc4a6fdd3e9d9bebe3f06dd65c64043dfaf3
Showing with 12 additions and 41 deletions.
  1. +0 −27 asdl/asdl_.py
  2. +1 −1 asdl/front_end.py
  3. +4 −4 asdl/gen_python.py
  4. +4 −4 asdl/py_meta.py
  5. +1 −1 build/dev.sh
  6. +2 −4 osh/asdl_gen.py
View
@@ -141,33 +141,6 @@ def LookupFieldType(self, field_name):
}
# TODO: Rename this to Reflection?
class TypeLookup(object):
"""Look up types by name.
They are put in a flat namespace.
"""
def __init__(self, runtime_type_lookup):
self.runtime_type_lookup = runtime_type_lookup # type name -> RuntimeType
def ByTypeName(self, type_name):
"""Given a string, return a type descriptor.
Used by generated code, e.g. in _devbuild/gen/osh_asdl.py.
Args:
type_name: string, e.g. 'word_part' or 'LiteralPart'
"""
#if not type_name in self.compound_types:
# print('FATAL: %s' % self.compound_types.keys())
#return self.compound_types[type_name]
if not type_name in self.runtime_type_lookup:
print('FATAL: %s' % self.runtime_type_lookup.keys())
return self.runtime_type_lookup[type_name]
def __repr__(self):
return repr(self.runtime_type_lookup)
# The following classes are the AST for the ASDL schema, i.e. the "meta-AST".
# See the EBNF at the top of the file to understand the logical connection
# between the various node types.
View
@@ -319,7 +319,7 @@ def _MakeReflection(module, app_types):
else:
raise AssertionError(ast_node)
return asdl.TypeLookup(type_lookup)
return type_lookup
def LoadSchema(f, app_types):
View
@@ -16,7 +16,7 @@ class GenClassesVisitor(visitor.AsdlVisitor):
def VisitSimpleSum(self, sum, name, depth):
self.Emit('class %s_e(py_meta.SimpleObj):' % name, depth)
self.Emit(' ASDL_TYPE = TYPE_LOOKUP.ByTypeName(%r)' % name, depth)
self.Emit(' ASDL_TYPE = TYPE_LOOKUP[%r]' % name, depth)
self.Emit('', depth)
# Just use #define, since enums aren't namespaced.
@@ -39,7 +39,7 @@ def _GenClass(self, desc, name, super_name, depth, tag_num=None):
# oheap serialization. TODO: measure the effect of __slots__, and then get
# rid of FIELDS? Or you can just make it an alias.
# FIELDS = self.__slots__.
self.Emit(' ASDL_TYPE = TYPE_LOOKUP.ByTypeName(%r)' % name, depth)
self.Emit(' ASDL_TYPE = TYPE_LOOKUP[%r]' % name, depth)
self.Emit(' __slots__ = %s' % quoted_fields, depth)
self.Emit('', depth)
@@ -77,7 +77,7 @@ def VisitConstructor(self, cons, def_name, tag_num, depth):
self._GenClass(cons, cons.name, def_name, depth, tag_num=tag_num)
else:
self.Emit("class %s(%s):" % (cons.name, def_name), depth)
self.Emit(' ASDL_TYPE = TYPE_LOOKUP.ByTypeName(%r)' % cons.name, depth)
self.Emit(' ASDL_TYPE = TYPE_LOOKUP[%r]' % cons.name, depth)
self.Emit(' tag = %d' % tag_num, depth)
self.Emit('', depth)
@@ -89,7 +89,7 @@ def VisitCompoundSum(self, sum, name, depth):
self.Emit('', depth)
self.Emit('class %s(py_meta.CompoundObj):' % name, depth)
self.Emit(' ASDL_TYPE = TYPE_LOOKUP.ByTypeName(%r)' % name, depth)
self.Emit(' ASDL_TYPE = TYPE_LOOKUP[%r]' % name, depth)
self.Emit('', depth)
# define command_t, and then make subclasses
View
@@ -126,7 +126,7 @@ def __init__(self, enum_id, name):
#
# Example:
# class bool_arg_type_e(py_meta.SimpleObj):
# ASDL_TYPE = TYPE_LOOKUP.ByTypeName('bool_arg_type')
# ASDL_TYPE = TYPE_LOOKUP['bool_arg_type']
# bool_arg_type_e.Undefined = bool_arg_type_e(1, 'Undefined')
def __hash__(self):
@@ -260,7 +260,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': type_lookup.ByTypeName(defn.name)}
class_attr = {'ASDL_TYPE': type_lookup[defn.name]}
cls = type(class_name, (SimpleObj, ), class_attr)
setattr(root, class_name, cls)
@@ -288,7 +288,7 @@ def MakeTypes(module, root, type_lookup):
tag_num[cons.name] = tag # for enum
class_attr = {
'ASDL_TYPE': type_lookup.ByTypeName(cons.name),
'ASDL_TYPE': type_lookup[cons.name],
'tag': tag, # Does this API change?
}
@@ -301,7 +301,7 @@ def MakeTypes(module, root, type_lookup):
setattr(root, enum_name, tag_enum)
elif isinstance(typ, asdl.Product):
class_attr = {'ASDL_TYPE': type_lookup.ByTypeName(defn.name)}
class_attr = {'ASDL_TYPE': type_lookup[defn.name]}
cls = type(defn.name, (DebugCompoundObj, ), class_attr)
setattr(root, defn.name, cls)
View
@@ -144,7 +144,7 @@ clean() {
minimal() {
mkdir -p _devbuild/gen
rm -v _devbuild/gen/*
rm -v -f _devbuild/gen/*
# So modules are importable.
touch _devbuild/__init__.py _devbuild/gen/__init__.py
View
@@ -57,10 +57,8 @@ def main(argv):
from core import util
f = util.GetResourceLoader().open('%s')
type_lookup = unpickle.load_v2_subset(f)
TYPE_LOOKUP = unpickle.load_v2_subset(f)
f.close()
TYPE_LOOKUP = asdl.TypeLookup(type_lookup)
""" % pickle_out_path)
v = gen_python.GenClassesVisitor(f)
@@ -71,7 +69,7 @@ def main(argv):
# s.decode('string-escape')! )
# In version 2, now I have 16 opcodes + STOP.
with open(pickle_out_path, 'w') as f:
pickle.dump(type_lookup.runtime_type_lookup, f, protocol=2)
pickle.dump(type_lookup, f, protocol=2)
from core.util import log
log('Wrote %s', pickle_out_path)

0 comments on commit 3bb4bc4

Please sign in to comment.