diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/host/python.lux | 48 |
1 files changed, 39 insertions, 9 deletions
diff --git a/stdlib/source/lux/host/python.lux b/stdlib/source/lux/host/python.lux index afdb923fc..134e35798 100644 --- a/stdlib/source/lux/host/python.lux +++ b/stdlib/source/lux/host/python.lux @@ -1,5 +1,5 @@ (.module: - [lux (#- Code not or and list if cond int) + [lux (#- Code not or and list if cond int comment) [control pipe] [data @@ -72,9 +72,9 @@ (-> Text SVar) (|>> :abstraction)) - (do-template [<name> <kind> <prefix>] + (do-template [<name> <brand> <prefix>] [(def: #export <name> - (-> SVar (Var <kind>)) + (-> SVar (Var <brand>)) (|>> :representation (format <prefix>) :abstraction))] [poly Poly "*"] @@ -95,6 +95,10 @@ (-> Int Literal) (|>> %i :abstraction)) + (def: #export (long value) + (-> Int Literal) + (:abstraction (format (%i value) "L"))) + (def: #export float (-> Frac Literal) (`` (|>> (cond> (~~ (do-template [<lux> <python>] @@ -110,9 +114,28 @@ [%f]) :abstraction))) + (def: sanitize + (-> Text Text) + (`` (|>> (~~ (do-template [<find> <replace>] + [(text.replace-all <find> <replace>)] + + ["\" "\\"] + [text.tab "\t"] + [text.vertical-tab "\v"] + [text.null "\0"] + [text.back-space "\b"] + [text.form-feed "\f"] + [text.new-line "\n"] + [text.carriage-return "\r"] + [text.double-quote (format "\" text.double-quote)] + )) + ))) + (def: #export string (-> Text Literal) - (|>> (text.enclose' text.double-quote) :abstraction)) + (|>> ..sanitize + (text.enclose [text.double-quote text.double-quote]) + :abstraction)) (def: (composite-literal left-delimiter right-delimiter entry-serializer) (All [a] @@ -122,7 +145,9 @@ (<| :abstraction ..expression (format left-delimiter - (|> entries (list@map entry-serializer) (text.join-with ",")) + (|> entries + (list@map entry-serializer) + (text.join-with ", ")) right-delimiter)))) (do-template [<name> <pre> <post>] @@ -154,9 +179,9 @@ (-> (Expression Any) (List (Expression Any)) (Computation Any)) (<| :abstraction ..expression - (format (:representation func) "(" (text.join-with "," (list@map ..code args)) ")"))) + (format (:representation func) "(" (text.join-with ", " (list@map ..code args)) ")"))) - (do-template [<name> <kind> <prefix>] + (do-template [<name> <brand> <prefix>] [(def: (<name> var) (-> (Expression Any) Text) (format <prefix> (:representation var)))] @@ -324,7 +349,7 @@ (..nest (:representation body!)) (|> excepts (list@map (function (_ [classes exception catch!]) - (format text.new-line "except (" (text.join-with "," (list@map ..code classes)) + (format text.new-line "except (" (text.join-with ", " (list@map ..code classes)) ") as " (:representation exception) ":" (..nest (:representation catch!))))) (text.join-with ""))))) @@ -344,12 +369,17 @@ (-> SVar (List (Ex [k] (Var k))) (Statement Any) (Statement Any)) (:abstraction (format "def " (:representation name) - "(" (|> args (list@map ..code) (text.join-with ",")) "):" + "(" (|> args (list@map ..code) (text.join-with ", ")) "):" (..nest (:representation body))))) (def: #export (import module-name) (-> Text (Statement Any)) (:abstraction (format "import " module-name))) + + (def: #export (comment commentary on) + (All [brand] (-> Text (Code brand) (Code brand))) + (:abstraction (format "# " (..sanitize commentary) text.new-line + (:representation on)))) ) (def: #export (cond clauses else!) |