コード例 #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 main(argv):
    try:
        action = argv[1]
    except IndexError:
        raise RuntimeError('Action required')

    # TODO:
    # generate builtin::echo, etc.
    #
    # And in Python do the same.

    option = _CreateSum(_OPT_ENUM, [opt.name for opt in option_def.All()])
    builtin = _CreateSum(_BUILTIN_ENUM,
                         [b.enum_name for b in builtin_def.All()])
    # TODO: could shrink array later.
    # [opt.name for opt in option_def.All() if opt.implemented])

    schema_ast = asdl_.Module('option', [], [option, builtin])

    if action == 'cpp':
        from asdl import gen_cpp

        out_prefix = argv[2]

        with open(out_prefix + '.h', 'w') as f:
            f.write("""\
#ifndef OPTION_ASDL_H
#define OPTION_ASDL_H

namespace option_asdl {
""")

            # TODO: Could suppress option_str
            v = gen_cpp.ClassDefVisitor(f, {}, simple_int_sums=_SIMPLE)
            v.VisitModule(schema_ast)

            f.write("""
}  // namespace option_asdl

#endif  // OPTION_ASDL_H
""")

        with open(out_prefix + '.cc', 'w') as f:
            f.write("""\
#include <assert.h>
#include "option_asdl.h"

namespace option_asdl {

""")

            v = gen_cpp.MethodDefVisitor(f, {}, simple_int_sums=_SIMPLE)

            v.VisitModule(schema_ast)

            f.write('}  // namespace option_asdl\n')

    elif action == 'mypy':
        from asdl import gen_python

        f = sys.stdout

        f.write("""\
from asdl import pybase

""")
        # option_i type
        v = gen_python.GenMyPyVisitor(f, None, simple_int_sums=_SIMPLE)
        v.VisitModule(schema_ast)

    else:
        raise RuntimeError('Invalid action %r' % action)