コード例 #1
0
ファイル: tst_read.py プロジェクト: cctbx/cctbx-playground
def exercise_eval_const_expression_simple(verbose):
  t_dir = libtbx.env.under_dist(
    module_name="fable", path="test/valid", test=op.isdir)
  file_name = "const_expressions.f"
  all_fprocs = read.process(file_names=[op.join(t_dir, file_name)])
  assert len(all_fprocs.all_in_input_order) == 2
  fproc = all_fprocs.all_in_input_order[0]
  val = fproc.eval_const_expression_simple(identifier="n5")
  assert val == 296356
  for identifier,expected_vals in [
        ("nums1", [3,2]),
        ("nums2", [1,3]),
        ("nums3", [4])]:
    vals = fproc.eval_dimensions_simple(
      dim_tokens=fproc.fdecl_by_identifier[identifier].dim_tokens)
    assert vals == expected_vals
  vals = fproc.eval_dimensions_simple(
    dim_tokens=fproc.fdecl_by_identifier["nums3"].dim_tokens,
    allow_power=False)
  assert vals == [None]
  fproc = all_fprocs.all_in_input_order[1]
  vals = fproc.eval_dimensions_simple(
    dim_tokens=fproc.fdecl_by_identifier["nums"].dim_tokens)
  assert vals == [None, None]
  #
  file_name = "const_expressions_2.f"
  all_fprocs = read.process(file_names=[op.join(t_dir, file_name)])
  assert len(all_fprocs.all_in_input_order) == 1
  fproc = all_fprocs.all_in_input_order[0]
  val = fproc.eval_const_expression_simple(identifier="n5f")
  assert approx_equal(val, 297845.226131)
コード例 #2
0
def exercise_eval_const_expression_simple(verbose):
    t_dir = libtbx.env.under_dist(module_name="fable",
                                  path="test/valid",
                                  test=op.isdir)
    file_name = "const_expressions.f"
    all_fprocs = read.process(file_names=[op.join(t_dir, file_name)])
    assert len(all_fprocs.all_in_input_order) == 2
    fproc = all_fprocs.all_in_input_order[0]
    val = fproc.eval_const_expression_simple(identifier="n5")
    assert val == 296356
    for identifier, expected_vals in [("nums1", [3, 2]), ("nums2", [1, 3]),
                                      ("nums3", [4])]:
        vals = fproc.eval_dimensions_simple(
            dim_tokens=fproc.fdecl_by_identifier[identifier].dim_tokens)
        assert vals == expected_vals
    vals = fproc.eval_dimensions_simple(
        dim_tokens=fproc.fdecl_by_identifier["nums3"].dim_tokens,
        allow_power=False)
    assert vals == [None]
    fproc = all_fprocs.all_in_input_order[1]
    vals = fproc.eval_dimensions_simple(
        dim_tokens=fproc.fdecl_by_identifier["nums"].dim_tokens)
    assert vals == [None, None]
    #
    file_name = "const_expressions_2.f"
    all_fprocs = read.process(file_names=[op.join(t_dir, file_name)])
    assert len(all_fprocs.all_in_input_order) == 1
    fproc = all_fprocs.all_in_input_order[0]
    val = fproc.eval_const_expression_simple(identifier="n5f")
    assert approx_equal(val, 297845.226131)
コード例 #3
0
def run(args):
    if (len(args) == 0): args = ["--help"]
    from libtbx.option_parser import option_parser
    import libtbx.load_env
    command_line = (option_parser(
        usage="%s [options] fortran_file ..." %
        libtbx.env.dispatcher_name).option(
            None, "--each", action="store_true",
            default=False).option(None,
                                  "--report_success",
                                  action="store_true",
                                  default=False).option(
                                      None,
                                      "--warnings",
                                      action="store_true",
                                      default=False)).process(args=args)
    co = command_line.options

    def sorry_exclusive(opt_name):
        from libtbx.utils import Sorry
        raise Sorry("%s: options are mutually exclusive: --each, --%s" %
                    (libtbx.env.dispatcher_name, opt_name))

    if (co.each):
        if (co.warnings): sorry_exclusive("warnings")
    from fable.read import process
    if (co.each):
        process_each(process=process,
                     file_names=command_line.args,
                     report_success=co.report_success)
    else:
        all_fprocs = process(file_names=command_line.args)
        if (co.warnings):
            for fproc in all_fprocs.all_in_input_order:
                report_equivalence_clusters_with_mixed_data_types(fproc=fproc)
コード例 #4
0
ファイル: read.py プロジェクト: rimmartin/cctbx_project
def run(args):
    if (len(args) == 0): args = ["--help"]
    import libtbx.load_env
    parser = optparse.OptionParser(usage="%s [options] fortran_file ..." %
                                   libtbx.env.dispatcher_name)
    parser.add_option("-?", action="help", help=optparse.SUPPRESS_HELP)
    parser.add_option("--each", action="store_true", default=False)
    parser.add_option("--report_success", action="store_true", default=False)
    parser.add_option("--report-success",
                      action="store_true",
                      help=optparse.SUPPRESS_HELP)
    parser.add_option("--warnings", action="store_true", default=False)
    co, files = parser.parse_args(args)

    def sorry_exclusive(opt_name):
        from libtbx.utils import Sorry
        raise Sorry("%s: options are mutually exclusive: --each, --%s" %
                    (libtbx.env.dispatcher_name, opt_name))

    if (co.each):
        if (co.warnings): sorry_exclusive("warnings")
    from fable.read import process
    if (co.each):
        process_each(process=process,
                     file_names=files,
                     report_success=co.report_success)
    else:
        all_fprocs = process(file_names=files)
        if (co.warnings):
            for fproc in all_fprocs.all_in_input_order:
                report_equivalence_clusters_with_mixed_data_types(fproc=fproc)
コード例 #5
0
def run(args):
    if not args: args = ["--help"]
    parser = optparse.OptionParser(
        usage="fable.read [options] fortran_file ...")
    parser.add_option("-?", action="help", help=optparse.SUPPRESS_HELP)
    parser.add_option("--each", action="store_true", default=False)
    parser.add_option("--report_success", action="store_true", default=False)
    parser.add_option("--report-success",
                      action="store_true",
                      help=optparse.SUPPRESS_HELP)
    parser.add_option("--warnings", action="store_true", default=False)

    co, files = parser.parse_args(args)
    if co.each and co.warnings:
        sys.exit(
            "fable.read: options are mutually exclusive: --each, --warnings")
    from fable.read import process
    if co.each:
        process_each(process=process,
                     file_names=files,
                     report_success=co.report_success)
    else:
        all_fprocs = process(file_names=files)
        if co.warnings:
            for fproc in all_fprocs.all_in_input_order:
                report_equivalence_clusters_with_mixed_data_types(fproc=fproc)
コード例 #6
0
ファイル: read.py プロジェクト: cctbx/cctbx-playground
def run(args):
  if (len(args) == 0): args = ["--help"]
  from libtbx.option_parser import option_parser
  import libtbx.load_env
  command_line = (option_parser(
    usage="%s [options] fortran_file ..." % libtbx.env.dispatcher_name)
    .option(None, "--each",
      action="store_true",
      default=False)
    .option(None, "--report_success",
      action="store_true",
      default=False)
    .option(None, "--warnings",
      action="store_true",
      default=False)
  ).process(args=args)
  co = command_line.options
  def sorry_exclusive(opt_name):
    from libtbx.utils import Sorry
    raise Sorry(
      "%s: options are mutually exclusive: --each, --%s"
        % (libtbx.env.dispatcher_name, opt_name))
  if (co.each):
    if (co.warnings): sorry_exclusive("warnings")
  from fable.read import process
  if (co.each):
    process_each(
      process=process,
      file_names=command_line.args,
      report_success=co.report_success)
  else:
    all_fprocs = process(file_names=command_line.args)
    if (co.warnings):
      for fproc in all_fprocs.all_in_input_order:
        report_equivalence_clusters_with_mixed_data_types(fproc=fproc)
コード例 #7
0
ファイル: read.py プロジェクト: cctbx/cctbx-playground
def process_each(process, file_names, report_success=False):
  import traceback
  n_fail = 0
  n_succ = 0
  for file_name in file_names:
    try:
      process(file_names=[file_name])
    except Exception:
      n_fail += 1
      print "FAILING:", file_name
      print traceback.format_exc(limit=None)
    else:
      if (report_success):
        print "SUCCESS:", file_name
      n_succ += 1
  if (n_fail != 0):
    print "Failing:", n_fail
  if (n_succ != 0):
    print "Success:", n_succ
コード例 #8
0
def process_each(process, file_names, report_success=False):
    import traceback
    n_fail = 0
    n_succ = 0
    for file_name in file_names:
        try:
            process(file_names=[file_name])
        except Exception:
            n_fail += 1
            print "FAILING:", file_name
            print traceback.format_exc(limit=None)
        else:
            if (report_success):
                print "SUCCESS:", file_name
            n_succ += 1
    if (n_fail != 0):
        print "Failing:", n_fail
    if (n_succ != 0):
        print "Success:", n_succ
コード例 #9
0
ファイル: tst_read.py プロジェクト: cctbx/cctbx-playground
def exercise_show():
  t_dir = libtbx.env.under_dist(
    module_name="fable", path="test/valid", test=op.isdir)
  all_fprocs = read.process(file_names=[op.join(t_dir, "subroutine_3.f")])
  from cStringIO import StringIO
  cio = StringIO()
  all_fprocs.show_counts_by_type(out=cio, prefix="$ ")
  assert not show_diff(cio.getvalue(), """\
$ Counts by Fortran procedure type:
$   program: 1
$   subroutine: 3
$   function: 0
$   blockdata: 0
""")
コード例 #10
0
def exercise_show():
    t_dir = libtbx.env.under_dist(module_name="fable",
                                  path="test/valid",
                                  test=op.isdir)
    all_fprocs = read.process(file_names=[op.join(t_dir, "subroutine_3.f")])
    from six.moves import StringIO
    cio = StringIO()
    all_fprocs.show_counts_by_type(out=cio, prefix="$ ")
    assert not show_diff(
        cio.getvalue(), """\
$ Counts by Fortran procedure type:
$   program: 1
$   subroutine: 3
$   function: 0
$   blockdata: 0
""")
コード例 #11
0
ファイル: tst_read.py プロジェクト: cctbx/cctbx-playground
def exercise_tokens_as_string(verbose):
  t_dir = libtbx.env.under_dist(
    module_name="fable", path="test/valid", test=op.isdir)
  from tokenization import tokens_as_string
  for file_name in sorted(os.listdir(t_dir)):
    if (not file_name.endswith(".f")): continue
    if (verbose):
      print "exercise_tokens_as_string:", file_name
    all_fprocs = read.process(file_names=[op.join(t_dir, file_name)])
    for fproc in all_fprocs.all_in_input_order:
      for ei in fproc.executable:
        if (ei.key == "write"):
          s = tokens_as_string(tokens=ei.iolist)
          if (verbose):
            print s
      for tokens in fproc.format.values():
        s = tokens_as_string(tokens=tokens)
        if (verbose):
          print s
      if (verbose):
        print
コード例 #12
0
ファイル: tst_read.py プロジェクト: cctbx/cctbx-playground
def exercise_build_fprocs_by_name():
  t_dir = libtbx.env.under_dist(
    module_name="fable", path="test/valid", test=op.isdir)
  for pair in [
        ("subroutine_3.f", "subroutine_4.f"),
        ("implied_program.f", "implied_program.f")]:
    file_names = [op.join(t_dir, file_name) for file_name in pair]
    all_fprocs = read.process(file_names=file_names)
    from libtbx.utils import Sorry
    try:
      all_fprocs.fprocs_by_name()
    except Sorry, e:
      if (pair[0] == "subroutine_3.f"):
        assert str(e).startswith("Fortran procedure name conflict:")
        assert str(e).endswith("  -----------------^")
      else:
        assert str(e).startswith("""\
Fortran procedure name conflict:
  1. definition: program_unnamed (implied)
    before """)
        assert str(e).endswith("implied_program.f(2)")
    else: raise Exception_expected
コード例 #13
0
def exercise_tokens_as_string(verbose):
    t_dir = libtbx.env.under_dist(module_name="fable",
                                  path="test/valid",
                                  test=op.isdir)
    from fable.tokenization import tokens_as_string
    for file_name in sorted(os.listdir(t_dir)):
        if (not file_name.endswith(".f")): continue
        if (verbose):
            print("exercise_tokens_as_string:", file_name)
        all_fprocs = read.process(file_names=[op.join(t_dir, file_name)])
        for fproc in all_fprocs.all_in_input_order:
            for ei in fproc.executable:
                if (ei.key == "write"):
                    s = tokens_as_string(tokens=ei.iolist)
                    if (verbose):
                        print(s)
            for tokens in fproc.format.values():
                s = tokens_as_string(tokens=tokens)
                if (verbose):
                    print(s)
            if (verbose):
                print()
コード例 #14
0
def exercise_build_fprocs_by_name():
    t_dir = libtbx.env.under_dist(module_name="fable",
                                  path="test/valid",
                                  test=op.isdir)
    for pair in [("subroutine_3.f", "subroutine_4.f"),
                 ("implied_program.f", "implied_program.f")]:
        file_names = [op.join(t_dir, file_name) for file_name in pair]
        all_fprocs = read.process(file_names=file_names)
        from libtbx.utils import Sorry
        try:
            all_fprocs.fprocs_by_name()
        except Sorry as e:
            if (pair[0] == "subroutine_3.f"):
                assert str(e).startswith("Fortran procedure name conflict:")
                assert str(e).endswith("  -----------------^")
            else:
                assert str(e).startswith("""\
Fortran procedure name conflict:
  1. definition: program_unnamed (implied)
    before """)
                assert str(e).endswith("implied_program.f(2)")
        else:
            raise Exception_expected
コード例 #15
0
ファイル: tst_read.py プロジェクト: cctbx/cctbx-playground
 def get(file_name):
   if (verbose):
     print "exercise_lenient:", file_name
   return read.process(file_names=[op.join(t_dir, file_name)])
コード例 #16
0
 def get_fprocs(file_name):
     if (verbose):
         print("exercise_valid:", file_name)
     read_already.add(file_name)
     return read.process(file_names=[op.join(t_dir, file_name)])
コード例 #17
0
 def fail(file_name):
     if (verbose):
         print("exercise_unsupported:", file_name)
     read.process(file_names=[op.join(t_dir, file_name)])
コード例 #18
0
 def fail(file_name):
     if (verbose):
         print("exercise_semantic_error:", file_name)
     read.process(file_names=[op.join(t_dir, file_name)])
コード例 #19
0
 def get(file_name):
     if (verbose):
         print("exercise_lenient:", file_name)
     return read.process(file_names=[op.join(t_dir, file_name)])
コード例 #20
0
ファイル: tst_read.py プロジェクト: cctbx/cctbx-playground
 def get_fprocs(file_name):
   if (verbose):
     print "exercise_valid:", file_name
   read_already.add(file_name)
   return read.process(file_names=[op.join(t_dir, file_name)])
コード例 #21
0
 def fail(file_name):
     if (verbose):
         print "exercise_syntax_error:", file_name
     read.process(file_names=[op.join(t_dir, file_name)])
コード例 #22
0
ファイル: tst_read.py プロジェクト: cctbx/cctbx-playground
 def fail(file_name):
   if (verbose):
     print "exercise_semantic_error:", file_name
   read.process(file_names=[op.join(t_dir, file_name)])
コード例 #23
0
def run(args):
    if (len(args) == 0): args = ["--help"]
    from libtbx.option_parser import option_parser
    import libtbx.load_env
    command_line = (option_parser(usage="%s [options] fortran_file ..." %
                                  libtbx.env.dispatcher_name).option(
                                      None,
                                      "--top_procedure",
                                      action="append",
                                      type="str").option(
                                          None,
                                          "--write_graphviz_dot",
                                          action="store",
                                          type="str")).process(args=args)
    co = command_line.options
    from fable.read import process
    all_fprocs = process(file_names=command_line.args)
    topological_fprocs = all_fprocs.build_bottom_up_fproc_list_following_calls(
        top_procedures=co.top_procedure)
    dep_cycles = topological_fprocs.dependency_cycles
    if (len(dep_cycles) != 0):
        print "Dependency cycles:", len(dep_cycles)
        for cycle in dep_cycles:
            print " ", " ".join(cycle)
        print
    print "Top-down procedure list:"
    print
    digraph_lhs_rhs = []
    for fproc in reversed(topological_fprocs.bottom_up_list):
        if (fproc.name is None):
            lhs = fproc.fproc_type
            print lhs
        else:
            lhs = fproc.name.value
            print fproc.fproc_type, fproc.name.value
        fwds = set(
            topological_fprocs.forward_uses_by_identifier.get(
                fproc.name.value, []))
        for identifier in sorted(fproc.fdecl_by_identifier.keys()):
            fdecl = fproc.fdecl_by_identifier[identifier]
            if (fdecl.is_fproc_name()): continue
            if (not fdecl.is_user_defined_callable()):
                continue
            called_name = fdecl.id_tok.value
            passed = fproc.externals_passed_by_arg_identifier.get(called_name)
            if (passed is None):
                digraph_lhs_rhs.append((lhs, called_name))
            else:
                called_name += "->" + ",".join(sorted(passed))
                for indirectly_called_name in passed:
                    digraph_lhs_rhs.append((lhs, indirectly_called_name))
            if (fdecl.is_function()):
                sz = ""
                if (fdecl.size_tokens is not None):
                    if (len(fdecl.size_tokens) == 1
                            and fdecl.size_tokens[0].is_integer()):
                        sz = "*%s" % fdecl.size_tokens[0].value
                    else:
                        sz = "*(*)"
                s = "%s (%s%s)" % (called_name, fdecl.data_type.value, sz)
            else:
                s = called_name
            if (called_name in fwds):
                s += " (dependency cycle)"
            print "  %s" % s
    print
    if (co.write_graphviz_dot is not None):
        f = open(co.write_graphviz_dot, "w")
        print >> f, "digraph G {"
        for lhs_rhs in digraph_lhs_rhs:
            print >> f, "  %s -> %s;" % lhs_rhs
        print >> f, "}"
        del f
コード例 #24
0
ファイル: tst_read.py プロジェクト: cctbx/cctbx-playground
 def fail(file_name):
   if (verbose):
     print "exercise_unsupported:", file_name
   read.process(file_names=[op.join(t_dir, file_name)])
コード例 #25
0
ファイル: show_calls.py プロジェクト: zhuligs/cctbx_project
def run(args):
    if not args: args = ["--help"]
    parser = optparse.OptionParser(
        usage="fable.show_calls [options] fortran_file ...")
    parser.add_option("-?", action="help", help=optparse.SUPPRESS_HELP)
    parser.add_option("--top_procedure", action="append", type="str")
    parser.add_option("--top-procedure",
                      action="append",
                      type="str",
                      help=optparse.SUPPRESS_HELP)
    parser.add_option("--write_graphviz_dot", action="store", type="str")
    parser.add_option("--write-graphviz-dot",
                      action="store",
                      type="str",
                      help=optparse.SUPPRESS_HELP)
    option, files = parser.parse_args(args)
    from fable.read import process
    all_fprocs = process(file_names=files)
    topological_fprocs = all_fprocs.build_bottom_up_fproc_list_following_calls(
        top_procedures=option.top_procedure)
    dep_cycles = topological_fprocs.dependency_cycles
    if len(dep_cycles) != 0:
        print "Dependency cycles:", len(dep_cycles)
        for cycle in dep_cycles:
            print " ", " ".join(cycle)
        print
    print "Top-down procedure list:"
    print
    digraph_lhs_rhs = []
    for fproc in reversed(topological_fprocs.bottom_up_list):
        if (fproc.name is None):
            lhs = fproc.fproc_type
            print lhs
        else:
            lhs = fproc.name.value
            print fproc.fproc_type, fproc.name.value
        fwds = set(
            topological_fprocs.forward_uses_by_identifier.get(
                fproc.name.value, []))
        for identifier in sorted(fproc.fdecl_by_identifier.keys()):
            fdecl = fproc.fdecl_by_identifier[identifier]
            if fdecl.is_fproc_name(): continue
            if not fdecl.is_user_defined_callable():
                continue
            called_name = fdecl.id_tok.value
            passed = fproc.externals_passed_by_arg_identifier.get(called_name)
            if passed is None:
                digraph_lhs_rhs.append((lhs, called_name))
            else:
                called_name += "->" + ",".join(sorted(passed))
                for indirectly_called_name in passed:
                    digraph_lhs_rhs.append((lhs, indirectly_called_name))
            if fdecl.is_function():
                sz = ""
                if fdecl.size_tokens:
                    if (len(fdecl.size_tokens) == 1
                            and fdecl.size_tokens[0].is_integer()):
                        sz = "*%s" % fdecl.size_tokens[0].value
                    else:
                        sz = "*(*)"
                s = "%s (%s%s)" % (called_name, fdecl.data_type.value, sz)
            else:
                s = called_name
            if called_name in fwds:
                s += " (dependency cycle)"
            print "  %s" % s
    print
    if option.write_graphviz_dot:
        with open(option.write_graphviz_dot, "w") as f:
            print >> f, "digraph G {"
            for lhs_rhs in digraph_lhs_rhs:
                print >> f, "  %s -> %s;" % lhs_rhs
            print >> f, "}"
コード例 #26
0
ファイル: show_calls.py プロジェクト: cctbx/cctbx-playground
def run(args):
  if (len(args) == 0): args = ["--help"]
  from libtbx.option_parser import option_parser
  import libtbx.load_env
  command_line = (option_parser(
    usage="%s [options] fortran_file ..." % libtbx.env.dispatcher_name)
    .option(None, "--top_procedure",
      action="append",
      type="str")
    .option(None, "--write_graphviz_dot",
      action="store",
      type="str")
  ).process(args=args)
  co = command_line.options
  from fable.read import process
  all_fprocs = process(file_names=command_line.args)
  topological_fprocs = all_fprocs.build_bottom_up_fproc_list_following_calls(
    top_procedures=co.top_procedure)
  dep_cycles = topological_fprocs.dependency_cycles
  if (len(dep_cycles) != 0):
    print "Dependency cycles:", len(dep_cycles)
    for cycle in dep_cycles:
      print " ", " ".join(cycle)
    print
  print "Top-down procedure list:"
  print
  digraph_lhs_rhs = []
  for fproc in reversed(topological_fprocs.bottom_up_list):
    if (fproc.name is None):
      lhs = fproc.fproc_type
      print lhs
    else:
      lhs = fproc.name.value
      print fproc.fproc_type, fproc.name.value
    fwds = set(
      topological_fprocs.forward_uses_by_identifier.get(
        fproc.name.value, []))
    for identifier in sorted(fproc.fdecl_by_identifier.keys()):
      fdecl = fproc.fdecl_by_identifier[identifier]
      if (fdecl.is_fproc_name()): continue
      if (not fdecl.is_user_defined_callable()):
        continue
      called_name = fdecl.id_tok.value
      passed = fproc.externals_passed_by_arg_identifier.get(called_name)
      if (passed is None):
        digraph_lhs_rhs.append((lhs, called_name))
      else:
        called_name += "->" + ",".join(sorted(passed))
        for indirectly_called_name in passed:
          digraph_lhs_rhs.append((lhs, indirectly_called_name))
      if (fdecl.is_function()):
        sz = ""
        if (fdecl.size_tokens is not None):
          if (len(fdecl.size_tokens) == 1
                and fdecl.size_tokens[0].is_integer()):
            sz = "*%s" % fdecl.size_tokens[0].value
          else:
            sz = "*(*)"
        s = "%s (%s%s)" % (called_name, fdecl.data_type.value, sz)
      else:
        s = called_name
      if (called_name in fwds):
        s += " (dependency cycle)"
      print "  %s" % s
  print
  if (co.write_graphviz_dot is not None):
    f = open(co.write_graphviz_dot, "w")
    print >> f, "digraph G {"
    for lhs_rhs in digraph_lhs_rhs:
      print >> f, "  %s -> %s;" % lhs_rhs
    print >> f, "}"
    del f