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 -------------------------- 1 file changed, 231 deletions(-) delete mode 100644 new-luxc/source/luxc/lang/translation/lua.lux (limited to 'new-luxc/source/luxc/lang/translation/lua.lux') 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))))) -- cgit v1.2.3