diff options
author | Eduardo Julian | 2018-03-06 01:07:43 -0400 |
---|---|---|
committer | Eduardo Julian | 2018-03-06 01:07:43 -0400 |
commit | 38bd6f35d81705ab0c04c85601ac5b236b62605a (patch) | |
tree | 4c2fd4f6369067965017aeea18ba68b1f658344d /new-luxc/source/luxc/lang/host/lua.lux | |
parent | 9bf491a18e4b772505c3767cf0249eb24f0a822b (diff) |
- Initial Lua backend implementation.
Diffstat (limited to '')
-rw-r--r-- | new-luxc/source/luxc/lang/host/lua.lux | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/new-luxc/source/luxc/lang/host/lua.lux b/new-luxc/source/luxc/lang/host/lua.lux new file mode 100644 index 000000000..943b0377e --- /dev/null +++ b/new-luxc/source/luxc/lang/host/lua.lux @@ -0,0 +1,188 @@ +(.module: + [lux #- not or and function] + (lux (data [text] + text/format + (coll [list "list/" Functor<List> Fold<List>])))) + +(type: #export Lua Text) + +(type: #export Expression Lua) + +(type: #export Statement Lua) + +(def: #export nil + Expression + "nil") + +(def: #export bool + (-> Bool Expression) + %b) + +(def: #export int + (-> Int Expression) + %i) + +(def: #export float + (-> Frac Expression) + %f) + +(def: #export (string value) + (-> Text Expression) + (%t value)) + +(def: #export (array elements) + (-> (List Expression) Expression) + (format "{" (text.join-with "," elements) "}")) + +(def: #export (nth idx array) + (-> Expression Expression Expression) + (format "(" array ")[" idx "]")) + +(def: #export (length array) + (-> Expression Expression) + (format "#(" array ")")) + +(def: #export (apply func args) + (-> Expression (List Expression) Expression) + (format func "(" (text.join-with "," args) ")")) + +(def: #export (method field table args) + (-> Text Expression (List Expression) Expression) + (format table ":" field "(" (text.join-with "," args) ")")) + +(def: #export (local! name value) + (-> Text (Maybe Expression) Statement) + (case value + #.None + (format "local " name ";") + + (#.Some value) + (format "local " name " = " value ";"))) + +(def: #export (global! name value) + (-> Text (Maybe Expression) Statement) + (case value + #.None + (format name ";") + + (#.Some value) + (format 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 " then! + " else " else! + " end;")) + +(def: #export (when! test then!) + (-> Expression Statement Statement) + (format "if " test + " then " then! + " end;")) + +(def: #export (cond! clauses else!) + (-> (List [Expression Statement]) Statement Statement) + (list/fold (.function [[test then!] next!] + (if! test then! next!)) + else! + (list.reverse clauses))) + +(def: #export (block! statements) + (-> (List Statement) Statement) + (text.join-with " " statements)) + +(def: #export (while! test body) + (-> Expression Statement Statement) + (format "while " test " do " body " end;")) + +(def: #export (for-in! vars source body) + (-> (List Text) Expression Statement Statement) + (format "for " (text.join-with "," vars) " in " source + " do " body " end;")) + +(def: #export (for-step! var from to step body) + (-> Text Expression Expression Expression Statement + Statement) + (format "for " var " = " from ", " to ", " step + " do " body " end;")) + +(def: #export (error message) + (-> Expression Expression) + (apply "error" (list message))) + +(def: #export (return! value) + (-> Expression Statement) + (format "return " value ";")) + +(def: #export (function args body) + (-> (List Text) Statement Expression) + (format "(" + (format "function " (format "(" (text.join-with ", " args) ")") + " " + body + " end") + ")")) + +(def: #export (function! name args body) + (-> Text (List Text) Statement Expression) + (format "function " name (format "(" (text.join-with ", " args) ")") + " " + body + " end;")) + +(def: #export (table 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 ")"))] + + [= "=="] + [< "<"] + [<= "<="] + [> ">"] + [>= ">="] + [+ "+"] + [- "-"] + [* "*"] + [/ "/"] + [// "//"] + [% "%"] + ) + +(do-template [<name> <op>] + [(def: #export (<name> param subject) + (-> Expression Expression Expression) + (format "(" param " " <op> " " subject ")"))] + + [or "or"] + [and "and"] + [bit-or "|"] + [bit-and "&"] + [bit-xor "~"] + ) + +(do-template [<name> <op>] + [(def: #export (<name> param subject) + (-> Expression Expression Expression) + (format "(" subject " " <op> " " param ")"))] + + [bit-shl "<<"] + [bit-shr ">>"] + ) + +(def: #export (not subject) + (-> Expression Expression) + (format "(not " subject ")")) |