From 0bd93d82eb7a50b9ce8be42800c388e87e6ca9bf Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Fri, 23 Feb 2018 23:10:28 -0400 Subject: - Added a code-generation utility module for JS. --- new-luxc/source/luxc/lang/host/js.lux | 106 ++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 new-luxc/source/luxc/lang/host/js.lux (limited to 'new-luxc/source/luxc/lang/host/js.lux') diff --git a/new-luxc/source/luxc/lang/host/js.lux b/new-luxc/source/luxc/lang/host/js.lux new file mode 100644 index 000000000..b4c5acd58 --- /dev/null +++ b/new-luxc/source/luxc/lang/host/js.lux @@ -0,0 +1,106 @@ +(.module: + [lux #- or and function] + (lux (data [text] + text/format + (coll [list "list/" Functor Fold])))) + +(type: #export JS Text) + +(type: #export Expression JS) + +(type: #export Statement JS) + +(def: #export (number value) + (-> Frac Expression) + (%f value)) + +(def: #export (string value) + (-> Text Expression) + (%t value)) + +(def: #export (apply func args) + (-> Expression (List Expression) Expression) + (format func "(" (text.join-with "," args) ")")) + +(def: #export (var! name value) + (-> Text (Maybe Expression) Statement) + (case value + #.None + (format "var " name ";") + + (#.Some value) + (format "var " name " = " value ";"))) + +(def: #export (set! name value) + (-> Text Expression Statement) + (format name " = " value ";")) + +(def: #export (if! test then! else!) + (-> Expression Statement Statement Statement) + (format "if(" test ") " + then! + " else " + else!)) + +(def: #export (cond! clauses else!) + (-> (List [Expression Statement]) Statement Statement) + (list/fold (.function [[test then!] next!] + (if! test then! next!)) + else! + clauses)) + +(def: #export (block! statements) + (-> (List Statement) Statement) + (format "{" (text.join-with "" statements) "}")) + +(def: #export (while! test body) + (-> Expression (List Statement) Statement) + (format "while(" test ") " (block! body))) + +(def: #export (throw! message) + (-> Expression Statement) + (format "throw Error(" message ");")) + +(def: #export (return! value) + (-> Expression Statement) + (format "return " value ";")) + +(def: #export (function name args body) + (-> Text (List Text) (List Statement) Expression) + (let [args (format "(" (text.join-with ", " args) ")") + function (format "function " name args " " (block! body))] + (format "(" function ")"))) + +(def: #export (? test then else) + (-> Expression Expression Expression Expression) + (format "(" test " ? " then " : " else ")")) + +(def: #export (object fields) + (-> (List [Text Expression]) Expression) + (format "{" + (|> fields + (list/map (.function [[key val]] + (format key ": " val))) + (text.join-with ", ")) + "}")) + +(do-template [ ] + [(def: #export ( param subject) + (-> Expression Expression Expression) + (format "(" subject " " " " param ")"))] + + [= "="] + [< "<"] + [<= "<="] + [> ">"] + [>= ">="] + [+ "+"] + [- "-"] + [* "*"] + [/ "/"] + [% "%"] + [or "||"] + [and "&&"] + [bit-or "|"] + [bit-and "&"] + ) -- cgit v1.2.3