Пример #1
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 = typed_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)
Пример #2
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)
Пример #3
0
def _ParseAndMakeTypes(schema_path, root):
    module = asdl.parse(schema_path)

    app_types = {}

    # Check for type errors
    if not asdl.check(module, app_types):
        raise AssertionError('ASDL file is invalid')
    py_meta.MakeTypes(module, root, app_types)
Пример #4
0
def _ParseAndMakeTypes(f, root):
    module = asdl.parse(f)

    app_types = {'id': asdl.UserType(Id)}

    # Check for type errors
    if not asdl.check(module, app_types):
        raise AssertionError('ASDL file is invalid')
    py_meta.MakeTypes(module, root, app_types)
Пример #5
0
def _ParseAndMakeTypes(f, root):
  # TODO: A better syntax for this might be:
  #
  #     id = external
  #
  # in osh.asdl.  Then we can show an error if it's not provided.
  app_types = {'id': asdl.UserType(Id)}

  module = asdl.parse(f)

  # Check for type errors
  if not asdl.check(module, app_types):
    raise AssertionError('ASDL file is invalid')
  py_meta.MakeTypes(module, root, app_types)
Пример #6
0
def _ParseAndMakeTypes(schema_path, root):
    module = asdl.parse(schema_path)

    app_types = {
        'identifier': asdl.UserType(Identifier),
        'bytes': asdl.UserType(Bytes),
        'object': asdl.UserType(PyObject),
        'constant': asdl.UserType(Constant),
        'singleton': asdl.UserType(Singleton),
    }

    # Check for type errors
    if not asdl.check(module, app_types):
        raise AssertionError('ASDL file is invalid')
    py_meta.MakeTypes(module, root, app_types)
Пример #7
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)
Пример #8
0
from core.id_kind import Id


def _LoadSchema(f):
    module = asdl.parse(f)

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

    # Check for type errors
    if not asdl.check(module, app_types):
        raise AssertionError('ASDL file is invalid')
    return module, type_lookup


f = util.GetResourceLoader().open('core/runtime.asdl')
root = sys.modules[__name__]
module, type_lookup = _LoadSchema(f)

if 0:
    py_meta.MakeTypes(module, root, type_lookup)
else:
    # Exported for the generated code to use
    TYPE_LOOKUP = type_lookup

    # Get the types from elsewhere
    from _devbuild.gen import runtime_asdl
    py_meta.AssignTypes(runtime_asdl, root)

f.close()
Пример #9
0

def IdInstance(i):
    return _ID_INSTANCES[i]


#
# Instantiate osh/types.asdl
#

f = util.GetResourceLoader().open('osh/types.asdl')
_asdl_module, _type_lookup = asdl.LoadSchema(f, {})  # no app_types

types = _AsdlModule()
if 0:
    py_meta.MakeTypes(_asdl_module, types, _type_lookup)
else:
    # Exported for the generated code to use
    TYPES_TYPE_LOOKUP = _type_lookup

    # Get the types from elsewhere
    from _devbuild.gen import types_asdl
    py_meta.AssignTypes(types_asdl, types)

f.close()

# Id -> bool_arg_type_e
BOOL_ARG_TYPES = {}  # type: dict

# Used by test_builtin.py
TEST_UNARY_LOOKUP = {}
Пример #10
0
#!/usr/bin/python
"""
arith_ast.py
"""

import os
import sys

from asdl import asdl_ as asdl
from asdl import py_meta

this_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
schema_path = os.path.join(this_dir, 'arith.asdl')

with open(schema_path) as f:
    module = asdl.parse(f)
root = sys.modules[__name__]
py_meta.MakeTypes(module, root)
Пример #11
0
#!/usr/bin/env python
"""
arith_ast.py
"""

import sys

from asdl import front_end
from asdl import py_meta
from core import util

f = util.GetResourceLoader().open('asdl/arith.asdl')
_asdl_module, _type_lookup = front_end.LoadSchema(f, {})  # no app_types
f.close()

root = sys.modules[__name__]
py_meta.MakeTypes(_asdl_module, root, _type_lookup)