diff options
Diffstat (limited to 'new-luxc/source/luxc/lang/translation/lua/procedure')
-rw-r--r-- | new-luxc/source/luxc/lang/translation/lua/procedure/common.jvm.lux | 640 | ||||
-rw-r--r-- | new-luxc/source/luxc/lang/translation/lua/procedure/host.jvm.lux | 87 |
2 files changed, 727 insertions, 0 deletions
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 new file mode 100644 index 000000000..2b277dec4 --- /dev/null +++ b/new-luxc/source/luxc/lang/translation/lua/procedure/common.jvm.lux @@ -0,0 +1,640 @@ +(.module: + lux + (lux (control [monad #+ do] + ["ex" exception #+ exception:] + ["p" parser]) + (data ["e" error] + [text] + text/format + (coll [list "list/" Functor<List>] + [dict #+ 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<Text>))) + +(def: (wrong-arity proc expected actual) + (-> Text Nat Nat Text) + (format "Wrong number of arguments for " (%t proc) "\n" + "Expected: " (|> expected nat-to-int %i) "\n" + " Actual: " (|> actual nat-to-int %i))) + +(syntax: (arity: [name s.local-symbol] [arity s.nat]) + (with-gensyms [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-symbol name)) (~ g!proc)) + (-> (-> (..Vector (~ (code.nat arity)) Expression) Expression) + (-> Text ..Proc)) + (function [(~ g!name)] + (function [(~ g!translate) (~ g!inputs)] + (case (~ g!inputs) + (^ (list (~+ g!input+))) + (do macro.Monad<Meta> + [(~+ (|> 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<Meta> + [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)) + +(def: (lux//noop valueO) + Unary + valueO) + +(exception: #export Wrong-Syntax) +(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]] +(do-template [<name> <op>] + [(def: (<name> [subjectO paramO]) + Binary + (<op> paramO subjectO))] + + [bit//and lua.bit-and] + [bit//or lua.bit-or] + [bit//xor lua.bit-xor] + ) + +(do-template [<name> <op>] + [(def: (<name> [subjectO paramO]) + Binary + (<op> paramO subjectO))] + + [bit//shift-left lua.bit-shl] + [bit//shift-right lua.bit-shr] + [bit//unsigned-shift-right runtimeT.bit//shift-right] + ) + +(def: bit//count + Unary + runtimeT.bit//count) + +## [[Arrays]] +(def: (array//new sizeO) + Unary + (runtimeT.array//new sizeO)) + +(def: (array//get [arrayO idxO]) + Binary + (runtimeT.array//get arrayO idxO)) + +(def: (array//put [arrayO idxO elemO]) + Trinary + (runtimeT.array//put arrayO idxO elemO)) + +(def: (array//remove [arrayO idxO]) + Binary + (runtimeT.array//remove arrayO idxO)) + +(def: array//size + Unary + lua.length) + +## [[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)) + +(do-template [<name> <const> <encode>] + [(def: (<name> _) + Nullary + (<encode> <const>))] + + [nat//min 0 lua.int] + [nat//max -1 lua.int] + + [frac//smallest Double::MIN_VALUE lua.float] + [frac//min (f/* -1.0 Double::MAX_VALUE) lua.float] + [frac//max Double::MAX_VALUE lua.float] + + [deg//min 0 lua.int] + [deg//max -1 lua.int] + ) + +(do-template [<name> <expression>] + [(def: (<name> _) + Nullary + <expression>)] + + [int//min "math.mininteger"] + [int//max "math.maxinteger"] + ) + +(do-template [<name> <expression>] + [(def: (<name> _) + Nullary + <expression>)] + + [frac//not-a-number (lua./ (lua.int 0) (lua.int 0))] + [frac//positive-infinity "math.huge"] + [frac//negative-infinity (lua.* (lua.int -1) "math.huge")] + ) + +(do-template [<name> <op>] + [(def: (<name> [subjectO paramO]) + Binary + (<op> paramO subjectO))] + + [int//add lua.+] + [int//sub lua.-] + [int//mul lua.*] + [int//div lua.//] + [int//rem lua.%] + + [nat//add lua.+] + [nat//sub lua.-] + [nat//mul lua.*] + [nat//div runtimeT.nat///] + [nat//rem runtimeT.nat//%] + + [deg//add lua.+] + [deg//sub lua.-] + [deg//mul runtimeT.deg//*] + [deg//div runtimeT.deg///] + [deg//rem lua.-] + [deg//scale lua.*] + [deg//reciprocal lua.//] + ) + +(do-template [<name> <op>] + [(def: (<name> [subjectO paramO]) + Binary + (<op> 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.<] + ) + +(do-template [<name> <cmp>] + [(def: (<name> [subjectO paramO]) + Binary + (<cmp> paramO subjectO))] + + [nat//= lua.=] + [nat//< runtimeT.nat//<] + [int//= lua.=] + [int//< lua.<] + [deg//= lua.=] + [deg//< runtimeT.nat//<] + ) + +(do-template [<name>] + [(def: (<name> inputO) + Unary + inputO)] + + [nat//to-int] + [int//to-nat] + ) + +(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)))))) + +(do-template [<name> <divisor>] + [(def: (<name> inputO) + Unary + (lua./ <divisor> inputO))] + + [int//to-frac (lua.float 1.0)] + [deg//to-frac (lua.bit-shl (lua.int 32) (lua.int 1))] + ) + +(do-template [<name> <transform>] + [(def: (<name> inputO) + Unary + (|> inputO <transform>))] + + [frac//to-int (<| (lua.apply "math.floor") (list))] + [frac//to-deg runtimeT.deg//from-frac] + [text//hash runtimeT.text//hash] + ) + +(def: nat//char + Unary + (|>> (list) (lua.apply "string.char"))) + +## [[Text]] +(do-template [<name> <op>] + [(def: <name> + Unary + (|>> (list) (lua.apply <op>)))] + + [text//size "string.len"] + [text//upper "string.upper"] + [text//lower "string.lower"] + ) + +(def: (text//trim inputO) + Unary + (lua.apply "string.match" (list inputO "^%s*(.-)%s*$"))) + +(def: (text//concat [subjectO paramO]) + Binary + (format "(" subjectO " .. " paramO ")")) + +(def: (text//contains? [subjectO paramO]) + Binary + (|> (lua.apply "string.find" (list subjectO paramO (lua.int 1) (lua.bool true))) + (lua.= lua.nil) + lua.not)) + +(def: (text//char [subjectO paramO]) + Binary + (runtimeT.text//char subjectO paramO)) + +(do-template [<name> <runtime>] + [(def: (<name> [subjectO paramO extraO]) + Trinary + (<runtime> subjectO paramO extraO))] + + [text//clip runtimeT.text//clip] + [text//replace-all runtimeT.text//replace-all] + [text//replace-once runtimeT.text//replace-once] + ) + +(def: (text//index [textO partO startO]) + Trinary + (runtimeT.text//index textO partO startO)) + +## [[Math]] +(do-template [<name> <method>] + [(def: (<name> inputO) + Unary + (lua.apply <method> (list inputO)))] + + [math//cos "math.cos"] + [math//sin "math.sin"] + [math//tan "math.tan"] + [math//acos "math.acos"] + [math//asin "math.asin"] + [math//atan "math.atan"] + [math//exp "math.exp"] + [math//log "math.log"] + [math//ceil "math.ceil"] + [math//floor "math.floor"] + ) + +(def: (math//pow [inputO paramO]) + Binary + (lua.apply "math.pow" (list inputO paramO))) + +## [[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)))) + +## [[Atoms]] +(def: atom//new + Unary + (|>> [runtimeT.atom//field] (list) lua.table)) + +(def: atom//read + Unary + (lua.nth (lua.string runtimeT.atom//field))) + +(def: (atom//compare-and-swap [atomO oldO newO]) + Trinary + (runtimeT.atom//compare-and-swap atomO oldO newO)) + +## [[Box]] +(def: box//new + Unary + (|>> (list) lua.array)) + +(def: box//read + Unary + (lua.nth (lua.int 1))) + +(def: (box//write [valueO boxO]) + Binary + (runtimeT.box//write valueO boxO)) + +## [[Processes]] +(def: (process//concurrency-level []) + Nullary + (lua.int 1)) + +(def: process//future + Unary + runtimeT.process//future) + +(def: (process//schedule [milli-secondsO procedureO]) + Binary + (runtimeT.process//schedule milli-secondsO procedureO)) + +## [Bundles] +(def: lux-procs + Bundle + (|> (dict.new text.Hash<Text>) + (install "noop" (unary lux//noop)) + (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<Text>) + (install "count" (unary bit//count)) + (install "and" (binary bit//and)) + (install "or" (binary bit//or)) + (install "xor" (binary bit//xor)) + (install "shift-left" (binary bit//shift-left)) + (install "unsigned-shift-right" (binary bit//unsigned-shift-right)) + (install "shift-right" (binary bit//shift-right)) + ))) + +(def: nat-procs + Bundle + (<| (prefix "nat") + (|> (dict.new text.Hash<Text>) + (install "+" (binary nat//add)) + (install "-" (binary nat//sub)) + (install "*" (binary nat//mul)) + (install "/" (binary nat//div)) + (install "%" (binary nat//rem)) + (install "=" (binary nat//=)) + (install "<" (binary nat//<)) + (install "min" (nullary nat//min)) + (install "max" (nullary nat//max)) + (install "to-int" (unary nat//to-int)) + (install "char" (unary nat//char))))) + +(def: int-procs + Bundle + (<| (prefix "int") + (|> (dict.new text.Hash<Text>) + (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 "min" (nullary int//min)) + (install "max" (nullary int//max)) + (install "to-nat" (unary int//to-nat)) + (install "to-frac" (unary int//to-frac))))) + +(def: deg-procs + Bundle + (<| (prefix "deg") + (|> (dict.new text.Hash<Text>) + (install "+" (binary deg//add)) + (install "-" (binary deg//sub)) + (install "*" (binary deg//mul)) + (install "/" (binary deg//div)) + (install "%" (binary deg//rem)) + (install "=" (binary deg//=)) + (install "<" (binary deg//<)) + (install "scale" (binary deg//scale)) + (install "reciprocal" (binary deg//reciprocal)) + (install "min" (nullary deg//min)) + (install "max" (nullary deg//max)) + (install "to-frac" (unary deg//to-frac))))) + +(def: frac-procs + Bundle + (<| (prefix "frac") + (|> (dict.new text.Hash<Text>) + (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 "not-a-number" (nullary frac//not-a-number)) + (install "positive-infinity" (nullary frac//positive-infinity)) + (install "negative-infinity" (nullary frac//negative-infinity)) + (install "to-deg" (unary frac//to-deg)) + (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<Text>) + (install "=" (binary text//=)) + (install "<" (binary text//<)) + (install "concat" (binary text//concat)) + (install "index" (trinary text//index)) + (install "size" (unary text//size)) + (install "hash" (unary text//hash)) + (install "replace-once" (trinary text//replace-once)) + (install "replace-all" (trinary text//replace-all)) + (install "char" (binary text//char)) + (install "clip" (trinary text//clip)) + (install "upper" (unary text//upper)) + (install "lower" (unary text//lower)) + ))) + +(def: array-procs + Bundle + (<| (prefix "array") + (|> (dict.new text.Hash<Text>) + (install "new" (unary array//new)) + (install "get" (binary array//get)) + (install "put" (trinary array//put)) + (install "remove" (binary array//remove)) + (install "size" (unary array//size)) + ))) + +(def: math-procs + Bundle + (<| (prefix "math") + (|> (dict.new text.Hash<Text>) + (install "cos" (unary math//cos)) + (install "sin" (unary math//sin)) + (install "tan" (unary math//tan)) + (install "acos" (unary math//acos)) + (install "asin" (unary math//asin)) + (install "atan" (unary math//atan)) + (install "exp" (unary math//exp)) + (install "log" (unary math//log)) + (install "ceil" (unary math//ceil)) + (install "floor" (unary math//floor)) + (install "pow" (binary math//pow)) + ))) + +(def: io-procs + Bundle + (<| (prefix "io") + (|> (dict.new text.Hash<Text>) + (install "log" (unary io//log)) + (install "error" (unary io//error)) + (install "exit" (unary io//exit)) + (install "current-time" (nullary io//current-time))))) + +(def: atom-procs + Bundle + (<| (prefix "atom") + (|> (dict.new text.Hash<Text>) + (install "new" (unary atom//new)) + (install "read" (unary atom//read)) + (install "compare-and-swap" (trinary atom//compare-and-swap))))) + +(def: box-procs + Bundle + (<| (prefix "box") + (|> (dict.new text.Hash<Text>) + (install "new" (unary box//new)) + (install "read" (unary box//read)) + (install "write" (binary box//write))))) + +(def: process-procs + Bundle + (<| (prefix "process") + (|> (dict.new text.Hash<Text>) + (install "concurrency-level" (nullary process//concurrency-level)) + (install "future" (unary process//future)) + (install "schedule" (binary process//schedule)) + ))) + +(def: #export procedures + Bundle + (<| (prefix "lux") + (|> lux-procs + (dict.merge bit-procs) + (dict.merge nat-procs) + (dict.merge int-procs) + (dict.merge deg-procs) + (dict.merge frac-procs) + (dict.merge text-procs) + (dict.merge array-procs) + (dict.merge math-procs) + (dict.merge io-procs) + (dict.merge atom-procs) + (dict.merge box-procs) + (dict.merge process-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 new file mode 100644 index 000000000..85af96ec9 --- /dev/null +++ b/new-luxc/source/luxc/lang/translation/lua/procedure/host.jvm.lux @@ -0,0 +1,87 @@ +(.module: + lux + (lux (control [monad #+ do]) + (data [text] + text/format + (coll [list "list/" Functor<List>] + [dict #+ Dict])) + [macro "macro/" Monad<Meta>]) + (luxc ["&" lang] + (lang ["la" analysis] + ["ls" synthesis] + (host [lua #+ Lua Expression Statement]))) + [///] + (/// [".T" runtime]) + (// ["@" common])) + +(do-template [<name> <lua>] + [(def: (<name> _) @.Nullary <lua>)] + + [lua//nil "nil"] + [lua//table "{}"] + ) + +(def: (lua//global proc translate inputs) + (-> Text @.Proc) + (case inputs + (^ (list [_ (#.Text name)])) + (do macro.Monad<Meta> + [] + (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<Meta> + [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<Text>) + (@.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<Meta> + [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<Text>) + (@.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)))) |