From de8af6044d590354563f4ea8849d98e8637c034a Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Wed, 16 Feb 2022 04:41:41 -0400 Subject: FIXED loop binding initialization in several back-ends. --- lux-js/source/program.lux | 24 ++-- stdlib/source/library/lux/target/python.lux | 28 +++- stdlib/source/library/lux/target/ruby.lux | 6 + .../language/lux/phase/analysis/function.lux | 10 +- .../language/lux/phase/extension/analysis/js.lux | 154 ++++++++++----------- .../language/lux/phase/generation/js/function.lux | 64 ++++----- .../language/lux/phase/generation/js/loop.lux | 104 ++++++++------ .../language/lux/phase/generation/js/runtime.lux | 91 ++++++------ .../language/lux/phase/generation/js/structure.lux | 2 +- .../language/lux/phase/generation/jvm/loop.lux | 17 ++- .../lux/phase/generation/python/function.lux | 2 +- .../language/lux/phase/generation/python/loop.lux | 77 +++++------ .../language/lux/phase/generation/ruby/loop.lux | 13 +- .../language/lux/phase/synthesis/function.lux | 33 ++--- stdlib/source/program/compositor.lux | 5 +- stdlib/source/unsafe/lux/data/binary.lux | 129 ++++++++--------- 16 files changed, 407 insertions(+), 352 deletions(-) diff --git a/lux-js/source/program.lux b/lux-js/source/program.lux index 824113da5..36e2fd023 100644 --- a/lux-js/source/program.lux +++ b/lux-js/source/program.lux @@ -42,7 +42,7 @@ [language [lux [program {"+" Program}] - [generation {"+" Context Host}] + [generation {"+" Host}] [analysis [macro {"+" Expander}]] [phase @@ -60,7 +60,8 @@ ["[0]" platform {"+" Platform}]] [meta ["[0]" cli] - [archive {"+" Archive}] + [archive {"+" Archive} + ["[0]" unit]] ["[0]" packager "_" ["[1]" script]]]]]]] [program @@ -472,7 +473,7 @@ (for [@.old (as_is (def: (evaluate! interpreter alias input) - (-> javax/script/ScriptEngine Context _.Expression (Try Any)) + (-> javax/script/ScriptEngine unit.ID _.Expression (Try Any)) (do try.monad [?output (javax/script/ScriptEngine::eval (_.code input) interpreter)] (case ?output @@ -489,7 +490,7 @@ (in []))) (def: (define! interpreter context custom input) - (-> javax/script/ScriptEngine Context (Maybe Text) _.Expression (Try [Text Any _.Statement])) + (-> javax/script/ScriptEngine unit.ID (Maybe Text) _.Expression (Try [Text Any _.Statement])) (let [global (maybe.else (reference.artifact context) custom) @global (_.var global)] @@ -533,7 +534,7 @@ {.#Some return})))) (def: (evaluate! alias input) - (-> Context _.Expression (Try Any)) + (-> unit.ID _.Expression (Try Any)) (do try.monad [?output (..eval (_.code input))] (case ?output @@ -550,7 +551,7 @@ (in []))) (def: (define! context custom input) - (-> Context (Maybe Text) _.Expression (Try [Text Any _.Statement])) + (-> unit.ID (Maybe Text) _.Expression (Try [Text Any _.Statement])) (let [global (maybe.else (reference.artifact context) custom) @global (_.var global)] @@ -599,7 +600,9 @@ [host ..host] (in [platform.#&file_system (for [@.old (file.async file.default) @.jvm (file.async file.default) - @.js file.default]) + ... TODO: Handle this in a safer manner. + ... This would crash if the compiler was run on a browser. + @.js (maybe.trusted file.default)]) platform.#host host platform.#phase js.generate platform.#runtime runtime.generate @@ -670,13 +673,14 @@ (program: [service cli.service] (let [extension ".js"] (exec (do async.monad - [_ (/.compiler [/static.#host @.js + [platform (async.future ..platform) + _ (/.compiler [/static.#host @.js /static.#host_module_extension extension /static.#target (cli.target service) /static.#artifact_extension extension] ..expander analysis.bundle - ..platform + (io.io platform) generation.bundle (function.constant extension/bundle.empty) ..program @@ -685,7 +689,7 @@ service [(packager.package _.use_strict _.code _.then ..scope) (format (cli.target service) - (# file.default separator) + (# (value@ platform.#&file_system platform) separator) "program" extension)])] (..declare_success! [])) diff --git a/stdlib/source/library/lux/target/python.lux b/stdlib/source/library/lux/target/python.lux index 90d8210ef..3b2b4bcf7 100644 --- a/stdlib/source/library/lux/target/python.lux +++ b/stdlib/source/library/lux/target/python.lux @@ -27,6 +27,9 @@ [type abstract]]]) +(def: input_separator + ", ") + (def: expression (-> Text Text) (text.enclosed ["(" ")"])) @@ -194,7 +197,7 @@ ... ..expression (format left_delimiter (|> entries - (list#each (|>> entry_serializer (text.suffix ", "))) + (list#each (|>> entry_serializer (text.suffix ..input_separator))) text.together) right_delimiter)))) @@ -227,7 +230,10 @@ (-> (Expression Any) (List (Expression Any)) (Computation Any)) (<| :abstraction ... ..expression - (format (:representation func) "(" (text.interposed ", " (list#each ..code args)) ")"))) + (format (:representation func) + "(" + (|> args (list#each ..code) (text.interposed ..input_separator)) + ")"))) (template [ ] [(def: .public @@ -303,16 +309,22 @@ (-> (List (Var Any)) (Expression Any) (Computation Any)) (<| :abstraction ..expression - (format "lambda " (|> arguments (list#each ..code) (text.interposed ", ")) ": " - (:representation body)))) + (format "lambda " (|> arguments (list#each ..code) (text.interposed ..input_separator)) + ": " (:representation body)))) (def: .public (set vars value) (-> (List (Location Any)) (Expression Any) (Statement Any)) (:abstraction - (format (|> vars (list#each ..code) (text.interposed ", ")) + (format (|> vars (list#each ..code) (text.interposed ..input_separator)) " = " (:representation value)))) + (def: .public multi + (-> (List (Expression Any)) (Expression Any)) + (|>> (list#each ..code) + (text.interposed ..input_separator) + :abstraction)) + (def: .public (delete where) (-> (Location Any) (Statement Any)) (:abstraction (format "del " (:representation where)))) @@ -387,7 +399,7 @@ (..nested (:representation body!)) (|> excepts (list#each (function (_ [classes exception catch!]) - (format \n+ "except (" (text.interposed ", " (list#each ..code classes)) + (format \n+ "except (" (|> classes (list#each ..code) (text.interposed ..input_separator)) ") as " (:representation exception) ":" (..nested (:representation catch!))))) text.together)))) @@ -418,7 +430,9 @@ (-> SVar (List (Ex (_ k) (Var k))) (Statement Any) (Statement Any)) (:abstraction (format "def " (:representation name) - "(" (|> args (list#each ..code) (text.interposed ", ")) "):" + "(" + (|> args (list#each ..code) (text.interposed ..input_separator)) + "):" (..nested (:representation body))))) (def: .public (import module_name) diff --git a/stdlib/source/library/lux/target/ruby.lux b/stdlib/source/library/lux/target/ruby.lux index 05b1bf768..b81be8aab 100644 --- a/stdlib/source/library/lux/target/ruby.lux +++ b/stdlib/source/library/lux/target/ruby.lux @@ -141,6 +141,12 @@ ["ARGV" command_line_arguments] ) + (def: .public multi + (-> (List Expression) Expression) + (|>> (list#each ..code) + (text.interposed ..input_separator) + :abstraction)) + (def: .public nil Literal (:abstraction "nil")) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/function.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/function.lux index b91550f39..1365d0e1e 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/function.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/analysis/function.lux @@ -9,6 +9,7 @@ ["[0]" try] ["[0]" exception {"+" exception:}]] [data + ["[0]" product] ["[0]" text ["%" format {"+" format}]] [collection @@ -26,7 +27,7 @@ ["[1][0]" inference] ["[1][0]" scope]] [/// - ["[1]" phase] + ["[1]" phase ("[1]#[0]" functor)] [reference {"+"} [variable {"+"}]]]]]) @@ -128,7 +129,6 @@ (def: .public (apply analyse argsC+ :function: functionA archive functionC) (-> Phase (List Code) Type Analysis Phase) - (<| (/.with_exception ..cannot_apply [:function: functionC argsC+]) - (do ///.monad - [[applyT argsA+] (/inference.general archive analyse :function: argsC+)]) - (in (/.reified [functionA argsA+])))) + (|> (/inference.general archive analyse :function: argsC+) + (///#each (|>> product.right [functionA] /.reified)) + (/.with_exception ..cannot_apply [:function: functionC argsC+]))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/js.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/js.lux index 95a530507..72a47712f 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/js.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/extension/analysis/js.lux @@ -1,32 +1,30 @@ (.using - [library - [lux "*" - ["[0]" ffi] - [abstract - ["[0]" monad {"+" do}]] - [control - ["<>" parser - ["" code {"+" Parser}]]] - [data - [collection - ["[0]" array {"+" Array}] - ["[0]" dictionary] - ["[0]" list]]] - ["[0]" type - ["[0]" check]] - ["@" target - ["_" js]]]] + [library + [lux "*" + ["[0]" ffi] + [abstract + ["[0]" monad {"+" do}]] + [control + ["<>" parser + ["" code {"+" Parser}]]] + [data + [collection + ["[0]" array {"+" Array}] + ["[0]" dictionary] + ["[0]" list]]] + ["[0]" type + ["[0]" check]] + ["@" target + ["_" js]]]] + [// + ["/" lux {"+" custom}] [// - ["/" lux {"+" custom}] - [// - ["[0]" bundle] - [// - ["[0]" analysis "_" - ["[1]/[0]" type]] - [// - ["[0]" analysis {"+" Analysis Operation Phase Handler Bundle}] - [/// - ["[0]" phase]]]]]]) + ["[0]" bundle] + [/// + ["[0]" analysis {"+" Analysis Operation Phase Handler Bundle} + ["[1]/[0]" type]] + [/// + ["[0]" phase]]]]]) (def: array::new Handler @@ -34,10 +32,10 @@ [.any (function (_ extension phase archive lengthC) (do phase.monad - [lengthA (analysis/type.with_type Nat - (phase archive lengthC)) - [var_id varT] (analysis/type.with_env check.var) - _ (analysis/type.infer (type (Array varT)))] + [lengthA (analysis/type.expecting Nat + (phase archive lengthC)) + [var_id varT] (analysis/type.check check.var) + _ (analysis/type.inference (type (Array varT)))] (in {analysis.#Extension extension (list lengthA)})))])) (def: array::length @@ -46,10 +44,10 @@ [.any (function (_ extension phase archive arrayC) (do phase.monad - [[var_id varT] (analysis/type.with_env check.var) - arrayA (analysis/type.with_type (type (Array varT)) - (phase archive arrayC)) - _ (analysis/type.infer Nat)] + [[var_id varT] (analysis/type.check check.var) + arrayA (analysis/type.expecting (type (Array varT)) + (phase archive arrayC)) + _ (analysis/type.inference Nat)] (in {analysis.#Extension extension (list arrayA)})))])) (def: array::read @@ -58,12 +56,12 @@ [(<>.and .any .any) (function (_ extension phase archive [indexC arrayC]) (do phase.monad - [indexA (analysis/type.with_type Nat - (phase archive indexC)) - [var_id varT] (analysis/type.with_env check.var) - arrayA (analysis/type.with_type (type (Array varT)) - (phase archive arrayC)) - _ (analysis/type.infer varT)] + [indexA (analysis/type.expecting Nat + (phase archive indexC)) + [var_id varT] (analysis/type.check check.var) + arrayA (analysis/type.expecting (type (Array varT)) + (phase archive arrayC)) + _ (analysis/type.inference varT)] (in {analysis.#Extension extension (list indexA arrayA)})))])) (def: array::write @@ -72,14 +70,14 @@ [($_ <>.and .any .any .any) (function (_ extension phase archive [indexC valueC arrayC]) (do phase.monad - [indexA (analysis/type.with_type Nat - (phase archive indexC)) - [var_id varT] (analysis/type.with_env check.var) - valueA (analysis/type.with_type varT - (phase archive valueC)) - arrayA (analysis/type.with_type (type (Array varT)) - (phase archive arrayC)) - _ (analysis/type.infer (type (Array varT)))] + [indexA (analysis/type.expecting Nat + (phase archive indexC)) + [var_id varT] (analysis/type.check check.var) + valueA (analysis/type.expecting varT + (phase archive valueC)) + arrayA (analysis/type.expecting (type (Array varT)) + (phase archive arrayC)) + _ (analysis/type.inference (type (Array varT)))] (in {analysis.#Extension extension (list indexA valueA arrayA)})))])) (def: array::delete @@ -88,12 +86,12 @@ [($_ <>.and .any .any) (function (_ extension phase archive [indexC arrayC]) (do phase.monad - [indexA (analysis/type.with_type Nat - (phase archive indexC)) - [var_id varT] (analysis/type.with_env check.var) - arrayA (analysis/type.with_type (type (Array varT)) - (phase archive arrayC)) - _ (analysis/type.infer (type (Array varT)))] + [indexA (analysis/type.expecting Nat + (phase archive indexC)) + [var_id varT] (analysis/type.check check.var) + arrayA (analysis/type.expecting (type (Array varT)) + (phase archive arrayC)) + _ (analysis/type.inference (type (Array varT)))] (in {analysis.#Extension extension (list indexA arrayA)})))])) (def: bundle::array @@ -113,10 +111,10 @@ [($_ <>.and .any (.tuple (<>.some .any))) (function (_ extension phase archive [constructorC inputsC]) (do [! phase.monad] - [constructorA (analysis/type.with_type Any - (phase archive constructorC)) - inputsA (monad.each ! (|>> (phase archive) (analysis/type.with_type Any)) inputsC) - _ (analysis/type.infer .Any)] + [constructorA (analysis/type.expecting Any + (phase archive constructorC)) + inputsA (monad.each ! (|>> (phase archive) (analysis/type.expecting Any)) inputsC) + _ (analysis/type.inference .Any)] (in {analysis.#Extension extension (list& constructorA inputsA)})))])) (def: object::get @@ -125,9 +123,9 @@ [($_ <>.and .text .any) (function (_ extension phase archive [fieldC objectC]) (do phase.monad - [objectA (analysis/type.with_type Any - (phase archive objectC)) - _ (analysis/type.infer .Any)] + [objectA (analysis/type.expecting Any + (phase archive objectC)) + _ (analysis/type.inference .Any)] (in {analysis.#Extension extension (list (analysis.text fieldC) objectA)})))])) @@ -137,10 +135,10 @@ [($_ <>.and .text .any (.tuple (<>.some .any))) (function (_ extension phase archive [methodC objectC inputsC]) (do [! phase.monad] - [objectA (analysis/type.with_type Any - (phase archive objectC)) - inputsA (monad.each ! (|>> (phase archive) (analysis/type.with_type Any)) inputsC) - _ (analysis/type.infer .Any)] + [objectA (analysis/type.expecting Any + (phase archive objectC)) + inputsA (monad.each ! (|>> (phase archive) (analysis/type.expecting Any)) inputsC) + _ (analysis/type.inference .Any)] (in {analysis.#Extension extension (list& (analysis.text methodC) objectA inputsA)})))])) @@ -164,7 +162,7 @@ [.text (function (_ extension phase archive name) (do phase.monad - [_ (analysis/type.infer Any)] + [_ (analysis/type.inference Any)] (in {analysis.#Extension extension (list (analysis.text name))})))])) (def: js::apply @@ -173,10 +171,10 @@ [($_ <>.and .any (<>.some .any)) (function (_ extension phase archive [abstractionC inputsC]) (do [! phase.monad] - [abstractionA (analysis/type.with_type Any - (phase archive abstractionC)) - inputsA (monad.each ! (|>> (phase archive) (analysis/type.with_type Any)) inputsC) - _ (analysis/type.infer Any)] + [abstractionA (analysis/type.expecting Any + (phase archive abstractionC)) + inputsA (monad.each ! (|>> (phase archive) (analysis/type.expecting Any)) inputsC) + _ (analysis/type.inference Any)] (in {analysis.#Extension extension (list& abstractionA inputsA)})))])) (def: js::type_of @@ -185,9 +183,9 @@ [.any (function (_ extension phase archive objectC) (do phase.monad - [objectA (analysis/type.with_type Any - (phase archive objectC)) - _ (analysis/type.infer .Text)] + [objectA (analysis/type.expecting Any + (phase archive objectC)) + _ (analysis/type.inference .Text)] (in {analysis.#Extension extension (list objectA)})))])) (def: js::function @@ -197,10 +195,10 @@ (function (_ extension phase archive [arity abstractionC]) (do phase.monad [.let [inputT (type.tuple (list.repeated arity Any))] - abstractionA (analysis/type.with_type (-> inputT Any) - (phase archive abstractionC)) - _ (analysis/type.infer (for [@.js ffi.Function] - Any))] + abstractionA (analysis/type.expecting (-> inputT Any) + (phase archive abstractionC)) + _ (analysis/type.inference (for [@.js ffi.Function] + Any))] (in {analysis.#Extension extension (list (analysis.nat arity) abstractionA)})))])) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/function.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/function.lux index 65e38cba2..5461530f7 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/function.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/function.lux @@ -1,37 +1,39 @@ (.using - [library - [lux {"-" function} - [abstract - ["[0]" monad {"+" do}]] - [data - ["[0]" product] - [text - ["%" format {"+" format}]] - [collection - ["[0]" list ("[1]#[0]" functor mix)]]] - [target - ["_" js {"+" Expression Computation Var Statement}]]]] - ["[0]" // "_" - ["[1][0]" runtime {"+" Operation Phase Phase! Generator}] + [library + [lux {"-" function} + [abstract + ["[0]" monad {"+" do}]] + [data + ["[0]" product] + [text + ["%" format {"+" format}]] + [collection + ["[0]" list ("[1]#[0]" functor mix)]]] + [target + ["_" js {"+" Expression Computation Var 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 {"+" Abstraction Reification Analysis}] + [synthesis {"+" Synthesis}] + ["[1][0]" generation] ["//[1]" /// "_" - [analysis {"+" Abstraction Application Analysis}] - [synthesis {"+" Synthesis}] - ["[1][0]" generation {"+" Context}] - ["//[1]" /// "_" - [arity {"+" Arity}] - ["[1][0]" phase ("[1]#[0]" monad)] - [reference - [variable {"+" Register Variable}]] - [meta - [archive - ["[0]" dependency]]]]]]]) + [arity {"+" Arity}] + ["[1][0]" phase ("[1]#[0]" monad)] + [reference + [variable {"+" Register Variable}]] + [meta + [archive + ["[0]" unit]] + ["[0]" cache "_" + ["[1]" artifact]]]]]]]) (def: .public (apply expression archive [functionS argsS+]) - (Generator (Application Synthesis)) + (Generator (Reification Synthesis)) (do [! ///////phase.monad] [functionO (expression archive functionS) argsO+ (monad.each ! (expression archive) argsS+)] @@ -65,13 +67,13 @@ (_.var "arguments")) (def: (@scope function_name) - (-> Context Text) + (-> unit.ID Text) (format (///reference.artifact function_name) "_scope")) (def: .public (function statement expression archive [environment arity bodyS]) (-> Phase! (Generator (Abstraction Synthesis))) (do [! ///////phase.monad] - [dependencies (dependency.dependencies archive bodyS) + [dependencies (cache.dependencies archive bodyS) [function_name body!] (/////generation.with_new_context archive dependencies (do ! [scope (# ! each ..@scope diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/loop.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/loop.lux index 9f03e9bb1..e97ee4c43 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/loop.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/loop.lux @@ -1,45 +1,64 @@ (.using - [library - [lux {"-" Scope} - [abstract - ["[0]" monad {"+" do}]] - [data - ["[0]" product] - ["[0]" text - ["%" format {"+" format}]] - [collection - ["[0]" list ("[1]#[0]" functor mix)]]] - [math - [number - ["n" nat]]] - [target - ["_" js {"+" Computation Var Expression Statement}]]]] - ["[0]" // "_" - [runtime {"+" Operation Phase Phase! Generator Generator!}] - ["[1][0]" case] - ["///[1]" //// "_" - [synthesis {"+" Scope Synthesis}] - ["[1][0]" generation] - ["//[1]" /// "_" - ["[1][0]" phase] - [reference - [variable {"+" Register}]]]]]) + [library + [lux {"-" Scope} + [abstract + ["[0]" monad {"+" do}]] + [data + ["[0]" product] + ["[0]" text + ["%" format {"+" format}]] + [collection + ["[0]" list ("[1]#[0]" functor mix)]]] + [math + [number + ["n" nat]]] + [target + ["_" js {"+" Computation Var Expression Statement}]]]] + ["[0]" // "_" + [runtime {"+" Operation Phase Phase! Generator Generator!}] + ["[1][0]" case] + ["///[1]" //// "_" + [synthesis {"+" Scope Synthesis}] + ["[1][0]" generation] + ["//[1]" /// "_" + ["[1][0]" phase] + [reference + [variable {"+" Register}]]]]]) (def: @scope (-> Nat Text) (|>> %.nat (format "scope"))) -(def: (setup initial? offset bindings body) - (-> Bit Register (List Expression) Statement Statement) - (|> bindings - list.enumeration - (list#each (function (_ [register value]) - (let [variable (//case.register (n.+ offset register))] - (if initial? - (_.define variable value) - (_.set variable value))))) - list.reversed - (list#mix _.then body))) +(def: $iteration + (-> Nat Var) + (|>> %.nat (format "iteration") _.var)) + +(def: (setup $iteration initial? offset bindings body) + (-> Var Bit Register (List Expression) Statement Statement) + (case bindings + (^ (list)) + body + + (^ (list binding)) + (let [$binding (//case.register offset)] + ($_ _.then + (if initial? + (_.define $binding binding) + (_.set $binding binding)) + body + )) + + _ + (|> bindings + list.enumeration + (list#each (function (_ [register _]) + (let [variable (//case.register (n.+ offset register))] + (if initial? + (_.define variable (_.at (_.i32 (.int register)) $iteration)) + (_.set variable (_.at (_.i32 (.int register)) $iteration)))))) + list.reversed + (list#mix _.then body) + (_.then (_.define $iteration (_.array bindings)))))) (def: .public (scope! statement expression archive [start initsS+ bodyS]) (Generator! (Scope Synthesis)) @@ -54,8 +73,11 @@ [@scope (# ! each ..@scope /////generation.next) initsO+ (monad.each ! (expression archive) initsS+) body! (/////generation.with_anchor [start @scope] - (statement expression archive bodyS))] - (in (..setup true start initsO+ + (statement expression archive bodyS)) + $iteration (# ! each ..$iteration /////generation.next)] + (in (..setup $iteration + true start + initsO+ (_.with_label (_.label @scope) (_.do_while (_.boolean true) body!))))))) @@ -80,10 +102,12 @@ (Generator! (List Synthesis)) (do [! ///////phase.monad] [[offset @scope] /////generation.anchor - argsO+ (monad.each ! (expression archive) argsS+)] + argsO+ (monad.each ! (expression archive) argsS+) + $iteration (# ! each ..$iteration /////generation.next)] (in ($_ _.then (_.define @temp (_.array argsO+)) - (..setup false offset + (..setup $iteration + false offset (|> argsO+ list.enumeration (list#each (function (_ [idx _]) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux index 7be5cfb48..26f54c884 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux @@ -1,47 +1,48 @@ (.using - [library - [lux {"-" i64} - ["[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 - ["_" js {"+" Expression Var Computation Statement}]] - [tool - [compiler - [language - [lux - ["$" version]]]]]]] - ["[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 {"-" i64} + ["[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 + ["_" js {"+" Expression Var Computation Statement}]] + [tool + [compiler + [language + [lux + ["$" version]]]]]]] + ["[0]" /// "_" + ["[1][0]" reference] + ["//[1]" /// "_" + ["[1][0]" synthesis {"+" Synthesis}] + ["[1][0]" generation] + ["//[1]" /// + ["[1][0]" phase] + [reference + [variable {"+" Register}]] + [meta + [archive {"+" Output Archive} + ["[0]" registry {"+" Registry}] + ["[0]" unit]]]]]]) (template [ ] [(type: .public @@ -774,8 +775,8 @@ (do ///////phase.monad [_ (/////generation.execute! ..runtime) _ (/////generation.save! ..module_id {.#None} ..runtime)] - (in [(|> artifact.empty - (artifact.resource true artifact.no_dependencies) + (in [(|> registry.empty + (registry.resource true unit.none) product.right) (sequence.sequence [..module_id {.#None} diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/structure.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/structure.lux index b1e96b617..96ed21e8a 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/structure.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/js/structure.lux @@ -11,7 +11,7 @@ ["///[1]" //// "_" ["[1][0]" synthesis {"+" Synthesis}] [analysis - [composite {"+" Variant Tuple}]] + [complex {"+" Variant Tuple}]] ["//[1]" /// ["[1][0]" phase ("[1]#[0]" monad)]]]]) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/loop.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/loop.lux index 60f6c3b2a..23914096a 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/loop.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/jvm/loop.lux @@ -79,13 +79,16 @@ initsI+ (monad.each ! (translate archive) initsS+) iterationG (generation.with_anchor [@begin offset] (translate archive iterationS)) - .let [initializationG (|> (list.enumeration initsI+) - (list#each (function (_ [index initG]) - ($_ _.composite - initG - (_.astore (n.+ offset index))))) - (monad.all _.monad))]] + .let [initializationG (list#each (function (_ [index initG]) + [initG (_.astore (n.+ offset index))]) + (list.enumeration initsI+))]] (in ($_ _.composite - initializationG + (|> initializationG + (list#each product.left) + (monad.all _.monad)) + (|> initializationG + list.reversed + (list#each product.right) + (monad.all _.monad)) (_.set_label @begin) iterationG)))) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/python/function.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/python/function.lux index fd9a3a0cc..3578fbeaa 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/python/function.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/python/function.lux @@ -81,7 +81,7 @@ @num_args (_.var "num_args") @self (_.var (///reference.artifact [function_module function_artifact])) apply_poly (.function (_ args func) - (_.apply_poly (list) args func)) + (_.apply/* func (list (_.splat_poly args)))) initialize_self! (_.set (list (//case.register 0)) @self) initialize! (list#mix (.function (_ post pre!) ($_ _.then diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/python/loop.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/python/loop.lux index 57040b638..d1a33d54d 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/python/loop.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/python/loop.lux @@ -1,48 +1,47 @@ (.using - [library - [lux {"-" Scope} - [abstract - ["[0]" monad {"+" do}]] - [data - ["[0]" product] - [text - ["%" format {"+" format}]] - [collection - ["[0]" list ("[1]#[0]" functor mix)] - ["[0]" set]]] - [math - [number - ["n" nat]]] - [target - ["_" python {"+" Expression SVar Statement}]]]] - ["[0]" // "_" - [runtime {"+" Operation Phase Generator Phase! Generator!}] - ["[1][0]" case] + [library + [lux {"-" Scope} + [abstract + ["[0]" monad {"+" do}]] + [data + ["[0]" product] + [text + ["%" format {"+" format}]] + [collection + ["[0]" list ("[1]#[0]" functor mix)] + ["[0]" set]]] + [math + [number + ["n" nat]]] + [target + ["_" python {"+" Expression SVar Statement}]]]] + ["[0]" // "_" + [runtime {"+" Operation Phase Generator Phase! Generator!}] + ["[1][0]" case] + ["/[1]" // "_" + ["[1][0]" reference] ["/[1]" // "_" - ["[1][0]" reference] + [synthesis + ["[0]" case]] ["/[1]" // "_" - [synthesis - ["[0]" case]] - ["/[1]" // "_" - ["[0]" synthesis {"+" Scope Synthesis}] - ["[1][0]" generation] - ["//[1]" /// "_" - ["[1][0]" phase] - [meta - ["[0]" cache "_" - ["[1]" artifact]]] - [reference - ["[1][0]" variable {"+" Register}]]]]]]]) + ["[0]" synthesis {"+" Scope Synthesis}] + ["[1][0]" generation] + ["//[1]" /// "_" + ["[1][0]" phase] + [meta + ["[0]" cache "_" + ["[1]" artifact]]] + [reference + ["[1][0]" variable {"+" Register}]]]]]]]) (def: (setup offset bindings body) (-> Register (List (Expression Any)) (Statement Any) (Statement Any)) - (|> bindings - list.enumeration - (list#each (function (_ [register value]) - (_.set (list (//case.register (n.+ offset register))) - value))) - list.reversed - (list#mix _.then body))) + (let [variables (|> bindings + list.enumeration + (list#each (|>> product.left (n.+ offset) //case.register)))] + ($_ _.then + (_.set variables (_.multi bindings)) + body))) (def: .public (set_scope body!) (-> (Statement Any) (Statement Any)) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/loop.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/loop.lux index 9ccd0151e..b69ce6b57 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/loop.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/generation/ruby/loop.lux @@ -33,13 +33,12 @@ (def: (setup offset bindings body) (-> Register (List Expression) Statement Statement) - (|> bindings - list.enumeration - (list#each (function (_ [register value]) - (_.set (list (//case.register (n.+ offset register))) - value))) - list.reversed - (list#mix _.then body))) + (let [variables (|> bindings + list.enumeration + (list#each (|>> product.left (n.+ offset) //case.register)))] + ($_ _.then + (_.set variables (_.multi bindings)) + body))) (def: symbol (_.symbol "lux_continue")) diff --git a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/function.lux b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/function.lux index d6fe2a3ea..4d6ec6354 100644 --- a/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/function.lux +++ b/stdlib/source/library/lux/tool/compiler/language/lux/phase/synthesis/function.lux @@ -9,7 +9,7 @@ ["[0]" maybe ("[1]#[0]" functor)] ["[0]" exception {"+" exception:}]] [data - ["[0]" text + [text ["%" format {"+" format}]] [collection ["[0]" list ("[1]#[0]" functor monoid)]]] @@ -24,9 +24,9 @@ ["/" synthesis {"+" Path Abstraction Synthesis Operation Phase}] [/// [arity {"+" Arity}] + ["[0]" phase ("[1]#[0]" monad)] ["[1][0]" reference - ["[1]/[0]" variable {"+" Register Variable}]] - ["[0]" phase ("[1]#[0]" monad)]]]]) + ["[1]/[0]" variable {"+" Register Variable}]]]]]) (exception: .public (cannot_find_foreign_variable_in_environment [foreign Register environment (Environment Synthesis)]) @@ -254,8 +254,7 @@ (def: .public (abstraction phase environment archive bodyA) (-> Phase (Environment Analysis) Phase) (do [! phase.monad] - [currying? /.currying? - environment (monad.each ! (phase archive) environment) + [environment (monad.each ! (phase archive) environment) bodyS (/.with_currying? true (/.with_locals 2 (phase archive bodyA))) @@ -272,14 +271,16 @@ _ (in [/.#environment environment /.#arity 1 - /.#body bodyS])))] - (in (if currying? - (/.function/abstraction abstraction) - (case (//loop.optimization false 1 (list) abstraction) - {.#Some [startL initsL bodyL]} - (/.function/abstraction [/.#environment environment - /.#arity (value@ /.#arity abstraction) - /.#body (/.loop/scope [startL initsL bodyL])]) - - {.#None} - (/.function/abstraction abstraction)))))) + /.#body bodyS]))) + currying? /.currying?] + (in (/.function/abstraction + (if currying? + abstraction + (case (//loop.optimization false 1 (list) abstraction) + {.#Some [startL initsL bodyL]} + [/.#environment environment + /.#arity (value@ /.#arity abstraction) + /.#body (/.loop/scope [startL initsL bodyL])] + + {.#None} + abstraction)))))) diff --git a/stdlib/source/program/compositor.lux b/stdlib/source/program/compositor.lux index 4e0599859..b398f85e3 100644 --- a/stdlib/source/program/compositor.lux +++ b/stdlib/source/program/compositor.lux @@ -8,6 +8,7 @@ ["[0]" monad {"+" do}]] [control ["[0]" io {"+" IO io}] + ["[0]" maybe] ["[0]" try {"+" Try}] [concurrency ["[0]" async {"+" Async} ("[1]#[0]" monad)]]] @@ -174,7 +175,9 @@ host_dependencies (..load_host_dependencies (value@ platform.#&file_system platform) compilation_host_dependencies) _ (..package! (for [@.old (file.async file.default) @.jvm (file.async file.default) - @.js file.default]) + ... TODO: Handle this in a safer manner. + ... This would crash if the compiler was run on a browser. + @.js (maybe.trusted file.default)]) host_dependencies packager,package static diff --git a/stdlib/source/unsafe/lux/data/binary.lux b/stdlib/source/unsafe/lux/data/binary.lux index d91893c38..ffc2b5e84 100644 --- a/stdlib/source/unsafe/lux/data/binary.lux +++ b/stdlib/source/unsafe/lux/data/binary.lux @@ -30,14 +30,8 @@ @.jvm (as_is ) @.js - (as_is (ffi.import: ArrayBuffer - ["[1]::[0]" - (new [ffi.Number])]) - - (ffi.import: Uint8Array - ["[1]::[0]" - (new [ArrayBuffer]) - (length ffi.Number)]) + (as_is (ffi.import: ArrayBuffer) + (ffi.import: Uint8Array) (type: .public Binary Uint8Array)) @@ -71,8 +65,11 @@ (|> .int "lux i64 f64" - ArrayBuffer::new - Uint8Array::new) + [] + ("js object new" ("js constant" "ArrayBuffer")) + [] + ("js object new" ("js constant" "Uint8Array")) + (:as ..Binary)) @.python (|> @@ -94,8 +91,8 @@ @.js (|> - Uint8Array::length - (: Frac) + ("js object get" "length") + (:as Frac) "lux f64 i64" .nat) @@ -123,64 +120,68 @@ (:as I64) ("lux i64 and" ))] (template: .public (bytes/1 index it) - [(: I64 - (`` (for [@.old (~~ ) - @.jvm (~~ ) - - @.js - (|> - (:as (array.Array .Frac)) - ("js array read" ) - "lux f64 i64" - .i64) - - @.python - (|> - (:as (array.Array .I64)) - ("python array read" )) - - @.scheme - (..bytevector-u8-ref [ ])] - - ... Default - (.case (array.read! ) - {.#Some it} - it - - {.#None} - (.i64 (: (I64 Any) 0))))))])) + [(<| (:as .I64) + (: (.I64 .Any)) + (`` (for [@.old (~~ ) + @.jvm (~~ ) + + @.js + (|> + (:as (array.Array .Frac)) + ("js array read" ) + "lux f64 i64" + .i64) + + @.python + (|> + (:as (array.Array .I64)) + ("python array read" )) + + @.scheme + (..bytevector-u8-ref [ ])] + + ... Default + (.case (array.read! ) + {.#Some it} + it + + {.#None} + (.i64 (: (I64 Any) 0))))))])) (template: .public (bytes/2 index' it') - [(let [index (: Nat index') - it (: ..Binary it')] - (: I64 - ($_ "lux i64 or" - ("lux i64 left-shift" 8 (..bytes/1 index it)) - (..bytes/1 ("lux i64 +" 1 index) it))))]) + [(<| (let [index (: Nat index') + it (: ..Binary it')]) + (:as .I64) + (: (.I64 .Any)) + ($_ "lux i64 or" + ("lux i64 left-shift" 8 (..bytes/1 index it)) + (..bytes/1 ("lux i64 +" 1 index) it)))]) (template: .public (bytes/4 index' it') - [(let [index (: Nat index') - it (: ..Binary it')] - (: I64 - ($_ "lux i64 or" - ("lux i64 left-shift" 24 (..bytes/1 index it)) - ("lux i64 left-shift" 16 (..bytes/1 ("lux i64 +" 1 index) it)) - ("lux i64 left-shift" 8 (..bytes/1 ("lux i64 +" 2 index) it)) - (..bytes/1 ("lux i64 +" 3 index) it))))]) + [(<| (let [index (: Nat index') + it (: ..Binary it')]) + (:as .I64) + (: (.I64 .Any)) + ($_ "lux i64 or" + ("lux i64 left-shift" 24 (..bytes/1 index it)) + ("lux i64 left-shift" 16 (..bytes/1 ("lux i64 +" 1 index) it)) + ("lux i64 left-shift" 8 (..bytes/1 ("lux i64 +" 2 index) it)) + (..bytes/1 ("lux i64 +" 3 index) it)))]) (template: .public (bytes/8 index' it') - [(let [index (: Nat index') - it (: ..Binary it')] - (: I64 - ($_ "lux i64 or" - ("lux i64 left-shift" 56 (..bytes/1 index it)) - ("lux i64 left-shift" 48 (..bytes/1 ("lux i64 +" 1 index) it)) - ("lux i64 left-shift" 40 (..bytes/1 ("lux i64 +" 2 index) it)) - ("lux i64 left-shift" 32 (..bytes/1 ("lux i64 +" 3 index) it)) - ("lux i64 left-shift" 24 (..bytes/1 ("lux i64 +" 4 index) it)) - ("lux i64 left-shift" 16 (..bytes/1 ("lux i64 +" 5 index) it)) - ("lux i64 left-shift" 8 (..bytes/1 ("lux i64 +" 6 index) it)) - (..bytes/1 ("lux i64 +" 7 index) it))))]) + [(<| (let [index (: Nat index') + it (: ..Binary it')]) + (:as .I64) + (: (.I64 .Any)) + ($_ "lux i64 or" + ("lux i64 left-shift" 56 (..bytes/1 index it)) + ("lux i64 left-shift" 48 (..bytes/1 ("lux i64 +" 1 index) it)) + ("lux i64 left-shift" 40 (..bytes/1 ("lux i64 +" 2 index) it)) + ("lux i64 left-shift" 32 (..bytes/1 ("lux i64 +" 3 index) it)) + ("lux i64 left-shift" 24 (..bytes/1 ("lux i64 +" 4 index) it)) + ("lux i64 left-shift" 16 (..bytes/1 ("lux i64 +" 5 index) it)) + ("lux i64 left-shift" 8 (..bytes/1 ("lux i64 +" 6 index) it)) + (..bytes/1 ("lux i64 +" 7 index) it)))]) (with_expansions [ (hex "FF") (: ..Binary it) -- cgit v1.2.3