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