From f2937706edb6887c5eb1a6a0b6668b1334f5ef3b Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 11 Apr 2019 22:30:05 -0400 Subject: WIP: Lua compiler. --- new-luxc/source/luxc/lang/translation/lua.lux | 231 ------------- .../source/luxc/lang/translation/lua/case.jvm.lux | 175 ---------- .../source/luxc/lang/translation/lua/eval.jvm.lux | 125 ------- .../luxc/lang/translation/lua/expression.jvm.lux | 88 ----- .../luxc/lang/translation/lua/function.jvm.lux | 82 ----- .../source/luxc/lang/translation/lua/loop.jvm.lux | 35 -- .../luxc/lang/translation/lua/primitive.jvm.lux | 34 -- .../lang/translation/lua/procedure/common.jvm.lux | 374 --------------------- .../lang/translation/lua/procedure/host.jvm.lux | 87 ----- .../luxc/lang/translation/lua/reference.jvm.lux | 36 -- .../luxc/lang/translation/lua/runtime.jvm.lux | 293 ---------------- .../luxc/lang/translation/lua/statement.jvm.lux | 48 --- .../luxc/lang/translation/lua/structure.jvm.lux | 31 -- 13 files changed, 1639 deletions(-) delete mode 100644 new-luxc/source/luxc/lang/translation/lua.lux delete mode 100644 new-luxc/source/luxc/lang/translation/lua/case.jvm.lux delete mode 100644 new-luxc/source/luxc/lang/translation/lua/eval.jvm.lux delete mode 100644 new-luxc/source/luxc/lang/translation/lua/expression.jvm.lux delete mode 100644 new-luxc/source/luxc/lang/translation/lua/function.jvm.lux delete mode 100644 new-luxc/source/luxc/lang/translation/lua/loop.jvm.lux delete mode 100644 new-luxc/source/luxc/lang/translation/lua/primitive.jvm.lux delete mode 100644 new-luxc/source/luxc/lang/translation/lua/procedure/common.jvm.lux delete mode 100644 new-luxc/source/luxc/lang/translation/lua/procedure/host.jvm.lux delete mode 100644 new-luxc/source/luxc/lang/translation/lua/reference.jvm.lux delete mode 100644 new-luxc/source/luxc/lang/translation/lua/runtime.jvm.lux delete mode 100644 new-luxc/source/luxc/lang/translation/lua/statement.jvm.lux delete mode 100644 new-luxc/source/luxc/lang/translation/lua/structure.jvm.lux (limited to 'new-luxc/source/luxc/lang/translation') diff --git a/new-luxc/source/luxc/lang/translation/lua.lux b/new-luxc/source/luxc/lang/translation/lua.lux deleted file mode 100644 index e79af1048..000000000 --- a/new-luxc/source/luxc/lang/translation/lua.lux +++ /dev/null @@ -1,231 +0,0 @@ -(.module: - lux - (lux (control ["ex" exception #+ exception:] - pipe - [monad #+ do]) - (data [bit] - [maybe] - ["e" error #+ Error] - [text "text/" Eq] - text/format - (coll [array])) - [macro] - [io #+ IO Process io] - [host #+ class: interface: object] - (world [file #+ File])) - (luxc [lang] - (lang [".L" variable #+ Register] - (host [lua #+ Lua Expression Statement])) - [".C" io])) - -(template [] - [(exception: #export ( {message Text}) - message)] - - [No-Active-Module-Buffer] - [Cannot-Execute] - - [No-Anchor] - ) - -(host.import: java/lang/Object) - -(host.import: java/lang/String - (getBytes [String] #try (Array byte))) - -(host.import: java/lang/CharSequence) - -(host.import: java/lang/Appendable - (append [CharSequence] Appendable)) - -(host.import: java/lang/StringBuilder - (new []) - (toString [] String)) - -(host.import: net/sandius/rembulan/StateContext) - -(host.import: net/sandius/rembulan/impl/StateContexts - (#static newDefaultInstance [] StateContext)) - -(host.import: net/sandius/rembulan/env/RuntimeEnvironment) - -(host.import: net/sandius/rembulan/env/RuntimeEnvironments - (#static system [] RuntimeEnvironment)) - -(host.import: net/sandius/rembulan/Table) - -(host.import: net/sandius/rembulan/lib/StandardLibrary - (#static in [RuntimeEnvironment] StandardLibrary) - (installInto [StateContext] Table)) - -(host.import: net/sandius/rembulan/Variable - (new [Object])) - -(host.import: net/sandius/rembulan/runtime/LuaFunction) - -(host.import: net/sandius/rembulan/load/ChunkLoader - (loadTextChunk [Variable String String] LuaFunction)) - -(host.import: net/sandius/rembulan/compiler/CompilerChunkLoader - (#static of [String] CompilerChunkLoader)) - -(host.import: net/sandius/rembulan/exec/DirectCallExecutor - (#static newExecutor [] DirectCallExecutor) - (call [StateContext Object (Array Object)] (Array Object))) - -(type: #export Anchor [Text Register]) - -(type: #export Host - {#context [Text Nat] - #anchor (Maybe Anchor) - #interpreter (-> Text (Error Any)) - #module-buffer (Maybe StringBuilder) - #program-buffer StringBuilder}) - -(def: #export init - (IO Host) - (io {#context ["" +0] - #anchor #.None - #interpreter (let [runtime-env (RuntimeEnvironments::system []) - std-lib (StandardLibrary::in [runtime-env]) - state-context (StateContexts::newDefaultInstance []) - table (StandardLibrary::installInto [state-context] std-lib) - variable (Variable::new [table]) - loader (CompilerChunkLoader::of ["_lux_definition"]) - executor (DirectCallExecutor::newExecutor [])] - (function (_ code) - (let [lua-function (ChunkLoader::loadTextChunk [variable "lux compilation" code] - loader)] - ("lux try" (io (DirectCallExecutor::call [state-context (:coerce Object lua-function) (array.new +0)] - executor)))))) - #module-buffer #.None - #program-buffer (StringBuilder::new [])})) - -(def: #export lua-module-name Text "module.lua") - -(def: #export init-module-buffer - (Meta Any) - (function (_ compiler) - (#e.Success [(update@ #.host - (|>> (:coerce Host) - (set@ #module-buffer (#.Some (StringBuilder::new []))) - (:coerce Nothing)) - compiler) - []]))) - -(def: #export (with-sub-context expr) - (All [a] (-> (Meta a) (Meta [Text a]))) - (function (_ compiler) - (let [old (:coerce Host (get@ #.host compiler)) - [old-name old-sub] (get@ #context old) - new-name (format old-name "___" (%i (.int old-sub)))] - (case (expr (set@ #.host - (:coerce Nothing (set@ #context [new-name +0] old)) - compiler)) - (#e.Success [compiler' output]) - (#e.Success [(update@ #.host - (|>> (:coerce Host) - (set@ #context [old-name (inc old-sub)]) - (:coerce Nothing)) - compiler') - [new-name output]]) - - (#e.Error error) - (#e.Error error))))) - -(def: #export context - (Meta Text) - (function (_ compiler) - (#e.Success [compiler - (|> (get@ #.host compiler) - (:coerce Host) - (get@ #context) - (let> [name sub] - name))]))) - -(def: #export (with-anchor anchor expr) - (All [a] (-> Anchor (Meta a) (Meta a))) - (function (_ compiler) - (let [old (:coerce Host (get@ #.host compiler))] - (case (expr (set@ #.host - (:coerce Nothing (set@ #anchor (#.Some anchor) old)) - compiler)) - (#e.Success [compiler' output]) - (#e.Success [(update@ #.host - (|>> (:coerce Host) - (set@ #anchor (get@ #anchor old)) - (:coerce Nothing)) - compiler') - output]) - - (#e.Error error) - (#e.Error error))))) - -(def: #export anchor - (Meta Anchor) - (function (_ compiler) - (case (|> compiler (get@ #.host) (:coerce Host) (get@ #anchor)) - (#.Some anchor) - (#e.Success [compiler anchor]) - - #.None - ((lang.throw No-Anchor "") compiler)))) - -(def: #export module-buffer - (Meta StringBuilder) - (function (_ compiler) - (case (|> compiler (get@ #.host) (:coerce Host) (get@ #module-buffer)) - #.None - ((lang.throw No-Active-Module-Buffer "") compiler) - - (#.Some module-buffer) - (#e.Success [compiler module-buffer])))) - -(def: #export program-buffer - (Meta StringBuilder) - (function (_ compiler) - (#e.Success [compiler (|> compiler (get@ #.host) (:coerce Host) (get@ #program-buffer))]))) - -(def: (execute code) - (-> Expression (Meta Any)) - (function (_ compiler) - (let [interpreter (|> compiler (get@ #.host) (:coerce Host) (get@ #interpreter))] - (case (interpreter code) - (#e.Error error) - ((lang.throw Cannot-Execute error) compiler) - - (#e.Success _) - (#e.Success [compiler []]))))) - -(def: #export variant-tag-field "_lux_tag") -(def: #export variant-flag-field "_lux_flag") -(def: #export variant-value-field "_lux_value") - -(def: #export unit Text "") - -(def: #export (definition-name [module name]) - (-> Name Text) - (lang.normalize-name (format module "$" name))) - -(def: #export (save code) - (-> Lua (Meta Any)) - (do macro.Monad - [module-buffer module-buffer - #let [_ (Appendable::append [(:coerce CharSequence code)] - module-buffer)]] - (execute code))) - -(def: #export (save-module! target) - (-> File (Meta (Process Any))) - (do macro.Monad - [module macro.current-module-name - module-buffer module-buffer - program-buffer program-buffer - #let [module-code (StringBuilder::toString [] module-buffer) - _ (Appendable::append [(:coerce CharSequence (format module-code "\n"))] - program-buffer)]] - (wrap (ioC.write target - (format (lang.normalize-name module) "/" lua-module-name) - (|> module-code - (String::getBytes ["UTF-8"]) - e.assume))))) diff --git a/new-luxc/source/luxc/lang/translation/lua/case.jvm.lux b/new-luxc/source/luxc/lang/translation/lua/case.jvm.lux deleted file mode 100644 index af4e61b7c..000000000 --- a/new-luxc/source/luxc/lang/translation/lua/case.jvm.lux +++ /dev/null @@ -1,175 +0,0 @@ -(.module: - lux - (lux (control [monad #+ do] - ["ex" exception #+ exception:]) - (data text/format - (coll [list "list/" Fold])) - [macro #+ "meta/" Monad]) - (luxc [lang] - (lang ["ls" synthesis] - (host [lua #+ Lua Expression Statement]))) - [//] - (// [".T" runtime] - [".T" primitive] - [".T" reference])) - -(def: (expression-block body) - (-> Statement Expression) - (lua.apply (lua.function (list) - body) - (list))) - -(def: #export (translate-let translate register valueS bodyS) - (-> (-> ls.Synthesis (Meta Expression)) Nat ls.Synthesis ls.Synthesis - (Meta Expression)) - (do macro.Monad - [valueO (translate valueS) - bodyO (translate bodyS)] - (wrap (expression-block - (lua.block! (list (lua.local! (referenceT.variable register) (#.Some valueO)) - (lua.return! bodyO))))))) - -(def: #export (translate-record-get translate valueS path) - (-> (-> ls.Synthesis (Meta Expression)) ls.Synthesis (List [Nat Bit]) - (Meta Expression)) - (do macro.Monad - [valueO (translate valueS)] - (wrap (list/fold (function (_ [idx tail?] source) - (let [method (if tail? - runtimeT.product//right - runtimeT.product//left)] - (method source (lua.int (:coerce Int idx))))) - valueO - path)))) - -(def: #export (translate-if testO thenO elseO) - (-> Expression Expression Expression Expression) - (expression-block - (lua.if! testO - (lua.return! thenO) - (lua.return! elseO)))) - -(def: savepoint - Expression - "pm_cursor_savepoint") - -(def: cursor - Expression - "pm_cursor") - -(def: (push-cursor! value) - (-> Expression Expression) - (lua.apply "table.insert" (list cursor value))) - -(def: save-cursor! - Statement - (lua.apply "table.insert" (list savepoint (runtimeT.array//copy cursor)))) - -(def: restore-cursor! - Statement - (lua.set! cursor (lua.apply "table.remove" (list savepoint)))) - -(def: cursor-top - Expression - (lua.nth (lua.length cursor) cursor)) - -(def: pop-cursor! - Statement - (lua.apply "table.remove" (list cursor))) - -(def: pm-error - Expression - (lua.string "PM-ERROR")) - -(exception: #export (Unrecognized-Path {message Text}) - message) - -(def: (translate-pattern-matching' translate path) - (-> (-> ls.Synthesis (Meta Expression)) Code (Meta Expression)) - (case path - (^code ("lux case exec" (~ bodyS))) - (do macro.Monad - [bodyO (translate bodyS)] - (wrap (lua.return! bodyO))) - - (^code ("lux case pop")) - (meta/wrap pop-cursor!) - - (^code ("lux case bind" (~ [_ (#.Nat register)]))) - (meta/wrap (lua.local! (referenceT.variable register) (#.Some cursor-top))) - - (^template [ ] - [_ ( value)] - (meta/wrap (lua.when! (lua.not (lua.= (|> value ) cursor-top)) - (lua.return! pm-error)))) - ([#.Nat (<| lua.int (:coerce Int))] - [#.Int lua.int] - [#.Rev (<| lua.int (:coerce Int))] - [#.Bit lua.bool] - [#.Frac lua.float] - [#.Text lua.string]) - - (^template [ ] - (^code ( (~ [_ (#.Nat idx)]))) - (meta/wrap (push-cursor! ( cursor-top (lua.int (:coerce Int idx)))))) - (["lux case tuple left" runtimeT.product//left] - ["lux case tuple right" runtimeT.product//right]) - - (^template [ ] - (^code ( (~ [_ (#.Nat idx)]))) - (meta/wrap (lua.block! (list (lua.set! "temp" (runtimeT.sum//get cursor-top (lua.int (:coerce Int idx)) )) - (lua.if! (lua.= lua.nil "temp") - (lua.return! pm-error) - (push-cursor! "temp")))))) - (["lux case variant left" lua.nil] - ["lux case variant right" (lua.string "")]) - - (^code ("lux case seq" (~ leftP) (~ rightP))) - (do macro.Monad - [leftO (translate-pattern-matching' translate leftP) - rightO (translate-pattern-matching' translate rightP)] - (wrap (lua.block! (list leftO rightO)))) - - (^code ("lux case alt" (~ leftP) (~ rightP))) - (do macro.Monad - [leftO (translate-pattern-matching' translate leftP) - rightO (translate-pattern-matching' translate rightP)] - (wrap (lua.block! (list (format "local alt_success, alt_value = " (lua.apply "pcall" (list (lua.function (list) - (lua.block! (list save-cursor! - leftO))))) ";") - (lua.if! "alt_success" - (lua.return! "alt_value") - (lua.if! (lua.= pm-error "alt_value") - (lua.block! (list restore-cursor! - rightO)) - (lua.error "alt_value"))))))) - - _ - (lang.throw Unrecognized-Path (%code path)) - )) - -(def: (translate-pattern-matching translate path) - (-> (-> ls.Synthesis (Meta Expression)) Code (Meta Expression)) - (do macro.Monad - [pattern-matching (translate-pattern-matching' translate path)] - (wrap (lua.block! (list (format "local success, value = pcall(function () " pattern-matching " end);") - (lua.if! "success" - (lua.return! "value") - (lua.if! (lua.= pm-error "value") - (lua.error (lua.string "Invalid expression for pattern-matching.")) - (lua.error "value")))))))) - -(def: (initialize-pattern-matching stack-init) - (-> Expression Statement) - (lua.block! (list (lua.local! "temp" #.None) - (lua.local! cursor (#.Some (lua.array (list stack-init)))) - (lua.local! savepoint (#.Some (lua.array (list))))))) - -(def: #export (translate-case translate valueS path) - (-> (-> ls.Synthesis (Meta Expression)) ls.Synthesis Code (Meta Expression)) - (do macro.Monad - [valueO (translate valueS) - pattern-matching (translate-pattern-matching translate path)] - (wrap (expression-block - (lua.block! (list (initialize-pattern-matching valueO) - pattern-matching)))))) diff --git a/new-luxc/source/luxc/lang/translation/lua/eval.jvm.lux b/new-luxc/source/luxc/lang/translation/lua/eval.jvm.lux deleted file mode 100644 index 17596ffa7..000000000 --- a/new-luxc/source/luxc/lang/translation/lua/eval.jvm.lux +++ /dev/null @@ -1,125 +0,0 @@ -(.module: - lux - (lux (control ["ex" exception #+ exception:]) - (data [bit] - [maybe] - ["e" error #+ Error] - text/format - (coll [array])) - [host]) - (luxc [lang] - (lang (host [lua #+ Lua Expression Statement]))) - [//]) - -(template [] - [(exception: #export ( {message Text}) - message)] - - [Unknown-Kind-Of-Host-Object] - [Null-Has-No-Lux-Representation] - [Cannot-Evaluate] - ) - -(host.import: java/lang/Object - (toString [] String) - (getClass [] (Class Object))) - -(host.import: java/lang/Long - (intValue [] Integer)) - -(host.import: net/sandius/rembulan/ByteString - (decode [] String)) - -(host.import: net/sandius/rembulan/Table - (rawget #as get-idx [long] #? Object) - (rawget #as get-key [Object] #? Object) - (rawlen [] long)) - -(host.import: net/sandius/rembulan/impl/DefaultTable) - -(def: (variant lux-object host-object) - (-> (-> Object (Error Any)) DefaultTable (Maybe Any)) - (case [(Table::get-key [//.variant-tag-field] host-object) - (Table::get-key [//.variant-flag-field] host-object) - (Table::get-key [//.variant-value-field] host-object)] - (^multi [(#.Some tag) ?flag (#.Some value)] - [(lux-object value) - (#.Some value)]) - (#.Some [(Long::intValue [] (:coerce Long tag)) - (: Any (case ?flag (#.Some _) "" #.None (host.null))) - value]) - - _ - #.None)) - -(def: (array lux-object host-object) - (-> (-> Object (Error Any)) DefaultTable (Maybe (Array Object))) - (let [init-num-keys (:coerce Nat (Table::rawlen [] host-object))] - (loop [num-keys init-num-keys - idx +0 - output (: (Array Object) - (array.new init-num-keys))] - (if (n/< num-keys idx) - (case (Table::get-idx (:coerce Long (inc idx)) host-object) - (#.Some member) - (case (lux-object member) - (#e.Success parsed-member) - (recur num-keys (inc idx) (array.write idx (:coerce Object parsed-member) output)) - - (#e.Error error) - #.None) - - #.None - (recur num-keys (inc idx) output)) - (#.Some output))))) - -(def: (lux-object host-object) - (-> Object (Error Any)) - (cond (host.null? host-object) - (ex.throw Null-Has-No-Lux-Representation "") - - (or (host.instance? java/lang/Boolean host-object) - (host.instance? java/lang/Long host-object) - (host.instance? java/lang/Double host-object) - (host.instance? java/lang/String host-object)) - (ex.return host-object) - - (host.instance? ByteString host-object) - (ex.return (ByteString::decode [] (:coerce ByteString host-object))) - - (host.instance? DefaultTable host-object) - (let [host-object (:coerce DefaultTable host-object)] - (case (variant lux-object host-object) - (#.Some value) - (ex.return value) - - #.None - (case (array lux-object host-object) - (#.Some value) - (ex.return value) - - #.None - (ex.throw Unknown-Kind-Of-Host-Object (format "SECOND " (Object::toString [] (:coerce Object host-object))))))) - - ## else - (ex.throw Unknown-Kind-Of-Host-Object (format "FIRST " (Object::toString [] (:coerce Object host-object)))) - )) - -(def: #export (eval code) - (-> Expression (Meta Any)) - (function (_ compiler) - (let [interpreter (|> compiler (get@ #.host) (:coerce //.Host) (get@ #//.interpreter))] - (case (interpreter (format "return " code ";")) - (#e.Error error) - ((lang.throw Cannot-Evaluate error) compiler) - - (#e.Success output) - (case (lux-object (|> output - (:coerce (Array Object)) - (array.read +0) - maybe.assume)) - (#e.Success parsed-output) - (#e.Success [compiler parsed-output]) - - (#e.Error error) - ((lang.throw Cannot-Evaluate error) compiler)))))) diff --git a/new-luxc/source/luxc/lang/translation/lua/expression.jvm.lux b/new-luxc/source/luxc/lang/translation/lua/expression.jvm.lux deleted file mode 100644 index 6597364bb..000000000 --- a/new-luxc/source/luxc/lang/translation/lua/expression.jvm.lux +++ /dev/null @@ -1,88 +0,0 @@ -(.module: - lux - (lux (control [monad #+ do] - ["ex" exception #+ exception:] - ["p" parser]) - (data ["e" error] - text/format) - [macro] - (macro ["s" syntax])) - (luxc ["&" lang] - (lang [".L" variable #+ Variable Register] - [".L" extension] - ["ls" synthesis] - (host [lua #+ Lua Expression Statement]))) - [//] - (// [".T" runtime] - [".T" primitive] - [".T" structure] - [".T" reference] - [".T" function] - [".T" loop] - [".T" case] - [".T" procedure])) - -(template [] - [(exception: #export ( {message Text}) - message)] - - [Invalid-Function-Syntax] - [Unrecognized-Synthesis] - ) - -(def: #export (translate synthesis) - (-> ls.Synthesis (Meta Expression)) - (case synthesis - (^code []) - (:: macro.Monad wrap runtimeT.unit) - - (^code [(~ singleton)]) - (translate singleton) - - (^template [ ] - [_ ( value)] - ( value)) - ([#.Bit primitiveT.translate-bit] - [#.Nat primitiveT.translate-nat] - [#.Int primitiveT.translate-int] - [#.Rev primitiveT.translate-rev] - [#.Frac primitiveT.translate-frac] - [#.Text primitiveT.translate-text]) - - (^code ((~ [_ (#.Nat tag)]) (~ [_ (#.Bit last?)]) (~ valueS))) - (structureT.translate-variant translate tag last? valueS) - - (^code [(~+ members)]) - (structureT.translate-tuple translate members) - - (^ [_ (#.Form (list [_ (#.Int var)]))]) - (referenceT.translate-variable var) - - [_ (#.Identifier definition)] - (referenceT.translate-definition definition) - - (^code ("lux let" (~ [_ (#.Nat register)]) (~ inputS) (~ exprS))) - (caseT.translate-let translate register inputS exprS) - - (^code ("lux case" (~ inputS) (~ pathPS))) - (caseT.translate-case translate inputS pathPS) - - (^code ("lux function" (~ [_ (#.Nat arity)]) [(~+ environment)] (~ bodyS))) - (case (s.run environment (p.some s.int)) - (#e.Success environment) - (functionT.translate-function translate environment arity bodyS) - - _ - (&.throw Invalid-Function-Syntax (%code synthesis))) - - (^code ("lux call" (~ functionS) (~+ argsS))) - (functionT.translate-apply translate functionS argsS) - - (^code ((~ [_ (#.Text procedure)]) (~+ argsS))) - (procedureT.translate-procedure translate procedure argsS) - ## (do macro.Monad - ## [translation (extensionL.find-translation procedure)] - ## (translation argsS)) - - _ - (&.throw Unrecognized-Synthesis (%code synthesis)))) diff --git a/new-luxc/source/luxc/lang/translation/lua/function.jvm.lux b/new-luxc/source/luxc/lang/translation/lua/function.jvm.lux deleted file mode 100644 index 451e9dbb4..000000000 --- a/new-luxc/source/luxc/lang/translation/lua/function.jvm.lux +++ /dev/null @@ -1,82 +0,0 @@ -(.module: - lux - (lux (control [monad #+ do]) - (data [product] - [text] - text/format - (coll [list "list/" Functor])) - [macro]) - (luxc ["&" lang] - (lang ["ls" synthesis] - [".L" variable #+ Variable] - (host [lua #+ Lua Expression Statement]))) - [//] - (// [".T" reference] - [".T" loop] - [".T" runtime])) - -(def: #export (translate-apply translate functionS argsS+) - (-> (-> ls.Synthesis (Meta Expression)) ls.Synthesis (List ls.Synthesis) (Meta Expression)) - (do macro.Monad - [functionO (translate functionS) - argsO+ (monad.map @ translate argsS+)] - (wrap (lua.apply functionO argsO+)))) - -(def: (input-declaration register) - (lua.local! (referenceT.variable (inc register)) - (#.Some (lua.nth (|> register inc .int %i) "curried")))) - -(def: (with-closure function-name inits function-definition) - (-> Text (List Expression) Statement (Meta Expression)) - (let [closure-name (format function-name "___CLOSURE")] - (case inits - #.Nil - (do macro.Monad - [_ (//.save function-definition)] - (wrap function-name)) - - _ - (do macro.Monad - [_ (//.save (lua.function! closure-name - (|> (list.enumerate inits) - (list/map (|>> product.left referenceT.closure))) - (lua.block! (list function-definition - (lua.return! function-name)))))] - (wrap (lua.apply closure-name inits)))))) - -(def: #export (translate-function translate env arity bodyS) - (-> (-> ls.Synthesis (Meta Expression)) - (List Variable) ls.Arity ls.Synthesis - (Meta Expression)) - (do macro.Monad - [[function-name bodyO] (//.with-sub-context - (do @ - [function-name //.context] - (//.with-anchor [function-name +1] - (translate bodyS)))) - closureO+ (monad.map @ referenceT.translate-variable env) - #let [args-initsO+ (|> (list.n/range +0 (dec arity)) - (list/map input-declaration)) - selfO (lua.local! (referenceT.variable +0) (#.Some function-name)) - arityO (|> arity .int %i) - pack (|>> (list) (lua.apply "table.pack"))]] - (with-closure function-name closureO+ - (lua.function! function-name (list "...") - (lua.block! (list (lua.local! "curried" (#.Some (pack "..."))) - (lua.local! "num_args" (#.Some (lua.length "curried"))) - (lua.if! (lua.= arityO "num_args") - (lua.block! (list selfO - (lua.block! args-initsO+) - (lua.while! (lua.bool #1) - (lua.return! bodyO)))) - (let [unpack (|>> (list) (lua.apply "table.unpack")) - recur (|>> (list) (lua.apply function-name))] - (lua.if! (lua.> arityO "num_args") - (let [slice (function (_ from to) - (runtimeT.array//sub "curried" from to)) - arity-args (unpack (slice (lua.int 1) arityO)) - output-func-args (unpack (slice (lua.+ (lua.int 1) arityO) "num_args"))] - (lua.return! (lua.apply (recur arity-args) - (list output-func-args)))) - (lua.return! (lua.function (list "...") - (lua.return! (recur (unpack (runtimeT.array//concat "curried" (pack "...")))))))))))))))) diff --git a/new-luxc/source/luxc/lang/translation/lua/loop.jvm.lux b/new-luxc/source/luxc/lang/translation/lua/loop.jvm.lux deleted file mode 100644 index 4bad74069..000000000 --- a/new-luxc/source/luxc/lang/translation/lua/loop.jvm.lux +++ /dev/null @@ -1,35 +0,0 @@ -(.module: - lux - (lux (control [monad #+ do]) - (data [text] - text/format - (coll [list "list/" Functor])) - [macro]) - (luxc [lang] - (lang ["ls" synthesis] - (host [lua #+ Lua Expression Statement]))) - [//] - (// [".T" reference])) - -(def: #export (translate-loop translate offset initsS+ bodyS) - (-> (-> ls.Synthesis (Meta Expression)) Nat (List ls.Synthesis) ls.Synthesis - (Meta Expression)) - (do macro.Monad - [loop-name (:: @ map (|>> %code lang.normalize-name) - (macro.gensym "loop")) - initsO+ (monad.map @ translate initsS+) - bodyO (//.with-anchor [loop-name offset] - (translate bodyS)) - #let [registersO+ (|> (list.n/range +0 (dec (list.size initsS+))) - (list/map (|>> (n/+ offset) referenceT.variable)))] - _ (//.save (lua.function! loop-name registersO+ - (lua.return! bodyO)))] - (wrap (lua.apply loop-name initsO+)))) - -(def: #export (translate-recur translate argsS+) - (-> (-> ls.Synthesis (Meta Expression)) (List ls.Synthesis) - (Meta Expression)) - (do macro.Monad - [[loop-name offset] //.anchor - argsO+ (monad.map @ translate argsS+)] - (wrap (lua.apply loop-name argsO+)))) diff --git a/new-luxc/source/luxc/lang/translation/lua/primitive.jvm.lux b/new-luxc/source/luxc/lang/translation/lua/primitive.jvm.lux deleted file mode 100644 index 230498fcb..000000000 --- a/new-luxc/source/luxc/lang/translation/lua/primitive.jvm.lux +++ /dev/null @@ -1,34 +0,0 @@ -(.module: - lux - (lux (control pipe) - (data [number] - text/format) - [macro "meta/" Monad]) - (luxc (lang (host [lua #+ Lua Expression Statement])))) - -(def: #export translate-bit - (-> Bit (Meta Expression)) - (|>> lua.bool meta/wrap)) - -(def: #export translate-int - (-> Int (Meta Expression)) - (|>> lua.int meta/wrap)) - -(def: #export translate-frac - (-> Frac (Meta Expression)) - (|>> (cond> [(f/= number.positive-infinity)] - [(new> "math.huge")] - - [(f/= number.negative-infinity)] - [(new> "(-1 * math.huge)")] - - [(f/= number.not-a-number)] - [(new> "(0/0)")] - - ## else - [%f]) - meta/wrap)) - -(def: #export translate-text - (-> Text (Meta Expression)) - (|>> %t meta/wrap)) diff --git a/new-luxc/source/luxc/lang/translation/lua/procedure/common.jvm.lux b/new-luxc/source/luxc/lang/translation/lua/procedure/common.jvm.lux deleted file mode 100644 index 2f1b652e3..000000000 --- a/new-luxc/source/luxc/lang/translation/lua/procedure/common.jvm.lux +++ /dev/null @@ -1,374 +0,0 @@ -(.module: - lux - (lux (control [monad #+ do] - ["ex" exception #+ exception:] - ["p" parser]) - (data ["e" error] - [text] - text/format - (coll [list "list/" Functor] - (dictionary ["dict" unordered #+ Dict]))) - [macro #+ with-gensyms] - (macro [code] - ["s" syntax #+ syntax:]) - [host]) - (luxc ["&" lang] - (lang ["la" analysis] - ["ls" synthesis] - (host [lua #+ Lua Expression Statement]))) - [///] - (/// [".T" runtime] - [".T" case] - [".T" function] - [".T" loop])) - -## [Types] -(type: #export Translator - (-> ls.Synthesis (Meta Expression))) - -(type: #export Proc - (-> Translator (List ls.Synthesis) (Meta Expression))) - -(type: #export Bundle - (Dict Text Proc)) - -(syntax: (Vector {size s.nat} elemT) - (wrap (list (` [(~+ (list.repeat size elemT))])))) - -(type: #export Nullary (-> (Vector +0 Expression) Expression)) -(type: #export Unary (-> (Vector +1 Expression) Expression)) -(type: #export Binary (-> (Vector +2 Expression) Expression)) -(type: #export Trinary (-> (Vector +3 Expression) Expression)) -(type: #export Variadic (-> (List Expression) Expression)) - -## [Utils] -(def: #export (install name unnamed) - (-> Text (-> Text Proc) - (-> Bundle Bundle)) - (dict.put name (unnamed name))) - -(def: #export (prefix prefix bundle) - (-> Text Bundle Bundle) - (|> bundle - dict.entries - (list/map (function (_ [key val]) [(format prefix " " key) val])) - (dict.from-list text.Hash))) - -(def: (wrong-arity proc expected actual) - (-> Text Nat Nat Text) - (format "Wrong number of arguments for " (%t proc) "\n" - "Expected: " (|> expected .int %i) "\n" - " Actual: " (|> actual .int %i))) - -(syntax: (arity: {name s.local-identifier} {arity s.nat}) - (with-gensyms [g_ g!proc g!name g!translate g!inputs] - (do @ - [g!input+ (monad.seq @ (list.repeat arity (macro.gensym "input")))] - (wrap (list (` (def: #export ((~ (code.local-identifier name)) (~ g!proc)) - (-> (-> (..Vector (~ (code.nat arity)) Expression) Expression) - (-> Text ..Proc)) - (function ((~ g_ ) (~ g!name)) - (function ((~ g_ ) (~ g!translate) (~ g!inputs)) - (case (~ g!inputs) - (^ (list (~+ g!input+))) - (do macro.Monad - [(~+ (|> g!input+ - (list/map (function (_ g!input) - (list g!input (` ((~ g!translate) (~ g!input)))))) - list.concat))] - ((~' wrap) ((~ g!proc) [(~+ g!input+)]))) - - (~' _) - (macro.fail (wrong-arity (~ g!name) +1 (list.size (~ g!inputs)))))))))))))) - -(arity: nullary +0) -(arity: unary +1) -(arity: binary +2) -(arity: trinary +3) - -(def: #export (variadic proc) - (-> Variadic (-> Text Proc)) - (function (_ proc-name) - (function (_ translate inputsS) - (do macro.Monad - [inputsI (monad.map @ translate inputsS)] - (wrap (proc inputsI)))))) - -## [Procedures] -## [[Lux]] -(def: (lux//is [leftO rightO]) - Binary - (lua.= leftO rightO)) - -(def: (lux//if [testO thenO elseO]) - Trinary - (caseT.translate-if testO thenO elseO)) - -(def: (lux//try riskyO) - Unary - (runtimeT.lux//try riskyO)) - -(exception: #export (Wrong-Syntax {message Text}) - message) - -(def: #export (wrong-syntax procedure args) - (-> Text (List ls.Synthesis) Text) - (format "Procedure: " procedure "\n" - "Arguments: " (%code (code.tuple args)))) - -(def: lux//loop - (-> Text Proc) - (function (_ proc-name) - (function (_ translate inputsS) - (case (s.run inputsS ($_ p.seq s.nat (s.tuple (p.many s.any)) s.any)) - (#e.Success [offset initsS+ bodyS]) - (loopT.translate-loop translate offset initsS+ bodyS) - - (#e.Error error) - (&.throw Wrong-Syntax (wrong-syntax proc-name inputsS))) - ))) - -(def: lux//recur - (-> Text Proc) - (function (_ proc-name) - (function (_ translate inputsS) - (loopT.translate-recur translate inputsS)))) - -## [[Bits]] -(template [ ] - [(def: ( [subjectO paramO]) - Binary - ( paramO subjectO))] - - [bit//and lua.bit-and] - [bit//or lua.bit-or] - [bit//xor lua.bit-xor] - ) - -(template [ ] - [(def: ( [subjectO paramO]) - Binary - ( paramO subjectO))] - - [bit//left-shift lua.bit-shl] - [bit//arithmetic-right-shift lua.bit-shr] - [bit//logical-right-shift runtimeT.bit//logical-right-shift] - ) - -## [[Numbers]] -(host.import: java/lang/Double - (#static MIN_VALUE Double) - (#static MAX_VALUE Double) - (#static NaN Double) - (#static POSITIVE_INFINITY Double) - (#static NEGATIVE_INFINITY Double)) - -(template [ ] - [(def: ( _) - Nullary - ( ))] - - [frac//smallest Double::MIN_VALUE lua.float] - [frac//min (f/* -1.0 Double::MAX_VALUE) lua.float] - [frac//max Double::MAX_VALUE lua.float] - ) - -(template [ ] - [(def: ( [subjectO paramO]) - Binary - ( paramO subjectO))] - - [int//add lua.+] - [int//sub lua.-] - [int//mul lua.*] - [int//div lua.//] - [int//rem lua.%] - ) - -(template [ ] - [(def: ( [subjectO paramO]) - Binary - ( paramO subjectO))] - - [frac//add lua.+] - [frac//sub lua.-] - [frac//mul lua.*] - [frac//div lua./] - [frac//rem lua.%] - [frac//= lua.=] - [frac//< lua.<] - - [text//= lua.=] - [text//< lua.<] - ) - -(template [ ] - [(def: ( [subjectO paramO]) - Binary - ( paramO subjectO))] - - [int//= lua.=] - [int//< lua.<] - ) - -(def: frac//encode - Unary - (|>> (list) (lua.apply "tostring"))) - -(def: (frac//decode inputO) - Unary - (lux//try (lua.function (list) - (lua.return! (lua.apply "tonumber" (list inputO)))))) - -(template [ ] - [(def: ( inputO) - Unary - (lua./ inputO))] - - [int//to-frac (lua.float 1.0)] - ) - -(template [ ] - [(def: ( inputO) - Unary - (|> inputO ))] - - [frac//to-int (<| (lua.apply "math.floor") (list))] - ) - -(def: int//char - Unary - (|>> (list) (lua.apply "string.char"))) - -## [[Text]] -(template [ ] - [(def: - Unary - (|>> (list) (lua.apply )))] - - [text//size "string.len"] - ) - -(def: (text//concat [subjectO paramO]) - Binary - (format "(" subjectO " .. " paramO ")")) - -(def: (text//char [subjectO paramO]) - Binary - (runtimeT.text//char subjectO paramO)) - -(template [ ] - [(def: ( [subjectO paramO extraO]) - Trinary - ( subjectO paramO extraO))] - - [text//clip runtimeT.text//clip] - [text//index runtimeT.text//index] - ) - -## [[IO]] -(def: (io//log messageO) - Unary - (lua.or (lua.apply "print" (list messageO)) - runtimeT.unit)) - -(def: io//error - Unary - lua.error) - -(def: io//exit - Unary - (|>> (list) (lua.apply "os.exit"))) - -(def: (io//current-time []) - Nullary - (|> (lua.apply "os.time" (list)) - (lua.* (lua.int 1,000)))) - -## [Bundles] -(def: lux-procs - Bundle - (|> (dict.new text.Hash) - (install "is" (binary lux//is)) - (install "try" (unary lux//try)) - (install "if" (trinary lux//if)) - (install "loop" lux//loop) - (install "recur" lux//recur) - )) - -(def: bit-procs - Bundle - (<| (prefix "bit") - (|> (dict.new text.Hash) - (install "and" (binary bit//and)) - (install "or" (binary bit//or)) - (install "xor" (binary bit//xor)) - (install "left-shift" (binary bit//left-shift)) - (install "logical-right-shift" (binary bit//logical-right-shift)) - (install "arithmetic-right-shift" (binary bit//arithmetic-right-shift)) - ))) - -(def: int-procs - Bundle - (<| (prefix "int") - (|> (dict.new text.Hash) - (install "+" (binary int//add)) - (install "-" (binary int//sub)) - (install "*" (binary int//mul)) - (install "/" (binary int//div)) - (install "%" (binary int//rem)) - (install "=" (binary int//=)) - (install "<" (binary int//<)) - (install "to-frac" (unary int//to-frac)) - (install "char" (unary int//char))))) - -(def: frac-procs - Bundle - (<| (prefix "frac") - (|> (dict.new text.Hash) - (install "+" (binary frac//add)) - (install "-" (binary frac//sub)) - (install "*" (binary frac//mul)) - (install "/" (binary frac//div)) - (install "%" (binary frac//rem)) - (install "=" (binary frac//=)) - (install "<" (binary frac//<)) - (install "smallest" (nullary frac//smallest)) - (install "min" (nullary frac//min)) - (install "max" (nullary frac//max)) - (install "to-int" (unary frac//to-int)) - (install "encode" (unary frac//encode)) - (install "decode" (unary frac//decode))))) - -(def: text-procs - Bundle - (<| (prefix "text") - (|> (dict.new text.Hash) - (install "=" (binary text//=)) - (install "<" (binary text//<)) - (install "concat" (binary text//concat)) - (install "index" (trinary text//index)) - (install "size" (unary text//size)) - (install "char" (binary text//char)) - (install "clip" (trinary text//clip)) - ))) - -(def: io-procs - Bundle - (<| (prefix "io") - (|> (dict.new text.Hash) - (install "log" (unary io//log)) - (install "error" (unary io//error)) - (install "exit" (unary io//exit)) - (install "current-time" (nullary io//current-time))))) - -(def: #export procedures - Bundle - (<| (prefix "lux") - (|> lux-procs - (dict.merge bit-procs) - (dict.merge int-procs) - (dict.merge frac-procs) - (dict.merge text-procs) - (dict.merge io-procs) - ))) diff --git a/new-luxc/source/luxc/lang/translation/lua/procedure/host.jvm.lux b/new-luxc/source/luxc/lang/translation/lua/procedure/host.jvm.lux deleted file mode 100644 index f53f3ba05..000000000 --- a/new-luxc/source/luxc/lang/translation/lua/procedure/host.jvm.lux +++ /dev/null @@ -1,87 +0,0 @@ -(.module: - lux - (lux (control [monad #+ do]) - (data [text] - text/format - (coll [list "list/" Functor] - (dictionary ["dict" unordered #+ Dict]))) - [macro "macro/" Monad]) - (luxc ["&" lang] - (lang ["la" analysis] - ["ls" synthesis] - (host [lua #+ Lua Expression Statement]))) - [///] - (/// [".T" runtime]) - (// ["@" common])) - -(template [ ] - [(def: ( _) @.Nullary )] - - [lua//nil "nil"] - [lua//table "{}"] - ) - -(def: (lua//global proc translate inputs) - (-> Text @.Proc) - (case inputs - (^ (list [_ (#.Text name)])) - (do macro.Monad - [] - (wrap name)) - - _ - (&.throw @.Wrong-Syntax (@.wrong-syntax proc inputs)))) - -(def: (lua//call proc translate inputs) - (-> Text @.Proc) - (case inputs - (^ (list& functionS argsS+)) - (do macro.Monad - [functionO (translate functionS) - argsO+ (monad.map @ translate argsS+)] - (wrap (lua.apply functionO argsO+))) - - _ - (&.throw @.Wrong-Syntax (@.wrong-syntax proc inputs)))) - -(def: lua-procs - @.Bundle - (|> (dict.new text.Hash) - (@.install "nil" (@.nullary lua//nil)) - (@.install "table" (@.nullary lua//table)) - (@.install "global" lua//global) - (@.install "call" lua//call))) - -(def: (table//call proc translate inputs) - (-> Text @.Proc) - (case inputs - (^ (list& tableS [_ (#.Text field)] argsS+)) - (do macro.Monad - [tableO (translate tableS) - argsO+ (monad.map @ translate argsS+)] - (wrap (lua.method field tableO argsO+))) - - _ - (&.throw @.Wrong-Syntax (@.wrong-syntax proc inputs)))) - -(def: (table//get [fieldO tableO]) - @.Binary - (runtimeT.lua//get tableO fieldO)) - -(def: (table//set [fieldO valueO tableO]) - @.Trinary - (runtimeT.lua//set tableO fieldO valueO)) - -(def: table-procs - @.Bundle - (<| (@.prefix "table") - (|> (dict.new text.Hash) - (@.install "call" table//call) - (@.install "get" (@.binary table//get)) - (@.install "set" (@.trinary table//set))))) - -(def: #export procedures - @.Bundle - (<| (@.prefix "lua") - (|> lua-procs - (dict.merge table-procs)))) diff --git a/new-luxc/source/luxc/lang/translation/lua/reference.jvm.lux b/new-luxc/source/luxc/lang/translation/lua/reference.jvm.lux deleted file mode 100644 index ea3f8e604..000000000 --- a/new-luxc/source/luxc/lang/translation/lua/reference.jvm.lux +++ /dev/null @@ -1,36 +0,0 @@ -(.module: - lux - (lux [macro] - (data [text] - text/format)) - (luxc ["&" lang] - (lang [".L" variable #+ Variable Register] - (host [lua #+ Lua Expression Statement]))) - [//] - (// [".T" runtime])) - -(template [ ] - [(def: #export ( register) - (-> Register Expression) - (format (%i (.int register)))) - - (def: #export ( register) - (-> Register (Meta Expression)) - (:: macro.Monad wrap ( register)))] - - [closure translate-captured "c"] - [variable translate-local "v"]) - -(def: #export (translate-variable var) - (-> Variable (Meta Expression)) - (if (variableL.captured? var) - (translate-captured (variableL.captured-register var)) - (translate-local (.nat var)))) - -(def: #export global - (-> Name Expression) - //.definition-name) - -(def: #export (translate-definition name) - (-> Name (Meta Expression)) - (:: macro.Monad wrap (global name))) diff --git a/new-luxc/source/luxc/lang/translation/lua/runtime.jvm.lux b/new-luxc/source/luxc/lang/translation/lua/runtime.jvm.lux deleted file mode 100644 index ce9c37db5..000000000 --- a/new-luxc/source/luxc/lang/translation/lua/runtime.jvm.lux +++ /dev/null @@ -1,293 +0,0 @@ -(.module: - lux - (lux (control ["p" parser "p/" Monad] - [monad #+ do]) - (data text/format - (coll [list "list/" Monad])) - [macro] - (macro [code] - ["s" syntax #+ syntax:]) - [io #+ Process]) - [//] - (luxc [lang] - (lang (host [lua #+ Lua Expression Statement])))) - -(def: prefix Text "LuxRuntime") - -(def: #export unit Expression (%t //.unit)) - -(def: (flag value) - (-> Bit Lua) - (if value - (lua.string "") - lua.nil)) - -(def: (variant' tag last? value) - (-> Expression Expression Expression Expression) - (lua.table (list [//.variant-tag-field tag] - [//.variant-flag-field last?] - [//.variant-value-field value]))) - -(def: #export (variant tag last? value) - (-> Nat Bit Expression Expression) - (variant' (%i (.int tag)) (flag last?) value)) - -(def: none - Expression - (variant +0 #0 unit)) - -(def: some - (-> Expression Expression) - (variant +1 #1)) - -(def: left - (-> Expression Expression) - (variant +0 #0)) - -(def: right - (-> Expression Expression) - (variant +1 #1)) - -(type: Runtime Lua) - -(def: declaration - (s.Syntax [Text (List Text)]) - (p.either (p.seq s.local-identifier (p/wrap (list))) - (s.form (p.seq s.local-identifier (p.some s.local-identifier))))) - -(syntax: (runtime: {[name args] declaration} - definition) - (let [implementation (code.local-identifier (format "@@" name)) - runtime (code.text (format "__" prefix "__" (lang.normalize-name name))) - argsC+ (list/map code.local-identifier args) - argsLC+ (list/map (|>> lang.normalize-name code.text) args) - declaration (` ((~ (code.local-identifier name)) - (~+ argsC+))) - type (` (-> (~+ (list.repeat (list.size argsC+) (` lua.Lua))) - lua.Lua))] - (wrap (list (` (def: #export (~ declaration) - (~ type) - (lua.apply (~ runtime) (list (~+ argsC+))))) - (` (def: (~ implementation) - Lua - (~ (case argsC+ - #.Nil - (` (lua.global! (~ runtime) (#.Some (~ definition)))) - - _ - (` (let [(~' @) (~ runtime) - (~+ (|> (list.zip2 argsC+ argsLC+) - (list/map (function (_ [left right]) (list left right))) - list/join))] - (lua.function! (~ runtime) (list (~+ argsLC+)) - (~ definition)))))))))))) - -(runtime: (array//copy array) - (lua.block! (list (lua.local! "temp" (#.Some (lua.array (list)))) - (lua.for-step! "idx" (lua.int 1) (lua.length array) (lua.int 1) - (lua.apply "table.insert" (list "temp" (lua.nth "idx" array)))) - (lua.return! "temp")))) - -(runtime: (array//sub array from to) - (lua.block! (list (lua.local! "temp" (#.Some (lua.array (list)))) - (lua.for-step! "idx" from to (lua.int 1) - (lua.apply "table.insert" (list "temp" (lua.nth "idx" array)))) - (lua.return! "temp")))) - -(runtime: (array//concat left right) - (let [copy! (function (_ input output) - (lua.for-step! "idx" (lua.int 1) (format input ".n") (lua.int 1) - (lua.apply "table.insert" (list output (lua.nth "idx" input)))))] - (lua.block! (list (lua.local! "temp" (#.Some (lua.array (list)))) - (copy! left "temp") - (copy! right "temp") - (lua.return! "temp"))))) - -(runtime: (lux//try op) - (lua.block! (list (format "local success, value = " (lua.apply "pcall" (list (lua.function (list) (lua.return! (lua.apply op (list unit)))))) ";") - (lua.if! "success" - (lua.return! (right "value")) - (lua.return! (left "value")))))) - -(runtime: (lux//program-args program-args) - (lua.block! (list (lua.local! "inputs" (#.Some none)) - (lua.for-step! "idx" (lua.length program-args) (lua.int 1) (lua.int -1) - (lua.set! "inputs" (some (lua.array (list (lua.nth "idx" program-args) - "inputs"))))) - (lua.return! "inputs")))) - -(def: runtime//lux - Runtime - (format @@lux//try - @@lux//program-args)) - -(runtime: (product//left product index) - (lua.block! (list (lua.local! "index_min_length" (#.Some (lua.+ (lua.int 1) index))) - (lua.if! (lua.>= "index_min_length" (lua.length product)) - ## No need for recursion - (lua.return! (lua.nth "index_min_length" product)) - ## Needs recursion - (lua.return! (product//left (lua.nth (lua.length product) - product) - (lua.- (lua.length product) - "index_min_length"))))))) - -(runtime: (product//right product index) - (lua.block! (list (lua.local! "index_min_length" (#.Some (lua.+ (lua.int 1) index))) - (lua.cond! (list [(lua.= "index_min_length" (lua.length product)) - ## Last element. - (lua.return! (lua.nth "index_min_length" product))] - [(lua.< "index_min_length" (lua.length product)) - ## Needs recursion - (lua.return! (product//right (lua.nth (lua.length product) - product) - (lua.- (lua.length product) - "index_min_length")))]) - ## Must slice - (lua.return! (array//sub product "index_min_length" (lua.length product))))))) - -(runtime: (sum//get sum wantedTag wantsLast) - (let [no-match! (lua.return! lua.nil) - sum-tag (format "sum." //.variant-tag-field) - sum-flag (format "sum." //.variant-flag-field) - sum-value (format "sum." //.variant-value-field) - is-last? (lua.= (lua.string "") sum-flag) - test-recursion! (lua.if! is-last? - ## Must recurse. - (lua.return! (sum//get sum-value (lua.- sum-tag wantedTag) wantsLast)) - no-match!)] - (lua.cond! (list [(lua.= sum-tag wantedTag) - (lua.if! (lua.= wantsLast sum-flag) - (lua.return! sum-value) - test-recursion!)] - - [(lua.> sum-tag wantedTag) - test-recursion!] - - [(lua.and (lua.< sum-tag wantedTag) - (lua.= (lua.string "") wantsLast)) - (lua.return! (variant' (lua.- wantedTag sum-tag) sum-flag sum-value))]) - - no-match!))) - -(def: runtime//adt - Runtime - (format @@product//left - @@product//right - @@sum//get)) - -(runtime: (bit//logical-right-shift param subject) - (let [mask (|> (lua.int 1) - (lua.bit-shl (lua.- param (lua.int 64))) - (lua.- (lua.int 1)))] - (lua.return! (|> subject - (lua.bit-shr param) - (lua.bit-and mask))))) - -(def: runtime//bit - Runtime - @@bit//logical-right-shift) - -(runtime: (text//index subject param start) - (lua.block! (list (lua.local! "idx" (#.Some (lua.apply "string.find" (list subject param start (lua.bool #1))))) - (lua.if! (lua.= lua.nil "idx") - (lua.return! none) - (lua.return! (some "idx")))))) - -(runtime: (text//clip text from to) - (lua.block! (list (lua.local! "size" (#.Some (lua.apply "string.len" (list text)))) - (lua.if! (lua.or (lua.> "size" from) - (lua.> "size" to)) - (lua.return! none) - (lua.return! (some (lua.apply "string.sub" (list text from to)))))))) - -(runtime: (text//char text idx) - (lua.block! (list (lua.local! "char" (#.Some (lua.apply "string.byte" (list text idx)))) - (lua.if! (lua.= lua.nil "char") - (lua.return! none) - (lua.return! (some "char")))))) - -(def: runtime//text - Runtime - (format @@text//index - @@text//clip - @@text//char)) - -(def: (check-index-out-of-bounds array idx body!) - (-> Expression Expression Statement Statement) - (lua.if! (lua.<= (lua.length array) - idx) - body! - (lua.error (lua.string "Array index out of bounds!")))) - -(runtime: (array//new size) - (lua.block! (list (lua.local! "output" (#.Some (lua.array (list)))) - (lua.for-step! "idx" (lua.int 1) size (lua.int 1) - (lua.apply "table.insert" (list "output" unit))) - (lua.return! "output")))) - -(runtime: (array//get array idx) - (<| (check-index-out-of-bounds array idx) - (lua.block! (list (lua.local! "temp" (#.Some (lua.nth idx array))) - (lua.if! (lua.or (lua.= lua.nil "temp") - (lua.= unit "temp")) - (lua.return! none) - (lua.return! (some "temp"))))))) - -(runtime: (array//put array idx value) - (<| (check-index-out-of-bounds array idx) - (lua.block! (list (lua.set! (lua.nth idx array) value) - (lua.return! array))))) - -(def: runtime//array - Runtime - (format @@array//sub - @@array//concat - @@array//copy - @@array//new - @@array//get - @@array//put - )) - -(runtime: (box//write value box) - (lua.block! (list (lua.set! (lua.nth (lua.int 0) box) - value) - (lua.return! unit)))) - -(def: runtime//box - Runtime - (format @@box//write)) - -(runtime: (lua//get object field) - (lua.block! (list (lua.local! "value" (#.Some (lua.nth field object))) - (lua.if! (lua.= lua.nil "value") - (lua.return! none) - (lua.return! (some "value")))))) - -(runtime: (lua//set object field value) - (lua.block! (list (lua.set! (lua.nth field object) value) - (lua.return! object)))) - -(def: runtime//lua - Runtime - (format @@lua//get - @@lua//set)) - -(def: runtime - Runtime - (format runtime//lux - runtime//adt - runtime//bit - runtime//text - runtime//array - runtime//box - runtime//lua)) - -(def: #export artifact Text (format prefix ".lua")) - -(def: #export translate - (Meta (Process Any)) - (do macro.Monad - [_ //.init-module-buffer - _ (//.save runtime)] - (//.save-module! artifact))) diff --git a/new-luxc/source/luxc/lang/translation/lua/statement.jvm.lux b/new-luxc/source/luxc/lang/translation/lua/statement.jvm.lux deleted file mode 100644 index 9c0181c1b..000000000 --- a/new-luxc/source/luxc/lang/translation/lua/statement.jvm.lux +++ /dev/null @@ -1,48 +0,0 @@ -(.module: - lux - (lux (control [monad #+ do]) - [macro] - (data text/format)) - (luxc (lang [".L" module] - (host [lua #+ Lua Expression Statement]))) - [//] - (// [".T" runtime] - [".T" reference] - [".T" eval])) - -(def: #export (translate-def name expressionT expressionO metaV) - (-> Text Type Expression Code (Meta Any)) - (do macro.Monad - [current-module macro.current-module-name - #let [def-name [current-module name]]] - (case (macro.get-identifier-ann (name-of #.alias) metaV) - (#.Some real-def) - (do @ - [[realT realA realV] (macro.find-def real-def) - _ (moduleL.define def-name [realT metaV realV])] - (wrap [])) - - _ - (do @ - [#let [def-name (referenceT.global def-name)] - _ (//.save (lua.global! def-name (#.Some expressionO))) - expressionV (evalT.eval def-name) - _ (moduleL.define def-name [expressionT metaV expressionV]) - _ (if (macro.type? metaV) - (case (macro.declared-tags metaV) - #.Nil - (wrap []) - - tags - (moduleL.declare-tags tags (macro.export? metaV) (:coerce Type expressionV))) - (wrap [])) - #let [_ (log! (format "DEF " (%name def-name)))]] - (wrap [])) - ))) - -(def: #export (translate-program programO) - (-> Expression (Meta Statement)) - (macro.fail "translate-program NOT IMPLEMENTED YET") - ## (hostT.save (format "var " (referenceT.variable +0) " = " runtimeT.lux//program-args "();" - ## "(" programO ")(null);")) - ) diff --git a/new-luxc/source/luxc/lang/translation/lua/structure.jvm.lux b/new-luxc/source/luxc/lang/translation/lua/structure.jvm.lux deleted file mode 100644 index b6eeaa013..000000000 --- a/new-luxc/source/luxc/lang/translation/lua/structure.jvm.lux +++ /dev/null @@ -1,31 +0,0 @@ -(.module: - lux - (lux (control [monad #+ do]) - (data [text] - text/format) - [macro]) - (luxc ["&" lang] - (lang [synthesis #+ Synthesis] - (host [js #+ JS Expression Statement]))) - [//] - (// [".T" runtime])) - -(def: #export (translate-tuple translate elemsS+) - (-> (-> Synthesis (Meta Expression)) (List Synthesis) (Meta Expression)) - (case elemsS+ - #.Nil - (:: macro.Monad wrap runtimeT.unit) - - (#.Cons singletonS #.Nil) - (translate singletonS) - - _ - (do macro.Monad - [elemsT+ (monad.map @ translate elemsS+)] - (wrap (format "{" (text.join-with "," elemsT+) "}"))))) - -(def: #export (translate-variant translate tag tail? valueS) - (-> (-> Synthesis (Meta Expression)) Nat Bit Synthesis (Meta Expression)) - (do macro.Monad - [valueT (translate valueS)] - (wrap (runtimeT.variant tag tail? valueT)))) -- cgit v1.2.3