コード例 #1
0
ファイル: id_kind_gen.py プロジェクト: sethwoodworth/oil
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)
コード例 #2
0
def _CreateModule(id_spec, ids):
    """ 
  Create a SYNTHETIC ASDL module to generate code from.
  """
    from asdl import asdl_

    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_.TypeDecl('Id', id_sum)
    kind_ = asdl_.TypeDecl('Kind', kind_sum)

    schema_ast = asdl_.Module('id_kind', [], [id_, kind_])
    return schema_ast
コード例 #3
0
def _CreateSum(sum_name, variant_names):
    """ 
  Similar to frontend/id_kind_gen.py
  Usage of SYNTHETIC ASDL module:

  C++:

  using option_asdl::opt_num
  opt_num::nounset

  Python:
  from _devbuild.gen.option_asdl import opt_num
  opt_num.nounset
  """
    sum_ = asdl_.Sum([asdl_.Constructor(name) for name in variant_names])
    typ = asdl_.Type(sum_name, sum_)
    return typ