def main(argv): try: action = argv[1] except IndexError: raise RuntimeError('Action required') # NOTE: This initialization must be identical to the one in core/meta.py. We # do it here to avoid circular dependencies. ID_SPEC = id_kind.IdSpec({}, {}) id_kind.AddKinds(ID_SPEC) id_kind.AddBoolKinds(ID_SPEC, bool_arg_type_e) # must come second id_kind.SetupTestBuiltin(ID_SPEC, {}, {}, {}, bool_arg_type_e) ids = ID_SPEC.id_str2int.items() ids.sort(key=lambda pair: pair[1]) # Sort by ID if action == 'c': for name, id_int in ids: print('#define id__%s %s' % (name, id_int)) elif action == 'mypy': from asdl import asdl_ from asdl import gen_python # # Create a SYNTHETIC ASDL module, and generate code from it. # id_sum = asdl_.Sum([asdl_.Constructor(name) for name, _ in ids]) variants2 = [ asdl_.Constructor(name) for name in ID_SPEC.kind_name_list ] kind_sum = asdl_.Sum(variants2) id_ = asdl_.Type('Id', id_sum) kind_ = asdl_.Type('Kind', kind_sum) schema_ast = asdl_.Module('id_kind', [id_, kind_]) #print(schema_ast) f = sys.stdout f.write("""\ from asdl import runtime """) # Minor style issue: we want Id and Kind, not Id_e and Kind_e v = gen_python.GenMyPyVisitor(f, None, e_suffix=False) v.VisitModule(schema_ast) else: raise RuntimeError('Invalid action %r' % action)
def TopLevel(): """Copy some metaprogramming that only happens at the top level.""" # from core/meta.py from core.meta import (_ID_TO_KIND_INTEGERS, BOOL_ARG_TYPES, TEST_UNARY_LOOKUP, TEST_BINARY_LOOKUP, TEST_OTHER_LOOKUP, types_asdl) from core import id_kind ID_SPEC = id_kind.IdSpec(_ID_TO_KIND_INTEGERS, BOOL_ARG_TYPES) id_kind.AddKinds(ID_SPEC) id_kind.AddBoolKinds(ID_SPEC, types_asdl.bool_arg_type_e) # must come second id_kind.SetupTestBuiltin(ID_SPEC, TEST_UNARY_LOOKUP, TEST_BINARY_LOOKUP, TEST_OTHER_LOOKUP, types_asdl.bool_arg_type_e) from osh import arith_parse spec = arith_parse.MakeShellSpec()
# type: (int) -> Id_t return ID_INSTANCES[i] BOOL_ARG_TYPES = {} # type: Dict[int, bool_arg_type_t] # Used by builtin_bracket.py TEST_UNARY_LOOKUP = {} # type: Dict[str, int] TEST_BINARY_LOOKUP = {} # type: Dict[str, int] TEST_OTHER_LOOKUP = {} # type: Dict[str, int] # # Initialize Id and Kind # ID_SPEC = id_kind.IdSpec(_ID_TO_KIND_INTEGERS, BOOL_ARG_TYPES) id_kind.AddKinds(ID_SPEC) id_kind.AddBoolKinds(ID_SPEC) # must come second id_kind.SetupTestBuiltin(ID_SPEC, TEST_UNARY_LOOKUP, TEST_BINARY_LOOKUP, TEST_OTHER_LOOKUP) # Debug Stats _kind_sizes = ID_SPEC.kind_sizes # # Redirect Tables associated with IDs # # These might be osh specific. #
# Id -> bool_arg_type_e BOOL_ARG_TYPES = {} # type: dict # Used by builtin_bracket.py TEST_UNARY_LOOKUP = {} TEST_BINARY_LOOKUP = {} TEST_OTHER_LOOKUP = {} # # Add attributes to Id and Kind # ID_SPEC = id_kind.IdSpec(Id, Kind, _ID_NAMES, _ID_INSTANCES, _ID_TO_KIND, BOOL_ARG_TYPES) id_kind.AddKinds(ID_SPEC) id_kind.AddBoolKinds(ID_SPEC, Id, types_asdl.bool_arg_type_e) # must come second # NOTE: Dependency on the types module here. This is the root cause of the # _BOOTSTRAP_LEVEL hack. id_kind.SetupTestBuiltin(Id, Kind, ID_SPEC, TEST_UNARY_LOOKUP, TEST_BINARY_LOOKUP, TEST_OTHER_LOOKUP, types_asdl.bool_arg_type_e) # Debug _kind_sizes = ID_SPEC.kind_sizes
def main(argv): try: action = argv[1] except IndexError: raise RuntimeError('Action required') # NOTE: This initialization must be identical to the one in core/meta.py. We # do it here to avoid circular dependencies. ID_SPEC = id_kind.IdSpec({}, {}) id_kind.AddKinds(ID_SPEC) id_kind.AddBoolKinds(ID_SPEC) # must come second id_kind.SetupTestBuiltin(ID_SPEC, {}, {}, {}) ids = ID_SPEC.id_str2int.items() ids.sort(key=lambda pair: pair[1]) # Sort by ID if action == 'c': for name, id_int in ids: print('#define id__%s %s' % (name, id_int)) elif action == 'cpp': from asdl import gen_cpp schema_ast = _CreateModule(ID_SPEC, ids) out_prefix = argv[2] with open(out_prefix + '.h', 'w') as f: f.write(""" #ifndef ID_KIND_ASDL_H #define ID_KIND_ASDL_H namespace id_kind_asdl { """) v = gen_cpp.ClassDefVisitor(f, {}, e_suffix=False) v.VisitModule(schema_ast) f.write(""" } // namespace id_kind_asdl #endif // ID_KIND_ASDL_H """) with open(out_prefix + '.cc', 'w') as f: f.write("""\ #include <assert.h> #include "id_kind_asdl.h" namespace id_kind_asdl { """) v = gen_cpp.MethodDefVisitor(f, {}, e_suffix=False) v.VisitModule(schema_ast) f.write('} // namespace id_kind_asdl\n') elif action == 'mypy': from asdl import gen_python schema_ast = _CreateModule(ID_SPEC, ids) #print(schema_ast) f = sys.stdout f.write("""\ from asdl import pybase from typing import List """) # Minor style issue: we want Id and Kind, not Id_e and Kind_e v = gen_python.GenMyPyVisitor(f, None, e_suffix=False) v.VisitModule(schema_ast) f.write(""" ID_INSTANCES = [ None, # unused index 0 """) for name, _ in ids: f.write(' Id.%s,\n' % name) f.write('] # type: List[Id_t]\n') f.write(""" KIND_INSTANCES = [ None, # unused index 0 """) for name in ID_SPEC.kind_name_list: f.write(' Kind.%s,\n' % name) f.write('] # type: List[Kind_t]\n') else: raise RuntimeError('Invalid action %r' % action)