From 9cbbf331a103d411ead15d57f83d3b6ed4b044d4 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 20 Aug 2019 23:36:11 -0400 Subject: Moved signature machinery to "lux/target/jvm/type/signature". --- stdlib/source/lux/target/jvm/type.lux | 110 ------------------ stdlib/source/lux/target/jvm/type/descriptor.lux | 41 ++++--- stdlib/source/lux/target/jvm/type/lux.lux | 2 +- stdlib/source/lux/target/jvm/type/reflection.lux | 2 +- stdlib/source/lux/target/jvm/type/signature.lux | 138 +++++++++++++++++++++++ 5 files changed, 160 insertions(+), 133 deletions(-) create mode 100644 stdlib/source/lux/target/jvm/type/signature.lux (limited to 'stdlib/source') diff --git a/stdlib/source/lux/target/jvm/type.lux b/stdlib/source/lux/target/jvm/type.lux index 4601003a8..a74fab40a 100644 --- a/stdlib/source/lux/target/jvm/type.lux +++ b/stdlib/source/lux/target/jvm/type.lux @@ -38,7 +38,6 @@ (def: lower-prefix "-") (def: upper-prefix "+") (def: object-suffix ";") -(def: #export object-class "java.lang.Object") (def: valid-var-characters/head (format "abcdefghijklmnopqrstuvwxyz" @@ -160,98 +159,9 @@ (type: #export Argument [Text Type]) -(type: #export Return - (Maybe Type)) - -(def: #export return-equivalence - (Equivalence Return) - (maybe.equivalence ..type-equivalence)) - -(type: #export Method - {#args (List Type) - #return Return - #exceptions (List Generic)}) - -(structure: #export method-equivalence - (Equivalence Method) - (def: (= [argsP returnP exceptionsP] [argsS returnS exceptionsS]) - (and (:: (list.equivalence ..type-equivalence) = argsP argsS) - (:: ..return-equivalence = returnP returnS) - (:: (list.equivalence ..generic-equivalence) = exceptionsP exceptionsS)))) - (type: #export (Typed a) [Type a]) -(template [ ] - [(def: #export Type (#Primitive ))] - - [boolean #Boolean] - [byte #Byte] - [short #Short] - [int #Int] - [long #Long] - [float #Float] - [double #Double] - [char #Char] - ) - -(template: #export (class name params) - (#..Generic (#..Class name params))) - -(template: #export (var name) - (#..Generic (#..Var name))) - -(template: #export (wildcard bound) - (#..Generic (#..Wildcard bound))) - -(def: #export (array depth elemT) - (-> Nat Type Type) - (case depth - 0 elemT - _ (#Array (array (dec depth) elemT)))) - -(def: #export (signature type) - (-> Type Text) - (case type - (#Primitive prim) - (case prim - #Boolean ..boolean-descriptor - #Byte ..byte-descriptor - #Short ..short-descriptor - #Int ..int-descriptor - #Long ..long-descriptor - #Float ..float-descriptor - #Double ..double-descriptor - #Char ..char-descriptor) - - (#Array sub) - (format ..array-prefix (signature sub)) - - (#Generic generic) - (case generic - (#Class class params) - (let [=params (if (list.empty? params) - "" - (format "<" - (|> params - (list@map (|>> #Generic signature)) - (text.join-with "")) - ">"))] - (format ..object-prefix (|> class //name.internal //name.read) =params ..object-suffix)) - - (#Var name) - (format ..var-prefix name ..object-suffix) - - (#Wildcard #.None) - ..wildcard-signature - - (^template [ ] - (#Wildcard (#.Some [ bound])) - (format (signature (#Generic bound)))) - ([#Lower ..lower-prefix] - [#Upper ..upper-prefix])) - )) - (template [ ] [(def: (Parser Text) @@ -313,23 +223,3 @@ (<>.after (.this ..array-prefix) recur) )))) - -(def: #export (method args return exceptions) - (-> (List Type) (Maybe Type) (List Generic) Method) - {#args args #return return #exceptions exceptions}) - -(def: #export (method-signature method) - (-> Method Text) - (format (|> (get@ #args method) - (list@map signature) - (text.join-with "") - (text.enclose ["(" ")"])) - (case (get@ #return method) - #.None - ..void-descriptor - - (#.Some return) - (signature return)) - (|> (get@ #exceptions method) - (list@map (|>> #Generic signature (format "^"))) - (text.join-with "")))) diff --git a/stdlib/source/lux/target/jvm/type/descriptor.lux b/stdlib/source/lux/target/jvm/type/descriptor.lux index 52b5037bc..b47f0edf3 100644 --- a/stdlib/source/lux/target/jvm/type/descriptor.lux +++ b/stdlib/source/lux/target/jvm/type/descriptor.lux @@ -20,40 +20,44 @@ (abstract: #export Void' {} Any) -(abstract: #export (Value' kind) {} Any) -(abstract: #export (Return kind) {} Any) +(abstract: #export (Value' brand) {} Any) +(abstract: #export (Return' brand) {} Any) (abstract: #export Method {} Any) -(abstract: #export (Descriptor kind) +(abstract: #export (Descriptor brand) {} Text - (type: #export Value (<| Return Value' Any)) - (type: #export Void (Return Void')) - - (type: #export Field Value) + (type: #export Return (<| Return' Any)) + (type: #export Value (<| Return' Value' Any)) + (type: #export Void (<| Return' Void')) (abstract: #export (Object' brand) {} Any) - (type: #export Object (<| Return Value' Object' Any)) + (type: #export Object (<| Return' Value' Object' Any)) (template [ ] [(with-expansions [ (template.identifier [ "'"])] (abstract: #export {} Any) (type: #export - (`` (<| Return Value' (~~ (template.splice )) ))))] + (`` (<| Return' Value' (~~ (template.splice )) ))))] [[] Primitive] [[Object'] Class] [[Object'] Array] ) - (template [ ] + (def: #export descriptor + (-> (Descriptor Any) Text) + (|>> :representation)) + + (template [ ] [(def: #export - (Descriptor ) + (Descriptor ) (:abstraction ))] + ["V" Void void] ["Z" Primitive boolean] ["B" Primitive byte] ["S" Primitive short] @@ -62,11 +66,10 @@ ["F" Primitive float] ["D" Primitive double] ["C" Primitive char] - ["V" Void void] ) - (def: class-prefix "L") - (def: class-suffix ";") + (def: #export class-prefix "L") + (def: #export class-suffix ";") (def: #export class (-> External (Descriptor Class)) @@ -78,7 +81,7 @@ (def: #export var (..class "java.lang.Object")) (def: #export wildcard (..class "java.lang.Object")) - (def: array-prefix "[") + (def: #export array-prefix "[") (def: #export array (-> (Descriptor Value) @@ -89,19 +92,15 @@ (def: #export (method [inputs output]) (-> [(List (Descriptor Value)) - (Descriptor (Return Any))] + (Descriptor Return)] (Descriptor Method)) (:abstraction (format (|> inputs - (list@map (|>> :representation)) + (list@map ..descriptor) (text.join-with "") (text.enclose ["(" ")"])) (:representation output)))) - (def: #export descriptor - (-> (Descriptor Any) Text) - (|>> :representation)) - (structure: #export equivalence (All [brand] (Equivalence (Descriptor brand))) diff --git a/stdlib/source/lux/target/jvm/type/lux.lux b/stdlib/source/lux/target/jvm/type/lux.lux index 1ebf4b74e..243861bd4 100644 --- a/stdlib/source/lux/target/jvm/type/lux.lux +++ b/stdlib/source/lux/target/jvm/type/lux.lux @@ -106,7 +106,7 @@ (type mapping elementT))))) (def: #export (return mapping input) - (-> Mapping //.Return (Check Type)) + (-> Mapping (Maybe //.Type) (Check Type)) (case input #.None (check@wrap Any) diff --git a/stdlib/source/lux/target/jvm/type/reflection.lux b/stdlib/source/lux/target/jvm/type/reflection.lux index b74a4ea30..b49205f5e 100644 --- a/stdlib/source/lux/target/jvm/type/reflection.lux +++ b/stdlib/source/lux/target/jvm/type/reflection.lux @@ -44,7 +44,7 @@ (^or (#//.Var name) (#//.Wildcard #.None) (#//.Wildcard (#.Some [#//.Lower bound]))) - //.object-class + "java.lang.Object" (#//.Wildcard (#.Some [#//.Upper bound])) (class (#//.Generic bound))) diff --git a/stdlib/source/lux/target/jvm/type/signature.lux b/stdlib/source/lux/target/jvm/type/signature.lux new file mode 100644 index 000000000..d19708f1e --- /dev/null +++ b/stdlib/source/lux/target/jvm/type/signature.lux @@ -0,0 +1,138 @@ +(.module: + [lux (#- int char) + [abstract + [equivalence (#+ Equivalence)]] + [data + ["." text ("#@." equivalence) + ["%" format (#+ format)]] + [collection + ["." list ("#@." functor)]]] + [macro + ["." template]] + [type + abstract]] + ["." // #_ + ["#." descriptor] + ["/#" // #_ + [encoding + ["#." name]]]]) + +(abstract: #export Void' {} Any) +(abstract: #export (Value' kind) {} Any) +(abstract: #export (Return' kind) {} Any) +(abstract: #export Method {} Any) + +(abstract: #export (Signature brand) + {} + + Text + + (type: #export Return (<| Return' Any)) + (type: #export Value (<| Return' Value' Any)) + (type: #export Void (<| Return' Void')) + + (abstract: #export (Object' brand) {} Any) + (type: #export Object (<| Return' Value' Object' Any)) + + (abstract: #export (Parameter' brand) {} Any) + (type: #export Parameter (<| Return' Value' Object' Parameter' Any)) + + (template [ ] + [(with-expansions [ (template.identifier [ "'"])] + (abstract: #export {} Any) + (type: #export + (`` (<| Return' Value' (~~ (template.splice )) ))))] + + [[] Primitive] + [[Object' Parameter'] Class] + [[Object'] Array] + ) + + (def: #export signature + (-> (Signature Any) Text) + (|>> :representation)) + + (template [ ] + [(def: #export + (Signature ) + (:abstraction (//descriptor.descriptor )))] + + [Void void //descriptor.void] + [Primitive boolean //descriptor.boolean] + [Primitive byte //descriptor.byte] + [Primitive short //descriptor.short] + [Primitive int //descriptor.int] + [Primitive long //descriptor.long] + [Primitive float //descriptor.float] + [Primitive double //descriptor.double] + [Primitive char //descriptor.char] + ) + + (def: #export array + (-> (Signature Value) (Signature Array)) + (|>> :representation + (format //descriptor.array-prefix) + :abstraction)) + + (def: #export wildcard + (Signature Parameter) + (:abstraction "*")) + + (def: var-prefix "T") + + (def: #export var + (-> Text (Signature Parameter)) + (|>> (text.enclose [..var-prefix //descriptor.class-suffix]) + :abstraction)) + + (def: lower-prefix "-") + (def: upper-prefix "+") + + (template [ ] + [(def: #export + (-> (Signature Class) (Signature Parameter)) + (|>> :representation (format ) :abstraction))] + + [lower ..lower-prefix] + [upper ..upper-prefix] + ) + + (def: #export (class name parameters) + (-> Text (List (Signature Parameter)) (Signature Class)) + (:abstraction + (format //descriptor.class-prefix + (|> name ///name.internal ///name.read) + (case parameters + #.Nil + "" + + _ + (format "<" + (|> parameters + (list@map ..signature) + (text.join-with "")) + ">")) + //descriptor.class-suffix))) + + (def: #export (method [inputs output exceptions]) + (-> [(List (Signature Value)) + (Signature Return) + (List (Signature Class))] + (Signature Method)) + (:abstraction + (format (|> inputs + (list@map ..signature) + (text.join-with "") + (text.enclose ["(" ")"])) + (:representation output) + (|> exceptions + (list@map (|>> :representation (format "^"))) + (text.join-with ""))))) + + (structure: #export equivalence + (All [brand] (Equivalence (Signature brand))) + + (def: (= parameter subject) + (text@= (:representation parameter) + (:representation subject)))) + ) -- cgit v1.2.3