From b63ac226cc2ea843f08f7c72b18d22602462c624 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sat, 7 Sep 2019 01:50:37 -0400 Subject: Modified compiler's machinery to use the new abstractions for descriptors and signatures. --- stdlib/source/lux/control/parser/code.lux | 11 - stdlib/source/lux/control/parser/text.lux | 11 +- stdlib/source/lux/control/try.lux | 9 + stdlib/source/lux/macro/syntax/common/reader.lux | 2 +- stdlib/source/lux/target/jvm/reflection.lux | 185 ++--- stdlib/source/lux/target/jvm/type.lux | 200 ++--- stdlib/source/lux/target/jvm/type/alias.lux | 112 +++ stdlib/source/lux/target/jvm/type/box.lux | 7 +- stdlib/source/lux/target/jvm/type/category.lux | 1 + stdlib/source/lux/target/jvm/type/descriptor.lux | 49 +- stdlib/source/lux/target/jvm/type/lux.lux | 138 ++-- stdlib/source/lux/target/jvm/type/parser.lux | 195 +++++ stdlib/source/lux/target/jvm/type/reflection.lux | 10 +- stdlib/source/lux/target/jvm/type/signature.lux | 4 +- .../tool/compiler/phase/extension/analysis/jvm.lux | 823 +++++++++------------ .../compiler/phase/extension/statement/lux.lux | 6 +- 16 files changed, 950 insertions(+), 813 deletions(-) create mode 100644 stdlib/source/lux/target/jvm/type/alias.lux create mode 100644 stdlib/source/lux/target/jvm/type/parser.lux (limited to 'stdlib/source') diff --git a/stdlib/source/lux/control/parser/code.lux b/stdlib/source/lux/control/parser/code.lux index b20f707a3..5ea2247d6 100644 --- a/stdlib/source/lux/control/parser/code.lux +++ b/stdlib/source/lux/control/parser/code.lux @@ -156,17 +156,6 @@ #.Nil (#try.Success [tokens #1]) _ (#try.Success [tokens #0])))) -(def: #export (lift outcome) - (All [a] (-> (Try a) (Parser a))) - (function (_ input) - (case outcome - (#try.Failure error) - (#try.Failure error) - - (#try.Success value) - (#try.Success [input value]) - ))) - (def: #export (run syntax inputs) (All [a] (-> (Parser a) (List Code) (Try a))) (case (syntax inputs) diff --git a/stdlib/source/lux/control/parser/text.lux b/stdlib/source/lux/control/parser/text.lux index bec295f39..44d568eaf 100644 --- a/stdlib/source/lux/control/parser/text.lux +++ b/stdlib/source/lux/control/parser/text.lux @@ -345,7 +345,7 @@ {#.doc "Run a parser with the given input, instead of the real one."} (All [a] (-> Text (Parser a) (Parser a))) (function (_ real-input) - (case (run parser local-input) + (case (..run parser local-input) (#try.Failure error) (#try.Failure error) @@ -363,3 +363,12 @@ #.None (exception.throw ..cannot-slice []))))) + +(def: #export (embed structured text) + (All [s a] + (-> (Parser a) + (//.Parser s Text) + (//.Parser s a))) + (do //.monad + [raw text] + (//.lift (..run structured raw)))) diff --git a/stdlib/source/lux/control/try.lux b/stdlib/source/lux/control/try.lux index 20d4dcab7..3b27fd6a3 100644 --- a/stdlib/source/lux/control/try.lux +++ b/stdlib/source/lux/control/try.lux @@ -110,6 +110,15 @@ (#Failure message) (error! message))) +(def: #export (maybe try) + (All [a] (-> (Try a) (Maybe a))) + (case try + (#Success value) + (#.Some value) + + (#Failure message) + #.None)) + (macro: #export (default tokens compiler) {#.doc (doc "Allows you to provide a default value that will be used" "if a (Try x) value turns out to be #Failure." diff --git a/stdlib/source/lux/macro/syntax/common/reader.lux b/stdlib/source/lux/macro/syntax/common/reader.lux index bd8e3953b..02d947e47 100644 --- a/stdlib/source/lux/macro/syntax/common/reader.lux +++ b/stdlib/source/lux/macro/syntax/common/reader.lux @@ -108,7 +108,7 @@ me-definition-raw (|> definition-raw ////.expand-all (////.run compiler) - s.lift)] + p.lift)] (s.local me-definition-raw (s.form (do @ [_ (s.text! "lux def") diff --git a/stdlib/source/lux/target/jvm/reflection.lux b/stdlib/source/lux/target/jvm/reflection.lux index e6ee7e630..992ac9977 100644 --- a/stdlib/source/lux/target/jvm/reflection.lux +++ b/stdlib/source/lux/target/jvm/reflection.lux @@ -20,10 +20,13 @@ ["." dictionary]]]] ["." // #_ [encoding - ["#." name]] + ["#." name (#+ External)]] ["/" type + [category (#+ Void Value Return Method Primitive Object Class Array Parameter)] ["#." lux (#+ Mapping)] - ["." reflection]]]) + ["#." descriptor] + ["#." reflection] + ["#." parser]]]) (import: #long java/lang/String) @@ -88,7 +91,7 @@ (getConstructors [] [(java/lang/reflect/Constructor java/lang/Object)]) (getDeclaredMethods [] [java/lang/reflect/Method])) -(exception: #export (unknown-class {class Text}) +(exception: #export (unknown-class {class External}) (exception.report ["Class" (%.text class)])) @@ -103,7 +106,7 @@ ) (def: #export (load name) - (-> Text (Try (java/lang/Class java/lang/Object))) + (-> External (Try (java/lang/Class java/lang/Object))) (case (java/lang/Class::forName name) (#try.Success class) (#try.Success class) @@ -112,17 +115,63 @@ (exception.throw ..unknown-class name))) (def: #export (sub? super sub) - (-> Text Text (Try Bit)) + (-> External External (Try Bit)) (do try.monad [super (..load super) sub (..load sub)] (wrap (java/lang/Class::isAssignableFrom sub super)))) -(def: #export (generic reflection) - (-> java/lang/reflect/Type (Try /.Generic)) +(def: (class' parameter reflection) + (-> (-> java/lang/reflect/Type (Try (/.Type Parameter))) + java/lang/reflect/Type + (Try (/.Type Class))) + (<| (case (host.check java/lang/Class reflection) + (#.Some class) + (let [class-name (|> class + (:coerce (java/lang/Class java/lang/Object)) + java/lang/Class::getName)] + (`` (if (or (~~ (template [] + [(text@= (/reflection.reflection ) + class-name)] + + [/reflection.boolean] + [/reflection.byte] + [/reflection.short] + [/reflection.int] + [/reflection.long] + [/reflection.float] + [/reflection.double] + [/reflection.char])) + (text.starts-with? /descriptor.array-prefix class-name)) + (exception.throw ..not-a-class reflection) + (#try.Success (/.class class-name (list)))))) + _) + (case (host.check java/lang/reflect/ParameterizedType reflection) + (#.Some reflection) + (let [raw (java/lang/reflect/ParameterizedType::getRawType reflection)] + (case (host.check java/lang/Class raw) + (#.Some raw) + (do try.monad + [paramsT (|> reflection + java/lang/reflect/ParameterizedType::getActualTypeArguments + array.to-list + (monad.map @ parameter))] + (wrap (/.class (|> raw + (:coerce (java/lang/Class java/lang/Object)) + java/lang/Class::getName) + paramsT))) + + _ + (exception.throw ..not-a-class raw))) + _) + ## else + (exception.throw ..cannot-convert-to-a-lux-type reflection))) + +(def: #export (parameter reflection) + (-> java/lang/reflect/Type (Try (/.Type Parameter))) (<| (case (host.check java/lang/reflect/TypeVariable reflection) (#.Some reflection) - (#try.Success (#/.Var (java/lang/reflect/TypeVariable::getName reflection))) + (#try.Success (/.var (java/lang/reflect/TypeVariable::getName reflection))) _) (case (host.check java/lang/reflect/WildcardType reflection) (#.Some reflection) @@ -136,105 +185,69 @@ (#.Some _) ## TODO: Array bounds should not be "erased" as they ## are right now. - (#try.Success (#/.Wildcard #.None)) + (#try.Success /.wildcard) _ - (:: try.monad map - (|>> [] #.Some #/.Wildcard) - (generic bound)))) - ([[_ (#.Some bound)] #/.Upper] - [[(#.Some bound) _] #/.Lower]) + (:: try.monad map (..class' parameter bound)))) + ([[_ (#.Some bound)] /.upper] + [[(#.Some bound) _] /.lower]) _ - (#try.Success (#/.Wildcard #.None))) - _) - (case (host.check java/lang/Class reflection) - (#.Some class) - (let [class-name (|> class - (:coerce (java/lang/Class java/lang/Object)) - java/lang/Class::getName)] - (case class-name - (^template [] - (^ (static )) - (exception.throw ..not-a-class reflection)) - ([reflection.boolean] [reflection.byte] [reflection.short] [reflection.int] - [reflection.long] [reflection.float] [reflection.double] [reflection.char]) - - _ - (if (text.starts-with? /.array-prefix class-name) - (exception.throw ..not-a-class reflection) - (#try.Success (#/.Class class-name (list)))))) + (#try.Success /.wildcard)) _) - (case (host.check java/lang/reflect/ParameterizedType reflection) - (#.Some reflection) - (let [raw (java/lang/reflect/ParameterizedType::getRawType reflection)] - (case (host.check java/lang/Class raw) - (#.Some raw) - (do try.monad - [paramsT (|> reflection - java/lang/reflect/ParameterizedType::getActualTypeArguments - array.to-list - (monad.map @ generic))] - (wrap (#/.Class (|> raw - (:coerce (java/lang/Class java/lang/Object)) - java/lang/Class::getName) - paramsT))) + (..class' parameter reflection))) - _ - (exception.throw ..not-a-class raw))) - _) - ## else - (exception.throw ..cannot-convert-to-a-lux-type reflection))) +(def: #export class + (-> java/lang/reflect/Type + (Try (/.Type Class))) + (..class' ..parameter)) (def: #export (type reflection) - (-> java/lang/reflect/Type (Try /.Type)) + (-> java/lang/reflect/Type (Try (/.Type Value))) (<| (case (host.check java/lang/Class reflection) (#.Some reflection) - (case (|> reflection - (:coerce (java/lang/Class java/lang/Object)) - java/lang/Class::getName) - (^template [ ] - (^ (static )) - (#try.Success )) - ([reflection.boolean /.boolean] - [reflection.byte /.byte] - [reflection.short /.short] - [reflection.int /.int] - [reflection.long /.long] - [reflection.float /.float] - [reflection.double /.double] - [reflection.char /.char]) - - class-name - (if (text.starts-with? /.array-prefix class-name) - (.run /.parse-signature (|> class-name //name.internal //name.read)) - (#try.Success (/.class class-name (list))))) + (let [class-name (|> reflection + (:coerce (java/lang/Class java/lang/Object)) + java/lang/Class::getName)] + (`` (cond (~~ (template [ ] + [(text@= (/reflection.reflection ) + class-name) + (#try.Success )] + + [/reflection.boolean /.boolean] + [/reflection.byte /.byte] + [/reflection.short /.short] + [/reflection.int /.int] + [/reflection.long /.long] + [/reflection.float /.float] + [/reflection.double /.double] + [/reflection.char /.char])) + (if (text.starts-with? /descriptor.array-prefix class-name) + (.run /parser.value (|> class-name //name.internal //name.read)) + (#try.Success (/.class class-name (list))))))) _) (case (host.check java/lang/reflect/GenericArrayType reflection) (#.Some reflection) (|> reflection java/lang/reflect/GenericArrayType::getGenericComponentType type - (:: try.monad map (/.array 1))) + (:: try.monad map /.array)) _) ## else - (:: try.monad map (|>> #/.Generic) - (..generic reflection)))) + (..parameter reflection))) (def: #export (return reflection) - (-> java/lang/reflect/Type (Try /.Return)) - (with-expansions [ (as-is (:: try.monad map (|>> #.Some) - (..type reflection)))] + (-> java/lang/reflect/Type (Try (/.Type Return))) + (with-expansions [ (as-is (..type reflection))] (case (host.check java/lang/Class reflection) (#.Some class) - (case (|> class - (:coerce (java/lang/Class java/lang/Object)) - java/lang/Class::getName) - (^ (static reflection.void)) - (#try.Success #.None) - - _ - ) + (let [class-name (|> reflection + (:coerce (java/lang/Class java/lang/Object)) + java/lang/Class::getName)] + (if (text@= (/reflection.reflection /reflection.void) + class-name) + (#try.Success /.void) + )) #.None ))) @@ -327,7 +340,7 @@ (template [ ] [(def: #export ( field class) - (-> Text (java/lang/Class java/lang/Object) (Try [Bit /.Type])) + (-> Text (java/lang/Class java/lang/Object) (Try [Bit (/.Type Value)])) (do try.monad [fieldJ (..field field class) #let [modifiers (java/lang/reflect/Field::getModifiers fieldJ)]] diff --git a/stdlib/source/lux/target/jvm/type.lux b/stdlib/source/lux/target/jvm/type.lux index d1af2ec02..d8b21a829 100644 --- a/stdlib/source/lux/target/jvm/type.lux +++ b/stdlib/source/lux/target/jvm/type.lux @@ -1,15 +1,12 @@ (.module: [lux (#- Type int char) [abstract - [equivalence (#+ Equivalence)] - [monad (#+ do)]] - [control - ["." function] - ["<>" parser ("#@." monad) - ["" text (#+ Parser)]]] + [equivalence (#+ Equivalence)]] [data - ["." text ("#@." equivalence) - ["%" format (#+ format)]] + ["." maybe] + ["." text] + [number + ["n" nat]] [collection ["." list ("#@." functor)]]] [type @@ -18,7 +15,7 @@ [encoding ["#." name (#+ External)]]] ["." / #_ - [category (#+ Void Value Return Method Primitive Object Class Array Parameter)] + [category (#+ Void Value Return Method Primitive Object Class Array Var Parameter)] ["#." signature (#+ Signature)] ["#." descriptor (#+ Descriptor)] ["#." reflection (#+ Reflection)]]) @@ -28,6 +25,17 @@ [(Signature category) (Descriptor category) (Reflection category)] + (type: #export Argument + [Text (Type Value)]) + + (type: #export (Typed a) + [(Type Value) a]) + + (type: #export Constraint + {#name Text + #super-class (Type Class) + #super-interfaces (List (Type Class))}) + (template [