(.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])) [///] (/// [".T" runtime]) (// ["@" common])) (do-template [ ] [(def: ( _) @.Nullary )] [js//null "null"] [js//undefined "undefined"] [js//object "{}"] ) (def: (js//global proc translate inputs) (-> Text @.Proc) (case inputs (^ (list [_ (#.Text name)])) (do macro.Monad [] (wrap name)) _ (&.throw @.Wrong-Syntax (@.wrong-syntax proc inputs)))) (def: (js//call proc translate inputs) (-> Text @.Proc) (case inputs (^ (list& functionS argsS+)) (do macro.Monad [functionJS (translate functionS) argsJS+ (monad.map @ translate argsS+)] (wrap (format "(" functionJS ")(" (text.join-with "," argsJS+) ")"))) _ (&.throw @.Wrong-Syntax (@.wrong-syntax proc inputs)))) (def: js-procs @.Bundle (|> (dict.new text.Hash) (@.install "null" (@.nullary js//null)) (@.install "undefined" (@.nullary js//undefined)) (@.install "object" (@.nullary js//object)) (@.install "global" js//global) (@.install "call" js//call))) (def: (object//new proc translate inputs) (-> Text @.Proc) (case inputs (^ (list& constructorS argsS+)) (do macro.Monad [constructorJS (translate constructorS) argsJS+ (monad.map @ translate argsS+)] (wrap (format "new (" constructorJS ")(" (text.join-with "," argsJS+) ")"))) _ (&.throw @.Wrong-Syntax (@.wrong-syntax proc inputs)))) (def: (object//call proc translate inputs) (-> Text @.Proc) (case inputs (^ (list& objectS fieldS argsS+)) (do macro.Monad [objectJS (translate objectS) fieldJS (translate fieldS) argsJS+ (monad.map @ translate argsS+)] (wrap (format runtimeT.js//call "(" objectJS "," fieldJS "," "[" (text.join-with "," argsJS+) "]" ")"))) _ (&.throw @.Wrong-Syntax (@.wrong-syntax proc inputs)))) (def: (object//get [fieldJS objectJS]) @.Binary (format runtimeT.js//get "(" objectJS "," fieldJS ")")) (def: (object//set [fieldJS valueJS objectJS]) @.Trinary (format runtimeT.js//set "(" objectJS "," fieldJS "," valueJS ")")) (def: (object//delete [fieldJS objectJS]) @.Binary (format runtimeT.js//delete "(" objectJS "," fieldJS ")")) (def: object-procs @.Bundle (<| (@.prefix "object") (|> (dict.new text.Hash) (@.install "new" object//new) (@.install "call" object//call) (@.install "get" (@.binary object//get)) (@.install "set" (@.trinary object//set)) (@.install "delete" (@.binary object//delete)) ))) (def: (array//literal elementsJS+) @.Variadic (format "[" (text.join-with "," elementsJS+) "]")) (def: (array//read [indexJS arrayJS]) @.Binary (format runtimeT.array//get "(" arrayJS "," indexJS ")")) (def: (array//write [indexJS valueJS arrayJS]) @.Trinary (format runtimeT.array//put "(" arrayJS "," indexJS "," valueJS ")")) (def: (array//delete [indexJS arrayJS]) @.Binary (format runtimeT.array//remove "(" arrayJS "," indexJS ")")) (def: (array//length arrayJS) @.Unary (format arrayJS ".length")) (def: array-procs @.Bundle (<| (@.prefix "array") (|> (dict.new text.Hash) (@.install "literal" (@.variadic array//literal)) (@.install "read" (@.binary array//read)) (@.install "write" (@.trinary array//write)) (@.install "delete" (@.binary array//delete)) (@.install "length" (@.unary array//length)) ))) (def: #export procedures @.Bundle (<| (@.prefix "js") (|> js-procs (dict.merge object-procs) (dict.merge array-procs) )))