src/Pure/System/scala_compiler.ML
author wenzelm
Fri, 21 May 2021 12:29:29 +0200
changeset 73761 ef1a18e20ace
parent 72294 25c6423ec538
child 73780 466fae6bf22e
permissions -rw-r--r--
clarified modules;
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
72294
25c6423ec538 clarified signature: proper eval/print via interpret;
wenzelm
parents: 71906
diff changeset
     9
  val toplevel: bool -> string -> unit
71906
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    10
  val static_check: string * Position.T -> 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
72294
25c6423ec538 clarified signature: proper eval/print via interpret;
wenzelm
parents: 71906
diff changeset
    18
fun toplevel interpret source =
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    19
  let val errors =
72294
25c6423ec538 clarified signature: proper eval/print via interpret;
wenzelm
parents: 71906
diff changeset
    20
    (interpret, source)
25c6423ec538 clarified signature: proper eval/print via interpret;
wenzelm
parents: 71906
diff changeset
    21
    |> let open XML.Encode in pair bool string end
25c6423ec538 clarified signature: proper eval/print via interpret;
wenzelm
parents: 71906
diff changeset
    22
    |> YXML.string_of_body
25c6423ec538 clarified signature: proper eval/print via interpret;
wenzelm
parents: 71906
diff changeset
    23
    |> \<^scala>\<open>scala_toplevel\<close>
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    24
    |> YXML.parse_body
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    25
    |> let open XML.Decode in list string end
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    26
  in if null errors then () else error (cat_lines errors) end;
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    27
71906
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    28
fun static_check (source, pos) =
72294
25c6423ec538 clarified signature: proper eval/print via interpret;
wenzelm
parents: 71906
diff changeset
    29
  toplevel false ("package test\nclass __Dummy__ { __dummy__ => " ^ source ^ " }")
71906
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    30
    handle ERROR msg => error (msg ^ Position.here pos);
71888
feb37a43ace6 clarified signature;
wenzelm
parents: 71887
diff changeset
    31
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    32
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    33
(* antiquotations *)
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    34
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    35
local
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    36
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    37
fun make_list bg en = space_implode "," #> enclose bg en;
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    38
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    39
fun print_args [] = ""
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    40
  | print_args xs = make_list "(" ")" xs;
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    41
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    42
fun print_types [] = ""
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    43
  | print_types Ts = make_list "[" "]" Ts;
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    44
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    45
fun print_class (c, Ts) = c ^ print_types Ts;
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    46
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    47
val types =
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    48
  Scan.optional (Parse.$$$ "[" |-- Parse.list1 Parse.name --| Parse.$$$ "]") [];
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    49
71905
wenzelm
parents: 71900
diff changeset
    50
val class =
wenzelm
parents: 71900
diff changeset
    51
  Scan.option
wenzelm
parents: 71900
diff changeset
    52
    (Parse.$$$ "(" |-- Parse.!!! (Parse.$$$ "in" |-- Parse.name -- types  --| Parse.$$$ ")"));
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    53
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    54
val arguments =
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    55
  (Parse.nat >> (fn n => replicate n "_") ||
71905
wenzelm
parents: 71900
diff changeset
    56
    Parse.list (Parse.underscore || Parse.name >> (fn T => "_ : " ^ T))) >> print_args;
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    57
71905
wenzelm
parents: 71900
diff changeset
    58
val args = Scan.optional (Parse.$$$ "(" |-- arguments --| Parse.$$$ ")") " _";
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    59
71900
f08cf9d8f90b breakable scala_name;
wenzelm
parents: 71891
diff changeset
    60
fun scala_name name =
f08cf9d8f90b breakable scala_name;
wenzelm
parents: 71891
diff changeset
    61
  let val latex = Latex.string (Latex.output_ascii_breakable "." name)
f08cf9d8f90b breakable scala_name;
wenzelm
parents: 71891
diff changeset
    62
  in Latex.enclose_block "\\isatt{" "}" [latex] end;
f08cf9d8f90b breakable scala_name;
wenzelm
parents: 71891
diff changeset
    63
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    64
in
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    65
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    66
val _ =
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    67
  Theory.setup
73761
ef1a18e20ace clarified modules;
wenzelm
parents: 72294
diff changeset
    68
    (Document_Output.antiquotation_verbatim_embedded \<^binding>\<open>scala\<close>
71906
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    69
      (Scan.lift Args.embedded_position)
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    70
      (fn _ => fn (s, pos) => (static_check (s, pos); s)) #>
71891
1b023a4498c3 check free-form Scala source;
wenzelm
parents: 71890
diff changeset
    71
73761
ef1a18e20ace clarified modules;
wenzelm
parents: 72294
diff changeset
    72
    Document_Output.antiquotation_raw_embedded \<^binding>\<open>scala_type\<close>
71906
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    73
      (Scan.lift (Args.embedded_position -- (types >> print_types)))
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    74
      (fn _ => fn ((t, pos), type_args) =>
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    75
        (static_check ("type _Test_" ^ type_args ^ " = " ^ t ^ type_args, pos);
71900
f08cf9d8f90b breakable scala_name;
wenzelm
parents: 71891
diff changeset
    76
          scala_name (t ^ type_args))) #>
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    77
73761
ef1a18e20ace clarified modules;
wenzelm
parents: 72294
diff changeset
    78
    Document_Output.antiquotation_raw_embedded \<^binding>\<open>scala_object\<close>
71906
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    79
      (Scan.lift Args.embedded_position)
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    80
      (fn _ => fn (x, pos) => (static_check ("val _test_ = " ^ x, pos); scala_name x)) #>
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    81
73761
ef1a18e20ace clarified modules;
wenzelm
parents: 72294
diff changeset
    82
    Document_Output.antiquotation_raw_embedded \<^binding>\<open>scala_method\<close>
71906
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    83
      (Scan.lift (class -- Args.embedded_position -- types -- args))
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    84
      (fn _ => fn (((class_context, (method, pos)), method_types), method_args) =>
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    85
        let
71905
wenzelm
parents: 71900
diff changeset
    86
          val class_types = (case class_context of SOME (_, Ts) => Ts | NONE => []);
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    87
          val def = "def _test_" ^ print_types (merge (op =) (method_types, class_types));
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    88
          val def_context =
71905
wenzelm
parents: 71900
diff changeset
    89
            (case class_context of
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    90
              NONE => def ^ " = "
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    91
            | SOME c => def ^ "(_this_ : " ^ print_class c ^ ") = _this_.");
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    92
          val source = def_context ^ method ^ method_args;
71906
a63072d875d1 proper error positions;
wenzelm
parents: 71905
diff changeset
    93
          val _ = static_check (source, pos);
71905
wenzelm
parents: 71900
diff changeset
    94
          val text = (case class_context of NONE => method | SOME c => print_class c ^ "." ^ method);
wenzelm
parents: 71900
diff changeset
    95
        in scala_name text end));
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    96
71881
71de0a253842 proper check of registered Scala functions;
wenzelm
parents:
diff changeset
    97
end;
71887
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    98
f7d15620dd8e antiquotations for Scala entities;
wenzelm
parents: 71881
diff changeset
    99
end;