diff options
author | Eduardo Julian | 2021-02-04 01:30:34 -0400 |
---|---|---|
committer | Eduardo Julian | 2021-02-04 01:30:34 -0400 |
commit | 571d816dfd0b056a1649f5057867abbfa4421f5d (patch) | |
tree | cccd86f9285bf4956d6b50aea669ad4e9e15ee13 /stdlib/source/lux/tool | |
parent | 3d457763e34d4dd1992427b3918b351ac684adb7 (diff) |
Updates for Lua compiler.
Diffstat (limited to 'stdlib/source/lux/tool')
11 files changed, 412 insertions, 342 deletions
diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/extension/analysis/lua.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/extension/analysis/lua.lux new file mode 100644 index 000000000..b431dc39b --- /dev/null +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/extension/analysis/lua.lux @@ -0,0 +1,34 @@ +(.module: + [lux #* + ["." host] + [abstract + ["." monad (#+ do)]] + [control + ["<>" parser + ["<c>" code (#+ Parser)]]] + [data + [collection + ["." array (#+ Array)] + ["." dictionary] + ["." list]]] + ["." type + ["." check]] + ["@" target + ["_" lua]]] + [// + ["/" lux (#+ custom)] + [// + ["." bundle] + [// + ["." analysis #_ + ["#/." type]] + [// + ["." analysis (#+ Analysis Operation Phase Handler Bundle)] + [/// + ["." phase]]]]]]) + +(def: #export bundle + Bundle + (<| (bundle.prefix "lua") + (|> bundle.empty + ))) 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 b9db6e702..2f1917de9 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 @@ -6,10 +6,11 @@ ["." function]] [data ["." product] - [number - ["f" frac]] [collection ["." dictionary]]] + [math + [number + ["f" frac]]] [target ["_" lua (#+ Expression Literal)]]] [//// @@ -24,45 +25,39 @@ (template: (!unary function) (|>> list _.apply/* (|> (_.var function)))) -(def: lux-procs +(def: lux_procs Bundle (|> /.empty (/.install "is" (binary (product.uncurry _.=))) (/.install "try" (unary //runtime.lux//try)))) -(def: i64-procs +(def: i64_procs Bundle (<| (/.prefix "i64") (|> /.empty - (/.install "and" (binary (product.uncurry _.bit-and))) - (/.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 "arithmetic-right-shift" (binary (product.uncurry _.bit-shr))) + (/.install "and" (binary (product.uncurry _.bit_and))) + (/.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 "=" (binary (product.uncurry _.=))) (/.install "+" (binary (product.uncurry _.+))) (/.install "-" (binary (product.uncurry _.-))) - ))) - -(def: int-procs - Bundle - (<| (/.prefix "int") - (|> /.empty (/.install "<" (binary (product.uncurry _.<))) (/.install "*" (binary (product.uncurry _.*))) (/.install "/" (binary (product.uncurry _./))) (/.install "%" (binary (product.uncurry _.%))) - (/.install "frac" (unary (_./ (_.float +1.0)))) - (/.install "char" (unary (!unary "string.char")))))) + (/.install "f64" (unary (_./ (_.float +1.0)))) + (/.install "char" (unary (!unary "string.char"))) + ))) -(def: frac//decode - (Unary (Expression Any)) +(def: f64//decode + (Unary Expression) (|>> list _.apply/* (|> (_.var "tonumber")) _.return (_.closure (list)) //runtime.lux//try)) -(def: frac-procs +(def: f64_procs Bundle - (<| (/.prefix "frac") + (<| (/.prefix "f64") (|> /.empty (/.install "+" (binary (product.uncurry _.+))) (/.install "-" (binary (product.uncurry _.-))) @@ -71,23 +66,23 @@ (/.install "%" (binary (product.uncurry _.%))) (/.install "=" (binary (product.uncurry _.=))) (/.install "<" (binary (product.uncurry _.<))) - (/.install "int" (unary (!unary "math.floor"))) + (/.install "i64" (unary (!unary "math.floor"))) (/.install "encode" (unary (!unary "tostring"))) - (/.install "decode" (unary ..frac//decode))))) + (/.install "decode" (unary ..f64//decode))))) (def: (text//char [subjectO paramO]) - (Binary (Expression Any)) + (Binary Expression) (//runtime.text//char subjectO paramO)) (def: (text//clip [paramO extraO subjectO]) - (Trinary (Expression Any)) + (Trinary Expression) (//runtime.text//clip subjectO paramO extraO)) (def: (text//index [startO partO textO]) - (Trinary (Expression Any)) + (Trinary Expression) (//runtime.text//index textO partO startO)) -(def: text-procs +(def: text_procs Bundle (<| (/.prefix "text") (|> /.empty @@ -101,17 +96,16 @@ ))) (def: (io//log! messageO) - (Unary (Expression Any)) + (Unary Expression) (_.or (_.apply/* (list messageO) (_.var "print")) //runtime.unit)) -(def: io-procs +(def: io_procs Bundle (<| (/.prefix "io") (|> /.empty (/.install "log" (unary ..io//log!)) (/.install "error" (unary (!unary "error"))) - (/.install "exit" (unary (!unary "os.exit"))) (/.install "current-time" (nullary (function (_ _) (|> (_.var "os.time") (_.apply/* (list)) @@ -120,10 +114,9 @@ (def: #export bundle Bundle (<| (/.prefix "lux") - (|> lux-procs - (dictionary.merge i64-procs) - (dictionary.merge int-procs) - (dictionary.merge frac-procs) - (dictionary.merge text-procs) - (dictionary.merge io-procs) + (|> lux_procs + (dictionary.merge i64_procs) + (dictionary.merge f64_procs) + (dictionary.merge text_procs) + (dictionary.merge io_procs) ))) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js.lux index 02197dc02..03913b84b 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js.lux @@ -17,7 +17,7 @@ ["/#" // #_ ["#." reference] ["/#" // #_ - ["." extension] + ["#." extension] ["/#" // #_ [analysis (#+)] ["." synthesis] @@ -109,7 +109,7 @@ (/function.apply expression archive application) (#synthesis.Extension extension) - (extension.apply archive expression extension) + (///extension.apply archive expression extension) )) (def: #export generate diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/case.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/case.lux index 38f5125ea..1bcd569c7 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/case.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/case.lux @@ -2,8 +2,6 @@ [lux (#- case let if) [abstract ["." monad (#+ do)]] - [control - ["." exception (#+ exception:)]] [data ["." maybe] ["." text] diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux index 9fa7107bb..f62b04c4e 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux @@ -51,15 +51,15 @@ [Bundle /////generation.Bundle] ) +(type: #export (Generator i) + (-> Phase Archive i (Operation Expression))) + (type: #export Phase! (-> Phase Archive Synthesis (Operation Statement))) (type: #export (Generator! i) (-> Phase! Phase Archive i (Operation Statement))) -(type: #export (Generator i) - (-> Phase Archive i (Operation Expression))) - (def: prefix Text "LuxRuntime") @@ -108,12 +108,9 @@ (case declaration (#.Left name) (let [g!name (code.local_identifier name)] - (wrap (list (` (def: (~ runtime) + (wrap (list (` (def: #export (~ g!name) Var (~ runtime_name))) - - (` (def: #export (~ g!name) - (~ runtime))) (` (def: (~ (code.local_identifier (format "@" name))) Statement @@ -125,13 +122,10 @@ (let [g!name (code.local_identifier name) inputsC (list\map code.local_identifier inputs) inputs_typesC (list\map (function.constant (` _.Expression)) inputs)] - (wrap (list (` (def: ((~ runtime) (~+ inputsC)) + (wrap (list (` (def: #export ((~ g!name) (~+ inputsC)) (-> (~+ inputs_typesC) Computation) (_.apply/* (~ runtime_name) (list (~+ inputsC))))) - (` (def: #export (~ g!name) - (~ runtime))) - (` (def: (~ (code.local_identifier (format "@" name))) Statement (..feature (~ runtime_name) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua.lux index 31ede85d1..2e3369915 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua.lux @@ -3,19 +3,24 @@ [abstract [monad (#+ do)]]] ["." / #_ - [runtime (#+ Phase)] + [runtime (#+ Phase Phase!)] ["#." primitive] ["#." structure] - ["#." reference ("#\." system)] + ["#." reference] ["#." case] ["#." loop] ["#." function] - ["//#" /// #_ - ["#." extension] + ["/#" // #_ + ["#." reference] ["/#" // #_ - ["." synthesis] - ["//#" /// #_ - ["#." phase ("#\." monad)]]]]]) + ["#." extension] + ["/#" // #_ + [analysis (#+)] + ["." synthesis] + ["//#" /// #_ + ["#." phase ("#\." monad)] + [reference (#+) + [variable (#+)]]]]]]]) (def: #export (generate archive synthesis) Phase @@ -35,7 +40,7 @@ (/structure.tuple generate archive members) (#synthesis.Reference value) - (/reference\reference archive value) + (//reference.reference /reference.system archive value) (^ (synthesis.branch/case case)) (/case.case generate archive case) @@ -46,6 +51,9 @@ (^ (synthesis.branch/if if)) (/case.if generate archive if) + (^ (synthesis.branch/get get)) + (/case.get generate archive get) + (^ (synthesis.loop/scope scope)) (/loop.scope generate archive scope) 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 b1861b93a..e6dad82e5 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 @@ -1,9 +1,7 @@ (.module: [lux (#- case let if) [abstract - [monad (#+ do)]] - [control - ["ex" exception (#+ exception:)]] + ["." monad (#+ do)]] [data ["." text] [collection @@ -12,27 +10,26 @@ [target ["_" lua (#+ Expression Var Statement)]]] ["." // #_ - ["#." runtime (#+ Operation Phase Generator)] + ["#." runtime (#+ Operation Phase Phase! Generator Generator!)] + ["#." reference] ["#." primitive] ["/#" // #_ ["#." reference] ["/#" // #_ - [synthesis - ["/" case]] + ["#." synthesis #_ + ["#/." case]] ["/#" // #_ - ["#." synthesis (#+ Synthesis Path)] - ["/#" // #_ - ["/#" // #_ - [reference (#+ Register)] - ["#." phase ("#\." monad)] - [meta - [archive (#+ Archive)]]]]]]]]) + ["#." synthesis (#+ Member Synthesis Path)] + ["//#" /// #_ + [reference + [variable (#+ Register)]] + ["#." phase ("#\." monad)] + [meta + [archive (#+ Archive)]]]]]]]) (def: #export register - (///reference.local _.var)) - -(def: #export capture - (///reference.foreign _.var)) + (-> Register Var) + (|>> (///reference.local //reference.system) :assume)) (def: #export (let generate archive [valueS register bodyS]) (Generator [Synthesis Register Synthesis]) @@ -45,8 +42,8 @@ (_.closure (list (..register register))) (_.apply/* (list valueO)))))) -(def: #export (record-get generate archive [valueS pathP]) - (Generator [Synthesis (List (Either Nat Nat))]) +(def: #export (get generate archive [pathP valueS]) + (Generator [(List Member) Synthesis]) (do ///////phase.monad [valueO (generate archive valueS)] (wrap (list\fold (function (_ side source) @@ -54,11 +51,11 @@ (^template [<side> <accessor>] [(<side> lefts) (<accessor> (_.int (.int lefts)))]) - ([#.Left //runtime.tuple//left] + ([#.Left //runtime.tuple//left] [#.Right //runtime.tuple//right]))] (method source))) valueO - pathP)))) + (list.reverse pathP))))) (def: #export (if generate archive [testS thenS elseS]) (Generator [Synthesis Synthesis Synthesis]) @@ -77,19 +74,19 @@ (def: @temp (_.var "lux_pm_temp")) (def: (push! value) - (-> (Expression Any) Statement) + (-> Expression Statement) (_.statement (|> (_.var "table.insert") (_.apply/* (list @cursor value))))) -(def: peek-and-pop - (Expression Any) +(def: peek_and_pop + Expression (|> (_.var "table.remove") (_.apply/* (list @cursor)))) (def: pop! Statement - (_.statement ..peek-and-pop)) + (_.statement ..peek_and_pop)) (def: peek - (Expression Any) + Expression (_.nth (_.length @cursor) @cursor)) (def: save! @@ -116,8 +113,8 @@ fail! (..push! @temp)))))] - [left-choice _.nil (<|)] - [right-choice (_.string "") inc] + [left_choice _.nil (<|)] + [right_choice (_.string "") inc] ) (def: (alternation pre! post!) @@ -131,81 +128,103 @@ ..restore! post!))) -(def: (pattern-matching' generate archive pathP) +(def: (pattern_matching' generate archive) (-> Phase Archive Path (Operation Statement)) - (.case pathP - (^ (/////synthesis.path/then bodyS)) - (///////phase\map _.return (generate archive bodyS)) - - #/////synthesis.Pop - (///////phase\wrap ..pop!) - - (#/////synthesis.Bind register) - (///////phase\wrap (_.let (list (..register register)) ..peek)) - - (^template [<tag> <format>] - [(^ (<tag> value)) - (///////phase\wrap (_.when (|> value <format> (_.= ..peek) _.not) - fail!))]) - ([/////synthesis.path/bit //primitive.bit] - [/////synthesis.path/i64 //primitive.i64] - [/////synthesis.path/f64 //primitive.f64] - [/////synthesis.path/text //primitive.text]) - - (^template [<complex> <simple> <choice>] - [(^ (<complex> idx)) - (///////phase\wrap (<choice> false idx)) - - (^ (<simple> idx nextP)) - (|> nextP - (pattern-matching' generate archive) - (///////phase\map (_.then (<choice> true idx))))]) - ([/////synthesis.side/left /////synthesis.simple-left-side ..left-choice] - [/////synthesis.side/right /////synthesis.simple-right-side ..right-choice]) - - (^ (/////synthesis.member/left 0)) - (///////phase\wrap (|> ..peek (_.nth (_.int +1)) ..push!)) - - (^template [<pm> <getter>] - [(^ (<pm> lefts)) - (///////phase\wrap (|> ..peek (<getter> (_.int (.int lefts))) ..push!))]) - ([/////synthesis.member/left //runtime.tuple//left] - [/////synthesis.member/right //runtime.tuple//right]) - - (^ (/////synthesis.!bind-top register thenP)) - (do ///////phase.monad - [then! (pattern-matching' generate archive thenP)] - (///////phase\wrap ($_ _.then - (_.let (list (..register register)) ..peek-and-pop) - then!))) - - (^template [<tag> <combinator>] - [(^ (<tag> preP postP)) - (do ///////phase.monad - [pre! (pattern-matching' generate archive preP) - post! (pattern-matching' generate archive postP)] - (wrap (<combinator> pre! post!)))]) - ([/////synthesis.path/seq _.then] - [/////synthesis.path/alt ..alternation]))) - -(def: (pattern-matching generate archive pathP) + (function (recur pathP) + (.case pathP + (#/////synthesis.Then bodyS) + (///////phase\map _.return (generate archive bodyS)) + + #/////synthesis.Pop + (///////phase\wrap ..pop!) + + (#/////synthesis.Bind register) + (///////phase\wrap (_.let (list (..register register)) ..peek)) + + (#/////synthesis.Bit_Fork when thenP elseP) + (do {! ///////phase.monad} + [then! (recur thenP) + else! (.case elseP + (#.Some elseP) + (recur elseP) + + #.None + (wrap ..fail!))] + (wrap (.if when + (_.if ..peek + then! + else!) + (_.if ..peek + else! + then!)))) + + (^template [<tag> <format>] + [(<tag> cons) + (do {! ///////phase.monad} + [clauses (monad.map ! (function (_ [match then]) + (do ! + [then! (recur then)] + (wrap [(_.= (|> match <format>) + ..peek) + then!]))) + (#.Cons cons))] + (wrap (_.cond clauses ..fail!)))]) + ([#/////synthesis.I64_Fork (<| _.int .int)] + [#/////synthesis.F64_Fork _.float] + [#/////synthesis.Text_Fork _.string]) + + (^template [<complex> <simple> <choice>] + [(^ (<complex> idx)) + (///////phase\wrap (<choice> false idx)) + + (^ (<simple> idx nextP)) + (///////phase\map (_.then (<choice> true idx)) (recur nextP))]) + ([/////synthesis.side/left /////synthesis.simple_left_side ..left_choice] + [/////synthesis.side/right /////synthesis.simple_right_side ..right_choice]) + + (^ (/////synthesis.member/left 0)) + (///////phase\wrap (|> ..peek (_.nth (_.int +1)) ..push!)) + + (^template [<pm> <getter>] + [(^ (<pm> lefts)) + (///////phase\wrap (|> ..peek (<getter> (_.int (.int lefts))) ..push!))]) + ([/////synthesis.member/left //runtime.tuple//left] + [/////synthesis.member/right //runtime.tuple//right]) + + (^ (/////synthesis.!bind_top register thenP)) + (do ///////phase.monad + [then! (recur thenP)] + (///////phase\wrap ($_ _.then + (_.let (list (..register register)) ..peek_and_pop) + then!))) + + (^template [<tag> <combinator>] + [(^ (<tag> preP postP)) + (do ///////phase.monad + [pre! (recur preP) + post! (recur postP)] + (wrap (<combinator> pre! post!)))]) + ([/////synthesis.path/seq _.then] + [/////synthesis.path/alt ..alternation])))) + +(def: (pattern_matching generate archive pathP) (-> Phase Archive Path (Operation Statement)) (do ///////phase.monad - [pattern-matching! (pattern-matching' generate archive pathP)] + [pattern_matching! (pattern_matching' generate archive pathP)] (wrap ($_ _.then (_.while (_.bool true) - pattern-matching!) - (_.statement (|> (_.var "error") (_.apply/* (list (_.string /.pattern-matching-error))))))))) + pattern_matching!) + (_.statement (|> (_.var "error") (_.apply/* (list (_.string ////synthesis/case.pattern_matching_error))))))))) (def: #export (case generate archive [valueS pathP]) (Generator [Synthesis Path]) (do ///////phase.monad [initG (generate archive valueS) - pattern-matching! (pattern-matching generate archive pathP)] + 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!) + pattern_matching!) (_.closure (list)) (_.apply/* (list)))))) 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 0d97b3b8c..7c07c8c6d 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 @@ -9,21 +9,22 @@ [collection ["." list ("#\." functor fold)]]] [target - ["_" lua (#+ Expression Statement)]]] + ["_" lua (#+ Var Expression Statement)]]] ["." // #_ - ["#." runtime (#+ Operation Phase Generator)] + ["#." runtime (#+ Operation Phase Phase! Generator)] ["#." reference] ["#." case] ["/#" // #_ ["#." reference] ["//#" /// #_ - [analysis (#+ Variant Tuple Environment Abstraction Application Analysis)] + [analysis (#+ Variant Tuple Abstraction Application Analysis)] [synthesis (#+ Synthesis)] - ["#." generation] + ["#." generation (#+ Context)] ["//#" /// #_ - [reference (#+ Register Variable)] [arity (#+ Arity)] - ["#." phase]]]]]) + ["#." phase ("#\." monad)] + [reference + [variable (#+ Register Variable)]]]]]]) (def: #export (apply generate archive [functionS argsS+]) (Generator (Application Synthesis)) @@ -33,16 +34,17 @@ (wrap (_.apply/* argsO+ functionO)))) (def: #export capture - (///reference.foreign _.var)) + (-> Register Var) + (|>> (///reference.foreign //reference.system) :assume)) -(def: (with-closure function-name inits function-definition) - (-> Text (List (Expression Any)) Statement (Operation (Expression Any))) +(def: (with_closure function_name inits function_definition) + (-> Text (List Expression) Statement (Operation Expression)) (case inits #.Nil (do ///////phase.monad - [_ (/////generation.execute! function-definition) - _ (/////generation.save! function-name function-definition)] - (wrap (|> (_.var function-name) (_.apply/* inits)))) + [_ (/////generation.execute! function_definition) + _ (/////generation.save! function_name function_definition)] + (wrap (|> (_.var function_name) (_.apply/* inits)))) _ (do {! ///////phase.monad} @@ -51,8 +53,8 @@ (|> (list.enumeration inits) (list\map (|>> product.left ..capture))) ($_ _.then - function-definition - (_.return (_.var function-name))))] + function_definition + (_.return (_.var function_name))))] _ (/////generation.execute! directive) _ (/////generation.save! (_.code @closure) directive)] (wrap (_.apply/* inits @closure))))) @@ -63,48 +65,47 @@ (def: #export (function generate archive [environment arity bodyS]) (Generator (Abstraction Synthesis)) (do {! ///////phase.monad} - [[function-name bodyO] (/////generation.with-new-context + [[function_name bodyO] (/////generation.with_new_context archive (do ! - [function-name (\ ! map ///reference.artifact-name - /////generation.context)] - (/////generation.with-anchor (_.var function-name) + [function_name (\ ! map ///reference.artifact + (/////generation.context archive))] + (/////generation.with_anchor (_.var function_name) (generate archive bodyS)))) - closureO+ (: (Operation (List (Expression Any))) - (monad.map ! (\ //reference.system variable) environment)) - #let [function-name (///reference.artifact-name function-name) + closureO+ (monad.map ! (generate archive) environment) + #let [function_name (///reference.artifact function_name) @curried (_.var "curried") arityO (|> arity .int _.int) - @num-args (_.var "num_args") - @self (_.var function-name) - initialize-self! (_.let (list (//case.register 0)) @self) + @num_args (_.var "num_args") + @self (_.var function_name) + initialize_self! (_.let (list (//case.register 0)) @self) initialize! (list\fold (.function (_ post pre!) ($_ _.then pre! (_.let (list (..input post)) (_.nth (|> post inc .int _.int) @curried)))) - initialize-self! + initialize_self! (list.indices arity)) pack (|>> (list) _.apply/* (|> (_.var "table.pack"))) unpack (|>> (list) _.apply/* (|> (_.var "table.unpack"))) - @var-args (_.var "...")]] - (with-closure function-name closureO+ - (_.function @self (list @var-args) + @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))) + (_.let (list @curried) (pack @var_args)) + (_.let (list @num_args) (_.the "n" @curried)) + (_.cond (list [(|> @num_args (_.= (_.int +0))) (_.return @self)] - [(|> @num-args (_.= arityO)) + [(|> @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)] + [(|> @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)))))))))) + (_.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 4b405a8af..817ba118a 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 @@ -4,33 +4,36 @@ ["." monad (#+ do)]] [data ["." product] - [number - ["n" nat]] [text ["%" format (#+ format)]] [collection ["." list ("#\." functor)]]] + [math + [number + ["n" nat]]] [target ["_" lua (#+ Expression Var)]]] ["." // #_ - [runtime (#+ Operation Phase Generator)] + [runtime (#+ Operation Phase Phase! Generator Generator!)] ["#." case] ["///#" //// #_ [synthesis (#+ Scope Synthesis)] ["#." generation] ["//#" /// #_ - ["#." phase]]]]) + ["#." phase] + [reference + [variable (#+ Register)]]]]]) -(def: loop-name +(def: loop_name (-> Nat Var) (|>> %.nat (format "loop") _.var)) (def: #export (scope generate archive [start initsS+ bodyS]) (Generator (Scope Synthesis)) (do {! ///////phase.monad} - [@loop (\ ! map ..loop-name /////generation.next) + [@loop (\ ! map ..loop_name /////generation.next) initsO+ (monad.map ! (generate archive) initsS+) - bodyO (/////generation.with-anchor @loop + bodyO (/////generation.with_anchor @loop (generate archive bodyS)) #let [directive (_.function @loop (|> initsS+ list.enumeration diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/reference.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/reference.lux index 8b6fedb0b..965ac68b3 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/reference.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/reference.lux @@ -3,10 +3,10 @@ [target ["_" lua (#+ Expression)]]] [/// - ["/" reference]]) + [reference (#+ System)]]) -(def: #export system - (let [constant (: (-> Text (Expression Any)) - _.var) - variable constant] - (/.system constant variable))) +(structure: #export system + (System Expression) + + (def: constant _.var) + (def: variable _.var)) 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 c34a998a4..72f8576f5 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 @@ -1,36 +1,44 @@ (.module: - [lux (#- inc) + [lux (#- Location inc) + ["." meta] [abstract - [monad (#+ do)]] + ["." monad (#+ do)]] [control ["." function] - ["p" parser - ["s" code]]] + ["<>" parser + ["<.>" code]]] [data - [number (#+ hex) - ["." i64]] - ["." text - ["%" format (#+ format)]] + ["." product] + ["." text ("#\." hash) + ["%" format (#+ format)] + ["." encoding]] [collection - ["." list ("#\." functor)]]] + ["." list ("#\." functor)] + ["." row]]] ["." macro - ["." code] - [syntax (#+ syntax:)]] + [syntax (#+ syntax:)] + ["." code]] + [math + [number (#+ hex) + ["." i64]]] [target ["_" lua (#+ Expression Location Var Computation Literal Statement)]]] ["." /// #_ ["#." reference] ["//#" /// #_ - ["#." synthesis] - ["#." generation (#+ Buffer)] - ["//#" /// #_ + ["#." synthesis (#+ Synthesis)] + ["#." generation] + ["//#" /// (#+ Output) ["#." phase] + [reference + [variable (#+ Register)]] [meta - [archive (#+ Archive)]]]]]) + [archive (#+ Archive) + ["." artifact (#+ Registry)]]]]]]) (template [<name> <base>] [(type: #export <name> - (<base> Var (Expression Any) Statement))] + (<base> Var Expression Statement))] [Operation /////generation.Operation] [Phase /////generation.Phase] @@ -39,9 +47,16 @@ ) (type: #export (Generator i) - (-> Phase Archive i (Operation (Expression Any)))) + (-> Phase Archive i (Operation Expression))) + +(type: #export Phase! + (-> Phase Archive Synthesis (Operation Statement))) -(def: prefix Text "LuxRuntime") +(type: #export (Generator! i) + (-> Phase! Phase Archive i (Operation Statement))) + +(def: prefix + "LuxRuntime") (def: #export unit (_.string /////synthesis.unit)) @@ -51,173 +66,173 @@ (_.string "") _.nil)) -(def: #export variant-tag-field "_lux_tag") -(def: #export variant-flag-field "_lux_flag") -(def: #export variant-value-field "_lux_value") +(def: #export variant_tag_field "_lux_tag") +(def: #export variant_flag_field "_lux_flag") +(def: #export variant_value_field "_lux_value") (def: (variant' tag last? value) - (-> (Expression Any) (Expression Any) (Expression Any) Literal) - (_.table (list [..variant-tag-field tag] - [..variant-flag-field last?] - [..variant-value-field value]))) + (-> Expression Expression Expression Literal) + (_.table (list [..variant_tag_field tag] + [..variant_flag_field last?] + [..variant_value_field value]))) (def: #export (variant tag last? value) - (-> Nat Bit (Expression Any) Literal) + (-> Nat Bit Expression Literal) (variant' (_.int (.int tag)) (flag last?) value)) (def: #export none Literal - (..variant 0 #0 unit)) + (..variant 0 #0 ..unit)) (def: #export some - (-> (Expression Any) Literal) + (-> Expression Literal) (..variant 1 #1)) (def: #export left - (-> (Expression Any) Literal) + (-> Expression Literal) (..variant 0 #0)) (def: #export right - (-> (Expression Any) Literal) + (-> Expression Literal) (..variant 1 #1)) -(def: runtime-name - (-> Text Var) - (|>> ///reference.sanitize - (format ..prefix "_") - _.var)) - (def: (feature name definition) (-> Var (-> Var Statement) Statement) (definition name)) -(syntax: #export (with-vars {vars (s.tuple (p.some s.local-identifier))} +(syntax: #export (with_vars {vars (<code>.tuple (<>.some <code>.local_identifier))} body) - (wrap (list (` (let [(~+ (|> vars - (list\map (function (_ var) - (list (code.local-identifier var) - (` (_.var (~ (code.text (///reference.sanitize var)))))))) - list.concat))] - (~ body)))))) - -(syntax: (runtime: {declaration (p.or s.local-identifier - (s.form (p.and s.local-identifier - (p.some s.local-identifier))))} + (do {! meta.monad} + [ids (monad.seq ! (list.repeat (list.size vars) meta.count))] + (wrap (list (` (let [(~+ (|> vars + (list.zip/2 ids) + (list\map (function (_ [id var]) + (list (code.local_identifier var) + (` (_.var (~ (code.text (format "v" (%.nat id))))))))) + list.concat))] + (~ body))))))) + +(syntax: (runtime: {declaration (<>.or <code>.local_identifier + (<code>.form (<>.and <code>.local_identifier + (<>.some <code>.local_identifier))))} code) - (case declaration - (#.Left name) - (macro.with-gensyms [g!_] - (let [nameC (code.local-identifier name) - code-nameC (code.local-identifier (format "@" name)) - runtime-nameC (` (runtime-name (~ (code.text name))))] - (wrap (list (` (def: #export (~ nameC) Var (~ runtime-nameC))) - (` (def: (~ code-nameC) - Statement - (..feature (~ runtime-nameC) - (function ((~ g!_) (~ nameC)) - (_.set (~ nameC) (~ code)))))))))) - - (#.Right [name inputs]) - (macro.with-gensyms [g!_] - (let [nameC (code.local-identifier name) - code-nameC (code.local-identifier (format "@" name)) - runtime-nameC (` (runtime-name (~ (code.text name)))) - inputsC (list\map code.local-identifier inputs) - inputs-typesC (list\map (function.constant (` (_.Expression Any))) - inputs)] - (wrap (list (` (def: #export ((~ nameC) (~+ inputsC)) - (-> (~+ inputs-typesC) (Computation Any)) - (_.apply/* (list (~+ inputsC)) (~ runtime-nameC)))) - (` (def: (~ code-nameC) - Statement - (..feature (~ runtime-nameC) - (function ((~ g!_) (~ g!_)) - (..with-vars [(~+ inputsC)] - (_.function (~ g!_) (list (~+ inputsC)) - (~ code))))))))))))) + (macro.with_gensyms [g!_ runtime] + (let [runtime_name (` (_.var (~ (code.text (%.code runtime)))))] + (case declaration + (#.Left name) + (macro.with_gensyms [g!_] + (let [g!name (code.local_identifier name)] + (wrap (list (` (def: #export (~ g!name) + Var + (~ runtime_name))) + + (` (def: (~ (code.local_identifier (format "@" name))) + Statement + (..feature (~ runtime_name) + (function ((~ g!_) (~ g!name)) + (_.set (~ g!name) (~ code)))))))))) + + (#.Right [name inputs]) + (macro.with_gensyms [g!_] + (let [g!name (code.local_identifier name) + inputsC (list\map code.local_identifier inputs) + inputs_typesC (list\map (function.constant (` _.Expression)) + inputs)] + (wrap (list (` (def: #export ((~ g!name) (~+ inputsC)) + (-> (~+ inputs_typesC) Computation) + (_.apply/* (list (~+ inputsC)) (~ runtime_name)))) + + (` (def: (~ (code.local_identifier (format "@" name))) + Statement + (..feature (~ runtime_name) + (function ((~ g!_) (~ g!_)) + (..with_vars [(~+ inputsC)] + (_.function (~ g!_) (list (~+ inputsC)) + (~ code))))))))))))))) (def: (nth index table) - (-> (Expression Any) (Expression Any) (Location Any)) + (-> Expression Expression Location) (_.nth (_.+ (_.int +1) index) table)) -(def: last-index (|>> _.length (_.- (_.int +1)))) +(def: last_index + (|>> _.length (_.- (_.int +1)))) ## No need to turn tuple//left and tuple//right into loops, as Lua ## does tail-call optimization. ## https://www.lua.org/pil/6.3.html (runtime: (tuple//left lefts tuple) - (with-vars [last-right] + (with_vars [last_right] ($_ _.then - (_.let (list last-right) (..last-index tuple)) - (_.if (_.> lefts last-right) + (_.let (list last_right) (..last_index tuple)) + (_.if (_.> lefts last_right) ## No need for recursion (_.return (..nth lefts tuple)) ## Needs recursion - (_.return (tuple//left (_.- last-right lefts) - (..nth last-right tuple))))))) + (_.return (tuple//left (_.- last_right lefts) + (..nth last_right tuple))))))) (runtime: (array//sub from to array) - (with-vars [temp idx] + (with_vars [temp idx] ($_ _.then (_.let (list temp) (_.array (list))) - (_.for-step idx from (_.- (_.int +1) to) (_.int +1) + (_.for_step idx from (_.- (_.int +1) to) (_.int +1) (|> (_.var "table.insert") (_.apply/* (list temp (..nth idx array))) _.statement)) (_.return temp)))) (runtime: (tuple//right lefts tuple) - (with-vars [last-right right-index] + (with_vars [last_right right_index] ($_ _.then - (_.let (list last-right) (..last-index tuple)) - (_.let (list right-index) (_.+ (_.int +1) lefts)) - (_.cond (list [(_.= last-right right-index) - (_.return (..nth right-index tuple))] - [(_.> last-right right-index) + (_.let (list last_right) (..last_index tuple)) + (_.let (list right_index) (_.+ (_.int +1) lefts)) + (_.cond (list [(_.= last_right right_index) + (_.return (..nth right_index tuple))] + [(_.> last_right right_index) ## Needs recursion. - (_.return (tuple//right (_.- last-right lefts) - (..nth last-right tuple)))]) - (_.return (array//sub right-index (_.length tuple) tuple))) + (_.return (tuple//right (_.- last_right lefts) + (..nth last_right tuple)))]) + (_.return (array//sub right_index (_.length tuple) tuple))) ))) (runtime: (sum//get sum wantsLast wantedTag) - (let [no-match! (_.return _.nil) - sum-tag (_.the ..variant-tag-field sum) - sum-flag (_.the ..variant-flag-field sum) - sum-value (_.the ..variant-value-field sum) - is-last? (_.= (_.string "") sum-flag) - test-recursion! (_.if is-last? + (let [no_match! (_.return _.nil) + sum_tag (_.the ..variant_tag_field sum) + sum_flag (_.the ..variant_flag_field sum) + sum_value (_.the ..variant_value_field sum) + is_last? (_.= (_.string "") sum_flag) + test_recursion! (_.if is_last? ## Must recurse. - (_.return (sum//get sum-value wantsLast (_.- sum-tag wantedTag))) - no-match!)] - (_.cond (list [(_.= sum-tag wantedTag) - (_.if (_.= wantsLast sum-flag) - (_.return sum-value) - test-recursion!)] + (_.return (sum//get sum_value wantsLast (_.- sum_tag wantedTag))) + no_match!)] + (_.cond (list [(_.= sum_tag wantedTag) + (_.if (_.= wantsLast sum_flag) + (_.return sum_value) + test_recursion!)] - [(_.> sum-tag wantedTag) - test-recursion!] + [(_.> sum_tag wantedTag) + test_recursion!] - [(_.and (_.< sum-tag wantedTag) + [(_.and (_.< sum_tag wantedTag) (_.= (_.string "") wantsLast)) - (_.return (variant' (_.- wantedTag sum-tag) sum-flag sum-value))]) + (_.return (variant' (_.- wantedTag sum_tag) sum_flag sum_value))]) - no-match!))) + no_match!))) (runtime: (array//copy array) - (with-vars [temp idx] + (with_vars [temp idx] ($_ _.then (_.let (list temp) (_.array (list))) - (<| (_.for-step idx (_.int +1) (_.length array) (_.int +1)) + (<| (_.for_step idx (_.int +1) (_.length array) (_.int +1)) (_.statement (|> (_.var "table.insert") (_.apply/* (list temp (_.nth idx array)))))) (_.return temp)))) (runtime: (array//concat left right) - (with-vars [temp idx] + (with_vars [temp idx] (let [copy! (function (_ input output) - (<| (_.for-step idx (_.int +1) (_.the "n" input) (_.int +1)) + (<| (_.for_step idx (_.int +1) (_.the "n" input) (_.int +1)) (_.statement (|> (_.var "table.insert") (_.apply/* (list output (_.nth idx input)))))))] ($_ _.then (_.let (list temp) (_.array (list))) @@ -237,7 +252,7 @@ )) (runtime: (lux//try risky) - (with-vars [success value] + (with_vars [success value] ($_ _.then (_.let (list success value) (|> risky (_.apply/* (list ..unit)) _.return (_.closure (list)) @@ -246,11 +261,11 @@ (_.return (..right value)) (_.return (..left value)))))) -(runtime: (lux//program-args raw) - (with-vars [tail head idx] +(runtime: (lux//program_args raw) + (with_vars [tail head idx] ($_ _.then (_.let (list tail) ..none) - (<| (_.for-step idx (_.length raw) (_.int +1) (_.int -1)) + (<| (_.for_step idx (_.length raw) (_.int +1) (_.int -1)) (_.set (list tail) (..some (_.array (list (_.nth idx raw) tail))))) (_.return tail)))) @@ -259,25 +274,25 @@ Statement ($_ _.then @lux//try - @lux//program-args + @lux//program_args )) -(runtime: (i64//logic-right-shift param subject) +(runtime: (i64//logic_right_shift param subject) (let [mask (|> (_.int +1) - (_.bit-shl (_.- param (_.int +64))) + (_.bit_shl (_.- param (_.int +64))) (_.- (_.int +1)))] (_.return (|> subject - (_.bit-shr param) - (_.bit-and mask))))) + (_.bit_shr param) + (_.bit_and mask))))) (def: runtime//i64 Statement ($_ _.then - @i64//logic-right-shift + @i64//logic_right_shift )) (runtime: (text//index subject param start) - (with-vars [idx] + (with_vars [idx] ($_ _.then (_.let (list idx) (_.apply/* (list subject param start (_.bool #1)) (_.var "string.find"))) @@ -286,7 +301,7 @@ (_.return (..some idx)))))) (runtime: (text//clip text from to) - (with-vars [size] + (with_vars [size] ($_ _.then (_.let (list size) (_.apply/* (list text) (_.var "string.len"))) (_.if (_.or (_.> size from) @@ -296,7 +311,7 @@ ))) (runtime: (text//char idx text) - (with-vars [char] + (with_vars [char] ($_ _.then (_.let (list char) (_.apply/* (list text idx) (_.var "string.byte"))) (_.if (_.= _.nil char) @@ -312,15 +327,15 @@ )) (runtime: (array//new size) - (with-vars [output idx] + (with_vars [output idx] ($_ _.then (_.let (list output) (_.array (list))) - (_.for-step idx (_.int +1) size (_.int +1) + (_.for_step idx (_.int +1) size (_.int +1) (_.statement (_.apply/* (list output ..unit) (_.var "table.insert")))) (_.return output)))) (runtime: (array//get array idx) - (with-vars [temp] + (with_vars [temp] ($_ _.then (_.let (list temp) (..nth idx array)) (_.if (_.or (_.= _.nil temp) @@ -366,9 +381,14 @@ (def: #export artifact ..prefix) (def: #export generate - (Operation (Buffer Statement)) - (/////generation.with-buffer - (do ///////phase.monad - [_ (/////generation.execute! ..runtime) - _ (/////generation.save! ..prefix ..runtime)] - /////generation.buffer))) + (Operation [Registry Output]) + (do ///////phase.monad + [_ (/////generation.execute! ..runtime) + _ (/////generation.save! "0" ..runtime)] + (wrap [(|> artifact.empty + artifact.resource + product.right) + (row.row ["0" + (|> ..runtime + _.code + (\ encoding.utf8 encode))])]))) |