diff options
Diffstat (limited to 'lux-ruby')
-rw-r--r-- | lux-ruby/source/program.lux | 410 |
1 files changed, 245 insertions, 165 deletions
diff --git a/lux-ruby/source/program.lux b/lux-ruby/source/program.lux index e373e20b5..044e97923 100644 --- a/lux-ruby/source/program.lux +++ b/lux-ruby/source/program.lux @@ -1,6 +1,7 @@ (.module: [lux #* [program (#+ program:)] + ["." debug] ["." host (#+ import:)] ["." meta] [abstract @@ -76,12 +77,28 @@ (toString [] java/lang/String) (getClass [] (java/lang/Class java/lang/Object))]) -(import: java/lang/Integer) +(import: java/lang/Integer + ["#::." + (longValue [] java/lang/Long)]) (import: java/lang/Long ["#::." (intValue [] java/lang/Integer)]) +(import: org/jruby/RubyString + ["#::." + (#static newInternalFromJavaExternal [org/jruby/Ruby java/lang/String] org/jruby/RubyString) + (asJavaString [] java/lang/String)]) + +(import: org/jruby/runtime/builtin/IRubyObject) + +(import: org/jruby/Ruby + ["#::." + (getCurrentContext [] org/jruby/runtime/ThreadContext) + (getNil [] org/jruby/runtime/builtin/IRubyObject)]) + +(import: org/jruby/RubyClass) + (import: org/jruby/RubyArray ["#::." (getLength [] int) @@ -91,12 +108,6 @@ ["#::." (get [java/lang/Object] #? java/lang/Object)]) -(import: org/jruby/Ruby - ["#::." - (getCurrentContext [] org/jruby/runtime/ThreadContext)]) - -(import: org/jruby/runtime/builtin/IRubyObject) - (import: org/jruby/runtime/ThreadContext) (template [<name>] @@ -123,140 +134,64 @@ (import: org/jruby/runtime/builtin/InstanceVariables) (import: org/jruby/runtime/builtin/InternalVariables) -(def: (lux_structure value) - (-> (Array java/lang/Object) org/jruby/runtime/builtin/IRubyObject) - (with_expansions [<checkers> (template [<name>] - [[<name> [] boolean]] - - [isNil] [isTaint] [isClass] [isFrozen] - [isImmediate] [isModule] [isSpecialConst] [isTrue] - [isUntrusted] [hasVariables]) - <markers> (template [<name>] - [[<name> [boolean] void]] - - [setFrozen] [setTaint] [setUntrusted]) - <nullaries> (template [<name>] - [[<name> [] org/jruby/runtime/builtin/IRubyObject]] - - [dup] [checkArrayType] [inspect] [checkStringType] - [checkStringType19] [id] [rbClone] [anyToString]) - <class> (template [<name>] - [[<name> [] org/jruby/RubyClass]] - - [getMetaClass] [getType] [getSingletonClass]) - <call> (template [<inputs>] - [[callMethod <inputs> - org/jruby/runtime/builtin/IRubyObject]] - - [[org/jruby/runtime/ThreadContext int java/lang/String]] - [[org/jruby/runtime/ThreadContext int java/lang/String org/jruby/runtime/builtin/IRubyObject]] - [[org/jruby/runtime/ThreadContext java/lang/String]] - [[org/jruby/runtime/ThreadContext java/lang/String org/jruby/runtime/builtin/IRubyObject]] - ## [[org/jruby/runtime/ThreadContext java/lang/String [org/jruby/runtime/builtin/IRubyObject]]] - [[org/jruby/runtime/ThreadContext java/lang/String [org/jruby/runtime/builtin/IRubyObject] org/jruby/runtime/Block]] - ) - <placeholders> (template [<name> <inputs> <output>] - [(org/jruby/runtime/builtin/IRubyObject - (<name> self (~~ (method_inputs <inputs>))) - <output> - (error! (template.text ["UNIMPLEMENTED METHOD: " <name>])))] - - [getRuntime [] org/jruby/Ruby] - [copySpecialInstanceVariables [org/jruby/runtime/builtin/IRubyObject] void] - [syncVariables [org/jruby/runtime/builtin/IRubyObject] void] - [syncVariables [(java/util/List (org/jruby/runtime/builtin/Variable java/lang/Object))] void] - [dataWrapStruct [java/lang/Object] void] - [addFinalizer [org/jruby/runtime/builtin/IRubyObject] void] - [removeFinalizers [] void] - [getVariable [int] java/lang/Object] - [setVariable [int java/lang/Object] void] - [getVariableList [] (java/util/List (org/jruby/runtime/builtin/Variable java/lang/Object))] - [getVariableNameList [] (java/util/List java/lang/String)] - [getVariableCount [] int] - [getJavaClass [] (java/lang/Class java/lang/Object)] - [asJavaString [] java/lang/String] - [getInstanceVariables [] org/jruby/runtime/builtin/InstanceVariables] - [getInternalVariables [] org/jruby/runtime/builtin/InternalVariables] - [convertToInteger [] org/jruby/RubyInteger] - [convertToInteger [java/lang/String] org/jruby/RubyInteger] - [convertToInteger [int java/lang/String] org/jruby/RubyInteger] - [convertToArray [] org/jruby/RubyArray] - [convertToHash [] org/jruby/RubyHash] - [convertToFloat [] org/jruby/RubyFloat] - [convertToString [] org/jruby/RubyString] - [asString [] org/jruby/RubyString] - [respondsTo [java/lang/String] boolean] - [respondsToMissing [java/lang/String] boolean] - [respondsToMissing [java/lang/String boolean] boolean] - [dataGetStruct [] java/lang/Object] - [dataGetStructChecked [] java/lang/Object] - [infectBy [org/jruby/runtime/builtin/IRubyObject] org/jruby/runtime/builtin/IRubyObject] - [eql [org/jruby/runtime/builtin/IRubyObject] boolean] - [toJava [(java/lang/Class java/lang/Object)] java/lang/Object] - - [op_eqq - [org/jruby/runtime/ThreadContext - org/jruby/runtime/builtin/IRubyObject] - org/jruby/runtime/builtin/IRubyObject] - - [op_equal - [org/jruby/runtime/ThreadContext - org/jruby/runtime/builtin/IRubyObject] - org/jruby/runtime/builtin/IRubyObject] - - [callSuper - [org/jruby/runtime/ThreadContext - [org/jruby/runtime/builtin/IRubyObject] - org/jruby/runtime/Block] - org/jruby/runtime/builtin/IRubyObject] - - [checkCallMethod - [org/jruby/runtime/ThreadContext - java/lang/String] - org/jruby/runtime/builtin/IRubyObject] - - [checkCallMethod - [org/jruby/runtime/ThreadContext - org/jruby/runtime/JavaSites$CheckedSites] - org/jruby/runtime/builtin/IRubyObject] - - <checkers> - <markers> - <nullaries> - <class> - <call> - )] - (`` (host.object [] [program/StructureValue - org/jruby/runtime/builtin/IRubyObject] - [] - ## Methods - (program/StructureValue - [] (getValue self) - java/lang/Object - (:coerce (Array java/lang/Object) value)) - - (org/jruby/runtime/builtin/IRubyObject - [] (callMethod self - {thread_context org/jruby/runtime/ThreadContext} - {member java/lang/String} - {inputs [org/jruby/runtime/builtin/IRubyObject]}) - org/jruby/runtime/builtin/IRubyObject - (exec - ("lux io log" (format "Was called: " (%.text member))) - (error! "OOPS!"))) - - <placeholders> - )))) +(import: org/jruby/embed/internal/LocalContextProvider + ["#::." + (getRuntime [] org/jruby/Ruby)]) + +(import: org/jruby/embed/ScriptingContainer + ["#::." + (new []) + (runScriptlet [java/lang/String] #try #? java/lang/Object) + (getProvider [] org/jruby/embed/internal/LocalContextProvider)]) + +## TODO; Figure out a way to not need "interpreter" to be a global variable. +(def: interpreter + (org/jruby/embed/ScriptingContainer::new)) + +(template: (!ruby_runtime) + (|> ..interpreter + org/jruby/embed/ScriptingContainer::getProvider + org/jruby/embed/internal/LocalContextProvider::getRuntime)) + +(template: (!ruby_thread_context) + (|> (!ruby_runtime) + org/jruby/Ruby::getCurrentContext)) + +(def: initial_ruby_runtime + (!ruby_runtime)) + +(import: org/jruby/java/proxies/JavaProxy + ["#::." + (new [org/jruby/Ruby org/jruby/RubyClass java/lang/Object])]) + +(import: org/jruby/internal/runtime/methods/DynamicMethod) + +(import: org/jruby/runtime/callsite/CacheEntry + ["#::." + (new [org/jruby/internal/runtime/methods/DynamicMethod int])]) + +(import: org/jruby/RubyFixnum + ["#::." + (new [org/jruby/Ruby long]) + (getLongValue [] long)]) + +(import: org/jruby/RubyFloat + ["#::." + (new [org/jruby/Ruby double])]) + +(import: org/jruby/RubyBoolean + ["#::." + (#static newBoolean [org/jruby/Ruby boolean] org/jruby/RubyBoolean)]) + +(import: org/jruby/RubyNil + ["#::." + (new [org/jruby/Ruby])]) (import: org/jruby/RubyProc ["#::." (call [org/jruby/runtime/ThreadContext [org/jruby/runtime/builtin/IRubyObject]] #try org/jruby/runtime/builtin/IRubyObject)]) -(import: org/jruby/javasupport/JavaArray - ["#::." - (new [org/jruby/Ruby java/lang/Object])]) - (type: Translator (-> java/lang/Object (Try Any))) @@ -321,6 +256,7 @@ [java/lang/Long #try.Success] [java/lang/Double #try.Success] [java/lang/String #try.Success] + [[java/lang/Object] #try.Success] [org/jruby/RubyArray (read_tuple read)] [org/jruby/RubyHash (read_variant read)] [org/jruby/RubySymbol #try.Success] @@ -329,42 +265,191 @@ (exception.throw ..unknown_kind_of_object host_object) ))) -(exception: (cannot_apply_a_non_function {object java/lang/Object}) +(def: ruby_nil + org/jruby/runtime/builtin/IRubyObject + (org/jruby/Ruby::getNil ..initial_ruby_runtime)) + +(def: lux_unit + org/jruby/RubyString + (org/jruby/RubyString::newInternalFromJavaExternal ..initial_ruby_runtime "")) + +(def: (wrapped_lux_value lux_structure value) + (-> (-> (Array java/lang/Object) org/jruby/runtime/builtin/IRubyObject) + (-> java/lang/Object org/jruby/runtime/builtin/IRubyObject)) + (<| (case (host.check [java/lang/Object] value) + (#.Some value) + (|> value (:coerce (Array java/lang/Object)) lux_structure) + + #.None) + (case (host.check java/lang/Boolean value) + (#.Some value) + (org/jruby/RubyBoolean::newBoolean ..initial_ruby_runtime value) + + #.None) + (case (host.check java/lang/Long value) + (#.Some value) + (org/jruby/RubyFixnum::new ..initial_ruby_runtime value) + + #.None) + (case (host.check java/lang/Double value) + (#.Some value) + (org/jruby/RubyFloat::new ..initial_ruby_runtime value) + + #.None) + (case (host.check java/lang/String value) + (#.Some value) + (org/jruby/RubyString::newInternalFromJavaExternal ..initial_ruby_runtime value) + + #.None) + (:coerce org/jruby/runtime/builtin/IRubyObject value))) + +(exception: (invalid_variant_access {field Text}) (exception.report - ["Non-function" (java/lang/Object::toString object)])) + ["Field" (%.text field)])) -(import: org/jruby/embed/internal/LocalContextProvider - ["#::." - (getRuntime [] org/jruby/Ruby)]) +(exception: (invalid_tuple_access {index Nat}) + (exception.report + ["Index" (%.nat index)])) + +(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)) + (host.object [] org/jruby/internal/runtime/methods/DynamicMethod [] + [{java/lang/String "[]"}] + + (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 + (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) + (#.Some value) + (wrapped_lux_value lux_structure value) + + #.None + (error! (exception.construct ..nil_has_no_lux_representation []))) + + field + (error! (exception.construct ..invalid_variant_access [field]))) + + #.None + (error! (format "lux_wrapper_access INVALID INDEX")))))))) + +(def: (lux_wrapper_equality value) + (-> (Array java/lang/Object) org/jruby/internal/runtime/methods/DynamicMethod) + (host.object [] org/jruby/internal/runtime/methods/DynamicMethod [] + [{java/lang/String "=="}] + + (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 + (let [reference (host.array_read 0 args)] + (case (..read (:coerce java/lang/Object reference)) + (#try.Success reference) + (org/jruby/RubyBoolean::newBoolean ..initial_ruby_runtime (is? (: Any reference) (: Any value))) + + (#try.Failure error) + (org/jruby/RubyBoolean::newBoolean ..initial_ruby_runtime false)))))) + +(def: (lux_wrapper_length value) + (-> (Array java/lang/Object) org/jruby/internal/runtime/methods/DynamicMethod) + (host.object [] org/jruby/internal/runtime/methods/DynamicMethod [] + [{java/lang/String "length"}] + + (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 + array.size + (org/jruby/RubyFixnum::new ..initial_ruby_runtime))))) + +(exception: (unknown_method {method Text}) + (exception.report + ["Method" (%.text method)])) -(import: org/jruby/embed/ScriptingContainer - ["#::." - (new []) - (runScriptlet [java/lang/String] #try #? java/lang/Object) - (getProvider [] org/jruby/embed/internal/LocalContextProvider)]) +(def: (lux_structure value) + (-> (Array java/lang/Object) org/jruby/runtime/builtin/IRubyObject) + (let [meta_class (host.object [] org/jruby/RubyClass [] + [{org/jruby/Ruby + ..initial_ruby_runtime}] -## TODO; Figure out a way to not need "interpreter" to be a global variable. -(def: interpreter (org/jruby/embed/ScriptingContainer::new)) + (org/jruby/RubyClass + [] (searchWithCache self + {method java/lang/String}) + org/jruby/runtime/callsite/CacheEntry + (case (:coerce Text method) + "[]" + (org/jruby/runtime/callsite/CacheEntry::new (..lux_wrapper_access lux_structure value) 0) + + "==" + (org/jruby/runtime/callsite/CacheEntry::new (..lux_wrapper_equality value) 1) + + "length" + (org/jruby/runtime/callsite/CacheEntry::new (..lux_wrapper_length value) 2) + + _ + (error! (exception.construct ..unknown_method [(:coerce Text method)])))))] + (org/jruby/java/proxies/JavaProxy::new ..initial_ruby_runtime meta_class (:coerce java/lang/Object value)))) + +(exception: (cannot_apply_a_non_function {object java/lang/Object}) + (exception.report + ["Non-function" (java/lang/Object::toString object)])) (def: ensure_macro (-> Macro (Maybe org/jruby/RubyProc)) (|>> (:coerce java/lang/Object) (host.check org/jruby/RubyProc))) -(template: (!ruby_runtime) - (|> ..interpreter - org/jruby/embed/ScriptingContainer::getProvider - org/jruby/embed/internal/LocalContextProvider::getRuntime)) - -(template: (!ruby_thread_context) - (|> (!ruby_runtime) - org/jruby/Ruby::getCurrentContext)) - (def: to_host (-> Any org/jruby/runtime/builtin/IRubyObject) (|>> (:coerce (Array java/lang/Object)) - ..lux_structure - ## (org/jruby/javasupport/JavaArray::new (!ruby_runtime)) - )) + ..lux_structure)) (def: (call_macro inputs lux macro) (-> (List Code) Lux org/jruby/RubyProc (Try (Try [Lux (List Code)]))) @@ -416,8 +501,7 @@ @global (_.global global)] (do try.monad [#let [definition (_.set (list @global) input)] - _ (run! definition) - value (run! @global)] + value (run! definition)] (wrap [global value definition])))) (def: (ingest context content) @@ -450,10 +534,6 @@ _.nil) program))) -(import: org/jruby/RubyString - ["#::." - (#static newInternalFromJavaExternal [org/jruby/Ruby java/lang/String] org/jruby/RubyString)]) - (def: extender Extender ## TODO: Stop relying on coercions ASAP. |