(.using [library [lux {"-" Location Code static int if function or and not comment local global symbol} ["@" target] [abstract [equivalence {"+" Equivalence}] [hash {"+" Hash}] ["[0]" enum]] [control [pipe {"+" case> cond> new>}] [parser ["<[0]>" code]]] [data ["[0]" text ["%" format {"+" format}]] [collection ["[0]" list ("[1]#[0]" functor mix)]]] [macro [syntax {"+" syntax:}] ["[0]" template] ["[0]" code]] [math [number ["n" nat] ["f" frac]]] [type abstract]]]) (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)))) (abstract: .public (Code brand) Text (implementation: .public equivalence (All (_ brand) (Equivalence (Code brand))) (def: (= reference subject) (# text.equivalence = (:representation reference) (:representation subject)))) (def: .public manual (-> Text Code) (|>> :abstraction)) (def: .public code (-> (Code Any) Text) (|>> :representation)) (template [ +] [(with_expansions [ (template.symbol [ "'"])] (abstract: ( 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]] ) (template [ +] [(with_expansions [ (template.symbol [ "'"])] (abstract: 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]] ) (template [ ] [(def: .public (-> Text ) (|>> (format ) :abstraction))] [LVar "l_" local] [CVar "C_" constant] [GVar "$" global] [IVar "@" instance] [SVar "@@" static] ) (template [ ] [(template [ ] [(def: .public (-> ) (|>> :representation (format ) :abstraction))] [ LVar ] [ Expression Computation] )] [LVar* "*" variadic splat] [LVar** "**" variadic_kv double_splat] ) (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] ) (template [ ] [(def: .public CVar (..manual ))] ["ARGV" command_line_arguments] ) (def: .public nil Literal (:abstraction "nil")) (def: .public bool (-> Bit Literal) (|>> (case> #0 "false" #1 "true") :abstraction)) (def: safe (-> Text Text) (`` (|>> (~~ (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)] )) ))) (template [ ] [(def: .public (-> Literal) (|>> :abstraction))] [%.int int Int (<|)] [%.text string Text ..safe] [(<|) symbol Text (format ":")] ) (def: .public float (-> Frac Literal) (|>> (cond> [(f.= f.positive_infinity)] [(new> "(+1.0/0.0)" [])] [(f.= f.negative_infinity)] [(new> "(-1.0/0.0)" [])] [(f.= f.not_a_number)] [(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.interposed ..input_separator) (text.enclosed ["[" "]"]) :abstraction)) (def: .public hash (-> (List [Expression Expression]) Computation) (|>> (list#each (.function (_ [k v]) (format (:representation k) " => " (:representation v)))) (text.interposed ..input_separator) (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 (|> (value@ #parameters it) (list#each (|>> :representation)) (text.interposed ..input_separator) (text.enclosed' "|")) (..nested (:representation (value@ #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!))))) (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)))) (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))) (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] ) (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)))) (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 (apply_lambda/* args lambda) (-> (List Expression) Expression Computation) (|> lambda (..do "call" args {.#None}))) (syntax: (arity_inputs [arity .nat]) (in (case arity 0 (.list) _ (|> (-- arity) (enum.range n.enum 0) (list#each (|>> %.nat code.local_symbol)))))) (syntax: (arity_types [arity .nat]) (in (list.repeated arity (` ..Expression)))) (template [ +] [(with_expansions [ (template.symbol ["apply/" ]) (arity_inputs ) (arity_types ) (template.spliced +)] (template [] [(`` (def: .public ((~~ (template.symbol [ "/" ])) ) (-> Computation) (..apply/* (.list ) {.#None} (..manual ))))] ))] [1 [["print"] ["include"] ["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"))))