From d37982f0af44714d95caf24d7f944e4e659b3e69 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 6 Jan 2022 14:28:32 -0400 Subject: Fixes for the pure-Lux JVM compiler machinery. [Part 2] --- stdlib/source/library/lux.lux | 2 +- stdlib/source/library/lux/target/jvm/bytecode.lux | 60 +- stdlib/source/library/lux/target/lua.lux | 107 ++-- .../lux/phase/extension/generation/lua/common.lux | 12 +- .../lux/phase/extension/generation/lua/host.lux | 28 +- .../language/lux/phase/generation/jvm/case.lux | 334 ++++++---- .../phase/generation/jvm/function/method/apply.lux | 2 +- .../jvm/function/method/implementation.lux | 2 +- .../lux/phase/generation/jvm/structure.lux | 6 +- .../language/lux/phase/generation/lua/case.lux | 91 +-- .../language/lux/phase/generation/lua/function.lux | 145 ++--- .../language/lux/phase/generation/lua/loop.lux | 4 +- .../lux/phase/generation/lua/primitive.lux | 2 +- .../language/lux/phase/generation/lua/runtime.lux | 182 +++--- stdlib/source/test/lux.lux | 20 +- stdlib/source/test/lux/extension.lux | 9 +- stdlib/source/test/lux/target/js.lux | 2 +- stdlib/source/test/lux/target/lua.lux | 709 +++++++++++++++++++++ stdlib/source/test/lux/target/python.lux | 17 +- stdlib/source/test/lux/target/ruby.lux | 2 +- 20 files changed, 1257 insertions(+), 479 deletions(-) create mode 100644 stdlib/source/test/lux/target/lua.lux (limited to 'stdlib/source') diff --git a/stdlib/source/library/lux.lux b/stdlib/source/library/lux.lux index 30f7bbc08..4aed1937b 100644 --- a/stdlib/source/library/lux.lux +++ b/stdlib/source/library/lux.lux @@ -679,7 +679,7 @@ {#Apply Text Either}}}}} ([_ val] ([_ state] - {#Right state val}))) + {#Right [state val]}))) #0) ("lux def" failure diff --git a/stdlib/source/library/lux/target/jvm/bytecode.lux b/stdlib/source/library/lux/target/jvm/bytecode.lux index 4e450d7bc..97871977f 100644 --- a/stdlib/source/library/lux/target/jvm/bytecode.lux +++ b/stdlib/source/library/lux/target/jvm/bytecode.lux @@ -128,6 +128,40 @@ ["Expected" (/stack.format expected)] ["Actual" (/stack.format actual)])) +(def: .public (set? label) + (-> Label (Bytecode (Maybe [Stack Address]))) + (function (_ state) + (let [[pool environment tracker] state] + {try.#Success [state + [..relative_identity + (case (dictionary.value label (value@ #known tracker)) + {.#Some [expected {.#Some address}]} + {.#Some [expected address]} + + _ + {.#None})]]}))) + +(def: .public (acknowledged? label) + (-> Label (Bytecode (Maybe Stack))) + (function (_ state) + (let [[pool environment tracker] state] + {try.#Success [state + [..relative_identity + (case (dictionary.value label (value@ #known tracker)) + {.#Some [expected {.#None}]} + {.#Some expected} + + _ + {.#None})]]}))) + +(def: .public stack + (Bytecode (Maybe Stack)) + (function (_ state) + (let [[pool environment tracker] state] + {try.#Success [state + [..relative_identity + (value@ /environment.#stack environment)]]}))) + (with_expansions [ (as_is (in [[pool environment (revised@ #known @@ -165,6 +199,14 @@ (: (Monad Try)) try.monad)) +(def: .public (when_continuous it) + (-> (Bytecode Any) (Bytecode Any)) + (do ..monad + [stack ..stack] + (.case stack + {.#None} (in []) + {.#Some _} it))) + (def: .public failure (-> Text Bytecode) (|>> {try.#Failure} function.constant)) @@ -186,7 +228,7 @@ (/address.move (estimator counter) counter)) (def: (bytecode consumption production registry [estimator bytecode] input) - (All (_ a) (-> U2 U2 Registry [Estimator (-> [a] Instruction)] [a] (Bytecode Any))) + (All (_ a) (-> U2 U2 Registry [Estimator (-> [a] Instruction)] a (Bytecode Any))) (function (_ [pool environment tracker]) (do [! try.monad] [environment' (|> environment @@ -301,14 +343,14 @@ [aload_2 $0 $1 @2 _.aload_2] [aload_3 $0 $1 @3 _.aload_3] - [iastore $3 $1 @_ _.iastore] - [lastore $4 $1 @_ _.lastore] - [fastore $3 $1 @_ _.fastore] - [dastore $4 $1 @_ _.dastore] - [aastore $3 $1 @_ _.aastore] - [bastore $3 $1 @_ _.bastore] - [castore $3 $1 @_ _.castore] - [sastore $3 $1 @_ _.sastore] + [iastore $3 $0 @_ _.iastore] + [lastore $4 $0 @_ _.lastore] + [fastore $3 $0 @_ _.fastore] + [dastore $4 $0 @_ _.dastore] + [aastore $3 $0 @_ _.aastore] + [bastore $3 $0 @_ _.bastore] + [castore $3 $0 @_ _.castore] + [sastore $3 $0 @_ _.sastore] [istore_0 $1 $0 @0 _.istore_0] [istore_1 $1 $0 @1 _.istore_1] diff --git a/stdlib/source/library/lux/target/lua.lux b/stdlib/source/library/lux/target/lua.lux index 72324192f..c99893692 100644 --- a/stdlib/source/library/lux/target/lua.lux +++ b/stdlib/source/library/lux/target/lua.lux @@ -1,31 +1,31 @@ (.using - [library - [lux {"-" Location Code Label int if cond function or and not let ^ local comment} - ["@" target] - [abstract - [equivalence {"+" Equivalence}] - [hash {"+" Hash}] - ["[0]" enum]] - [control - [pipe {"+" case> cond> new>}] - [parser - ["<[0]>" code]]] - [data - ["[0]" text - ["%" format {"+" format}]] - [collection - ["[0]" list ("[1]#[0]" functor mix)]]] - [macro - [syntax {"+" syntax:}] - ["[0]" template] - ["[0]" code]] - [math - [number - ["n" nat] - ["i" int] - ["f" frac]]] - [type - abstract]]]) + [library + [lux {"-" Location Code Label int if function or and not let ^ local comment} + ["@" target] + [abstract + [equivalence {"+" Equivalence}] + [hash {"+" Hash}] + ["[0]" enum]] + [control + [pipe {"+" case> cond> new>}] + [parser + ["<[0]>" code]]] + [data + ["[0]" text + ["%" format {"+" format}]] + [collection + ["[0]" list ("[1]#[0]" functor mix)]]] + [macro + [syntax {"+" syntax:}] + ["[0]" template] + ["[0]" code]] + [math + [number + ["n" nat] + ["i" int] + ["f" frac]]] + [type + abstract]]]) ... Added the carriage return for better Windows compatibility. (def: \n+ @@ -89,7 +89,7 @@ Literal (:abstraction "nil")) - (def: .public bool + (def: .public boolean (-> Bit Literal) (|>> (case> #0 "false" #1 "true") @@ -142,7 +142,7 @@ (|>> ..safe (text.enclosed' text.double_quote) :abstraction)) (def: .public multi - (-> (List Expression) Literal) + (-> (List Expression) Expression) (|>> (list#each ..code) (text.interposed ..input_separator) :abstraction)) @@ -159,15 +159,15 @@ (|>> (list#each (.function (_ [key value]) (format key " = " (:representation value)))) (text.interposed ..input_separator) - (text.enclosed ["{" "}"]) + (text.enclosed ["({" "})"]) :abstraction)) (def: .public (item idx array) (-> Expression Expression Access) - (:abstraction (format (:representation array) "[" (:representation idx) "]"))) + (:abstraction (format "(" (:representation array) ")[" (:representation idx) "]"))) (def: .public (the field table) - (-> Text Expression Computation) + (-> Text Expression Access) (:abstraction (format (:representation table) "." field))) (def: .public length @@ -176,7 +176,7 @@ (text.enclosed ["#(" ")"]) :abstraction)) - (def: .public (apply/* args func) + (def: .public (apply args func) (-> (List Expression) Expression Computation) (|> args (list#each ..code) @@ -339,9 +339,9 @@ (text.enclosed ["(" ")"]) :abstraction)) - (template [ ] + (template [ ] [(def: .public ( name args body!) - (-> Var (List Var) Statement Statement) + (-> (List Var) Statement Statement) (:abstraction (format " " (:representation name) (|> args @@ -350,8 +350,8 @@ (..nested (:representation body!)) \n+ "end")))] - [function "function"] - [local_function "local function"] + [function "function" Location] + [local_function "local function" Var] ) (def: .public break @@ -372,13 +372,6 @@ (:abstraction (format "-- " commentary \n+ (:representation on)))) ) -(def: .public (cond clauses else!) - (-> (List [Expression Statement]) Statement Statement) - (list#mix (.function (_ [test then!] next!) - (..if test then! next!)) - else! - (list.reversed clauses))) - (syntax: (arity_inputs [arity .nat]) (in (case arity 0 (.list) @@ -390,37 +383,23 @@ (in (list.repeated arity (` ..Expression)))) (template [ +] - [(with_expansions [ (template.symbol ["apply/" ]) - (arity_inputs ) + [(with_expansions [ (arity_inputs ) (arity_types ) (template.spliced +)] - (def: .public ( function ) - (-> Expression Computation) - (..apply/* (.list ) function)) - (template [] - [(`` (def: .public (~~ (template.symbol [ "/" ])) - ( (..var ))))] + [(`` (def: .public ((~~ (template.symbol [ "/" ])) ) + (-> Computation) + (..apply (.list ) (..var ))))] ))] [1 [["error"] + ["pcall"] ["print"] ["require"] ["type"] ["ipairs"]]] - [2 - [["print"] - ["error"]]] - - [3 - [["print"]]] - - [4 - []] - - [5 - []] + [["error"]]] ) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux index 988b31f55..bcaa03ee6 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux @@ -55,7 +55,7 @@ (/////.except extension.invalid_syntax [extension_name %synthesis input])))) (template: (!unary function) - [(|>> list _.apply/* (|> (_.var function)))]) + [(|>> list _.apply (|> (_.var function)))]) (def: .public (statement expression archive synthesis) Phase! @@ -159,12 +159,12 @@ (/.install "/" (binary (product.uncurried //runtime.i64//division))) (/.install "%" (binary (product.uncurried //runtime.i64//remainder))) (/.install "f64" (unary (_./ (_.float +1.0)))) - (/.install "char" (unary (_.apply/1 (_.var "utf8.char")))) + (/.install "char" (unary (function (_ it) (_.apply (list it) (_.var "utf8.char"))))) ))) (def: f64//decode (Unary Expression) - (|>> list _.apply/* (|> (_.var "tonumber")) _.return (_.closure (list)) //runtime.lux//try)) + (|>> list _.apply (|> (_.var "tonumber")) _.return (_.closure (list)) //runtime.lux//try)) (def: f64_procs Bundle @@ -174,11 +174,11 @@ (/.install "-" (binary (product.uncurried _.-))) (/.install "*" (binary (product.uncurried _.*))) (/.install "/" (binary (product.uncurried _./))) - (/.install "%" (binary (product.uncurried (function.flipped (_.apply/2 (_.var "math.fmod")))))) + (/.install "%" (binary (product.uncurried (function (_ parameter subject) (_.apply (list subject parameter) (_.var "math.fmod")))))) (/.install "=" (binary (product.uncurried _.=))) (/.install "<" (binary (product.uncurried _.<))) (/.install "i64" (unary (!unary "math.floor"))) - (/.install "encode" (unary (_.apply/2 (_.var "string.format") (_.string "%.17g")))) + (/.install "encode" (unary (function (_ it) (_.apply (list (_.string "%.17g") it) (_.var "string.format"))))) (/.install "decode" (unary ..f64//decode))))) (def: (text//char [paramO subjectO]) @@ -211,7 +211,7 @@ (def: (io//log! messageO) (Unary Expression) - (|> (_.apply/* (list messageO) (_.var "print")) + (|> (_.apply (list messageO) (_.var "print")) (_.or //runtime.unit))) (def: io_procs diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/lua/host.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/lua/host.lux index 4a9997ec7..23469d067 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/lua/host.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/generation/lua/host.lux @@ -109,11 +109,11 @@ (function (_ extension phase archive inputS) (do [! ////////phase.monad] [inputG (phase archive inputS)] - (in (_.apply/1 (<| (_.closure (list $input)) - (_.return (|> (_.var "string.byte") - (_.apply/* (list $input (_.int +1) (_.length $input))) - (_.apply/1 (_.var "table.pack"))))) - inputG))))])) + (in (<| (_.apply (list inputG)) + (_.closure (list $input)) + (_.return (_.apply (list (_.apply (list $input (_.int +1) (_.length $input)) + (_.var "string.byte"))) + (_.var "table.pack")))))))])) (def: utf8::decode (custom @@ -121,9 +121,9 @@ (function (_ extension phase archive inputS) (do [! ////////phase.monad] [inputG (phase archive inputS)] - (in (|> inputG - (_.apply/1 (_.var "table.unpack")) - (_.apply/1 (_.var "string.char"))))))])) + (in (_.apply (list (_.apply (list inputG) + (_.var "table.unpack"))) + (_.var "string.char")))))])) (def: utf8 Bundle @@ -146,7 +146,7 @@ (do [! ////////phase.monad] [abstractionG (phase archive abstractionS) inputsG (monad.each ! (phase archive) inputsS)] - (in (_.apply/* inputsG abstractionG))))])) + (in (_.apply inputsG abstractionG))))])) (def: lua::power (custom @@ -177,11 +177,11 @@ (variable "input")) (list.repeated (.nat arity) []))] (in (<| (_.closure g!inputs) - _.statement + _.return (case (.nat arity) - 0 (_.apply/1 abstractionG //runtime.unit) - 1 (_.apply/* g!inputs abstractionG) - _ (_.apply/1 abstractionG (_.array g!inputs)))))))])) + 0 (_.apply (list //runtime.unit) abstractionG) + 1 (_.apply g!inputs abstractionG) + _ (_.apply (list (_.array g!inputs)) abstractionG))))))])) (def: .public bundle Bundle @@ -196,5 +196,5 @@ (/.install "power" lua::power) (/.install "import" lua::import) (/.install "function" lua::function) - (/.install "script universe" (nullary (function.constant (_.bool reference.universe)))) + (/.install "script universe" (nullary (function.constant (_.boolean reference.universe)))) ))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/case.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/case.lux index d4f994a5d..b28f5b5a7 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/case.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/case.lux @@ -23,19 +23,13 @@ ["[1][0]" value] ["[1][0]" structure] [//// - ["[0]" synthesis {"+" Path Synthesis}] + ["[0]" synthesis {"+" Path Fork Synthesis}] ["[0]" generation] [/// ["[0]" phase ("operation#[0]" monad)] [reference [variable {"+" Register}]]]]]) -(def: equals_name - "equals") - -(def: equals_type - (type.method [(list) (list //type.value) type.boolean (list)])) - (def: (pop_alt stack_depth) (-> Nat (Bytecode Any)) (.case stack_depth @@ -55,10 +49,6 @@ (-> (I64 Any) (Bytecode Any)) (|>> .int _.long)) -(def: double - (-> Frac (Bytecode Any)) - (|>> _.double)) - (def: peek (Bytecode Any) ($_ _.composite @@ -90,178 +80,240 @@ (..int lefts) //runtime.right_projection)) -(def: (path' stack_depth @else @end phase archive path) - (-> Nat Label Label (Generator Path)) - (.case path - {synthesis.#Pop} - (operation#in ..pop) - - {synthesis.#Bind register} - (operation#in ($_ _.composite - ..peek - (_.astore register))) +(def: equals@Object + (.let [class (type.class "java.lang.Object" (list)) + method (type.method [(list) (list //type.value) type.boolean (list)])] + (_.invokevirtual class "equals" method))) - {synthesis.#Then bodyS} - (do phase.monad - [bodyG (phase archive bodyS)] - (in ($_ _.composite - (..pop_alt stack_depth) - bodyG - (_.goto @end)))) - - (^template [ ] - [(^ ( lefts)) - (operation#in - (do _.monad - [@success _.new_label - @fail _.new_label] +(def: (path|bind register) + (-> Register (Operation (Bytecode Any))) + (operation#in ($_ _.composite + ..peek + (_.astore register)))) + +(def: (path|bit_fork again @else [when thenP elseP]) + (-> (-> Path (Operation (Bytecode Any))) + Label [Bit Path (Maybe Path)] + (Operation (Bytecode Any))) + (do phase.monad + [then! (again thenP) + else! (.case elseP + {.#Some elseP} + (again elseP) + + {.#None} + (in (_.goto @else))) + .let [if! (.if when _.ifeq _.ifne)]] + (in (do _.monad + [@else _.new_label] ($_ _.composite ..peek - (_.checkcast //type.variant) - (//structure.lefts lefts ) - (//structure.right? ) - //runtime.case - _.dup - (_.ifnull @fail) - (_.goto @success) - (_.set_label @fail) - _.pop - (_.goto @else) - (_.set_label @success) - //runtime.push)))]) - ([synthesis.side/left false] - [synthesis.side/right true]) + (//value.unwrap type.boolean) + (if! @else) + then! + (_.set_label @else) + else!))))) - (^template [ ] - [(^ ( lefts)) - (operation#in ($_ _.composite - ..peek - ( lefts) - //runtime.push))]) - ([synthesis.member/left ..left_projection] - [synthesis.member/right ..right_projection]) +(template [ ] + [(def: ( again @else cons) + (-> (-> Path (Operation (Bytecode Any))) + Label (Fork Path) + (Operation (Bytecode Any))) + (do [! phase.monad] + [fork! (monad.mix ! (function (_ [test thenP] else!) + (do ! + [then! (again thenP)] + (in (do _.monad + [@else _.new_label] + ($_ _.composite + + ( test) + + ( @else) + + then! + (_.set_label @else) + else!))))) + ($_ _.composite + + (_.goto @else)) + {.#Item cons})] + (in ($_ _.composite + ..peek + + fork!))))] - ... Extra optimization - (^ (synthesis.path/seq - (synthesis.member/left 0) - (synthesis.!bind_top register thenP))) - (do phase.monad - [thenG (path' stack_depth @else @end phase archive thenP)] - (in ($_ _.composite - ..peek - (_.checkcast //type.tuple) - _.iconst_0 - _.aaload - (_.astore register) - thenG))) + [path|i64_fork (I64 Any) (//value.unwrap type.long) _.dup2 _.pop2 ..long _.lcmp _.ifne] + [path|f64_fork Frac (//value.unwrap type.double) _.dup2 _.pop2 _.double _.dcmpl _.ifne] + [path|text_fork Text (# _.monad in []) _.dup _.pop _.string ..equals@Object _.ifeq] + ) + +(def: (path' stack_depth @else @end phase archive) + (-> Nat Label Label (Generator Path)) + (function (again path) + (.case path + {synthesis.#Pop} + (operation#in ..pop) + + {synthesis.#Bind register} + (..path|bind register) + + (^template [ ] + [{ it} + ( again @else it)]) + ([synthesis.#Bit_Fork ..path|bit_fork] + [synthesis.#I64_Fork ..path|i64_fork] + [synthesis.#F64_Fork ..path|f64_fork] + [synthesis.#Text_Fork ..path|text_fork]) - ... Extra optimization - (^template [ ] - [(^ (synthesis.path/seq - ( lefts) - (synthesis.!bind_top register thenP))) - (do phase.monad - [then! (path' stack_depth @else @end phase archive thenP)] - (in ($_ _.composite - ..peek - (_.checkcast //type.tuple) - (..int lefts) - - (_.astore register) - then!)))]) - ([synthesis.member/left //runtime.left_projection] - [synthesis.member/right //runtime.right_projection]) + {synthesis.#Then bodyS} + (do phase.monad + [body! (phase archive bodyS)] + (in ($_ _.composite + (..pop_alt stack_depth) + body! + (_.when_continuous (_.goto @end))))) + + (^template [ ] + [(^ ( lefts)) + (operation#in + (do _.monad + [@success _.new_label] + ($_ _.composite + ..peek + (_.checkcast //type.variant) + (//structure.lefts lefts) + (//structure.right? ) + //runtime.case + _.dup + (_.ifnonnull @success) + _.pop + (_.goto @else) + (_.set_label @success) + //runtime.push)))]) + ([#0 synthesis.side/left] + [#1 synthesis.side/right]) - {synthesis.#Alt leftP rightP} - (do phase.monad - [@alt_else //runtime.forge_label - left! (path' (++ stack_depth) @alt_else @end phase archive leftP) - right! (path' stack_depth @else @end phase archive rightP)] - (in ($_ _.composite - _.dup - left! - (_.set_label @alt_else) - _.pop - right!))) - - {synthesis.#Seq leftP rightP} - (do phase.monad - [left! (path' stack_depth @else @end phase archive leftP) - right! (path' stack_depth @else @end phase archive rightP)] - (in ($_ _.composite - left! - right!))) + (^template [ ] + [(^ ( lefts)) + (operation#in ($_ _.composite + ..peek + ( lefts) + //runtime.push)) - _ - (undefined) - )) + ... Extra optimization + (^ (synthesis.path/seq + ( lefts) + (synthesis.!bind_top register thenP))) + (do phase.monad + [then! (path' stack_depth @else @end phase archive thenP)] + (in ($_ _.composite + ..peek + ( lefts) + (_.astore register) + then!)))]) + ([synthesis.member/left ..left_projection] + [synthesis.member/right ..right_projection]) + + {synthesis.#Seq leftP rightP} + (do phase.monad + [left! (path' stack_depth @else @end phase archive leftP) + right! (path' stack_depth @else @end phase archive rightP)] + (in ($_ _.composite + left! + right!))) + + {synthesis.#Alt leftP rightP} + (do phase.monad + [@alt_else //runtime.forge_label + left! (path' (++ stack_depth) @alt_else @end phase archive leftP) + right! (path' stack_depth @else @end phase archive rightP)] + (in ($_ _.composite + _.dup + left! + (_.set_label @alt_else) + _.pop + right!))) + ))) (def: (path @end phase archive path) (-> Label (Generator Path)) (do phase.monad [@else //runtime.forge_label - pathG (..path' 1 @else @end phase archive path)] + path! (..path' 1 @else @end phase archive path)] (in ($_ _.composite - pathG - (_.set_label @else) - _.pop - //runtime.pm_failure - _.aconst_null - (_.goto @end))))) + path! + (do _.monad + [?@else (_.acknowledged? @else)] + (.case ?@else + {.#None} + (in []) + + {.#Some _} + ($_ _.composite + (_.set_label @else) + _.pop ... TODO: Comment this line + //runtime.pm_failure + _.aconst_null ... TODO: Comment this line + (_.goto @end) + ))) + )))) -(def: .public (if phase archive [conditionS thenS elseS]) +(def: .public (if phase archive [testS thenS elseS]) (Generator [Synthesis Synthesis Synthesis]) (do phase.monad - [conditionG (phase archive conditionS) - thenG (phase archive thenS) - elseG (phase archive elseS)] + [test! (phase archive testS) + then! (phase archive thenS) + else! (phase archive elseS)] (in (do _.monad [@else _.new_label @end _.new_label] ($_ _.composite - conditionG + test! (//value.unwrap type.boolean) (_.ifeq @else) - thenG - (_.goto @end) + then! + (_.when_continuous (_.goto @end)) (_.set_label @else) - elseG + else! (_.set_label @end)))))) (def: .public (let phase archive [inputS register bodyS]) (Generator [Synthesis Register Synthesis]) (do phase.monad - [inputG (phase archive inputS) - bodyG (phase archive bodyS)] + [input! (phase archive inputS) + body! (phase archive bodyS)] (in ($_ _.composite - inputG + input! (_.astore register) - bodyG)))) + body!)))) (def: .public (get phase archive [path recordS]) (Generator [(List synthesis.Member) Synthesis]) (do phase.monad - [recordG (phase archive recordS)] - (in (list#mix (function (_ step so_far) - (.let [next (.case step - {.#Left lefts} - (..left_projection lefts) - - {.#Right lefts} - (..right_projection lefts))] - (_.composite so_far next))) - recordG + [record! (phase archive recordS)] + (in (list#mix (function (_ step so_far!) + (.let [next! (.case step + {.#Left lefts} + (..left_projection lefts) + + {.#Right lefts} + (..right_projection lefts))] + ($_ _.composite + so_far! + next!))) + record! (list.reversed path))))) (def: .public (case phase archive [valueS path]) (Generator [Synthesis Path]) (do phase.monad [@end //runtime.forge_label - valueG (phase archive valueS) - pathG (..path @end phase archive path)] + value! (phase archive valueS) + path! (..path @end phase archive path)] (in ($_ _.composite _.aconst_null - valueG + value! //runtime.push - pathG + path! (_.set_label @end))))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function/method/apply.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function/method/apply.lux index 050ca318a..a7f0d7ac6 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function/method/apply.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function/method/apply.lux @@ -149,7 +149,7 @@ current_partials (..inputs ..this_offset apply_arity) missing_partials - (_.invokevirtual class //init.name (//init.type environment function_arity)) + (_.invokespecial class //init.name (//init.type environment function_arity)) _.areturn))))))) (monad.all _.monad))]] ($_ _.composite diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function/method/implementation.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function/method/implementation.lux index 664e0fbc8..6c8a9ee75 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function/method/implementation.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/function/method/implementation.lux @@ -35,7 +35,7 @@ {.#Some ($_ _.composite (_.set_label @begin) body - _.areturn + (_.when_continuous _.areturn) )})) (def: .public method diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/structure.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/structure.lux index cf9f6b02e..5a1ec9ea6 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/structure.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/structure.lux @@ -55,8 +55,8 @@ _ (_.anewarray $Object)] (monad.all ! membersI)))))) -(def: .public (lefts lefts right?) - (-> Nat Bit (Bytecode Any)) +(def: .public (lefts lefts) + (-> Nat (Bytecode Any)) (case lefts 0 _.iconst_0 1 _.iconst_1 @@ -87,7 +87,7 @@ (do phase.monad [valueI (phase archive valueS)] (in (do _.monad - [_ (..lefts lefts right?) + [_ (..lefts lefts) _ (..right? right?) _ valueI] (_.invokestatic //runtime.class "variant" diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/case.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/case.lux index 3afa582f0..16c8d5c19 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/case.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/case.lux @@ -1,34 +1,34 @@ (.using - [library - [lux {"-" case let if} - [abstract - ["[0]" monad {"+" do}]] - [data - ["[0]" text - ["%" format {"+" format}]] - [collection - ["[0]" list ("[1]#[0]" functor mix)] - ["[0]" set]]] - [target - ["_" lua {"+" Expression Var Statement}]]]] - ["[0]" // "_" - ["[1][0]" runtime {"+" Operation Phase Phase! Generator Generator!}] + [library + [lux {"-" case let if} + [abstract + ["[0]" monad {"+" do}]] + [data + ["[0]" text + ["%" format {"+" format}]] + [collection + ["[0]" list ("[1]#[0]" functor mix)] + ["[0]" set]]] + [target + ["_" lua {"+" Expression Var Statement}]]]] + ["[0]" // "_" + ["[1][0]" runtime {"+" Operation Phase Phase! Generator Generator!}] + ["[1][0]" reference] + ["[1][0]" primitive] + ["/[1]" // "_" ["[1][0]" reference] - ["[1][0]" primitive] ["/[1]" // "_" - ["[1][0]" reference] + ["[1][0]" synthesis "_" + ["[1]/[0]" case]] ["/[1]" // "_" - ["[1][0]" synthesis "_" - ["[1]/[0]" case]] - ["/[1]" // "_" - ["[1][0]" synthesis {"+" Member Synthesis Path}] - ["[1][0]" generation] - ["//[1]" /// "_" - [reference - ["[1][0]" variable {"+" Register}]] - ["[1][0]" phase ("[1]#[0]" monad)] - [meta - [archive {"+" Archive}]]]]]]]) + ["[1][0]" synthesis {"+" Member Synthesis Path}] + ["[1][0]" generation] + ["//[1]" /// "_" + [reference + ["[1][0]" variable {"+" Register}]] + ["[1][0]" phase ("[1]#[0]" monad)] + [meta + [archive {"+" Archive}]]]]]]]) (def: .public register (-> Register Var) @@ -47,7 +47,7 @@ (in (|> bodyO _.return (_.closure (list (..register register))) - (_.apply/* (list valueO)))))) + (_.apply (list valueO)))))) (def: .public (let! statement expression archive [valueS register bodyS]) (Generator! [Synthesis Register Synthesis]) @@ -83,7 +83,7 @@ (_.return thenO) (_.return elseO)) (_.closure (list)) - (_.apply/* (list)))))) + (_.apply (list)))))) (def: .public (if! statement expression archive [testS thenS elseS]) (Generator! [Synthesis Synthesis Synthesis]) @@ -101,11 +101,11 @@ (def: (push! value) (-> Expression Statement) - (_.statement (|> (_.var "table.insert") (_.apply/* (list @cursor value))))) + (_.statement (|> (_.var "table.insert") (_.apply (list @cursor value))))) (def: peek_and_pop Expression - (|> (_.var "table.remove") (_.apply/* (list @cursor)))) + (|> (_.var "table.remove") (_.apply (list @cursor)))) (def: pop! Statement @@ -118,17 +118,17 @@ (def: save! Statement (_.statement (|> (_.var "table.insert") - (_.apply/* (list @savepoint - (_.apply/* (list @cursor - (_.int +1) - (_.length @cursor) - (_.int +1) - (_.table (list))) - (_.var "table.move"))))))) + (_.apply (list @savepoint + (_.apply (list @cursor + (_.int +1) + (_.length @cursor) + (_.int +1) + (_.table (list))) + (_.var "table.move"))))))) (def: restore! Statement - (_.set (list @cursor) (|> (_.var "table.remove") (_.apply/* (list @savepoint))))) + (_.set (list @cursor) (|> (_.var "table.remove") (_.apply (list @savepoint))))) (def: fail! _.break) @@ -152,7 +152,7 @@ (def: (alternation pre! post!) (-> Statement Statement Statement) ($_ _.then - (_.while (_.bool true) + (_.while (_.boolean true) ($_ _.then ..save! pre!)) @@ -200,7 +200,10 @@ ..peek) then!]))) {.#Item item})] - (in (_.cond clauses ..fail!)))]) + (in (list#mix (function (_ [when then!] else!) + (_.if when then! else!)) + ..fail! + clauses)))]) ([/////synthesis.#I64_Fork (<| _.int .int)] [/////synthesis.#F64_Fork _.float] [/////synthesis.#Text_Fork _.string]) @@ -244,9 +247,9 @@ (do ///////phase.monad [pattern_matching! (pattern_matching' statement expression archive pathP)] (in ($_ _.then - (_.while (_.bool true) + (_.while (_.boolean true) pattern_matching!) - (_.statement (|> (_.var "error") (_.apply/* (list (_.string ////synthesis/case.pattern_matching_error))))))))) + (_.statement (|> (_.var "error") (_.apply (list (_.string ////synthesis/case.pattern_matching_error))))))))) (def: .public dependencies (-> Path (List Var)) @@ -278,4 +281,4 @@ (..case! statement expression archive) (# ///////phase.monad each (|>> (_.closure (list)) - (_.apply/* (list)))))) + (_.apply (list)))))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/function.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/function.lux index 649692ccc..5ce1e0b7a 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/function.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/function.lux @@ -1,43 +1,43 @@ (.using - [library - [lux {"-" Tuple Variant Label function} - [abstract - ["[0]" monad {"+" do}]] - [control - pipe] - [data - ["[0]" product] - [text - ["%" format {"+" format}]] - [collection - ["[0]" list ("[1]#[0]" functor mix)]]] - [target - ["_" lua {"+" Var Expression Label Statement}]]]] - ["[0]" // "_" - ["[1][0]" runtime {"+" Operation Phase Phase! Generator}] + [library + [lux {"-" Tuple Variant Label function} + [abstract + ["[0]" monad {"+" do}]] + [control + pipe] + [data + ["[0]" product] + [text + ["%" format {"+" format}]] + [collection + ["[0]" list ("[1]#[0]" functor mix)]]] + [target + ["_" lua {"+" Var Expression Label Statement}]]]] + ["[0]" // "_" + ["[1][0]" runtime {"+" Operation Phase Phase! Generator}] + ["[1][0]" reference] + ["[1][0]" case] + ["/[1]" // "_" ["[1][0]" reference] - ["[1][0]" case] - ["/[1]" // "_" - ["[1][0]" reference] + ["//[1]" /// "_" + [analysis {"+" Variant Tuple Abstraction Application Analysis}] + [synthesis {"+" Synthesis}] + ["[1][0]" generation {"+" Context}] ["//[1]" /// "_" - [analysis {"+" Variant Tuple Abstraction Application Analysis}] - [synthesis {"+" Synthesis}] - ["[1][0]" generation {"+" Context}] - ["//[1]" /// "_" - [arity {"+" Arity}] - ["[1][0]" phase ("[1]#[0]" monad)] - [meta - [archive - ["[0]" dependency]]] - [reference - [variable {"+" Register Variable}]]]]]]) + [arity {"+" Arity}] + ["[1][0]" phase ("[1]#[0]" monad)] + [meta + [archive + ["[0]" dependency]]] + [reference + [variable {"+" Register Variable}]]]]]]) (def: .public (apply expression archive [functionS argsS+]) (Generator (Application Synthesis)) (do [! ///////phase.monad] [functionO (expression archive functionS) argsO+ (monad.each ! (expression archive) argsS+)] - (in (_.apply/* argsO+ functionO)))) + (in (_.apply argsO+ functionO)))) (def: capture (-> Register Var) @@ -57,7 +57,7 @@ ($_ _.then (_.local_function @self @args body!) (_.return @self))) - (_.apply/* inits @self)]))) + (_.apply inits @self)]))) (def: input (|>> ++ //case.register)) @@ -90,51 +90,52 @@ initialize_self! (list.indices arity)) pack (|>> (list) _.array) - unpack (_.apply/1 (_.var "table.unpack")) + unpack (: (-> Expression Expression) + (.function (_ it) + (_.apply (list it) (_.var "table.unpack")))) @var_args (_.var "...")] .let [[definition instantiation] (with_closure closureO+ @self (list @var_args) ($_ _.then (_.local/1 @curried (pack @var_args)) (_.local/1 @num_args (_.length @curried)) - (_.cond (list [(|> @num_args (_.= arityO)) - ($_ _.then - initialize! - (_.set_label @scope) - body!)] - [(|> @num_args (_.> arityO)) - (let [arity_inputs (_.apply/5 (_.var "table.move") - @curried - (_.int +1) - arityO - (_.int +1) - (_.array (list))) - extra_inputs (_.apply/5 (_.var "table.move") - @curried - (_.+ (_.int +1) arityO) - @num_args - (_.int +1) - (_.array (list)))] - (_.return (|> @self - (_.apply/* (list (unpack arity_inputs))) - (_.apply/* (list (unpack extra_inputs))))))]) - ... (|> @num_args (_.< arityO)) - (_.return (_.closure (list @var_args) - (let [@extra_args (_.var "extra_args")] - ($_ _.then - (_.local/1 @extra_args (pack @var_args)) - (_.return (|> (_.array (list)) - (_.apply/5 (_.var "table.move") - @curried - (_.int +1) - @num_args - (_.int +1)) - (_.apply/5 (_.var "table.move") - @extra_args - (_.int +1) - (_.length @extra_args) - (_.+ (_.int +1) @num_args)) - unpack - (_.apply/1 @self)))))))) + (<| (_.if (|> @num_args (_.= arityO)) + ($_ _.then + initialize! + (_.set_label @scope) + body!)) + (_.if (|> @num_args (_.> arityO)) + (let [arity_inputs (_.apply (list @curried + (_.int +1) + arityO + (_.int +1) + (_.array (list))) + (_.var "table.move")) + extra_inputs (_.apply (list @curried + (_.+ (_.int +1) arityO) + @num_args + (_.int +1) + (_.array (list))) + (_.var "table.move"))] + (_.return (|> @self + (_.apply (list (unpack arity_inputs))) + (_.apply (list (unpack extra_inputs))))))) + ... (|> @num_args (_.< arityO)) + (_.return (_.closure (list @var_args) + (let [@extra_args (_.var "extra_args")] + ($_ _.then + (_.local/1 @extra_args (pack @var_args)) + (_.return (_.apply (list (unpack (_.apply (list @extra_args + (_.int +1) + (_.length @extra_args) + (_.+ (_.int +1) @num_args) + (_.apply (list @curried + (_.int +1) + @num_args + (_.int +1) + (_.array (list))) + (_.var "table.move"))) + (_.var "table.move")))) + @self))))))) ))] _ (/////generation.execute! definition) _ (/////generation.save! (product.right function_name) {.#None} definition)] diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/loop.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/loop.lux index c58a5d476..06135b240 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/loop.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/loop.lux @@ -108,10 +108,10 @@ scope!) (_.return @loop) )) - (|> @context (_.apply/* foreigns))])))] + (_.apply foreigns @context)])))] _ (/////generation.execute! directive) _ (/////generation.save! artifact_id {.#None} directive)] - (in (|> instantiation (_.apply/* initsO+)))))) + (in (_.apply initsO+ instantiation))))) (def: .public (again! statement expression archive argsS+) (Generator! (List Synthesis)) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/primitive.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/primitive.lux index 3c879b684..556371e6a 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/primitive.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/primitive.lux @@ -9,7 +9,7 @@ (-> Literal) )] - [bit Bit _.bool] + [bit Bit _.boolean] [i64 (I64 Any) (|>> .int _.int)] [f64 Frac _.float] [text Text _.string] diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux index f20a6cb12..40525dd00 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux @@ -1,42 +1,42 @@ (.using - [library - [lux {"-" Label Location} - ["[0]" meta] - [abstract - ["[0]" monad {"+" do}]] - [control - ["[0]" function] - ["<>" parser - ["<[0]>" code]]] - [data - ["[0]" product] - ["[0]" text ("[1]#[0]" hash) - ["%" format {"+" format}] - [encoding - ["[0]" utf8]]] - [collection - ["[0]" list ("[1]#[0]" functor)] - ["[0]" sequence]]] - ["[0]" macro - [syntax {"+" syntax:}] - ["[0]" code]] - [math - [number {"+" hex} - ["[0]" i64]]] - ["@" target - ["_" lua {"+" Expression Location Var Computation Literal Label Statement}]]]] - ["[0]" /// "_" - ["[1][0]" reference] - ["//[1]" /// "_" - ["[1][0]" synthesis {"+" Synthesis}] - ["[1][0]" generation] - ["//[1]" /// - ["[1][0]" phase] - [reference - [variable {"+" Register}]] - [meta - [archive {"+" Output Archive} - ["[0]" artifact {"+" Registry}]]]]]]) + [library + [lux {"-" Label Location} + ["[0]" meta] + [abstract + ["[0]" monad {"+" do}]] + [control + ["[0]" function] + ["<>" parser + ["<[0]>" code]]] + [data + ["[0]" product] + ["[0]" text ("[1]#[0]" hash) + ["%" format {"+" format}] + [encoding + ["[0]" utf8]]] + [collection + ["[0]" list ("[1]#[0]" functor)] + ["[0]" sequence]]] + ["[0]" macro + [syntax {"+" syntax:}] + ["[0]" code]] + [math + [number {"+" hex} + ["[0]" i64]]] + ["@" target + ["_" lua {"+" Expression Location Var Computation Literal Label Statement}]]]] + ["[0]" /// "_" + ["[1][0]" reference] + ["//[1]" /// "_" + ["[1][0]" synthesis {"+" Synthesis}] + ["[1][0]" generation] + ["//[1]" /// + ["[1][0]" phase] + [reference + [variable {"+" Register}]] + [meta + [archive {"+" Output Archive} + ["[0]" artifact {"+" Registry}]]]]]]) (template [ ] [(type: .public @@ -148,7 +148,7 @@ inputs)] (in (list (` (def: .public ((~ g!name) (~+ inputsC)) (-> (~+ inputs_typesC) Computation) - (_.apply/* (list (~+ inputsC)) (~ runtime_name)))) + (_.apply (list (~+ inputsC)) (~ runtime_name)))) (` (def: (~ (code.local_symbol (format "@" name))) Statement @@ -170,7 +170,7 @@ (_.set (list tuple) (..item last_index_right tuple))))] (runtime: (tuple//left lefts tuple) (with_vars [last_index_right] - (<| (_.while (_.bool true)) + (<| (_.while (_.boolean true)) ($_ _.then (_.local/1 last_index_right (..last_index tuple)) (_.if (_.> lefts last_index_right) @@ -181,21 +181,21 @@ (runtime: (tuple//right lefts tuple) (with_vars [last_index_right right_index] - (<| (_.while (_.bool true)) + (<| (_.while (_.boolean true)) ($_ _.then (_.local/1 last_index_right (..last_index tuple)) (_.local/1 right_index (_.+ (_.int +1) lefts)) - (_.cond (list [(_.= last_index_right right_index) - (_.return (..item right_index tuple))] - [(_.> last_index_right right_index) - ... Needs recursion. - ]) - (_.return (_.apply/* (list tuple - (_.+ (_.int +1) right_index) - (_.length tuple) - (_.int +1) - (_.array (list))) - (_.var "table.move")))) + (<| (_.if (_.= last_index_right right_index) + (_.return (..item right_index tuple))) + (_.if (_.> last_index_right right_index) + ... Needs recursion. + ) + (_.return (_.apply (list tuple + (_.+ (_.int +1) right_index) + (_.length tuple) + (_.int +1) + (_.array (list))) + (_.var "table.move")))) ))))) (runtime: (sum//get sum expected##right? expected##lefts) @@ -208,24 +208,22 @@ (_.- actual##lefts) (_.- (_.int +1)))) (_.set (list sum) actual##value))] - (<| (_.while (_.bool true)) - (_.cond (list [(_.= expected##lefts actual##lefts) - (_.if (_.= expected##right? actual##right?) - (_.return actual##value) - mismatch!)] - - [(_.< expected##lefts actual##lefts) - (_.if (_.= ..unit actual##right?) - recur! - mismatch!)] - - [(_.= ..unit expected##right?) - (_.return (variant' (|> actual##lefts - (_.- expected##lefts) - (_.- (_.int +1))) - actual##right? - actual##value))]) - mismatch!)))) + (<| (_.while (_.boolean true)) + (_.if (_.= expected##lefts actual##lefts) + (_.if (_.= expected##right? actual##right?) + (_.return actual##value) + mismatch!)) + (_.if (_.< expected##lefts actual##lefts) + (_.if (_.= ..unit actual##right?) + recur! + mismatch!)) + (_.if (_.= ..unit expected##right?) + (_.return (variant' (|> actual##lefts + (_.- expected##lefts) + (_.- (_.int +1))) + actual##right? + actual##value))) + mismatch!))) (def: runtime//adt Statement @@ -238,9 +236,9 @@ (runtime: (lux//try risky) (with_vars [success value] ($_ _.then - (_.let (list success value) (|> risky (_.apply/* (list ..unit)) + (_.let (list success value) (|> risky (_.apply (list ..unit)) _.return (_.closure (list)) - list _.apply/* (|> (_.var "pcall")))) + list _.apply (|> (_.var "pcall")))) (_.if success (_.return (..right value)) (_.return (..left value)))))) @@ -306,18 +304,17 @@ (def: (find_byte_index subject param start) (-> Expression Expression Expression Expression) - (_.apply/4 (_.var "string.find") subject param start (_.bool #1))) + (_.apply (list subject param start (_.boolean #1)) + (_.var "string.find"))) (def: (char_index subject byte_index) (-> Expression Expression Expression) - (|> byte_index - (_.apply/3 (_.var "utf8.len") subject (_.int +1)))) + (_.apply (list subject (_.int +1) byte_index) + (_.var "utf8.len"))) (def: (byte_index subject char_index) (-> Expression Expression Expression) - (|> char_index - (_.+ (_.int +1)) - (_.apply/2 (_.var "utf8.offset") subject))) + (_.apply (list subject (_.+ (_.int +1) char_index)) (_.var "utf8.offset"))) (def: lux_index (-> Expression Expression) @@ -352,22 +349,23 @@ ))))) (runtime: (text//clip text offset length) - (with_expansions [ (_.return (_.apply/3 (_.var "string.sub") text (_.+ (_.int +1) offset) (_.+ offset length))) - (_.return (_.apply/3 (_.var "string.sub") - text - (..byte_index text offset) - (|> (_.+ offset length) - ... (_.+ (_.int +1)) - (..byte_index text) - (_.- (_.int +1)))))] + (with_expansions [ (_.return (_.apply (list text (_.+ (_.int +1) offset) (_.+ offset length)) + (_.var "string.sub"))) + (_.return (_.apply (list text + (..byte_index text offset) + (|> (_.+ offset length) + ... (_.+ (_.int +1)) + (..byte_index text) + (_.- (_.int +1)))) + (_.var "string.sub")))] (for [@.lua ] (_.if ..on_rembulan? )))) (runtime: (text//size subject) - (with_expansions [ (_.return (_.apply/1 (_.var "string.len") subject)) - (_.return (_.apply/1 (_.var "utf8.len") subject))] + (with_expansions [ (_.return (_.apply (list subject) (_.var "string.len"))) + (_.return (_.apply (list subject) (_.var "utf8.len")))] (for [@.lua ] (_.if ..on_rembulan? @@ -376,17 +374,17 @@ (runtime: (text//char idx text) (with_expansions [ (with_vars [char] ($_ _.then - (_.local/1 char (_.apply/* (list text idx) - (_.var "string.byte"))) + (_.local/1 char (_.apply (list text idx) + (_.var "string.byte"))) (_.if (_.= _.nil char) (_.statement (_.error/1 (_.string "[Lux Error] Cannot get char from text."))) (_.return char)))) (with_vars [offset char] ($_ _.then - (_.local/1 offset (_.apply/2 (_.var "utf8.offset") text idx)) + (_.local/1 offset (_.apply (list text idx) (_.var "utf8.offset"))) (_.if (_.= _.nil offset) (_.statement (_.error/1 (_.string "[Lux Error] Cannot get char from text."))) - (_.return (_.apply/2 (_.var "utf8.codepoint") text offset)))))] + (_.return (_.apply (list text offset) (_.var "utf8.codepoint"))))))] (for [@.lua ] (_.if ..on_rembulan? diff --git a/stdlib/source/test/lux.lux b/stdlib/source/test/lux.lux index 255d15c71..631f754e4 100644 --- a/stdlib/source/test/lux.lux +++ b/stdlib/source/test/lux.lux @@ -65,8 +65,9 @@ (~~ (.for ["{old}" (~~ (.as_is ["[1]/[0]" jvm])) "JVM" (~~ (.as_is ["[1]/[0]" jvm])) "JavaScript" (~~ (.as_is ["[1]/[0]" js])) - "Ruby" (~~ (.as_is ["[1]/[0]" ruby])) - "Python" (~~ (.as_is ["[1]/[0]" python]))] + "Lua" (~~ (.as_is ["[1]/[0]" lua])) + "Python" (~~ (.as_is ["[1]/[0]" python])) + "Ruby" (~~ (.as_is ["[1]/[0]" ruby]))] (~~ (.as_is))))] ]))) @@ -101,8 +102,9 @@ (~~ (for [@.jvm (~~ (as_is /target/jvm.test)) @.old (~~ (as_is /target/jvm.test)) @.js (~~ (as_is /target/js.test)) - @.ruby (~~ (as_is /target/ruby.test)) - @.python (~~ (as_is /target/python.test))] + @.lua (~~ (as_is /target/lua.test)) + @.python (~~ (as_is /target/python.test)) + @.ruby (~~ (as_is /target/ruby.test))] (~~ (as_is)))) (~~ (for [@.old (~~ (as_is))] (~~ (as_is /extension.test)))) @@ -181,21 +183,21 @@ (case (/.try expected) {.#Left _} false - + {.#Right actual} (n.= expected actual))) (_.cover [/.undefined] (case (/.try (/.undefined)) {.#Left _} true - + {.#Right _} false)) (_.cover [/.panic!] (case (/.try (/.panic! expected_error)) {.#Left actual_error} (text.contains? expected_error actual_error) - + {.#Right _} false)) ))) @@ -1116,7 +1118,7 @@ (value@ .#mappings) (list#each product.left) (set.of_list text.hash)) - + correct_locals! (and (n.= 4 (value@ .#counter locals/2)) (set#= expected_locals/2 @@ -1149,7 +1151,7 @@ (binding? captured? let/0))] (and correct_locals! correct_closure!)) - + _ false))))) diff --git a/stdlib/source/test/lux/extension.lux b/stdlib/source/test/lux/extension.lux index 92b314dc7..a754e9bd2 100644 --- a/stdlib/source/test/lux/extension.lux +++ b/stdlib/source/test/lux/extension.lux @@ -150,6 +150,9 @@ @.js (generation.save! artifact_id {.#None} (js.comment commentary (js.statement (js.string commentary)))) + @.python (generation.save! artifact_id {.#None} + (python.comment commentary + (python.statement (python.string commentary)))) @.lua (generation.save! artifact_id {.#None} (lua.comment commentary (lua.statement (lua.string commentary)))) @@ -159,8 +162,10 @@ (generation.log! commentary))))] (in directive.no_requirements))) - ... TODO: No longer skip testing Lua after Rembulan isn't being used anymore. - (for [@.lua (as_is)] + (for [... TODO: No longer skip testing Lua after Rembulan isn't being used anymore. + @.lua (as_is) + ... TODO: No longer skip testing Python. + @.python (as_is)] (`` ((~~ (static ..directive)) (n.* 2 3)))) )) diff --git a/stdlib/source/test/lux/target/js.lux b/stdlib/source/test/lux/target/js.lux index cc60dd896..ae190fade 100644 --- a/stdlib/source/test/lux/target/js.lux +++ b/stdlib/source/test/lux/target/js.lux @@ -80,7 +80,7 @@ (try#each (function (_ it) (case it {.#None} true - {.#Some _} true))) + {.#Some _} false))) (try.else false))) (_.cover [/.boolean] (expression (|>> (:as Bit) (bit#= boolean)) diff --git a/stdlib/source/test/lux/target/lua.lux b/stdlib/source/test/lux/target/lua.lux new file mode 100644 index 000000000..2558f41c8 --- /dev/null +++ b/stdlib/source/test/lux/target/lua.lux @@ -0,0 +1,709 @@ +(.using + [library + [lux "*" + ["_" test {"+" Test}] + ["[0]" ffi] + ["[0]" static] + [abstract + [monad {"+" do}] + [\\specification + ["$[0]" equivalence] + ["$[0]" hash]]] + [control + [pipe {"+" case>}] + ["[0]" function] + ["[0]" maybe ("[1]#[0]" functor)] + ["[0]" try {"+" Try} ("[1]#[0]" functor)]] + [data + ["[0]" bit ("[1]#[0]" equivalence)] + ["[0]" text {"+" \n} ("[1]#[0]" equivalence) + ["%" format {"+" format}]] + [collection + ["[0]" list ("[1]#[0]" functor)]]] + [macro + ["[0]" template]] + ["[0]" math + ["[0]" random {"+" Random} ("[1]#[0]" monad)] + [number + ["n" nat] + ["i" int] + ["f" frac] + ["[0]" i64]]]]] + [\\library + ["[0]" /]]) + +... http://www.lua.org/manual/5.3/manual.html#pdf-load +(ffi.import: (load [Text] "?" (-> Any Any))) + +(def: (expression ??? it) + (-> (-> Any Bit) /.Expression Bit) + (|> it + /.code + (format "return ") + ..load + (maybe#each (|>> (function.on []) ???)) + (maybe.else false))) + +(def: test|literal + Test + (do [! random.monad] + [boolean random.bit + int random.int + float random.frac + string (random.ascii/upper 5)] + ($_ _.and + (_.cover [/.nil] + (|> /.nil + /.code + ..load + (case> {.#None} true + {.#Some _} false))) + (_.cover [/.boolean] + (expression (|>> (:as Bit) (bit#= boolean)) + (/.boolean boolean))) + (_.cover [/.int] + (expression (|>> (:as Int) (i.= int)) + (/.int int))) + (_.cover [/.float] + (expression (|>> (:as Frac) (f.= float)) + (/.float float))) + (_.cover [/.string] + (expression (|>> (:as Text) (text#= string)) + (/.string string))) + ))) + +(def: test|boolean + Test + (do [! random.monad] + [left random.bit + right random.bit] + (`` ($_ _.and + (~~ (template [ ] + [(_.cover [] + (let [expected ( left right)] + (expression (|>> (:as Bit) (bit#= expected)) + ( (/.boolean left) (/.boolean right)))))] + + [/.or .or] + [/.and .and] + )) + (_.cover [/.not] + (expression (|>> (:as Bit) (bit#= (not left))) + (/.not (/.boolean left)))) + )))) + +(template [] + [(`` (def: (~~ (template.symbol [int/ ])) + (Random Int) + (let [mask (|> 1 (i64.left_shifted (-- )) --)] + (random#each (|>> (i64.and mask) .int) random.nat))))] + + [16] + [32] + ) + +(def: test|int + Test + (do [! random.monad] + [left random.int + right random.int + shift (# ! each (n.% 65) random.nat) + + parameter (random.only (|>> (i.= +0) not) + random.int) + subject random.int] + (`` ($_ _.and + (~~ (template [ ] + [(_.cover [] + (let [expected ( left right)] + (expression (|>> (:as Int) (i.= expected)) + ( (/.int left) (/.int right)))))] + + [/.bit_or i64.or] + [/.bit_xor i64.xor] + [/.bit_and i64.and] + )) + (_.cover [/.opposite] + (expression (|>> (:as Int) (i.= (i.- left +0))) + (/.opposite (/.int left)))) + (_.cover [/.bit_shl] + (let [expected (i64.left_shifted shift left)] + (expression (|>> (:as Int) (i.= expected)) + (/.bit_shl (/.int (.int shift)) + (/.int left))))) + (_.cover [/.bit_shr] + (let [expected (i64.right_shifted shift left)] + (expression (|>> (:as Int) (i.= expected)) + (/.bit_shr (/.int (.int shift)) + (/.int left))))) + (_.cover [/.//] + (let [expected (if (or (i.= (i.signum parameter) (i.signum subject)) + (i.= +0 (i.% parameter subject))) + (i./ parameter subject) + (-- (i./ parameter subject)))] + (expression (|>> (:as Int) (i.= expected)) + (/.// (/.int parameter) (/.int subject))))) + )))) + +(def: test|float + Test + (do [! random.monad] + [parameter (random.only (|>> (f.= +0.0) not) + random.safe_frac) + subject random.safe_frac] + (`` ($_ _.and + (~~ (template [
]
+                  [(_.cover []
+                            (let [expected ( (
 parameter) (
 subject))]
+                              (expression (|>> (:as Frac) (f.= expected))
+                                          ( (/.float (
 parameter)) (/.float (
 subject))))))]
+
+                  [/.+ f.+ |>]
+                  [/.- f.- |>]
+                  [/.* f.* |>]
+                  [/./ f./ |>]
+                  [/.% f.mod |>]
+                  [/.^ math.pow f.abs]
+                  ))
+            (~~ (template [ ]
+                  [(_.cover []
+                            (let [expected ( parameter subject)]
+                              (expression (|>> (:as Bit) (bit#= expected))
+                                          ( (/.float parameter) (/.float subject)))))]
+
+                  [/.<  f.<]
+                  [/.<= f.<=]
+                  [/.>  f.>]
+                  [/.>= f.>=]
+                  [/.=  f.=]
+                  ))
+            ))))
+
+(def: test|string
+  Test
+  (do random.monad
+    [left (random.ascii/lower 8)
+     right (random.ascii/lower 8)
+     .let [expected (format left right)]]
+    ($_ _.and
+        (_.cover [/.concat]
+                 (expression (|>> (:as Text) (text#= expected))
+                             (|> (/.string left)
+                                 (/.concat (/.string right)))))
+        )))
+
+(def: test|array
+  Test
+  (do [! random.monad]
+    [size (# ! each (|>> (n.% 10) ++) random.nat)
+     index (# ! each (n.% size) random.nat)
+     items (random.list size random.safe_frac)
+     .let [expected (|> items
+                        (list.item index)
+                        maybe.trusted)]]
+    ($_ _.and
+        (_.cover [/.array /.item]
+                 (and (expression (|>> (:as Frac) (f.= expected))
+                                  (/.item (/.int (.int (++ index)))
+                                          (/.array (list#each /.float items))))
+                      (expression (|>> (:as Bit))
+                                  (|> (/.array (list#each /.float items))
+                                      (/.item (/.int (.int (++ size))))
+                                      (/.= /.nil)))))
+        (_.cover [/.length]
+                 (expression (|>> (:as Int) (i.= (.int size)))
+                             (/.length (/.array (list#each /.float items)))))
+        )))
+
+(def: test|table
+  Test
+  (do [! random.monad]
+    [expected random.safe_frac
+     dummy (random.only (|>> (f.= expected) not)
+                        random.safe_frac)
+
+     size (# ! each (|>> (n.% 10) ++) random.nat)
+     index (# ! each (n.% size) random.nat)
+     items (random.list size random.safe_frac)
+
+     $self (# ! each /.var (random.ascii/lower 10))
+     $table (# ! each /.var (random.ascii/lower 11))
+     $arg (# ! each /.var (random.ascii/lower 12))
+     field (random.ascii/upper 5)
+     non_field (random.only (|>> (text#= field) not)
+                            (random.ascii/upper 5))
+     method (random.ascii/upper 6)]
+    ($_ _.and
+        (_.cover [/.table /.the]
+                 (and (expression (|>> (:as Frac) (f.= expected))
+                                  (/.the field (/.table (list [field (/.float expected)]))))
+                      (expression (|>> (:as Bit))
+                                  (|> (/.table (list [field (/.float expected)]))
+                                      (/.the non_field)
+                                      (/.= /.nil)))))
+        (_.cover [/.do /.function]
+                 (expression (|>> (:as Frac) (f.= expected))
+                             (|> ($_ /.then
+                                     (/.local/1 $table (/.table (list [field (/.float expected)])))
+                                     (/.function (/.the method $table) (list $self $arg)
+                                       (/.if (/.= (/.float dummy) $arg)
+                                         (/.return (/.the field $self))
+                                         (/.return $arg)))
+                                     (/.return (/.do method (list (/.float dummy)) $table)))
+                                 (/.closure (list))
+                                 (/.apply (list)))))
+        )))
+
+(def: test|computation
+  Test
+  (do [! random.monad]
+    [test random.bit
+     then random.safe_frac
+     else random.safe_frac
+
+     boolean random.bit
+     int random.int
+     float random.frac
+     string (random.ascii/upper 5)
+
+     comment (random.ascii/upper 10)]
+    ($_ _.and
+        ..test|boolean
+        ..test|int
+        ..test|float
+        ..test|string
+        ..test|array
+        ..test|table
+        (_.cover [/.type/1]
+                 (and (expression (|>> (:as Text) (text#= "boolean"))
+                                  (/.type/1 (/.boolean boolean)))
+                      (expression (|>> (:as Text) (text#= "number"))
+                                  (/.type/1 (/.int int)))
+                      (expression (|>> (:as Text) (text#= "number"))
+                                  (/.type/1 (/.float float)))
+                      (expression (|>> (:as Text) (text#= "string"))
+                                  (/.type/1 (/.string string)))
+                      (expression (|>> (:as Text) (text#= "nil"))
+                                  (/.type/1 /.nil))
+                      (expression (|>> (:as Text) (text#= "table"))
+                                  (/.type/1 (/.table (list [string (/.float float)]))))
+                      (expression (|>> (:as Text) (text#= "table"))
+                                  (/.type/1 (/.array (list (/.boolean boolean)
+                                                           (/.float float)
+                                                           (/.string string)))))
+                      ))
+        (_.cover [/.require/1]
+                 (expression (|>> (:as Int) (i.= (i.abs int)))
+                             (|> (/.require/1 (/.string "math"))
+                                 (/.the "abs")
+                                 (/.apply (list (/.int int))))))
+        (_.cover [/.comment]
+                 (expression (|>> (:as Frac) (f.= then))
+                             (/.comment comment
+                               (/.float then))))
+        )))
+
+(def: test|expression
+  Test
+  (`` ($_ _.and
+          (_.for [/.Literal]
+                 ..test|literal)
+          (_.for [/.Computation]
+                 ..test|computation)
+          )))
+
+(def: test/var
+  Test
+  (do [! random.monad]
+    [float/0 random.safe_frac
+     float/1 random.safe_frac
+     float/2 random.safe_frac
+     foreign (random.ascii/lower 10)
+     local (random.only (|>> (text#= foreign) not)
+                        (random.ascii/lower 10))
+     .let [$foreign (/.var foreign)
+           $local (/.var local)]]
+    ($_ _.and
+        (_.cover [/.var]
+                 (expression (|>> (:as Frac) (f.= float/0))
+                             (|> (/.return $foreign)
+                                 (/.closure (list $foreign))
+                                 (/.apply (list (/.float float/0))))))
+        (_.cover [/.let]
+                 (expression (|>> (:as Frac) (f.= float/1))
+                             (|> ($_ /.then
+                                     (/.let (list $local) (/.float float/1))
+                                     (/.return $local))
+                                 (/.closure (list $foreign))
+                                 (/.apply (list (/.float float/0))))))
+        (_.cover [/.local/1]
+                 (expression (|>> (:as Frac) (f.= float/1))
+                             (|> ($_ /.then
+                                     (/.local/1 $local (/.float float/1))
+                                     (/.return $local))
+                                 (/.closure (list $foreign))
+                                 (/.apply (list (/.float float/0))))))
+        (_.cover [/.local]
+                 (expression (|>> (:as Frac) (f.= float/1))
+                             (|> ($_ /.then
+                                     (/.local (list $local))
+                                     (/.set (list $local) (/.float float/1))
+                                     (/.return $local))
+                                 (/.closure (list $foreign))
+                                 (/.apply (list (/.float float/0))))))
+        )))
+
+(def: test/location
+  Test
+  (do [! random.monad]
+    [float/0 random.safe_frac
+     float/1 random.safe_frac
+     int/0 ..int/16
+     $foreign (# ! each /.var (random.ascii/lower 10))
+     $arg/0 (# ! each /.var (random.ascii/lower 11))
+     $arg/1 (# ! each /.var (random.ascii/lower 12))
+     field (random.ascii/upper 10)]
+    ($_ _.and
+        (_.cover [/.set]
+                 (expression (|>> (:as Frac) (f.= (f.+ float/0 float/0)))
+                             (|> ($_ /.then
+                                     (/.set (list $foreign) (/.+ $foreign $foreign))
+                                     (/.return $foreign))
+                                 (/.closure (list $foreign))
+                                 (/.apply (list (/.float float/0))))))
+        (_.cover [/.multi]
+                 (and (expression (|>> (:as Frac) (f.= float/0))
+                                  (|> ($_ /.then
+                                          (/.set (list $arg/0 $arg/1) (/.multi (list (/.float float/0) (/.float float/1))))
+                                          (/.return $arg/0))
+                                      (/.closure (list))
+                                      (/.apply (list))))
+                      (expression (|>> (:as Frac) (f.= float/1))
+                                  (|> ($_ /.then
+                                          (/.set (list $arg/0 $arg/1) (/.multi (list (/.float float/0) (/.float float/1))))
+                                          (/.return $arg/1))
+                                      (/.closure (list))
+                                      (/.apply (list))))))
+        (_.cover [/.Access]
+                 (and (expression (|>> (:as Frac) (f.= (f.+ float/0 float/0)))
+                                  (let [@ (/.item (/.int +1) $foreign)]
+                                    (|> ($_ /.then
+                                            (/.set (list $foreign) (/.array (list $foreign)))
+                                            (/.set (list @) (/.+ @ @))
+                                            (/.return @))
+                                        (/.closure (list $foreign))
+                                        (/.apply (list (/.float float/0))))))
+                      (expression (|>> (:as Frac) (f.= (f.+ float/0 float/0)))
+                                  (let [@ (/.the field $foreign)]
+                                    (|> ($_ /.then
+                                            (/.set (list $foreign) (/.table (list [field $foreign])))
+                                            (/.set (list @) (/.+ @ @))
+                                            (/.return @))
+                                        (/.closure (list $foreign))
+                                        (/.apply (list (/.float float/0))))))))
+        (_.for [/.Var]
+               ..test/var)
+        )))
+
+(def: test|label
+  Test
+  (do [! random.monad]
+    [input ..int/16
+
+     full_iterations (# ! each (|>> (n.% 20) ++) random.nat)
+     expected_iterations (# ! each (|>> (n.% full_iterations) .int) random.nat)
+
+     $input (# ! each /.var (random.ascii/lower 10))
+     $output (# ! each /.var (random.ascii/lower 11))
+     $index (# ! each /.var (random.ascii/lower 12))
+
+     @loop (# ! each /.label (random.ascii/lower 13))
+     
+     .let [expected (i.* expected_iterations input)
+           expected_iterations (/.int expected_iterations)]]
+    ($_ _.and
+        (_.cover [/.break]
+                 (let [=for_in (expression (|>> (:as Int) (i.= expected))
+                                           (|> ($_ /.then
+                                                   (/.local/1 $output (/.int +0))
+                                                   (/.for_in (list $index $input) (/.ipairs/1 (/.array (list.repeated full_iterations $input)))
+                                                             ($_ /.then
+                                                                 (/.when (/.> expected_iterations $index)
+                                                                         /.break)
+                                                                 (/.set (list $output) (/.+ $input $output))))
+                                                   (/.return $output))
+                                               (/.closure (list $input))
+                                               (/.apply (list (/.int input)))))
+                       
+                       full_iterations (/.int (.int full_iterations))
+                       =while (expression (|>> (:as Int) (i.= expected))
+                                          (|> ($_ /.then
+                                                  (/.local/1 $index (/.int +0))
+                                                  (/.local/1 $output (/.int +0))
+                                                  (/.while (/.< full_iterations $index)
+                                                           ($_ /.then
+                                                               (/.when (/.= expected_iterations $index)
+                                                                       /.break)
+                                                               (/.set (list $output) (/.+ $input $output))
+                                                               (/.set (list $index) (/.+ (/.int +1) $index))
+                                                               ))
+                                                  (/.return $output))
+                                              (/.closure (list $input))
+                                              (/.apply (list (/.int input)))))
+                       =repeat (expression (|>> (:as Int) (i.= expected))
+                                           (|> ($_ /.then
+                                                   (/.local/1 $index (/.int +0))
+                                                   (/.local/1 $output (/.int +0))
+                                                   (/.repeat (/.= full_iterations $index)
+                                                             ($_ /.then
+                                                                 (/.when (/.= expected_iterations $index)
+                                                                         /.break)
+                                                                 (/.set (list $output) (/.+ $input $output))
+                                                                 (/.set (list $index) (/.+ (/.int +1) $index))
+                                                                 ))
+                                                   (/.return $output))
+                                               (/.closure (list $input))
+                                               (/.apply (list (/.int input)))))
+                       =for_step (expression (|>> (:as Int) (i.= expected))
+                                             (|> ($_ /.then
+                                                     (/.local/1 $output (/.int +0))
+                                                     (/.for_step $index (/.int +0) full_iterations (/.int +1)
+                                                                 ($_ /.then
+                                                                     (/.when (/.= expected_iterations $index)
+                                                                             /.break)
+                                                                     (/.set (list $output) (/.+ $input $output))))
+                                                     (/.return $output))
+                                                 (/.closure (list $input))
+                                                 (/.apply (list (/.int input)))))]
+                   (and =while
+                        =repeat
+                        =for_step
+                        =for_in)))
+        (_.cover [/.label /.set_label /.go_to]
+                 (expression (|>> (:as Int) (i.= expected))
+                             (|> ($_ /.then
+                                     (/.local/1 $index (/.int +0))
+                                     (/.local/1 $output (/.int +0))
+                                     (/.set_label @loop)
+                                     (/.if (/.< expected_iterations $index)
+                                       ($_ /.then
+                                           (/.set (list $output) (/.+ $input $output))
+                                           (/.set (list $index) (/.+ (/.int +1) $index))
+                                           (/.go_to @loop))
+                                       (/.return $output)))
+                                 (/.closure (list $input))
+                                 (/.apply (list (/.int input))))))
+        )))
+
+(def: test|loop
+  Test
+  (do [! random.monad]
+    [input ..int/16
+     iterations (# ! each (n.% 10) random.nat)
+     .let [$input (/.var "input")
+           $output (/.var "output")
+           $index (/.var "index")
+           expected (i.* (.int iterations) input)]]
+    ($_ _.and
+        (_.cover [/.while]
+                 (expression (|>> (:as Int) (i.= expected))
+                             (|> ($_ /.then
+                                     (/.local/1 $index (/.int +0))
+                                     (/.local/1 $output (/.int +0))
+                                     (/.while (/.< (/.int (.int iterations)) $index)
+                                              ($_ /.then
+                                                  (/.set (list $output) (/.+ $input $output))
+                                                  (/.set (list $index) (/.+ (/.int +1) $index))
+                                                  ))
+                                     (/.return $output))
+                                 (/.closure (list $input))
+                                 (/.apply (list (/.int input))))))
+        (_.cover [/.repeat]
+                 (expression (|>> (:as Int) (i.= expected))
+                             (|> ($_ /.then
+                                     (/.local/1 $index (/.int +0))
+                                     (/.local/1 $output (/.int +0))
+                                     (/.repeat (/.= (/.int (.int iterations)) $index)
+                                               ($_ /.then
+                                                   (/.set (list $output) (/.+ $input $output))
+                                                   (/.set (list $index) (/.+ (/.int +1) $index))
+                                                   ))
+                                     (/.return $output))
+                                 (/.closure (list $input))
+                                 (/.apply (list (/.int input))))))
+        (_.cover [/.for_step]
+                 (expression (|>> (:as Int) (i.= expected))
+                             (|> ($_ /.then
+                                     (/.local/1 $output (/.int +0))
+                                     (/.for_step $index (/.int +0) (/.int (.int (-- iterations))) (/.int +1)
+                                                 (/.set (list $output) (/.+ $input $output)))
+                                     (/.return $output))
+                                 (/.closure (list $input))
+                                 (/.apply (list (/.int input))))))
+        (_.cover [/.for_in /.ipairs/1]
+                 (expression (|>> (:as Int) (i.= expected))
+                             (|> ($_ /.then
+                                     (/.local/1 $output (/.int +0))
+                                     (/.for_in (list $index $input) (/.ipairs/1 (/.array (list.repeated iterations $input)))
+                                               (/.set (list $output) (/.+ $input $output)))
+                                     (/.return $output))
+                                 (/.closure (list $input))
+                                 (/.apply (list (/.int input))))))
+        (_.for [/.Label]
+               ..test|label)
+        )))
+
+(def: test|exception
+  Test
+  (do [! random.monad]
+    [expected random.safe_frac
+     dummy (random.only (|>> (f.= expected) not)
+                        random.safe_frac)
+     $verdict (# ! each /.var (random.ascii/lower 10))
+     $outcome (# ! each /.var (random.ascii/lower 11))]
+    ($_ _.and
+        (_.cover [/.pcall/1]
+                 (expression (|>> (:as Frac) (f.= expected))
+                             (|> ($_ /.then
+                                     (/.let (list $verdict $outcome) (/.pcall/1 (/.closure (list)
+                                                                                           (/.return (/.float expected)))))
+                                     (/.if $verdict
+                                       (/.return $outcome)
+                                       (/.return (/.float dummy))))
+                                 (/.closure (list))
+                                 (/.apply (list)))))
+        (_.cover [/.error/1]
+                 (expression (|>> (:as Frac) (f.= expected))
+                             (|> ($_ /.then
+                                     (/.let (list $verdict $outcome) (/.pcall/1 (/.closure (list)
+                                                                                           ($_ /.then
+                                                                                               (/.statement (/.error/1 (/.float expected)))
+                                                                                               (/.return (/.float dummy))))))
+                                     (/.if $verdict
+                                       (/.return (/.float dummy))
+                                       (/.return $outcome)))
+                                 (/.closure (list))
+                                 (/.apply (list)))))
+        )))
+
+(def: test|function
+  Test
+  (do [! random.monad]
+    [float/0 random.safe_frac
+     iterations (# ! each (n.% 10) random.nat)
+     $self (# ! each /.var (random.ascii/lower 1))
+     $arg/0 (# ! each /.var (random.ascii/lower 2))
+     field (random.ascii/lower 3)
+     $class (# ! each /.var (random.ascii/upper 4))]
+    ($_ _.and
+        (_.cover [/.closure /.return]
+                 (expression (|>> (:as Frac) (f.= float/0))
+                             (/.apply (list)
+                                      (/.closure (list) (/.return (/.float float/0))))))
+        (_.cover [/.local_function]
+                 (expression (|>> (:as Int) .nat (n.= iterations))
+                             (|> ($_ /.then
+                                     (/.local_function $self (list $arg/0)
+                                                       (/.if (/.< (/.int (.int iterations)) $arg/0)
+                                                         (/.return (/.apply (list (/.+ (/.int +1) $arg/0)) $self))
+                                                         (/.return $arg/0)))
+                                     (/.return (/.apply (list (/.int +0)) $self)))
+                                 (/.closure (list))
+                                 (/.apply (list)))))
+        (do [! random.monad]
+          [float/0 random.safe_frac
+           float/1 random.safe_frac
+           float/2 random.safe_frac
+           $arg/0 (# ! each /.var (random.ascii/lower 10))
+           $arg/1 (# ! each /.var (random.ascii/lower 11))
+           $arg/2 (# ! each /.var (random.ascii/lower 12))]
+          (`` ($_ _.and
+                  (_.cover [/.apply]
+                           (expression (|>> (:as Frac) (f.= ($_ f.+ float/0 float/1 float/2)))
+                                       (/.apply (list (/.float float/0)
+                                                      (/.float float/1)
+                                                      (/.float float/2))
+                                                (/.closure (list $arg/0 $arg/1 $arg/2) (/.return ($_ /.+ $arg/0 $arg/1 $arg/2))))))
+                  )))
+        )))
+
+(def: test|branching
+  Test
+  (do [! random.monad]
+    [float/0 random.safe_frac
+     float/1 random.safe_frac
+     ??? random.bit]
+    ($_ _.and
+        (_.cover [/.if]
+                 (expression (|>> (:as Frac) (f.= (if ??? float/0 float/1)))
+                             (|> (/.if (/.boolean ???)
+                                   (/.return (/.float float/0))
+                                   (/.return (/.float float/1)))
+                                 (/.closure (list))
+                                 (/.apply (list)))))
+        (_.cover [/.when]
+                 (expression (|>> (:as Frac) (f.= (if ??? float/0 float/1)))
+                             (|> ($_ /.then
+                                     (/.when (/.boolean ???)
+                                             (/.return (/.float float/0)))
+                                     (/.return (/.float float/1)))
+                                 (/.closure (list))
+                                 (/.apply (list)))))
+        )))
+
+(def: test|binding
+  Test
+  ($_ _.and
+      ..test|function
+      (_.for [/.Location]
+             ..test/location)
+      ))
+
+(def: test|control
+  Test
+  ($_ _.and
+      ..test|branching
+      ..test|loop
+      ..test|exception
+      ))
+
+(def: test|statement
+  Test
+  (do [! random.monad]
+    [float/0 random.safe_frac
+     float/1 random.safe_frac
+     $arg/0 (# ! each /.var (random.ascii/lower 10))
+     $arg/1 (# ! each /.var (random.ascii/lower 11))]
+    (`` ($_ _.and
+            (_.cover [/.statement /.then /.print/1]
+                     (expression (|>> (:as Frac) (f.= float/0))
+                                 (|> ($_ /.then
+                                         (/.statement (/.print/1 $arg/0))
+                                         (/.return $arg/0))
+                                     (/.closure (list $arg/0))
+                                     (/.apply (list (/.float float/0))))))
+            ..test|binding
+            ..test|control
+            ))))
+
+(def: .public test
+  Test
+  (do [! random.monad]
+    [.let [random (# ! each /.int random.int)]
+     expected random.int]
+    (<| (_.covering /._)
+        (_.for [/.Code /.code])
+        (`` ($_ _.and
+                (_.for [/.equivalence]
+                       ($equivalence.spec /.equivalence random))
+                (_.for [/.hash]
+                       ($hash.spec /.hash random))
+                
+                (_.cover [/.manual]
+                         (expression (|>> (:as Int) (i.= expected))
+                                     (/.manual (/.code (/.int expected)))))
+                (_.for [/.Expression]
+                       ..test|expression)
+                (_.for [/.Statement]
+                       ..test|statement)
+                )))))
diff --git a/stdlib/source/test/lux/target/python.lux b/stdlib/source/test/lux/target/python.lux
index 49d74c1b3..e936ba850 100644
--- a/stdlib/source/test/lux/target/python.lux
+++ b/stdlib/source/test/lux/target/python.lux
@@ -29,25 +29,12 @@
 
 (def: (expression ??? it)
   (-> (-> Any Bit) (/.Expression Any) Bit)
-  ... (case (|> it /.code ..eval)
-  ...   {try.#Success it}
-  ...   (|> it
-  ...       (maybe#each ???)
-  ...       (maybe.else false))
-  
-  ...   {try.#Failure error}
-  ...   (exec
-  ...     ("lux io log" "try.#Failure")
-  ...     ("lux io log" error)
-  ...     ("lux io log" (|> it /.code))
-  ...     false))
   (|> it
       /.code
       ..eval
       (try#each (|>> (maybe#each ???)
                      (maybe.else false)))
-      (try.else false))
-  )
+      (try.else false)))
 
 (def: test|literal
   Test
@@ -64,7 +51,7 @@
                      (try#each (function (_ it)
                                  (case it
                                    {.#None} true
-                                   {.#Some _} true)))
+                                   {.#Some _} false)))
                      (try.else false)))
         (_.cover [/.bool]
                  (expression (|>> (:as Bit) (bit#= bool))
diff --git a/stdlib/source/test/lux/target/ruby.lux b/stdlib/source/test/lux/target/ruby.lux
index 80d4a161f..87e781ebc 100644
--- a/stdlib/source/test/lux/target/ruby.lux
+++ b/stdlib/source/test/lux/target/ruby.lux
@@ -50,7 +50,7 @@
                      (try#each (function (_ it)
                                  (case it
                                    {.#None} true
-                                   {.#Some _} true)))
+                                   {.#Some _} false)))
                      (try.else false)))
         (_.cover [/.bool]
                  (expression (|>> (:as Bit) (bit#= bool))
-- 
cgit v1.2.3