aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/host/js.lux
diff options
context:
space:
mode:
Diffstat (limited to 'new-luxc/source/luxc/lang/host/js.lux')
-rw-r--r--new-luxc/source/luxc/lang/host/js.lux106
1 files changed, 106 insertions, 0 deletions
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<List> Fold<List>]))))
+
+(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 [<name> <op>]
+ [(def: #export (<name> param subject)
+ (-> Expression Expression Expression)
+ (format "(" subject " " <op> " " param ")"))]
+
+ [= "="]
+ [< "<"]
+ [<= "<="]
+ [> ">"]
+ [>= ">="]
+ [+ "+"]
+ [- "-"]
+ [* "*"]
+ [/ "/"]
+ [% "%"]
+ [or "||"]
+ [and "&&"]
+ [bit-or "|"]
+ [bit-and "&"]
+ )