diff options
Diffstat (limited to 'stdlib/source/library')
7 files changed, 278 insertions, 103 deletions
diff --git a/stdlib/source/library/lux.lux b/stdlib/source/library/lux.lux index bf922ec17..6046012c7 100644 --- a/stdlib/source/library/lux.lux +++ b/stdlib/source/library/lux.lux @@ -89,10 +89,10 @@ {2 #0 Text Text}}) #1) -... (type .public (List a) +... (type .public (List of) ... (Variant ... {#End} -... {#Item a (List a)})) +... {#Item of (List of)})) (.def# List (.is_type# {9 #1 @@ -110,59 +110,22 @@ {4 #0 0}}}}}}) #1) -... (type .public Tag -... (Nominal "#Tag")) -(.def# Tag - (.is_type# - {9 #1 [..prelude "Tag"] - {0 #0 "#Tag" {0 #0}}}) - #1) - -... (type .public Slot -... (Nominal "#Slot")) -(.def# Slot - (.is_type# - {9 #1 [..prelude "Slot"] - {0 #0 "#Slot" {0 #0}}}) - #1) - -(.def# Label' - (.is_type# - {1 #0 [Any {2 #0 [Nat {2 #0 [Bit {9 #0 Symbol List}]}]}]}) - #0) - -(.def# list_tags - (.is# {9 #0 Symbol List} - {0 #1 [[..prelude "#End"] - {0 #1 [[..prelude "#Item"] - {0 #0}]}]}) - #0) -(.def# #End (.as# Tag [(.is# Label' {0 #1 [0 #0 ..list_tags]}) List]) #1) -(.def# #Item (.as# Tag [(.is# Label' {0 #1 [0 #1 ..list_tags]}) List]) #1) - -... (type .public (Maybe a) -... {#None} -... {#Some a}) +... (type .public (Maybe of) +... (Variant +... {#None} +... {#Some of})) (.def# Maybe (.is_type# {9 #1 [..prelude "Maybe"] {7 #0 - {#End} + {0 #0} {1 #0 ... None Any ... Some {4 #0 1}}}}) #1) -(.def# maybe_tags - (.is# {9 #0 Symbol List} - {0 #1 [[..prelude "#None"] - {0 #1 [[..prelude "#Some"] - {0 #0}]}]}) - #0) -(.def# #None (.as# Tag [(.is# Label' {0 #1 [0 #0 ..maybe_tags]}) Maybe]) #1) -(.def# #Some (.as# Tag [(.is# Label' {0 #1 [0 #1 ..maybe_tags]}) Maybe]) #1) ... (type .public Type ... (Rec Type @@ -185,9 +148,9 @@ ({Type_List ({Type_Pair {9 #0 - {0 #0 ["" {#End}]} + {0 #0 ["" {0 #0}]} {7 #0 - {#End} + {0 #0} {1 #0 ... Nominal {2 #0 Text Type_List} @@ -222,9 +185,67 @@ {2 #0 Symbol Type}}}}}}}}}}}}}} (.is_type# {2 #0 Type Type}))} (.is_type# {9 #0 Type List}))} - (.is_type# {9 #0 {0 #0 ["" {#End}]} {4 #0 0}}))}) + (.is_type# {9 #0 {0 #0 ["" {0 #0}]} {4 #0 0}}))}) + #1) + +... (type .public Label +... [(Maybe [Nat Bit (List Symbol)]) +... Type]) +(.def# Label + (.is_type# + {9 #1 [..prelude "Label"] + {2 #0 + {9 #0 {2 #0 Nat + {2 #0 Bit + {9 #0 Symbol List}}} + Maybe} + Type}}) + #1) + +... (type .public Tag +... (Nominal "#Tag")) +(.def# Tag + (.is_type# + {9 #1 [..prelude "Tag"] + {0 #0 "#Tag" {0 #0}}}) + #1) + +... (type .public Slot +... (Nominal "#Slot")) +(.def# Slot + (.is_type# + {9 #1 [..prelude "Slot"] + {0 #0 "#Slot" {0 #0}}}) #1) +(.def# tag + (.is# {3 #0 Label Tag} + ([_ it] (.as# Tag it))) + #0) + +(.def# slot + (.is# {3 #0 Label Slot} + ([_ it] (.as# Slot it))) + #0) + +(.def# list_tags + (.is# {9 #0 Symbol List} + {0 #1 [[..prelude "#End"] + {0 #1 [[..prelude "#Item"] + {0 #0}]}]}) + #0) +(.def# #End (..tag [{0 #1 [0 #0 ..list_tags]} List]) #1) +(.def# #Item (..tag [{0 #1 [0 #1 ..list_tags]} List]) #1) + +(.def# maybe_tags + (.is# {9 #0 Symbol List} + {0 #1 [[..prelude "#None"] + {0 #1 [[..prelude "#Some"] + {0 #0}]}]}) + #0) +(.def# #None (..tag [{0 #1 [0 #0 ..maybe_tags]} Maybe]) #1) +(.def# #Some (..tag [{0 #1 [0 #1 ..maybe_tags]} Maybe]) #1) + (.def# type_tags (.is# {9 #0 Symbol List} {0 #1 [[..prelude "#Nominal"] @@ -240,36 +261,17 @@ {0 #1 [[..prelude "#Named"] {0 #0}]}]}]}]}]}]}]}]}]}]}]}) #0) -(.def# #Nominal (.as# Tag [(.is# Label' {#Some [0 #0 ..type_tags]}) Type]) #1) -(.def# #Sum (.as# Tag [(.is# Label' {#Some [1 #0 ..type_tags]}) Type]) #1) -(.def# #Product (.as# Tag [(.is# Label' {#Some [2 #0 ..type_tags]}) Type]) #1) -(.def# #Function (.as# Tag [(.is# Label' {#Some [3 #0 ..type_tags]}) Type]) #1) -(.def# #Parameter (.as# Tag [(.is# Label' {#Some [4 #0 ..type_tags]}) Type]) #1) -(.def# #Var (.as# Tag [(.is# Label' {#Some [5 #0 ..type_tags]}) Type]) #1) -(.def# #Ex (.as# Tag [(.is# Label' {#Some [6 #0 ..type_tags]}) Type]) #1) -(.def# #UnivQ (.as# Tag [(.is# Label' {#Some [7 #0 ..type_tags]}) Type]) #1) -(.def# #ExQ (.as# Tag [(.is# Label' {#Some [8 #0 ..type_tags]}) Type]) #1) -(.def# #Apply (.as# Tag [(.is# Label' {#Some [9 #0 ..type_tags]}) Type]) #1) -(.def# #Named (.as# Tag [(.is# Label' {#Some [9 #1 ..type_tags]}) Type]) #1) - -... (type .public Label -... [(Maybe [Nat Bit (List Symbol)]) Type]) -(.def# Label - (.is# Type - {#Named [..prelude "Label"] - {#Product {#Apply {#Product Nat {#Product Bit {#Apply Symbol List}}} Maybe} - Type}}) - #1) - -(.def# tag - (.is# {#Function Label Tag} - ([_ it] (.as# Tag it))) - #0) - -(.def# slot - (.is# {#Function Label Slot} - ([_ it] (.as# Slot it))) - #0) +(.def# #Nominal (..tag [{#Some [0 #0 ..type_tags]} Type]) #1) +(.def# #Sum (..tag [{#Some [1 #0 ..type_tags]} Type]) #1) +(.def# #Product (..tag [{#Some [2 #0 ..type_tags]} Type]) #1) +(.def# #Function (..tag [{#Some [3 #0 ..type_tags]} Type]) #1) +(.def# #Parameter (..tag [{#Some [4 #0 ..type_tags]} Type]) #1) +(.def# #Var (..tag [{#Some [5 #0 ..type_tags]} Type]) #1) +(.def# #Ex (..tag [{#Some [6 #0 ..type_tags]} Type]) #1) +(.def# #UnivQ (..tag [{#Some [7 #0 ..type_tags]} Type]) #1) +(.def# #ExQ (..tag [{#Some [8 #0 ..type_tags]} Type]) #1) +(.def# #Apply (..tag [{#Some [9 #0 ..type_tags]} Type]) #1) +(.def# #Named (..tag [{#Some [9 #1 ..type_tags]} Type]) #1) ... (type .public Location ... (Record diff --git a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++.lux b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++.lux index 395d31e39..6b1423ba0 100644 --- a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++.lux +++ b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++.lux @@ -29,7 +29,7 @@ ["[1][0]" complex] ["[1][0]" reference] ... ["[1][0]" function] - ... ["[1][0]" when] + ["[1][0]" when] ... ["[1][0]" loop] [/// ["[0]" extension] @@ -59,8 +59,8 @@ (synthesis.tuple @ it) (/complex.tuple phase archive it) - [@ {synthesis.#Reference reference}] - (when reference + [@ {synthesis.#Reference it}] + (when it {reference.#Variable it} (/reference.variable it) @@ -70,14 +70,14 @@ ... (synthesis.branch/when @ [valueS pathS]) ... (/when.when phase archive [valueS pathS]) - ... (synthesis.branch/exec @ [this that]) - ... (/when.exec phase archive [this that]) + (synthesis.branch/exec @ it) + (/when.exec phase archive it) - ... (synthesis.branch/let @ [inputS register bodyS]) - ... (/when.let phase archive [inputS register bodyS]) + (synthesis.branch/let @ it) + (/when.let phase archive it) - ... (synthesis.branch/if @ [conditionS thenS elseS]) - ... (/when.if phase archive [conditionS thenS elseS]) + (synthesis.branch/if @ it) + (/when.if phase archive it) ... (synthesis.branch/get @ [path recordS]) ... (/when.get phase archive [path recordS]) diff --git a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/reference.lux b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/reference.lux index b43340b91..f449c76bf 100644 --- a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/reference.lux +++ b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/reference.lux @@ -32,7 +32,7 @@ (with_template [<prefix> <name>] [(def .public <name> (-> Register - /.Value) + _.Local) (|>> %.nat (%.format <prefix>) _.local))] @@ -43,12 +43,12 @@ ) (def .public this - /.Value + _.Local (..local 0)) (def .public variable (-> Variable - (Operation /.Value)) + (Operation _.Local)) (|>> (|.when {variable.#Local it} (..local it) @@ -59,7 +59,7 @@ (def .public (constant archive it) (-> Archive Symbol - (Operation /.Value)) + (Operation _.Reference)) (phase#each (|>> product.left reference.artifact _.local) diff --git a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/when.lux b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/when.lux new file mode 100644 index 000000000..1f58294e5 --- /dev/null +++ b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/when.lux @@ -0,0 +1,124 @@ +... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. + +(.require + [library + [lux (.except Type Label if let exec when int) + [abstract + ["[0]" monad (.only do)]] + [control + ["[0]" function]] + [data + [collection + ["[0]" list (.use "[1]#[0]" mix)]] + ["[0]" text (.use "[1]#[0]" equivalence) + ["%" \\format (.only format)]]] + [math + [number + ["n" nat] + ["[0]" i32]]] + [meta + [macro + ["^" pattern]] + [compiler + [target + ["_" c++]]]]]] + ["[0]" // + ["[1][0]" type] + ["[1][0]" reference] + ["[1][0]" runtime (.only Operation Phase Term)] + [//// + ["[0]" phase (.use "[1]#[0]" functor)] + ["[0]" translation] + ["[0]" synthesis (.only Path Fork) + [access + ["[0]" member (.only Member)]]] + [/// + [reference + [variable (.only Register)]]]]]) + +(type (If of) + (Record + [#when of + #then of + #else of])) + +(def .public (if next archive it) + (Term If) + (do phase.monad + [when (next archive (the #when it)) + then (next archive (the #then it)) + else (next archive (the #else it))] + (in (_.? when then else)))) + +(type (Let of) + (Record + [#input of + #register Register + #body of])) + +(def (sub_bindings body) + (-> synthesis.Term + [(List [Register synthesis.Term]) synthesis.Term]) + (.when body + (synthesis.branch/let @ [input register body]) + (.let [[tail body] (sub_bindings body)] + [(list.partial [register input] tail) body]) + + _ + [(list) body])) + +(def .public (let next archive it) + (Term Let) + (do [! phase.monad] + [.let [[tail body] (sub_bindings (the #body it)) + head_binding (the #register it)] + bindings (monad.each ! (function (_ [binding value]) + (do ! + [value (next archive value)] + (in (_.variable (//reference.local binding) //type.value value)))) + (list.partial [head_binding (the #input it)] + tail)) + body (next archive body)] + (in (_.on (list) + (_.lambda (.when head_binding + 0 (list) + _ (list _.all_by_value)) + (list) + {.#Some //type.value} + (list#mix _.then + (_.return body) + (list.reversed bindings)) + ))))) + +(type (Exec of) + (Record + [#before of + #after of])) + +(def (sub_statements after) + (-> synthesis.Term + [(List synthesis.Term) synthesis.Term]) + (.when after + (synthesis.branch/exec @ [before after]) + (.let [[tail after] (sub_statements after)] + [(list.partial before tail) after]) + + _ + [(list) after])) + +(def .public (exec next archive it) + (Term Exec) + (do [! phase.monad] + [.let [[tail after] (sub_statements (the #after it))] + all_before (monad.each ! (|>> (next archive) + (phase#each _.;)) + (list.partial (the #before it) tail)) + after (next archive after)] + (in (_.on (list) + (_.lambda (list _.all_by_value) + (list) + {.#Some //type.value} + (list#mix _.then + (_.return after) + (list.reversed all_before))))))) diff --git a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/function/field/variable/count.lux b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/function/field/variable/count.lux index 2080ea9c5..3f26f3b5f 100644 --- a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/function/field/variable/count.lux +++ b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/function/field/variable/count.lux @@ -13,13 +13,15 @@ ["_" bytecode (.only Bytecode)] ["[0]" type] [encoding - [name (.only External)] ["[0]" signed]]]]]]]] - ["[0]" //// - ["[1][0]" abstract]]) + [/// + [constant + ["[0]" arity]] + [// + ["[0]" abstract]]]) (def .public field "partials") -(def .public type type.int) +(def .public type arity.type) (def .public initial (Bytecode Any) @@ -28,12 +30,6 @@ try.trusted _.bipush)) -(def this - _.aload_0) - (def .public value (Bytecode Any) - (all _.composite - ..this - (_.getfield ////abstract.class ..field ..type) - )) + (_.getfield abstract.class ..field ..type)) diff --git a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/function/method/apply.lux b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/function/method/apply.lux index ceb7ee756..c18822e3c 100644 --- a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/function/method/apply.lux +++ b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/function/method/apply.lux @@ -153,6 +153,7 @@ (_.new class) _.dup current_environment + ////reference.this ///count.value (..increment apply_arity) current_partials @@ -162,6 +163,7 @@ _.areturn))))))) (monad.all _.monad))]] (all _.composite + ////reference.this ///count.value (_.tableswitch (try.trusted (signed.s4 +0)) @default [@labelsH @labelsT]) cases)))}))) diff --git a/stdlib/source/library/lux/meta/compiler/target/c++.lux b/stdlib/source/library/lux/meta/compiler/target/c++.lux index eda5a1440..014bf887a 100644 --- a/stdlib/source/library/lux/meta/compiler/target/c++.lux +++ b/stdlib/source/library/lux/meta/compiler/target/c++.lux @@ -8,7 +8,8 @@ [abstract [equivalence (.only Equivalence)]] [control - ["|" pipe]] + ["|" pipe] + ["[0]" maybe (.use "[1]#[0]" functor)]] [data ["[0]" text (.only \n \t) (.use "[1]#[0]" equivalence) ["%" \\format]] @@ -31,6 +32,7 @@ (def term_delimiters ["(" ")"]) (def template_delimiters ["<" ">"]) (def initialization_delimiters ["{" "}"]) +(def capture_delimiters ["[" "]"]) (nominal.def .public (Code of) Text @@ -68,7 +70,8 @@ (,, (template.spliced <type>+))))] [Code - [[Type [of]] + [[Capture []] + [Type [of]] [Expression [of]] [Statement [of]]]] @@ -268,6 +271,13 @@ (|>> %> ..statement)) + (def .public return + (-> Expression + Statement) + (|>> %> + (%.format "return ") + ..statement)) + (with_template [<name> <command>] [(def .public <name> (-> Expression @@ -389,6 +399,11 @@ Definition) (..statement (%.format (%> type) " const " (%> name) " = " (%> value)))) + (def .public (variable name type value) + (-> Local Type Expression + Definition) + (..statement (%.format (%> type) " " (%> name) " = " (%> value)))) + (def .public (structure_definition name [fields methods]) (-> Local [(List [Local Type]) (List Method)] Definition) @@ -402,4 +417,40 @@ (list#each ..code methods)) (text.interposed \n)))))) + + (def captures + (-> (List Capture) + Text) + (|>> (list#each ..code) + (text.interposed ..parameter_separator) + (text.enclosed ..capture_delimiters))) + + (with_template [<sigil> <name>] + [(def .public <name> + Capture + (<% <sigil>))] + + ["=" all_by_value] + ["&" all_by_reference] + ) + + (def .public (lambda captures inputs output body) + (-> (List Capture) (List Argument) (Maybe Type) Statement + Expression) + (<| <% + (text.enclosed ..term_delimiters) + (%.format (..captures captures) (..arguments inputs) + (|> output + (maybe#each (|>> %> (%.format " -> "))) + (maybe.else "")) + " " (..block (%> body))))) + + (def .public (? when then else) + (-> Expression Expression Expression + Expression) + (<| <% + (text.enclosed ..term_delimiters) + (%.format (%> when) + " ? " (%> then) + " : " (%> else)))) ) |