From cbc41f10fb3e0e776767d2266b22068172b0f69a Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Mon, 1 Mar 2021 01:49:30 -0400 Subject: Done with Ruby. --- lux-ruby/source/program.lux | 196 ++++++++++++++++++++++++++++++++------------ 1 file changed, 142 insertions(+), 54 deletions(-) (limited to 'lux-ruby/source') diff --git a/lux-ruby/source/program.lux b/lux-ruby/source/program.lux index 044e97923..64a34c003 100644 --- a/lux-ruby/source/program.lux +++ b/lux-ruby/source/program.lux @@ -29,6 +29,7 @@ [math [number ["n" nat] + ["i" int] ["." i64]]] ["." world #_ ["." file] @@ -90,6 +91,10 @@ (#static newInternalFromJavaExternal [org/jruby/Ruby java/lang/String] org/jruby/RubyString) (asJavaString [] java/lang/String)]) +(import: org/jruby/RubySymbol + ["#::." + (asJavaString [] java/lang/String)]) + (import: org/jruby/runtime/builtin/IRubyObject) (import: org/jruby/Ruby @@ -110,6 +115,11 @@ (import: org/jruby/runtime/ThreadContext) +(import: org/jruby/RubyRange + ["#::." + (first [org/jruby/runtime/ThreadContext] org/jruby/runtime/builtin/IRubyObject) + (size [org/jruby/runtime/ThreadContext] org/jruby/runtime/builtin/IRubyObject)]) + (template [] [(host.interface: (getValue [] java/lang/Object)) @@ -311,6 +321,18 @@ (exception.report ["Index" (%.nat index)])) +(exception: (invalid_index {index java/lang/Object}) + (exception.report + ["Class" (|> index + java/lang/Object::getClass + java/lang/Object::toString)] + ["Index" (|> index + java/lang/Object::toString)])) + +(import: java/util/Arrays + ["#::." + (#static [t] copyOfRange [[t] int int] [t])]) + (def: (lux_wrapper_access lux_structure value) (-> (-> (Array java/lang/Object) org/jruby/runtime/builtin/IRubyObject) (-> (Array java/lang/Object) org/jruby/internal/runtime/methods/DynamicMethod)) @@ -327,48 +349,61 @@ {block org/jruby/runtime/Block}) org/jruby/runtime/builtin/IRubyObject (let [member (host.array_read 0 args)] - (case (host.check org/jruby/RubyFixnum member) - (#.Some member) - (case (array.read (org/jruby/RubyFixnum::getLongValue member) value) - (#.Some value) - (wrapped_lux_value lux_structure value) - - #.None - (error! (exception.construct ..invalid_tuple_access [(org/jruby/RubyFixnum::getLongValue member)]))) - - #.None - (case (host.check org/jruby/RubyString member) - (#.Some member) - (case (:coerce Text (org/jruby/RubyString::asJavaString member)) - (^ (static runtime.variant_tag_field)) - (|> value - (array.read 0) - maybe.assume - (:coerce java/lang/Integer) - java/lang/Integer::longValue - (org/jruby/RubyFixnum::new ..initial_ruby_runtime)) - - (^ (static runtime.variant_flag_field)) - (case (array.read 1 value) - #.None - ..ruby_nil - - (#.Some flag) - ..lux_unit) - - (^ (static runtime.variant_value_field)) - (case (array.read 2 value) + (<| (case (host.check org/jruby/RubyFixnum member) + (#.Some member) + (case (array.read (org/jruby/RubyFixnum::getLongValue member) value) (#.Some value) (wrapped_lux_value lux_structure value) #.None - (error! (exception.construct ..nil_has_no_lux_representation []))) + (error! (exception.construct ..invalid_tuple_access [(org/jruby/RubyFixnum::getLongValue member)]))) + + #.None) + (case (host.check org/jruby/RubyString member) + (#.Some member) + (case (:coerce Text (org/jruby/RubyString::asJavaString member)) + (^ (static runtime.variant_tag_field)) + (|> value + (array.read 0) + maybe.assume + (:coerce java/lang/Integer) + java/lang/Integer::longValue + (org/jruby/RubyFixnum::new ..initial_ruby_runtime)) + + (^ (static runtime.variant_flag_field)) + (case (array.read 1 value) + #.None + ..ruby_nil + + (#.Some flag) + ..lux_unit) + + (^ (static runtime.variant_value_field)) + (case (array.read 2 value) + (#.Some value) + (wrapped_lux_value lux_structure value) - field - (error! (exception.construct ..invalid_variant_access [field]))) - - #.None - (error! (format "lux_wrapper_access INVALID INDEX")))))))) + #.None + (error! (exception.construct ..nil_has_no_lux_representation []))) + + field + (error! (exception.construct ..invalid_variant_access [field]))) + + #.None) + (case (host.check org/jruby/RubyRange member) + (#.Some member) + (case [(|> member (org/jruby/RubyRange::first thread_context) (host.check org/jruby/RubyFixnum)) + (|> member (org/jruby/RubyRange::size thread_context) (host.check org/jruby/RubyFixnum))] + [(#.Some first) (#.Some size)] + (let [first (org/jruby/RubyFixnum::getLongValue first) + size (org/jruby/RubyFixnum::getLongValue size)] + (lux_structure (java/util/Arrays::copyOfRange value first (i.+ first size)))) + + _ + (error! (exception.construct ..invalid_index (:coerce java/lang/Object member)))) + + #.None) + (error! (exception.construct ..invalid_index (:coerce java/lang/Object member)))))))) (def: (lux_wrapper_equality value) (-> (Array java/lang/Object) org/jruby/internal/runtime/methods/DynamicMethod) @@ -410,6 +445,63 @@ array.size (org/jruby/RubyFixnum::new ..initial_ruby_runtime))))) +(def: (lux_wrapper_to_s value) + (-> (Array java/lang/Object) org/jruby/internal/runtime/methods/DynamicMethod) + (host.object [] org/jruby/internal/runtime/methods/DynamicMethod [] + [{java/lang/String "to_s"}] + + (org/jruby/internal/runtime/methods/DynamicMethod + [] (call self + {thread_context org/jruby/runtime/ThreadContext} + {self org/jruby/runtime/builtin/IRubyObject} + {module org/jruby/RubyModule} + {method java/lang/String} + {args [org/jruby/runtime/builtin/IRubyObject]} + {block org/jruby/runtime/Block}) + org/jruby/runtime/builtin/IRubyObject + (|> value + debug.inspect + (org/jruby/RubyString::newInternalFromJavaExternal ..initial_ruby_runtime))))) + +(exception: (invalid_operation {method Text}) + (exception.report + ["Method" (%.text method)])) + +(def: (lux_wrapper_respond_to? value) + (-> (Array java/lang/Object) org/jruby/internal/runtime/methods/DynamicMethod) + (host.object [] org/jruby/internal/runtime/methods/DynamicMethod [] + [{java/lang/String "respond_to?"}] + + (org/jruby/internal/runtime/methods/DynamicMethod + [] (call self + {thread_context org/jruby/runtime/ThreadContext} + {self org/jruby/runtime/builtin/IRubyObject} + {module org/jruby/RubyModule} + {method java/lang/String} + {args [org/jruby/runtime/builtin/IRubyObject]} + {block org/jruby/runtime/Block}) + org/jruby/runtime/builtin/IRubyObject + (case (|> args + (host.array_read 0) + (host.check org/jruby/RubySymbol)) + (#.Some method) + (|> (case (|> method + org/jruby/RubySymbol::asJavaString + (:coerce Text)) + (^or "==" "equal?" + "to_s" "inspect" + "[]" "length" "respond_to?" + ## "to_hash" + ) + true + + _ + false) + (org/jruby/RubyBoolean::newBoolean ..initial_ruby_runtime)) + + #.None + (error! (exception.construct ..invalid_operation ["respond_to?"])))))) + (exception: (unknown_method {method Text}) (exception.report ["Method" (%.text method)])) @@ -428,12 +520,18 @@ "[]" (org/jruby/runtime/callsite/CacheEntry::new (..lux_wrapper_access lux_structure value) 0) - "==" + (^or "==" "equal?") (org/jruby/runtime/callsite/CacheEntry::new (..lux_wrapper_equality value) 1) "length" (org/jruby/runtime/callsite/CacheEntry::new (..lux_wrapper_length value) 2) + (^or "to_s" "inspect") + (org/jruby/runtime/callsite/CacheEntry::new (..lux_wrapper_to_s value) 3) + + "respond_to?" + (org/jruby/runtime/callsite/CacheEntry::new (..lux_wrapper_respond_to? value) 4) + _ (error! (exception.construct ..unknown_method [(:coerce Text method)])))))] (org/jruby/java/proxies/JavaProxy::new ..initial_ruby_runtime meta_class (:coerce java/lang/Object value)))) @@ -519,7 +617,7 @@ (run! (_.global (reference.artifact context)))))))))) (def: platform - (IO (Platform _.LVar _.Expression _.Statement)) + (IO (Platform Register _.Expression _.Statement)) (do io.monad [host ..host] (wrap {#platform.&file_system (file.async file.default) @@ -530,9 +628,9 @@ (def: (program context program) (Program _.Expression _.Statement) - (_.statement (_.apply/* (list (runtime.lux//program_args _.command_line_arguments) - _.nil) - program))) + (_.statement (_.apply_lambda/* (list (runtime.lux//program_args _.command_line_arguments) + _.nil) + program))) (def: extender Extender @@ -564,14 +662,6 @@ (-> Any (Promise Any)) (promise.future (\ world/program.default exit +0))) -(def: (scope body!) - (-> _.Statement _.Statement) - (let [@program (_.local "lux_program")] - ($_ _.then - (_.function @program (list) body!) - (_.statement (_.apply/* (list) @program)) - ))) - (program: [{service /cli.service}] (let [extension ".rb"] (exec (do promise.monad @@ -585,15 +675,13 @@ generation.bundle extension/bundle.empty ..program - [_.LVar - _.Expression - _.Statement] + [Register _.Expression _.Statement] ..extender service [(packager.package (: _.Statement (_.manual "")) _.code _.then - ..scope) + (|>>)) (format (/cli.target service) (\ file.default separator) "program" -- cgit v1.2.3