(.require [library [lux (.except Location Code static int if function or and not comment local global symbol the) [abstract [equivalence (.only Equivalence)] [hash (.only Hash)] ["[0]" enum]] [control ["[0]" pipe]] [data ["[0]" text (.only) ["%" \\format (.only format)]] [collection ["[0]" list (.use "[1]#[0]" functor mix)]]] [math [number ["n" nat] ["f" frac]]] [meta ["@" target] ["[0]" code (.only) ["<[1]>" \\parser]] [macro [syntax (.only syntax)] ["[0]" template]] [type [primitive (.except)]]]]]) (def input_separator ", ") (def statement_suffix ";") ... Added the carriage return for better Windows compatibility. (def \n+ Text (format text.carriage_return text.new_line)) (def nested (-> Text Text) (.let [nested_new_line (format text.new_line text.tab)] (|>> (format \n+) (text.replaced text.new_line nested_new_line)))) (primitive .public (Code brand) Text (def .public equivalence (All (_ brand) (Equivalence (Code brand))) (implementation (def (= reference subject) (at text.equivalence = (representation reference) (representation subject))))) (def .public manual (-> Text Code) (|>> abstraction)) (def .public code (-> (Code Any) Text) (|>> representation)) (with_template [ +] [(with_expansions [ (template.symbol [ "'"])] (primitive ( brand) Any) (`` (type .public (|> Any (,, (template.spliced +))))))] [Expression [Code]] [Computation [Expression' Code]] [Location [Computation' Expression' Code]] [Var [Location' Computation' Expression' Code]] [LVar [Var' Location' Computation' Expression' Code]] [Statement [Code]] ) (with_template [ +] [(with_expansions [ (template.symbol [ "'"])] (primitive Any) (`` (type .public (|> (,, (template.spliced +))))))] [Literal [Computation' Expression' Code]] [Access [Location' Computation' Expression' Code]] [CVar [Var' Location' Computation' Expression' Code]] [GVar [Var' Location' Computation' Expression' Code]] [IVar [Var' Location' Computation' Expression' Code]] [SVar [Var' Location' Computation' Expression' Code]] [LVar* [LVar' Var' Location' Computation' Expression' Code]] [LVar** [LVar' Var' Location' Computation' Expression' Code]] ) (with_template [ ] [(def .public (-> Text ) (|>> (format ) abstraction))] [LVar "l_" local] [CVar "C_" constant] [GVar "$" global] [IVar "@" instance] [SVar "@@" static] ) (with_template [ ] [(with_template [ ] [(def .public (-> ) (|>> representation (format ) abstraction))] [ LVar ] [ Expression Computation] )] [LVar* "*" variadic splat] [LVar** "**" variadic_kv double_splat] ) (with_template [ ] [(def .public GVar (..global ))] ["!" latest_error_message] ["@" latest_error_location] ["_" last_string_read] ["." last_line_number_read] ["&" last_string_matched] ["~" last_regexp_match] ["=" case_insensitivity_flag] ["/" input_record_separator] ["\" output_record_separator] ["0" script_name] ["$" process_id] ["?" exit_status] ["stdout" stdout] ["stdin" stdin] ) (with_template [ ] [(def .public CVar (..manual ))] ["ARGV" command_line_arguments] ) (def .public multi (-> (List Expression) Expression) (|>> (list#each ..code) (text.interposed ..input_separator) abstraction)) (def .public nil Literal (abstraction "nil")) (def .public bool (-> Bit Literal) (|>> (pipe.case #0 "false" #1 "true") abstraction)) (def safe (-> Text Text) (`` (|>> (,, (with_template [ ] [(text.replaced )] ["\" "\\"] [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)] )) ))) (with_template [ ] [(def .public (-> Literal) (|>> abstraction))] [%.int int Int (<|)] [%.text string Text ..safe] [(<|) symbol Text (format ":")] ) (def .public float (-> Frac Literal) (|>> (pipe.cond [(f.= f.positive_infinity)] [(pipe.new "(+1.0/0.0)" [])] [(f.= f.negative_infinity)] [(pipe.new "(-1.0/0.0)" [])] [(f.= f.not_a_number)] [(pipe.new "(+0.0/-0.0)" [])] ... else [%.frac]) abstraction)) (def .public (array_range from to array) (-> Expression Expression Expression Computation) (|> (format (representation from) ".." (representation to)) (text.enclosed ["[" "]"]) (format (representation array)) abstraction)) (def .public array (-> (List Expression) Computation) (|>> (list#each (|>> representation (text.suffix ..input_separator))) text.together (text.enclosed ["[" "]"]) abstraction)) (def .public hash (-> (List [Expression Expression]) Computation) (|>> (list#each (.function (_ [k v]) (format (representation k) " => " (representation v) ..input_separator))) text.together (text.enclosed ["{" "}"]) abstraction)) (def (control_structure content) (-> Text Text) (format content \n+ "end" ..statement_suffix)) (type .public Block (Record [#parameters (List Var) #body Statement])) (def (block it) (-> Block Text) (|> (format (|> (.the #parameters it) (list#each (|>> representation)) (text.interposed ..input_separator) (text.enclosed' "|")) (..nested (representation (.the #body it)))) (text.enclosed ["{" "}"]))) (def .public (apply arguments block func) (-> (List Expression) (Maybe Block) Expression Computation) (let [arguments (|> arguments (list#each (|>> representation)) (text.interposed ..input_separator) (text.enclosed ["(" ")"])) block (case block {.#None} "" {.#Some [inputs block]} (|> block representation nested control_structure (format " do " (|> inputs (list#each (|>> representation)) (text.interposed ..input_separator) (text.enclosed' "|")))))] (abstraction (format (representation func) arguments block)))) (def .public (the field object) (-> Text Expression Access) (abstraction (format (representation object) "." field))) (def .public (item idx array) (-> Expression Expression Access) (|> (representation idx) (text.enclosed ["[" "]"]) (format (representation array)) abstraction)) (def .public (? test then else) (-> Expression Expression Expression Computation) (|> (format (representation test) " ? " (representation then) " : " (representation else)) (text.enclosed ["(" ")"]) abstraction)) (def .public statement (-> Expression Statement) (|>> representation (text.suffix ..statement_suffix) abstraction)) (def .public (then pre! post!) (-> Statement Statement Statement) (abstraction (format (representation pre!) \n+ (representation post!)))) (def .public (set vars value) (-> (List Location) Expression Statement) (abstraction (format (|> vars (list#each (|>> representation)) (text.interposed ..input_separator)) " = " (representation value) ..statement_suffix))) (def .public (if test then! else!) (-> Expression Statement Statement Statement) (<| abstraction ..control_structure (format "if " (representation test) (..nested (representation then!)) \n+ "else" (..nested (representation else!))))) (with_template [ ] [(def .public ( test then!) (-> Expression Statement Statement) (<| abstraction ..control_structure (format " " (representation test) (..nested (representation then!)))))] [when "if"] [while "while"] ) (def .public (for_in var array iteration!) (-> LVar Expression Statement Statement) (<| abstraction ..control_structure (format "for " (representation var) " in " (representation array) " do " (..nested (representation iteration!))))) (type .public Rescue (Record [#classes (List Text) #exception LVar #rescue Statement])) (def .public (begin body! rescues) (-> Statement (List Rescue) Statement) (<| abstraction ..control_structure (format "begin" (..nested (representation body!)) (|> rescues (list#each (.function (_ [classes exception rescue]) (format \n+ "rescue " (text.interposed ..input_separator classes) " => " (representation exception) (..nested (representation rescue))))) (text.interposed \n+))))) (def .public (catch expectation block) (-> Expression Block Expression) (<| abstraction (format "catch(" (representation expectation) ") " (..block block)))) (def .public (return value) (-> Expression Statement) (abstraction (format "return " (representation value) ..statement_suffix))) (def .public (raise message) (-> Expression Expression) (abstraction (format "raise " (representation message)))) (with_template [ ] [(def .public Statement (|> (text.suffix ..statement_suffix) abstraction))] [next "next"] [redo "redo"] [break "break"] ) (def .public initialize LVar (..manual "initialize")) (def .public (function name args body!) (-> LVar (List LVar) Statement Statement) (<| abstraction ..control_structure (format "def " (representation name) (|> args (list#each (|>> representation)) (text.interposed ..input_separator) (text.enclosed ["(" ")"])) (..nested (representation body!))))) (def .public (lambda name block) (-> (Maybe LVar) Block Literal) (let [proc (format "lambda " (..block block))] (|> (case name {.#None} proc {.#Some name} (format (representation name) " = " proc)) (text.enclosed ["(" ")"]) abstraction))) (with_template [ ] [(def .public ( parameter subject) (-> Expression Expression Computation) (abstraction (format "(" (representation subject) " " " " (representation parameter) ")")))] ["==" =] [ "<" <] ["<=" <=] [ ">" >] [">=" >=] [ "+" +] [ "-" -] [ "*" *] [ "/" /] [ "%" %] ["**" pow] ["||" or] ["&&" and] [ "|" bit_or] [ "&" bit_and] [ "^" bit_xor] ["<<" bit_shl] [">>" bit_shr] ) (with_template [ ] [(def .public ( subject) (-> Expression Computation) (abstraction (format "(" (representation subject) ")")))] ["!" not] ["~" bit_not] ["-" opposite] ) (def .public (comment commentary on) (All (_ brand) (-> Text (Code brand) (Code brand))) (abstraction (format "# " (..safe commentary) \n+ (representation on)))) (with_template [] [(`` (def .public ((,, (template.symbol [ "/*"])) attributes) (-> (List Text) Statement) (..statement (..apply (list#each ..string attributes) {.#None} (..manual )))))] ["attr_reader"] ["attr_writer"] ["attr_accessor"]) ) (def .public (do method arguments block object) (-> Text (List Expression) (Maybe Block) Expression Computation) (|> object (..the method) (..apply arguments block))) (def .public new (-> (List Expression) (Maybe Block) Expression Computation) (..do "new")) (def .public (class definition) (-> Block Computation) (|> (..manual "Class") (..new (list) {.#Some definition}))) (def .public (module definition) (-> Block Computation) (|> (..manual "Module") (..new (list) {.#Some definition}))) (def .public (apply_lambda args lambda) (-> (List Expression) Expression Computation) (|> lambda (..do "call" args {.#None}))) (def arity_inputs (syntax (_ [arity .nat]) (in (case arity 0 (.list) _ (|> (-- arity) (enum.range n.enum 0) (list#each (|>> %.nat code.local))))))) (def arity_types (syntax (_ [arity .nat]) (in (list.repeated arity (` ..Expression))))) (with_template [ +] [(with_expansions [ (template.symbol ["apply/" ]) (arity_inputs ) (arity_types ) (template.spliced +)] (with_template [] [(`` (def .public ((,, (template.symbol [ "/" ])) ) (-> Computation) (..apply (.list ) {.#None} (..manual ))))] ))] [0 [["gets"]]] [1 [["print"] ["include"] ["extend"] ["require"] ["defined?"]]] [2 [["print"] ["alias_method"]]] ) (def .public (throw/1 error) (-> Expression Statement) (..statement (..apply (list error) {.#None} (..manual "throw")))) (def .public (throw/2 tag value) (-> Expression Expression Statement) (..statement (..apply (list tag value) {.#None} (..manual "throw")))) (def .public (class_variable_set var value object) (-> SVar Expression Expression Computation) (..do "class_variable_set" (list (..string (..code var)) value) {.#None} object)) (def .public (class_variable_get var object) (-> SVar Expression Computation) (..do "class_variable_get" (list (..string (..code var))) {.#None} object))