From 69edb6de2ecf62881bcde1b8013c98450a6a52bc Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Fri, 26 Feb 2021 02:34:17 -0400 Subject: Got JRuby to cooperate. --- stdlib/source/lux/host.old.lux | 2 +- stdlib/source/lux/target/ruby.lux | 86 +++++++++++++++++--- .../lux/phase/extension/generation/ruby/common.lux | 18 ++--- .../language/lux/phase/generation/js/runtime.lux | 3 +- .../language/lux/phase/generation/lua/runtime.lux | 3 +- .../lux/phase/generation/python/runtime.lux | 28 ++++--- .../language/lux/phase/generation/reference.lux | 10 ++- .../language/lux/phase/generation/ruby/case.lux | 42 ++++++++-- .../lux/phase/generation/ruby/function.lux | 94 ++++++++++++---------- .../language/lux/phase/generation/ruby/loop.lux | 56 ++++++++++--- .../language/lux/phase/generation/ruby/runtime.lux | 86 ++++++++++---------- 11 files changed, 278 insertions(+), 150 deletions(-) (limited to 'stdlib') diff --git a/stdlib/source/lux/host.old.lux b/stdlib/source/lux/host.old.lux index cc7fe53e4..3a69f2464 100644 --- a/stdlib/source/lux/host.old.lux +++ b/stdlib/source/lux/host.old.lux @@ -1322,7 +1322,7 @@ {unchecked (p.maybe s.any)}) {#.doc (doc "Checks whether an object is an instance of a particular class." "Caveat emptor: Cannot check for polymorphism, so avoid using parameterized classes." - (case (check String "YOLO") + (case (check java/lang/String "YOLO") (#.Some value_as_string) #.None))} (with_gensyms [g!_ g!unchecked] diff --git a/stdlib/source/lux/target/ruby.lux b/stdlib/source/lux/target/ruby.lux index c170f3504..e884d6c70 100644 --- a/stdlib/source/lux/target/ruby.lux +++ b/stdlib/source/lux/target/ruby.lux @@ -2,17 +2,26 @@ [lux (#- Location Code static int if cond function or and not comment) ["@" target] ["." host] + [abstract + [equivalence (#+ Equivalence)] + [hash (#+ Hash)] + ["." enum]] [control - [pipe (#+ case> cond> new>)]] + [pipe (#+ case> cond> new>)] + [parser + ["<.>" code]]] [data ["." text ["%" format (#+ format)]] [collection ["." list ("#\." functor fold)]]] [macro - ["." template]] + [syntax (#+ syntax:)] + ["." template] + ["." code]] [math [number + ["n" nat] ["f" frac]]] [type abstract]]) @@ -39,6 +48,18 @@ (abstract: #export (Code brand) Text + (structure: #export code_equivalence + (All [brand] (Equivalence (Code brand))) + + (def: (= reference subject) + (\ text.equivalence = (:representation reference) (:representation subject)))) + + (structure: #export code_hash + (All [brand] (Hash (Code brand))) + + (def: &equivalence ..code_equivalence) + (def: hash (|>> :representation (\ text.hash hash)))) + (def: #export manual (-> Text Code) (|>> :abstraction)) @@ -201,6 +222,15 @@ (format (:representation func)) :abstraction)) + (def: #export (apply_lambda/* args lambda) + (-> (List Expression) Expression Computation) + (|> args + (list\map (|>> :representation)) + (text.join_with ..input_separator) + (text.enclose ["[" "]"]) + (format (:representation lambda)) + :abstraction)) + (def: #export (the field object) (-> Text Expression Access) (:abstraction (format (:representation object) "." field))) @@ -251,9 +281,9 @@ (<| :abstraction ..block (format "if " (:representation test) - text.new_line (..nest (:representation then!)) + (..nest (:representation then!)) text.new_line "else" - text.new_line (..nest (:representation else!))))) + (..nest (:representation else!))))) (template [ ] [(def: #export ( test then!) @@ -261,7 +291,7 @@ (<| :abstraction ..block (format " " (:representation test) - text.new_line (..nest (:representation then!)))))] + (..nest (:representation then!)))))] [when "if"] [while "while"] @@ -274,7 +304,7 @@ (format "for " (:representation var) " in " (:representation array) " do " - text.new_line (..nest (:representation iteration!))))) + (..nest (:representation iteration!))))) (type: #export Rescue {#classes (List Text) @@ -285,13 +315,12 @@ (-> Statement (List Rescue) Statement) (<| :abstraction ..block - (format "begin" - text.new_line (:representation body!) + (format "begin" (..nest (:representation body!)) (|> rescues (list\map (.function (_ [classes exception rescue]) (format text.new_line "rescue " (text.join_with ..input_separator classes) " => " (:representation exception) - text.new_line (..nest (:representation rescue))))) + (..nest (:representation rescue))))) (text.join_with text.new_line))))) (def: #export (return value) @@ -315,7 +344,7 @@ ) (def: #export (function name args body!) - (-> LVar (List Var) Statement Statement) + (-> LVar (List LVar) Statement Statement) (<| :abstraction ..block (format "def " (:representation name) @@ -323,7 +352,7 @@ (list\map (|>> :representation)) (text.join_with ..input_separator) (text.enclose ["(" ")"])) - text.new_line (:representation body!)))) + (..nest (:representation body!))))) (def: #export (lambda name args body!) (-> (Maybe LVar) (List Var) Statement Literal) @@ -392,3 +421,38 @@ (..if test then! next!)) else! (list.reverse clauses))) + +(syntax: (arity_inputs {arity .nat}) + (wrap (case arity + 0 (.list) + _ (|> (dec arity) + (enum.range n.enum 0) + (list\map (|>> %.nat code.local_identifier)))))) + +(syntax: (arity_types {arity .nat}) + (wrap (list.repeat arity (` ..Expression)))) + +(template [ +] + [(with_expansions [ (template.identifier ["apply/" ]) + (arity_inputs ) + (arity_types ) + (template.splice +)] + (def: #export ( function ) + (-> Expression Computation) + (..apply/* (.list ) function)) + + (template [] + [(`` (def: #export (~~ (template.identifier [ "/" ])) + ( (..local ))))] + + ))] + + [1 + [["print"]]] + + [2 + []] + + [3 + []] + ) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/ruby/common.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/ruby/common.lux index d43f3833a..9f04b35d2 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/ruby/common.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/ruby/common.lux @@ -71,22 +71,17 @@ (/.install "=" (binary (product.uncurry _.=))) (/.install "+" (binary (..keep_i64 (product.uncurry _.+)))) (/.install "-" (binary (..keep_i64 (product.uncurry _.-)))) - ))) - -(def: int_procs - Bundle - (<| (/.prefix "int") - (|> /.empty (/.install "<" (binary (product.uncurry _.<))) (/.install "*" (binary (..keep_i64 (product.uncurry _.*)))) (/.install "/" (binary (product.uncurry _./))) (/.install "%" (binary (product.uncurry _.%))) - (/.install "frac" (unary (_./ (_.float +1.0)))) - (/.install "char" (unary (_.do "chr" (list))))))) + (/.install "f64" (unary (_./ (_.float +1.0)))) + (/.install "char" (unary (_.do "chr" (list (_.string "UTF-8"))))) + ))) -(def: frac_procs +(def: f64_procs Bundle - (<| (/.prefix "frac") + (<| (/.prefix "f64") (|> /.empty (/.install "+" (binary (product.uncurry _.+))) (/.install "-" (binary (product.uncurry _.-))) @@ -155,8 +150,7 @@ (<| (/.prefix "lux") (|> lux_procs (dictionary.merge ..i64_procs) - (dictionary.merge ..int_procs) - (dictionary.merge ..frac_procs) + (dictionary.merge ..f64_procs) (dictionary.merge ..text_procs) (dictionary.merge ..io_procs) ))) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux index 53213d3f1..f434e9dbd 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux @@ -213,8 +213,7 @@ test_recursion!)] [(_.< wanted_tag sum_tag) test_recursion!] - [(_.and (_.> wanted_tag sum_tag) - (_.= ..unit wants_last)) + [(_.= ..unit wants_last) extrac_sub_variant!]) no_match!)))) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux index 20d825912..fd1cfa2b4 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/lua/runtime.lux @@ -220,8 +220,7 @@ test_recursion!)] [(_.< wanted_tag sum_tag) test_recursion!] - [(_.and (_.> wanted_tag sum_tag) - (_.= ..unit wants_last)) + [(_.= ..unit wants_last) extrac_sub_variant!]) no_match!)))) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux index 22234bcc4..933bcf6b0 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux @@ -66,7 +66,7 @@ (def: (flag value) (-> Bit Literal) (if value - (_.unicode "") + ..unit _.none)) (def: (variant' tag last? value) @@ -243,24 +243,26 @@ sum_tag (_.nth (_.int +0) sum) sum_flag (_.nth (_.int +1) sum) sum_value (_.nth (_.int +2) sum) - is_last? (_.= (_.unicode "") sum_flag) + is_last? (_.= ..unit sum_flag) test_recursion! (_.if is_last? ## Must recurse. - (_.return (sum//get sum_value wantsLast (_.- sum_tag wantedTag))) + ($_ _.then + (_.set (list sum) sum_value) + (_.set (list wantedTag) (_.- sum_tag wantedTag))) no_match!)] - (_.cond (list [(_.= sum_tag wantedTag) - (_.if (_.= wantsLast sum_flag) - (_.return sum_value) - test_recursion!)] + (<| (_.while (_.bool true)) + (_.cond (list [(_.= wantedTag sum_tag) + (_.if (_.= wantsLast sum_flag) + (_.return sum_value) + test_recursion!)] - [(_.> sum_tag wantedTag) - test_recursion!] + [(_.< wantedTag sum_tag) + test_recursion!] - [(_.and (_.< sum_tag wantedTag) - (_.= (_.unicode "") wantsLast)) - (_.return (variant' (_.- wantedTag sum_tag) sum_flag sum_value))]) + [(_.= ..unit wantsLast) + (_.return (variant' (_.- wantedTag sum_tag) sum_flag sum_value))]) - no_match!))) + no_match!)))) (def: runtime//adt (Statement Any) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/reference.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/reference.lux index 6bfd7182e..5f4d3fbd1 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/reference.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/reference.lux @@ -18,13 +18,17 @@ ## into the local variables of some scoping function. (def: #export universe (for {## In the case of Lua, there is a limit of 200 locals in a function's scope. - @.lua (not ("lua script universe"))} + @.lua (not ("lua script universe")) + ## Cannot make all definitions be local variables because of limitations with JRuby. + @.ruby (not ("ruby script universe"))} #0)) (def: universe_label Text - (for {@.lua (format "u" (%.nat (if ..universe 1 0)))} - "")) + (with_expansions [