(.module: [lux #* [cli (#+ program:)] ["." io (#+ IO io)] [control pipe [monad (#+ do)] ["." exception (#+ exception:)]] [data ["." maybe] ["." error (#+ Error)] [number ["." i64]] ["." text ("#@." hash) format] [collection ["." array (#+ Array)] ["." list ("#@." functor)]]] [macro ["." template]] [world ["." file]] ["." host (#+ import: interface: do-to object) ["_" python]] [tool [compiler ["." name] ["." synthesis] [phase [macro (#+ Expander)] ["." generation ["." python ["." runtime] ["." extension]]]] [default ["." platform (#+ Platform)]]]]] [program ["/" compositor ["/." cli]]]) (import: #long java/lang/String) (import: #long (java/lang/Class a) (getCanonicalName [] java/lang/String)) (import: #long java/lang/Object (new []) (toString [] java/lang/String) (getClass [] (java/lang/Class java/lang/Object))) (import: #long java/lang/Integer (longValue [] java/lang/Long)) (import: #long java/lang/Long (intValue [] java/lang/Integer)) (import: #long java/lang/Number (intValue [] java/lang/Integer) (longValue [] long) (doubleValue [] double)) (def: (inspect object) (-> java/lang/Object Text) (<| (case (host.check java/lang/Boolean object) (#.Some value) (%b value) #.None) (case (host.check java/lang/String object) (#.Some value) (%t value) #.None) (case (host.check java/lang/Long object) (#.Some value) (%i (.int value)) #.None) (case (host.check java/lang/Number object) (#.Some value) (%f (java/lang/Number::doubleValue value)) #.None) (case (host.check (Array java/lang/Object) object) (#.Some value) (let [value (:coerce (Array java/lang/Object) value)] (case (array.read 0 value) (^multi (#.Some tag) [(host.check java/lang/Integer tag) (#.Some tag)] [[(array.read 1 value) (array.read 2 value)] [last? (#.Some choice)]]) (let [last? (case last? (#.Some _) #1 #.None #0)] (|> (format (%n (.nat (java/lang/Integer::longValue tag))) " " (%b last?) " " (inspect choice)) (text.enclose ["(" ")"]))) _ (|> value array.to-list (list@map inspect) (text.join-with " ") (text.enclose ["[" "]"])))) #.None) (java/lang/Object::toString object))) (import: #long org/python/core/PyType (getName [] java/lang/String)) (import: #long org/python/core/PyNone) (import: #long org/python/core/PyBoolean) (import: #long org/python/core/PyInteger) (import: #long org/python/core/PyLong) (import: #long org/python/core/PyFloat) (import: #long org/python/core/PyTuple) (import: #long org/python/core/PyList) (import: #long org/python/core/PyString (new [java/lang/String])) (import: #long org/python/core/PyObject (asInt [] java/lang/Integer) (asLong [] long) (asDouble [] double) (asString [] java/lang/String) (__nonzero__ [] boolean) (__getitem__ [int] #try org/python/core/PyObject) (__getitem__ #as __getitem__dict [org/python/core/PyObject] #try org/python/core/PyObject) (__len__ [] int) (getType [] org/python/core/PyType)) (import: #long org/python/core/PyFunction (__call__ [(Array org/python/core/PyObject)] org/python/core/PyObject)) (import: #long org/python/core/PyArray (new [(java/lang/Class java/lang/Object) java/lang/Object]) (getArray [] java/lang/Object)) (import: #long org/python/util/PythonInterpreter (new []) (exec [String] #try void) (eval [String] #try PyObject)) (type: Translator (-> org/python/core/PyObject (Error Any))) (def: (read-tuple read host-object) (-> Translator Translator) (let [size (|> host-object org/python/core/PyObject::__len__ .nat)] (loop [idx 0 output (:coerce (Array Any) (array.new size))] (if (n/< size idx) (case (org/python/core/PyObject::__getitem__ (.int idx) host-object) (#error.Failure error) (#error.Failure error) (#error.Success value) (case (read value) (#error.Failure error) (#error.Failure error) (#error.Success lux-value) (recur (inc idx) (array.write idx lux-value output)))) (#error.Success output))))) (def: python-type (-> org/python/core/PyObject Text) (|>> org/python/core/PyObject::getType org/python/core/PyType::getName (:coerce Text))) (exception: (unknown-kind-of-object {object java/lang/Object}) (exception.report ["Object" (java/lang/Object::toString object)])) (def: (read-variant read host-object) (-> Translator Translator) (case [(org/python/core/PyObject::__getitem__ +0 host-object) (org/python/core/PyObject::__getitem__ +1 host-object) (org/python/core/PyObject::__getitem__ +2 host-object)] (^or [(#error.Failure error) _ _] [_ (#error.Failure error) _] [_ _ (#error.Failure error)]) (#error.Failure error) (^multi [(#error.Success tag) (#error.Success flag) (#error.Success value)] [(read tag) (#error.Success tag)] [(read value) (#error.Success value)]) (#error.Success [tag (: Any (case (host.check org/python/core/PyNone flag) (#.Some _) (host.null) #.None synthesis.unit)) value]) _ (exception.throw ..unknown-kind-of-object host-object))) (def: (read host-object) Translator (`` (<| (~~ (do-template [ ] [(case (host.check host-object) (#.Some host-object) (#error.Success (<| host-object)) _)] [org/python/core/PyNone (new> [] [])] [org/python/core/PyBoolean org/python/core/PyObject::__nonzero__] [org/python/core/PyInteger org/python/core/PyObject::asInt] [org/python/core/PyLong org/python/core/PyObject::asLong] [org/python/core/PyFloat org/python/core/PyObject::asDouble] [org/python/core/PyString org/python/core/PyObject::asString] [org/python/core/PyFunction (|>)] [org/python/core/PyArray org/python/core/PyArray::getArray] [(Array java/lang/Object) (|>)] )) (~~ (do-template [ ] [(case (host.check host-object) (#.Some host-object) (<| host-object) _)] [org/python/core/PyTuple (..read-variant read)] [org/python/core/PyList (..read-tuple read)] )) (exec (log! (java/lang/Class::getCanonicalName (java/lang/Object::getClass (:coerce java/lang/Object host-object)))) (log! (python-type host-object)) (exception.throw ..unknown-kind-of-object host-object))))) (exception: (cannot-apply-a-non-function {object java/lang/Object}) (exception.report ["Object" (java/lang/Object::toString object)])) (def: (ensure-macro macro) (-> Macro (Maybe org/python/core/PyFunction)) (host.check org/python/core/PyFunction (:coerce java/lang/Object macro))) (def: object-class (java/lang/Class java/lang/Object) (java/lang/Object::getClass (java/lang/Object::new))) (def: to-host (-> Any org/python/core/PyObject) (|>> (:coerce java/lang/Object) (org/python/core/PyArray::new ..object-class))) (def: (call-macro inputs lux macro) (-> (List Code) Lux org/python/core/PyFunction (Error (Error [Lux (List Code)]))) (<| (:coerce (Error (Error [Lux (List Code)]))) ..read (org/python/core/PyFunction::__call__ (|> (host.array org/python/core/PyObject 2) (host.array-write 0 (..to-host inputs)) (host.array-write 1 (..to-host lux))) macro))) (def: (expander macro inputs lux) Expander (case (ensure-macro macro) (#.Some macro) (case (call-macro inputs lux macro) (#error.Success output) (|> output (:coerce org/python/core/PyObject) ..read (:coerce (Error (Error [Lux (List Code)])))) (#error.Failure error) (#error.Failure error)) #.None (exception.throw cannot-apply-a-non-function (:coerce java/lang/Object macro))) ) (def: separator "___") (type: Host (generation.Host (_.Expression Any) (_.Statement Any))) (def: host (IO Host) (io (let [interpreter (org/python/util/PythonInterpreter::new) evaluate! (: (-> Text (_.Expression Any) (Error Any)) (function (evaluate! alias input) (do error.monad [output (org/python/util/PythonInterpreter::eval (_.code input) interpreter)] (..read output)))) execute! (: (-> Text (_.Statement Any) (Error Nothing)) (function (execute! alias input) (do error.monad [_ (org/python/util/PythonInterpreter::exec (_.code input) interpreter)] (wrap (:coerce Nothing [])))))] (: Host (structure (def: evaluate! evaluate!) (def: execute! execute!) (def: (define! [module name] input) (let [global (format (text.replace-all .module-separator ..separator module) ..separator (name.normalize name) "___" (%n (text@hash name))) @global (_.var global)] (do error.monad [#let [definition (_.set (list @global) input)] _ (execute! global definition) value (evaluate! global @global)] (wrap [global value definition]))))))))) (def: platform (IO (Platform IO _.SVar (_.Expression Any) (_.Statement Any))) (do io.monad [host ..host] (wrap {#platform.&monad io.monad #platform.&file-system file.system #platform.host host #platform.phase python.generate #platform.runtime runtime.generate}))) (def: (program program) (-> (_.Expression Any) (_.Statement Any)) ($_ _.then (_.import "sys") (_.when (_.= (_.string "__main__") (_.var "__name__")) (_.statement (_.apply/2 program (runtime.lux//program-args (|> (_.var "sys") (_.the "argv"))) _.none))))) (program: [{service /cli.service}] (/.compiler ..expander ..platform extension.bundle ..program service))