Exemplo n.º 1
0
def main(argv):
  try:
    action = argv[1]
  except IndexError:
    raise RuntimeError('Action required')

  if action == 'py':  # Prints the module
    schema_path = argv[2]

    with open(schema_path) as f:
      module = asdl.parse(f)

    app_types = {'id': asdl.UserType(Id)}
    type_lookup = asdl.ResolveTypes(module, app_types)

    # Note this is a big tree.  But we really want a graph of pointers to
    # instances.
    # Type(name, Product(...))
    # Type(name, Sum([Constructor(...), ...]))
    #print(module)

    root = sys.modules[__name__]
    # NOTE: We shouldn't pass in app_types for arith.asdl, but this is just a
    # demo.
    py_meta.MakeTypes(module, root, type_lookup)

    print('Dynamically created a Python module with these types:')
    for name in dir(root):
      print('\t' + name)

  elif action == 'arith-encode':  # oheap encoding
    expr = argv[2]
    out_path = argv[3]

    obj = arith_parse.ParseShell(expr)
    print('Encoding %r into binary:' % expr)
    print(obj)

    enc = encode.Params()
    with open(out_path, 'wb') as f:
      out = encode.BinOutput(f)
      encode.EncodeRoot(obj, enc, out)

  elif action == 'arith-format':  # pretty printing
    expr = argv[2]

    obj = arith_parse.ParseShell(expr)
    #out = fmt.TextOutput(sys.stdout)
    tree = fmt.MakeTree(obj)
    #treee= ['hi', 'there', ['a', 'b'], 'c']
    f = fmt.DetectConsoleOutput(sys.stdout)
    fmt.PrintTree(tree, f)
    print()

    # Might need to print the output?
    # out.WriteToFile?

  else:
    raise RuntimeError('Invalid action %r' % action)
Exemplo n.º 2
0
def main(argv):
    try:
        action = argv[1]
    except IndexError:
        raise RuntimeError('Action required')

    if action == 'py':  # Prints the module
        # Called by asdl/run.sh py-cpp

        schema_path = argv[2]
        app_types = {'id': asdl.UserType('id_kind_asdl', 'Id_t')}
        with open(schema_path) as f:
            schema_ast, type_lookup = front_end.LoadSchema(f, app_types)

        root = sys.modules[__name__]
        # NOTE: We shouldn't pass in app_types for arith.asdl, but this is just a
        # demo.
        py_meta.MakeTypes(schema_ast, root, type_lookup)

        log('AST for this ASDL schema:')
        schema_ast.Print(sys.stdout, 0)
        print()

        log('Dynamically created a Python module with these types:')
        for name in dir(root):
            print('\t' + name)

        if 1:
            # NOTE: It can be pickled, but not marshaled
            import marshal
            import cPickle
            print(dir(marshal))
            out_path = schema_path + '.pickle'
            with open(out_path, 'w') as f:
                #marshal.dump(type_lookup, f)
                # Version 2 is the highest protocol for Python 2.7.
                cPickle.dump(type_lookup.runtime_type_lookup, f, protocol=2)

            print('runtime_type_lookup:')
            for name, desc in type_lookup.runtime_type_lookup.items():
                print(name)
                print(desc)
            print()
            print('Wrote %s' % out_path)

    elif action == 'arith-format':  # pretty printing
        expr = argv[2]

        obj = arith_parse.ParseShell(expr)
        tree = obj.PrettyTree()
        #treee= ['hi', 'there', ['a', 'b'], 'c']
        f = fmt.DetectConsoleOutput(sys.stdout)
        fmt.PrintTree(tree, f)
        print()

        # Might need to print the output?
        # out.WriteToFile?

    else:
        raise RuntimeError('Invalid action %r' % action)
Exemplo n.º 3
0
def main(argv):
    try:
        action = argv[1]
    except IndexError:
        raise RuntimeError('Action required')

    if action == 'py':
        schema_path = argv[2]

        module = asdl.parse(schema_path)
        root = sys.modules[__name__]
        # NOTE: We shouldn't pass in app_types for arith.asdl, but this is just a
        # demo.
        py_meta.MakeTypes(module, root, app_types={'id': asdl.UserType(Id)})
        print(dir(root))

    elif action == 'arith-encode':
        expr = argv[2]
        out_path = argv[3]

        obj = arith_parse.ParseShell(expr)
        print('Encoding %r into binary:' % expr)
        print(obj)

        enc = encode.Params()
        with open(out_path, 'wb') as f:
            out = encode.BinOutput(f)
            encode.EncodeRoot(obj, enc, out)

    elif action == 'arith-format':
        expr = argv[2]

        obj = arith_parse.ParseShell(expr)
        #out = fmt.TextOutput(sys.stdout)
        tree = fmt.MakeTree(obj)
        #treee= ['hi', 'there', ['a', 'b'], 'c']
        fmt.PrintTree(tree, sys.stdout)

        # Might need to print the output?
        # out.WriteToFile?
    else:
        raise RuntimeError('Invalid action %r' % action)