diff options
author | Eduardo Julian | 2021-02-07 04:56:58 -0400 |
---|---|---|
committer | Eduardo Julian | 2021-02-07 04:56:58 -0400 |
commit | d99c47989a1047cd24019fd5ce434e701b5d3519 (patch) | |
tree | 19bfb0f5e4713e5dcd0c71bbd7b88d09d75dfe5d /stdlib/source/lux/tool | |
parent | 571d816dfd0b056a1649f5057867abbfa4421f5d (diff) |
Mo' updates, less problems.
Diffstat (limited to 'stdlib/source/lux/tool')
5 files changed, 137 insertions, 80 deletions
diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux index 2f1917de9..7d7ce2fbf 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/lua/common.lux @@ -39,13 +39,13 @@ (/.install "or" (binary (product.uncurry _.bit_or))) (/.install "xor" (binary (product.uncurry _.bit_xor))) (/.install "left-shift" (binary (product.uncurry _.bit_shl))) - (/.install "logical-right-shift" (binary (product.uncurry //runtime.i64//logic_right_shift))) + (/.install "right-shift" (binary (product.uncurry //runtime.i64//right_shift))) (/.install "=" (binary (product.uncurry _.=))) (/.install "+" (binary (product.uncurry _.+))) (/.install "-" (binary (product.uncurry _.-))) (/.install "<" (binary (product.uncurry _.<))) (/.install "*" (binary (product.uncurry _.*))) - (/.install "/" (binary (product.uncurry _./))) + (/.install "/" (binary (product.uncurry _.//))) (/.install "%" (binary (product.uncurry _.%))) (/.install "f64" (unary (_./ (_.float +1.0)))) (/.install "char" (unary (!unary "string.char"))) @@ -97,8 +97,8 @@ (def: (io//log! messageO) (Unary Expression) - (_.or (_.apply/* (list messageO) (_.var "print")) - //runtime.unit)) + (|> (_.apply/* (list messageO) (_.var "print")) + (_.or //runtime.unit))) (def: io_procs Bundle diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/case.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/case.lux index e6dad82e5..3c56c2dfa 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/case.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/case.lux @@ -3,7 +3,8 @@ [abstract ["." monad (#+ do)]] [data - ["." text] + ["." text + ["%" format (#+ format)]] [collection ["." list ("#\." functor fold)] ["." set]]] @@ -20,9 +21,10 @@ ["#/." case]] ["/#" // #_ ["#." synthesis (#+ Member Synthesis Path)] + ["#." generation] ["//#" /// #_ [reference - [variable (#+ Register)]] + ["#." variable (#+ Register)]] ["#." phase ("#\." monad)] [meta [archive (#+ Archive)]]]]]]]) @@ -31,6 +33,10 @@ (-> Register Var) (|>> (///reference.local //reference.system) :assume)) +(def: #export capture + (-> Register Var) + (|>> (///reference.foreign //reference.system) :assume)) + (def: #export (let generate archive [valueS register bodyS]) (Generator [Synthesis Register Synthesis]) (do ///////phase.monad @@ -139,7 +145,7 @@ (///////phase\wrap ..pop!) (#/////synthesis.Bind register) - (///////phase\wrap (_.let (list (..register register)) ..peek)) + (///////phase\wrap (_.local/1 (..register register) ..peek)) (#/////synthesis.Bit_Fork when thenP elseP) (do {! ///////phase.monad} @@ -195,7 +201,7 @@ (do ///////phase.monad [then! (recur thenP)] (///////phase\wrap ($_ _.then - (_.let (list (..register register)) ..peek_and_pop) + (_.local/1 (..register register) ..peek_and_pop) then!))) (^template [<tag> <combinator>] @@ -216,15 +222,34 @@ pattern_matching!) (_.statement (|> (_.var "error") (_.apply/* (list (_.string ////synthesis/case.pattern_matching_error))))))))) +(def: #export dependencies + (-> Path (List Var)) + (|>> ////synthesis/case.storage + (get@ #////synthesis/case.dependencies) + set.to_list + (list\map (function (_ variable) + (.case variable + (#///////variable.Local register) + (..register register) + + (#///////variable.Foreign register) + (..capture register)))))) + (def: #export (case generate archive [valueS pathP]) (Generator [Synthesis Path]) (do ///////phase.monad [initG (generate archive valueS) - pattern_matching! (pattern_matching generate archive pathP)] - (wrap (|> ($_ _.then - (_.local (list @temp)) - (_.let (list @cursor) (_.array (list initG))) - (_.let (list @savepoint) (_.array (list))) - pattern_matching!) - (_.closure (list)) - (_.apply/* (list)))))) + [[case_module case_artifact] pattern_matching!] (/////generation.with_new_context archive + (pattern_matching generate archive pathP)) + #let [@case (_.var (///reference.artifact [case_module case_artifact])) + @dependencies+ (..dependencies (/////synthesis.path/seq (/////synthesis.path/then valueS) + pathP)) + directive (_.function @case @dependencies+ + ($_ _.then + (_.local (list @temp)) + (_.local/1 @cursor (_.array (list initG))) + (_.local/1 @savepoint (_.array (list))) + pattern_matching!))] + _ (/////generation.execute! directive) + _ (/////generation.save! (%.nat case_artifact) directive)] + (wrap (_.apply/* @dependencies+ @case)))) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/function.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/function.lux index 7c07c8c6d..c7fe7f51c 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/function.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/function.lux @@ -6,6 +6,8 @@ pipe] [data ["." product] + [text + ["%" format (#+ format)]] [collection ["." list ("#\." functor fold)]]] [target @@ -37,23 +39,24 @@ (-> Register Var) (|>> (///reference.foreign //reference.system) :assume)) -(def: (with_closure function_name inits function_definition) - (-> Text (List Expression) Statement (Operation Expression)) +(def: (with_closure function_name inits @function @args @body) + (-> Text (List Expression) Var (List Var) Statement (Operation Expression)) (case inits #.Nil (do ///////phase.monad - [_ (/////generation.execute! function_definition) + [#let [function_definition (_.function @function @args @body)] + _ (/////generation.execute! function_definition) _ (/////generation.save! function_name function_definition)] - (wrap (|> (_.var function_name) (_.apply/* inits)))) + (wrap (_.var function_name))) _ (do {! ///////phase.monad} - [@closure (\ ! map _.var (/////generation.gensym "closure")) - #let [directive (_.function @closure + [#let [@closure (_.var (format function_name "_closure")) + directive (_.function @closure (|> (list.enumeration inits) (list\map (|>> product.left ..capture))) ($_ _.then - function_definition + (_.local_function @function @args @body) (_.return (_.var function_name))))] _ (/////generation.execute! directive) _ (/////generation.save! (_.code @closure) directive)] @@ -77,35 +80,35 @@ arityO (|> arity .int _.int) @num_args (_.var "num_args") @self (_.var function_name) - initialize_self! (_.let (list (//case.register 0)) @self) + initialize_self! (_.local/1 (//case.register 0) @self) initialize! (list\fold (.function (_ post pre!) ($_ _.then pre! - (_.let (list (..input post)) (_.nth (|> post inc .int _.int) @curried)))) + (_.local/1 (..input post) (_.nth (|> post inc .int _.int) @curried)))) initialize_self! (list.indices arity)) - pack (|>> (list) _.apply/* (|> (_.var "table.pack"))) + pack (|>> (list) _.array) unpack (|>> (list) _.apply/* (|> (_.var "table.unpack"))) @var_args (_.var "...")]] (with_closure function_name closureO+ - (_.function @self (list @var_args) - ($_ _.then - (_.let (list @curried) (pack @var_args)) - (_.let (list @num_args) (_.the "n" @curried)) - (_.cond (list [(|> @num_args (_.= (_.int +0))) - (_.return @self)] - [(|> @num_args (_.= arityO)) - ($_ _.then - initialize! - (_.return bodyO))] - [(|> @num_args (_.> arityO)) - (let [arity_inputs (//runtime.array//sub (_.int +0) arityO @curried) - extra_inputs (//runtime.array//sub arityO @num_args @curried)] - (_.return (|> @self - (_.apply/* (list (unpack arity_inputs))) - (_.apply/* (list (unpack extra_inputs))))))]) - ## (|> @num_args (_.< arityO)) - (_.return (_.closure (list @var_args) - (_.return (|> @self (_.apply/* (list (unpack (//runtime.array//concat @curried (pack @var_args)))))))))) - ))) + @self (list @var_args) + ($_ _.then + (_.local/1 @curried (pack @var_args)) + (_.local/1 @num_args (_.length @curried)) + (_.cond (list [(|> @num_args (_.= (_.int +0))) + (_.return @self)] + [(|> @num_args (_.= arityO)) + ($_ _.then + initialize! + (_.return bodyO))] + [(|> @num_args (_.> arityO)) + (let [arity_inputs (//runtime.array//sub (_.int +0) arityO @curried) + extra_inputs (//runtime.array//sub arityO @num_args @curried)] + (_.return (|> @self + (_.apply/* (list (unpack arity_inputs))) + (_.apply/* (list (unpack extra_inputs))))))]) + ## (|> @num_args (_.< arityO)) + (_.return (_.closure (list @var_args) + (_.return (|> @self (_.apply/* (list (unpack (//runtime.array//concat @curried (pack @var_args)))))))))) + )) )) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/loop.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/loop.lux index 817ba118a..b1b8a47cb 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/loop.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/loop.lux @@ -7,22 +7,25 @@ [text ["%" format (#+ format)]] [collection - ["." list ("#\." functor)]]] + ["." list ("#\." functor)] + ["." set]]] [math [number ["n" nat]]] [target - ["_" lua (#+ Expression Var)]]] + ["_" lua (#+ Var Expression Statement)]]] ["." // #_ [runtime (#+ Operation Phase Phase! Generator Generator!)] ["#." case] - ["///#" //// #_ - [synthesis (#+ Scope Synthesis)] - ["#." generation] + ["/#" // #_ + ["#." reference] ["//#" /// #_ - ["#." phase] - [reference - [variable (#+ Register)]]]]]) + ["."synthesis (#+ Scope Synthesis)] + ["#." generation] + ["//#" /// #_ + ["#." phase] + [reference + [variable (#+ Register)]]]]]]) (def: loop_name (-> Nat Var) @@ -30,18 +33,49 @@ (def: #export (scope generate archive [start initsS+ bodyS]) (Generator (Scope Synthesis)) - (do {! ///////phase.monad} - [@loop (\ ! map ..loop_name /////generation.next) - initsO+ (monad.map ! (generate archive) initsS+) - bodyO (/////generation.with_anchor @loop - (generate archive bodyS)) - #let [directive (_.function @loop (|> initsS+ - list.enumeration - (list\map (|>> product.left (n.+ start) //case.register))) - (_.return bodyO))] - _ (/////generation.execute! directive) - _ (/////generation.save! (_.code @loop) directive)] - (wrap (_.apply/* initsO+ @loop)))) + (case initsS+ + ## function/false/non-independent loop + #.Nil + (generate archive bodyS) + + ## true loop + _ + (do {! ///////phase.monad} + [@loop (\ ! map ..loop_name /////generation.next) + initsO+ (monad.map ! (generate archive) initsS+) + [loop_name bodyO] (/////generation.with_new_context archive + (do ! + [@loop (\ ! map (|>> ///reference.artifact _.var) + (/////generation.context archive))] + (/////generation.with_anchor @loop + (generate archive bodyS)))) + #let [@loop (_.var (///reference.artifact loop_name)) + locals (|> initsS+ + list.enumeration + (list\map (|>> product.left (n.+ start) //case.register))) + [directive instantiation] (: [Statement Expression] + (case (|> (synthesis.path/then bodyS) + //case.dependencies + (set.from_list _.hash) + (set.difference (set.from_list _.hash locals)) + set.to_list) + #.Nil + [(_.function @loop locals + (_.return bodyO)) + @loop] + + foreigns + (let [@context (_.var (format (///reference.artifact loop_name) "_context"))] + [(_.function @context foreigns + ($_ _.then + (<| (_.local_function @loop locals) + (_.return bodyO)) + (_.return @loop) + )) + (_.apply/* foreigns @context)])))] + _ (/////generation.execute! directive) + _ (/////generation.save! (_.code @loop) directive)] + (wrap (_.apply/* initsO+ instantiation))))) (def: #export (recur generate archive argsS+) (Generator (List Synthesis)) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux index 72f8576f5..d7b0f1cd3 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux @@ -58,7 +58,8 @@ (def: prefix "LuxRuntime") -(def: #export unit (_.string /////synthesis.unit)) +(def: #export unit + (_.string /////synthesis.unit)) (def: (flag value) (-> Bit Literal) @@ -232,7 +233,7 @@ (runtime: (array//concat left right) (with_vars [temp idx] (let [copy! (function (_ input output) - (<| (_.for_step idx (_.int +1) (_.the "n" input) (_.int +1)) + (<| (_.for_step idx (_.int +1) (_.length input) (_.int +1)) (_.statement (|> (_.var "table.insert") (_.apply/* (list output (_.nth idx input)))))))] ($_ _.then (_.let (list temp) (_.array (list))) @@ -277,7 +278,7 @@ @lux//program_args )) -(runtime: (i64//logic_right_shift param subject) +(runtime: (i64//right_shift param subject) (let [mask (|> (_.int +1) (_.bit_shl (_.- param (_.int +64))) (_.- (_.int +1)))] @@ -288,7 +289,7 @@ (def: runtime//i64 Statement ($_ _.then - @i64//logic_right_shift + @i64//right_shift )) (runtime: (text//index subject param start) @@ -301,22 +302,16 @@ (_.return (..some idx)))))) (runtime: (text//clip text from to) - (with_vars [size] - ($_ _.then - (_.let (list size) (_.apply/* (list text) (_.var "string.len"))) - (_.if (_.or (_.> size from) - (_.> size to)) - (_.return ..none) - (_.return (..some (_.apply/* (list text from to) (_.var "string.sub"))))) - ))) + (_.return (_.apply/* (list text from to) (_.var "string.sub")))) (runtime: (text//char idx text) (with_vars [char] ($_ _.then (_.let (list char) (_.apply/* (list text idx) (_.var "string.byte"))) (_.if (_.= _.nil char) - (_.return ..none) - (_.return (..some char)))))) + (_.statement (_.apply/* (list (_.string "[Lux Error] Cannot get char from text.")) + (_.var "error"))) + (_.return char))))) (def: runtime//text Statement |