diff options
author | Eduardo Julian | 2022-01-22 03:55:22 -0400 |
---|---|---|
committer | Eduardo Julian | 2022-01-22 03:55:22 -0400 |
commit | 971c90ca9bcaa656f2e5682d61ca8054a59a8fea (patch) | |
tree | a63f5c5a4f59d26752b06a77dd96255f8c780e35 /stdlib/source/library/lux/tool/compiler | |
parent | 14bf4ffe5d7d88692ab895f96a2bb6a829a406de (diff) |
Fixes for the pure-Lux JVM compiler machinery. [Part 10]
Diffstat (limited to 'stdlib/source/library/lux/tool/compiler')
12 files changed, 264 insertions, 73 deletions
diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/analysis/macro.lux b/stdlib/source/library/lux/tool/compiler/language/lux/analysis/macro.lux index 586117fb9..98fb50427 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/analysis/macro.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/analysis/macro.lux @@ -1,17 +1,17 @@ (.using - [library - [lux "*" - [abstract - [monad {"+" do}]] - [control - ["[0]" try {"+" Try}] - ["[0]" exception {"+" exception:}]] - [data - ["[0]" text - ["%" format {"+" format}]]] - ["[0]" meta]]] - [///// - ["[0]" phase]]) + [library + [lux "*" + [abstract + [monad {"+" do}]] + [control + ["[0]" try {"+" Try}] + ["[0]" exception {"+" exception:}]] + [data + ["[0]" text + ["%" format {"+" format}]]] + ["[0]" meta]]] + [///// + ["[0]" phase]]) (exception: .public (expansion_failed [macro Symbol inputs (List Code) @@ -32,22 +32,22 @@ (type: .public Expander (-> Macro (List Code) Lux (Try (Try [Lux (List Code)])))) -(def: .public (expand expander name macro inputs) +(def: .public (expansion expander name macro inputs) (-> Expander Symbol Macro (List Code) (Meta (List Code))) (function (_ state) (do try.monad [output (expander macro inputs state)] (case output - {try.#Success output} - {try.#Success output} - {try.#Failure error} - ((meta.failure (exception.error ..expansion_failed [name inputs error])) state))))) + ((meta.failure (exception.error ..expansion_failed [name inputs error])) state) -(def: .public (expand_one expander name macro inputs) + _ + output)))) + +(def: .public (single_expansion expander name macro inputs) (-> Expander Symbol Macro (List Code) (Meta Code)) (do meta.monad - [expansion (expand expander name macro inputs)] + [expansion (..expansion expander name macro inputs)] (case expansion (^ (list single)) (in single) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis.lux index fb64abaf3..3add55843 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis.lux @@ -113,7 +113,7 @@ (case ?macro {.#Some macro} (do ! - [expansion (//extension.lifted (/macro.expand_one expander def_name macro argsC+))] + [expansion (//extension.lifted (/macro.single_expansion expander def_name macro argsC+))] (compile archive expansion)) _ diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/directive.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/directive.lux index fefafe199..6fbc49090 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/directive.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/directive.lux @@ -109,7 +109,7 @@ {.#None} (//.except ..macro_was_not_found macro_name))] - (//extension.lifted (///analysis/macro.expand expander macro_name macro inputs))) + (//extension.lifted (///analysis/macro.expansion expander macro_name macro inputs))) _ (//.except ..invalid_macro_call code))))] diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/jvm.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/jvm.lux index 0f076e04a..ba635d72f 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/jvm.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/jvm.lux @@ -2128,9 +2128,13 @@ not)) sub_set)) -(exception: .public (class_parameter_mismatch [expected (List Text) +(exception: .public (class_parameter_mismatch [name Text + declaration (Type Class) + expected (List Text) actual (List (Type Parameter))]) (exception.report + ["Class" (%.text name)] + ["Declaration" (signature.signature (jvm.signature declaration))] ["Expected (amount)" (%.nat (list.size expected))] ["Expected (parameters)" (exception.listing %.text expected)] ["Actual (amount)" (%.nat (list.size actual))] @@ -2140,11 +2144,11 @@ (-> java/lang/ClassLoader (Type Class) (Operation Aliasing)) (do phase.monad [.let [[name actual_parameters] (jvm_parser.read_class class)] - class (phase.lifted (reflection!.load class_loader name)) - .let [expected_parameters (|> (java/lang/Class::getTypeParameters class) + jvm_class (phase.lifted (reflection!.load class_loader name)) + .let [expected_parameters (|> (java/lang/Class::getTypeParameters jvm_class) (array.list {.#None}) (list#each (|>> java/lang/reflect/TypeVariable::getName)))] - _ (phase.assertion ..class_parameter_mismatch [expected_parameters actual_parameters] + _ (phase.assertion ..class_parameter_mismatch [name class expected_parameters actual_parameters] (n.= (list.size expected_parameters) (list.size actual_parameters)))] (in (|> (list.zipped/2 expected_parameters actual_parameters) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/directive/jvm.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/directive/jvm.lux index 72f978083..bdf4d3e11 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/directive/jvm.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/directive/jvm.lux @@ -6,10 +6,12 @@ ["[0]" monad {"+" do}]] [control [pipe {"+" case>}] + ["[0]" try {"+" Try} ("[1]#[0]" functor)] ["<>" parser ("[1]#[0]" monad) ["<[0]>" code {"+" Parser}] ["<[0]>" text]]] [data + [binary {"+" Binary}] ["[0]" product] [text ["%" format {"+" format}]] @@ -82,6 +84,13 @@ (Parser Declaration) (<code>.form (<>.and <code>.text (<>.some jvm.var)))) +(def: method_privacy + (-> ffi.Privacy (Modifier method.Method)) + (|>> (case> {ffi.#PublicP} method.public + {ffi.#PrivateP} method.private + {ffi.#ProtectedP} method.protected + {ffi.#DefaultP} modifier.empty))) + (def: visibility (Parser (Modifier field.Field)) (`` ($_ <>.either @@ -180,11 +189,9 @@ jvm.overriden_method_definition )) -(def: (constraint name) - (-> Text Constraint) - [type.#name name - type.#super_class (type.class "java.lang.Object" (list)) - type.#super_interfaces (list)]) +(def: $Object + (Type Class) + (type.class "java.lang.Object" (list))) (def: constant::modifier (Modifier field.Field) @@ -223,7 +230,7 @@ ... TODO: Handle annotations. {#Variable [name visibility state annotations type]} (field.field (modifier#composite visibility state) - name type (sequence.sequence)))) + name type sequence.empty))) (def: (method_definition archive supers [mapping selfT] [analyse synthesize generate]) (-> Archive @@ -252,11 +259,189 @@ (directive.lifted_synthesis (synthesize archive methodA))))) +(def: (mock_class [name parameters] super interfaces fields methods modifier) + (-> Declaration (Type Class) (List (Type Class)) + (List (Resource field.Field)) (List (Resource method.Method)) (Modifier class.Class) + (Try [External Binary])) + (let [class_name (|>> parser.read_class product.left name.internal) + signature (signature.inheritance (list#each type.signature parameters) + (type.signature super) + (list#each type.signature interfaces))] + (try#each (|>> (format.result class.writer) + [name]) + (class.class version.v6_0 + ($_ modifier#composite + class.public + modifier) + (name.internal name) + {.#Some signature} + (class_name super) + (list#each class_name interfaces) + fields + methods + sequence.empty)))) + +(def: (mock_field it) + (-> ..Field (Resource field.Field)) + (case it + ... TODO: Handle constants + {#Constant [name annotations type term]} + (undefined) + + {#Variable [name visibility state annotations type]} + (field.field ($_ modifier#composite visibility state) name type sequence.empty))) + +(def: (mock_value valueT) + (-> (Type Value) (Bytecode Any)) + (case (type.primitive? valueT) + {.#Left classT} + _.aconst_null + + {.#Right primitiveT} + (cond (# type.equivalence = type.long primitiveT) + _.lconst_0 + + (# type.equivalence = type.float primitiveT) + _.fconst_0 + + (# type.equivalence = type.double primitiveT) + _.dconst_0 + + ... type.boolean type.byte type.short type.int type.char + _.iconst_0))) + +(def: (mock_return returnT) + (-> (Type Return) (Bytecode Any)) + (case (type.void? returnT) + {.#Right returnT} + _.return + + {.#Left valueT} + ($_ _.composite + (mock_value valueT) + (case (type.primitive? valueT) + {.#Left classT} + _.areturn + + {.#Right primitiveT} + (cond (# type.equivalence = type.long primitiveT) + _.lreturn + + (# type.equivalence = type.float primitiveT) + _.freturn + + (# type.equivalence = type.double primitiveT) + _.dreturn + + ... type.boolean type.byte type.short type.int type.char + _.ireturn))))) + +(def: constructor_name + "<init>") + +(def: (mock_method super method) + (-> (Type Class) ..Method_Definition (Resource method.Method)) + (case method + {#Constructor [privacy strict_floating_point? annotations variables exceptions + self arguments constructor_arguments + body]} + (method.method ($_ modifier#composite + (..method_privacy privacy) + (if strict_floating_point? + method.strict + modifier.empty)) + ..constructor_name + (type.method [variables (list#each product.right arguments) type.void exceptions]) + (list) + {.#Some ($_ _.composite + (_.aload 0) + (|> constructor_arguments + (list#each (|>> product.left ..mock_value)) + (monad.all _.monad)) + (|> (type.method [(list) (list#each product.left constructor_arguments) type.void (list)]) + (_.invokespecial super ..constructor_name)) + _.return + )}) + + {#Overriden_Method [super name strict_floating_point? annotations variables + self arguments return exceptions + body]} + (method.method ($_ modifier#composite + method.public + (if strict_floating_point? + method.strict + modifier.empty)) + name + (type.method [variables (list#each product.right arguments) return exceptions]) + (list) + {.#Some (..mock_return return)}) + + {#Virtual_Method [name privacy final? strict_floating_point? annotations variables + self arguments return exceptions + body]} + (method.method ($_ modifier#composite + (..method_privacy privacy) + (if strict_floating_point? + method.strict + modifier.empty) + (if final? + method.final + modifier.empty)) + name + (type.method [variables (list#each product.right arguments) return exceptions]) + (list) + {.#Some (..mock_return return)}) + + {#Static_Method [name privacy strict_floating_point? annotations + variables exceptions arguments return + body]} + (method.method ($_ modifier#composite + method.static + (..method_privacy privacy) + (if strict_floating_point? + method.strict + modifier.empty)) + name + (type.method [variables (list#each product.right arguments) return exceptions]) + (list) + {.#Some (..mock_return return)}) + + ... {#Abstract [name privacy annotations + ... variables arguments return exceptions]} + ... (method.method ($_ modifier#composite + ... method.abstract + ... (..method_privacy privacy)) + ... name + ... (type.method [variables (list#each product.right arguments) return exceptions]) + ... (list) + ... {.#None}) + )) + +(def: (mock declaration super interfaces inheritance fields methods) + (-> Declaration + (Type Class) (List (Type Class)) + (Modifier class.Class) (List ..Field) (List ..Method_Definition) + (Try [External Binary])) + (mock_class declaration super interfaces + (list#each ..mock_field fields) + (list#each (..mock_method super) methods) + inheritance)) + +(template [<name> <type> <parser>] + [(def: <name> + (Parser <type>) + (do [! <>.monad] + [raw <code>.text] + (<>.lifted (<text>.result <parser> raw))))] + + [class_declaration [External (List (Type Var))] parser.declaration'] + ) + (def: jvm::class (Handler Anchor (Bytecode Any) Definition) (/.custom [($_ <>.and - ..declaration + ..class_declaration jvm.class (<code>.tuple (<>.some jvm.class)) ..inheritance @@ -264,38 +449,49 @@ (<code>.tuple (<>.some ..field)) (<code>.tuple (<>.some ..method))) (function (_ extension phase archive - [[name parameters] - super_class - super_interfaces + [class_declaration + super + interfaces inheritance ... TODO: Handle annotations. annotations fields methods]) (do [! phase.monad] - [parameters (directive.lifted_analysis + [.let [[name parameters] class_declaration] + mock (<| phase.lifted + (..mock class_declaration + super + interfaces + inheritance + fields + methods)) + ... Necessary for reflection to work properly during analysis. + _ (directive.lifted_generation + (generation.execute! mock)) + parameters (directive.lifted_analysis (typeA.with_env (jvm.parameter_types parameters))) .let [mapping (list#mix (function (_ [parameterJ parameterT] mapping) (dictionary.has (parser.name parameterJ) parameterT mapping)) luxT.fresh parameters)] - super_classT (directive.lifted_analysis - (typeA.with_env - (luxT.check (luxT.class mapping) (..signature super_class)))) - super_interfaceT+ (directive.lifted_analysis - (typeA.with_env - (monad.each check.monad - (|>> ..signature (luxT.check (luxT.class mapping))) - super_interfaces))) + superT (directive.lifted_analysis + (typeA.with_env + (luxT.check (luxT.class mapping) (..signature super)))) + interfaceT+ (directive.lifted_analysis + (typeA.with_env + (monad.each check.monad + (|>> ..signature (luxT.check (luxT.class mapping))) + interfaces))) .let [selfT (jvm.inheritance_relationship_type {.#Primitive name (list#each product.right parameters)} - super_classT - super_interfaceT+)] + superT + interfaceT+)] state (extension.lifted phase.state) .let [analyse (value@ [directive.#analysis directive.#phase] state) synthesize (value@ [directive.#synthesis directive.#phase] state) generate (value@ [directive.#generation directive.#phase] state)] - methods (monad.each ! (..method_definition archive (list& super_class super_interfaces) [mapping selfT] [analyse synthesize generate]) + methods (monad.each ! (..method_definition archive (list& super interfaces) [mapping selfT] [analyse synthesize generate]) methods) ... _ (directive.lifted_generation ... (generation.save! true ["" name] @@ -303,10 +499,10 @@ ... (class.class version.v6_0 ... (modifier#composite class.public inheritance) ... (name.internal name) (list#each (|>> product.left parser.name ..constraint) parameters) - ... super_class super_interfaces + ... super interfaces ... (list#each ..field_definition fields) ... (list) ... TODO: Add methods - ... (sequence.sequence))])) + ... sequence.empty)])) _ (directive.lifted_generation (generation.log! (format "JVM Class " name)))] (in directive.no_requirements)))])) @@ -322,22 +518,13 @@ (list) {.#None}))) -(template [<name> <type> <parser>] - [(def: <name> - (Parser <type>) - (do [! <>.monad] - [raw <code>.text] - (<>.lifted (<text>.result <parser> raw))))] - - [class_declaration [External (List (Type Var))] parser.declaration'] - ) - (def: jvm::class::interface (Handler Anchor (Bytecode Any) Definition) (/.custom [($_ <>.and ..class_declaration (<code>.tuple (<>.some jvm.class)) + ... TODO: Handle annotations. (<code>.tuple (<>.some ..annotation)) (<>.some jvm.method_declaration)) (function (_ extension_name phase archive [[name parameters] supers annotations method_declarations]) @@ -351,13 +538,15 @@ class.abstract class.interface) (name.internal name) - (type.declaration name parameters) + {.#Some (signature.inheritance (list#each type.signature parameters) + (type.signature $Object) + (list#each type.signature supers))} (name.internal "java.lang.Object") (list#each (|>> parser.read_class product.left name.internal) supers) (list) (list#each ..method_declaration method_declarations) - (sequence.sequence))) + sequence.empty)) ... module generation.module ... module_id (generation.module_id module archive) artifact_id (generation.learn_custom name artifact.no_dependencies) @@ -375,7 +564,6 @@ (-> java/lang/ClassLoader Extender (Bundle Anchor (Bytecode Any) Definition)) (<| (bundle.prefix "jvm") (|> bundle.empty - ... TODO: Finish handling methods and un-comment. - ... (dictionary.has "class" jvm::class) + (dictionary.has "class" jvm::class) (dictionary.has "class interface" ..jvm::class::interface) ))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/jvm/host.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/jvm/host.lux index dda74b0e1..d8dc8d591 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/jvm/host.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/jvm/host.lux @@ -1225,7 +1225,7 @@ //////.lifted (class.class version.v6_0 ($_ modifier#composite class.public class.final) (name.internal anonymous_class_name) - (type.declaration anonymous_class_name (list)) + {.#None} (name.internal (..reflection super_class)) (list#each (|>> ..reflection name.internal) super_interfaces) (foreign.variables total_environment) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function.lux index 2227d9f1d..59206b6fb 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function.lux @@ -111,7 +111,7 @@ class (phase.lifted (class.class version.v6_0 ..modifier (name.internal function_class) - (type.declaration function_class (list)) + {.#None} (..internal /abstract.class) (list) fields methods diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/host.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/host.lux index 79c72c425..6ebc13360 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/host.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/host.lux @@ -122,7 +122,7 @@ bytecode (class.class version.v6_0 class.public (encoding/name.internal bytecode_name) - (type.declaration bytecode_name (list)) + {.#None} (encoding/name.internal "java.lang.Object") (list) (list (field.field ..value::modifier ..value::field ..value::type (sequence.sequence))) (list (method.method ..init::modifier "<clinit>" ..init::type diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/program.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/program.lux index bf0bb032d..357922e6c 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/program.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/program.lux @@ -157,7 +157,7 @@ (class.class version.v6_0 ..program::modifier (name.internal class) - (type.declaration class (list)) + {.#None} super_class (list) (list) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/runtime.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/runtime.lux index 74956a7e5..57a446860 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/runtime.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/runtime.lux @@ -545,7 +545,7 @@ (class.class jvm/version.v6_0 modifier (name.internal class) - (type.declaration class (list)) + {.#None} (name.internal (..reflection ^Object)) (list) (list) (let [[left_projection::method right_projection::method] projection::method2] @@ -615,7 +615,7 @@ (class.class jvm/version.v6_0 modifier (name.internal class) - (type.declaration class (list)) + {.#None} (name.internal (..reflection ^Object)) (list) (list partial_count) (list& <init>::method apply::method+) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/synthesis.lux b/stdlib/source/library/lux/tool/compiler/language/lux/synthesis.lux index f2b061315..4f87318aa 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/synthesis.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/synthesis.lux @@ -120,7 +120,7 @@ (type: .public (Function s) (Variant {#Abstraction (Abstraction' s)} - {#Apply s (List s)})) + {#Apply (Apply' s)})) (type: .public (Control s) (Variant diff --git a/stdlib/source/library/lux/tool/compiler/meta/archive.lux b/stdlib/source/library/lux/tool/compiler/meta/archive.lux index c09aff7e6..35e167067 100644 --- a/stdlib/source/library/lux/tool/compiler/meta/archive.lux +++ b/stdlib/source/library/lux/tool/compiler/meta/archive.lux @@ -152,8 +152,7 @@ (exception.except ..module_is_only_reserved [module]) {.#None} - (exception.except ..unknown_document [module - (dictionary.keys _#resolver)])))) + (exception.except ..unknown_document [module (dictionary.keys _#resolver)])))) (def: .public (archived? archive module) (-> Archive Module Bit) |