src/Pure/System/scala_compiler.ML
author wenzelm
Mon, 25 May 2020 20:43:19 +0200
changeset 71888 feb37a43ace6
parent 71887 src/Pure/System/scala_check.ML@f7d15620dd8e
child 71890 3b35b0fd7fe8
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71888
feb37a43ace6 clarified signature;
wenzelm
parents: 71887
diff changeset
     1
(*  Title:      Pure/System/scala_compiler.ML
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
     3
71888
feb37a43ace6 clarified signature;
wenzelm
parents: 71887
diff changeset
     4
Scala compiler operations.
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
     5
*)
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
     6
71888
feb37a43ace6 clarified signature;
wenzelm
parents: 71887
diff changeset
     7
signature SCALA_COMPILER =
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
     8
sig
71888
feb37a43ace6 clarified signature;
wenzelm
parents: 71887
diff changeset
     9
  val toplevel: string -> unit
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    10
  val declaration: string -> unit
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    11
end;
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    12
71888
feb37a43ace6 clarified signature;
wenzelm
parents: 71887
diff changeset
    13
structure Scala_Compiler: SCALA_COMPILER =
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    14
struct
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    15
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    16
(* check declaration *)
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    17
71888
feb37a43ace6 clarified signature;
wenzelm
parents: 71887
diff changeset
    18
fun toplevel source =
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    19
  let val errors =
71888
feb37a43ace6 clarified signature;
wenzelm
parents: 71887
diff changeset
    20
    \<^scala>\<open>scala_toplevel\<close> source
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    21
    |> YXML.parse_body
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    22
    |> let open XML.Decode in list string end
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    23
  in if null errors then () else error (cat_lines errors) end;
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    24
71888
feb37a43ace6 clarified signature;
wenzelm
parents: 71887
diff changeset
    25
fun declaration source =
feb37a43ace6 clarified signature;
wenzelm
parents: 71887
diff changeset
    26
  toplevel ("package test\nobject Test { " ^ source ^ " }");
feb37a43ace6 clarified signature;
wenzelm
parents: 71887
diff changeset
    27
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    28
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    29
(* antiquotations *)
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    30
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    31
local
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    32
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    33
fun make_list bg en = space_implode "," #> enclose bg en;
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    34
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    35
fun print_args [] = ""
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    36
  | print_args xs = make_list "(" ")" xs;
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    37
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    38
fun print_types [] = ""
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    39
  | print_types Ts = make_list "[" "]" Ts;
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    40
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    41
fun print_class (c, Ts) = c ^ print_types Ts;
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    42
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    43
val types =
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    44
  Scan.optional (Parse.$$$ "[" |-- Parse.list1 Parse.name --| Parse.$$$ "]") [];
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    45
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    46
val in_class =
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    47
  Parse.$$$ "(" |-- Parse.!!! (Parse.$$$ "in" |-- Parse.name -- types  --| Parse.$$$ ")");
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    48
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    49
val arguments =
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    50
  (Parse.nat >> (fn n => replicate n "_") ||
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    51
    Parse.list (Parse.underscore || Parse.name >> (fn T => "_ : " ^ T))) >> print_args
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    52
  || Scan.succeed " _";
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    53
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    54
val method_arguments = Scan.optional (Parse.$$$ "(" |-- arguments --| Parse.$$$ ")") " _";
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    55
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    56
in
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    57
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    58
val _ =
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    59
  Theory.setup
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    60
    (Thy_Output.antiquotation_verbatim_embedded \<^binding>\<open>scala_type\<close>
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    61
      (Scan.lift (Parse.embedded -- (types >> print_types)))
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    62
      (fn _ => fn (t, type_args) =>
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    63
        (declaration ("type _Test_" ^ type_args ^ " = " ^ t ^ type_args); t ^ type_args)) #>
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    64
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    65
    Thy_Output.antiquotation_verbatim_embedded \<^binding>\<open>scala_object\<close>
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    66
      (Scan.lift Parse.embedded)
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    67
      (fn _ => fn object => (declaration ("val _test_ = " ^ object); object)) #>
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    68
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    69
    Thy_Output.antiquotation_verbatim_embedded \<^binding>\<open>scala_method\<close>
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    70
      (Scan.lift (Scan.option in_class -- Parse.embedded -- types -- method_arguments))
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    71
      (fn _ => fn (((class, method), method_types), method_args) =>
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    72
        let
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    73
          val class_types = (case class of SOME (_, Ts) => Ts | NONE => []);
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    74
          val def = "def _test_" ^ print_types (merge (op =) (method_types, class_types));
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    75
          val def_context =
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    76
            (case class of
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    77
              NONE => def ^ " = "
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    78
            | SOME c => def ^ "(_this_ : " ^ print_class c ^ ") = _this_.");
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    79
          val source = def_context ^ method ^ method_args;
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    80
          val _ = declaration source;
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    81
        in (case class of NONE => method | SOME c => print_class c ^ "." ^ method) end));
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    82
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    83
end;
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    84
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    85
end;