(.module: [lux #* [program (#+ program:)] ["." host (#+ import:)] [abstract [monad (#+ do)]] [control [pipe (#+ new>)] ["." try (#+ Try)] ["." exception (#+ exception:)] ["." io (#+ IO io)] [concurrency ["." promise (#+ Promise)]]] [data ["." maybe] ["." text ("#\." hash) ["%" format (#+ format)] ["." encoding]] [collection ["." array (#+ Array)]]] [macro ["." template]] [math [number ["n" nat] ["." i64]]] ["." world #_ ["." file] ["#/." program]] ["@" target ["_" python]] [tool [compiler [phase (#+ Operation Phase)] [reference [variable (#+ Register)]] [language [lux [program (#+ Program)] [generation (#+ Context Host)] ["." synthesis] [analysis [macro (#+ Expander)]] [phase ["." extension (#+ Bundle Extender Handler) ["#/." bundle] ["." analysis #_ ["#" python]] ["." generation #_ ["#" python]]] [generation ["." reference] ["." python ["." runtime]]]]]] [default ["." platform (#+ Platform)]] [meta ["." packager #_ ["#" script]]]]]] [program ["/" compositor ["/." cli] ["/." static]]]) (for {@.old (as_is (import: java/lang/String) (import: (java/lang/Class a)) (import: java/lang/Object ["#::." (new []) (toString [] java/lang/String) (getClass [] (java/lang/Class java/lang/Object))]) (import: org/python/core/PyNone) (import: org/python/core/PyBoolean) (import: org/python/core/PyInteger) (import: org/python/core/PyLong) (import: org/python/core/PyFloat) (import: org/python/core/PyTuple) (import: org/python/core/PyList) (import: org/python/core/PyString ["#::." (new [java/lang/String])]) (import: 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)]) (import: org/python/core/PyFunction ["#::." (__call__ [[org/python/core/PyObject]] #try org/python/core/PyObject)]) (import: org/python/core/ThreadState) (import: org/python/core/PyArray ["#::." (new [(java/lang/Class java/lang/Object) java/lang/Object]) (getArray [] java/lang/Object)]) (import: org/python/util/PythonInterpreter ["#::." (new []) (exec [java/lang/String] #try void) (eval [java/lang/String] #try PyObject)]) (type: Translator (-> org/python/core/PyObject (Try 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) (#try.Failure try) (#try.Failure try) (#try.Success value) (case (read value) (#try.Failure try) (#try.Failure try) (#try.Success lux_value) (recur (inc idx) (array.write! idx lux_value output)))) (#try.Success output))))) (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 [(#try.Failure try) _ _] [_ (#try.Failure try) _] [_ _ (#try.Failure try)]) (#try.Failure try) (^multi [(#try.Success tag) (#try.Success flag) (#try.Success value)] [(read tag) (#try.Success tag)] [(read value) (#try.Success value)]) (#try.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 (`` (<| (~~ (template [ ] [(case (host.check host_object) (#.Some host_object) (#try.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] [[java/lang/Object] (|>)] )) (~~ (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)] )) ## (exception.throw ..unknown_kind_of_object host_object) (exception.return host_object)))) (exception: (cannot_apply_a_non_function {object java/lang/Object}) (exception.report ["Non-function" (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: ensure_function (-> Any (Maybe org/python/core/PyFunction)) (|>> (:coerce java/lang/Object) (host.check org/python/core/PyFunction))) ) @.python (as_is)}) (for {@.old (as_is (def: (call_macro inputs lux macro) (-> (List Code) Lux org/python/core/PyFunction (Try (Try [Lux (List Code)]))) (:assume (do try.monad [expansion (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)] (..read expansion)))) (def: (expander macro inputs lux) Expander (case (ensure_macro macro) (#.Some macro) (case (..call_macro inputs lux macro) (#try.Success output) (|> output (:coerce org/python/core/PyObject) ..read (:coerce (Try (Try [Lux (List Code)])))) (#try.Failure error) (#try.Failure error)) #.None (exception.throw ..cannot_apply_a_non_function (:coerce java/lang/Object macro))))) @.python (def: (expander macro inputs lux) Expander (#try.Success ((:coerce Macro' macro) inputs lux)))}) (for {@.old (def: host (IO (Host (_.Expression Any) (_.Statement Any))) (io (let [interpreter (org/python/util/PythonInterpreter::new) evaluate! (: (-> Context (_.Expression Any) (Try Any)) (function (evaluate! context input) (do try.monad [output (org/python/util/PythonInterpreter::eval (_.code input) interpreter)] (..read output)))) execute! (: (-> (_.Statement Any) (Try Any)) (function (execute! input) (org/python/util/PythonInterpreter::exec (_.code input) interpreter)))] (: (Host (_.Expression Any) (_.Statement Any)) (structure (def: evaluate! evaluate!) (def: execute! execute!) (def: (define! context input) (let [global (reference.artifact context) @global (_.var global)] (do try.monad [#let [definition (_.set (list @global) input)] _ (execute! definition) value (evaluate! context @global)] (wrap [global value definition])))) (def: (ingest context content) (|> content (\ encoding.utf8 decode) try.assume (:coerce (_.Statement Any)))) (def: (re_learn context content) (execute! content)) (def: (re_load context content) (do try.monad [_ (execute! content)] (evaluate! context (_.var (reference.artifact context)))))))))) @.python (as_is (import: (dict [] host.Dict)) (import: (eval [host.String host.Dict] #try Any)) (def: host (IO (Host (_.Expression Any) (_.Statement Any))) (io (: (Host (_.Expression Any) (_.Statement Any)) (let [globals (..dict []) evaluate! (: (-> Context (_.Expression Any) (Try Any)) (function (evaluate! context input) (..eval [(_.code input) globals]))) execute! (: (-> (_.Statement Any) (Try Any)) (function (execute! input) (host.try ("python exec" (_.code input) globals)))) define! (: (-> Context (_.Expression Any) (Try [Text Any (_.Statement Any)])) (function (define! context input) (let [global (reference.artifact context) @global (_.var global)] (do try.monad [#let [definition (_.set (list @global) input)] _ (execute! definition) value (evaluate! context @global)] (wrap [global value definition])))))] (structure (def: evaluate! evaluate!) (def: execute! execute!) (def: define! define!) (def: (ingest context content) (|> content (\ encoding.utf8 decode) try.assume (:coerce (_.Statement Any)))) (def: (re_learn context content) (execute! content)) (def: (re_load context content) (do try.monad [_ (execute! content)] (evaluate! context (_.var (reference.artifact context)))))))))))}) (def: platform (IO (Platform Register (_.Expression Any) (_.Statement Any))) (do io.monad [host ..host] (wrap {#platform.&file_system (file.async file.default) #platform.host host #platform.phase python.generate #platform.runtime runtime.generate #platform.write (|>> _.code (\ encoding.utf8 encode))}))) (def: (program context program) (Program (_.Expression Any) (_.Statement Any)) ($_ _.then (_.import "sys") (_.when (_.= (_.string "__main__") (_.var "__name__")) (_.statement (_.apply/2 program (|> (_.var "sys") (_.the "argv") ## The first entry in the list will be the program.py file itself ## so, it must be removed so only the program's arguments are left. (_.slice_from (_.int +1)) runtime.lux//program_args) _.none))))) (for {@.old (as_is (exception: #export (cannot_parse_phase_inputs {arity Nat}) (exception.report ["Arity" (%.nat arity)])) (def: (host_phase phase) (All [s i o] (-> (Phase [Bundle s] i o) org/python/core/PyObject)) (host.object [] org/python/core/PyObject [] [] ## Methods (org/python/core/PyObject [] (__call__ self {_ org/python/core/ThreadState} {input/0 org/python/core/PyObject}) org/python/core/PyObject (case [(..read input/0)] [(#try.Success input/0)] (host_phase (:assume ((:coerce (-> Nat Nat Nat []) phase) (:coerce Nat input/0)))) _ (error! (exception.construct ..cannot_parse_phase_inputs [1])))) (org/python/core/PyObject [] (__call__ self {_ org/python/core/ThreadState} {input/0 org/python/core/PyObject} {input/1 org/python/core/PyObject}) org/python/core/PyObject (case [(..read input/0) (..read input/1)] [(#try.Success input/0) (#try.Success input/1)] (host_phase (:assume ((:coerce (-> Nat Nat Nat []) phase) (:coerce Nat input/0) (:coerce Nat input/1)))) _ (error! (exception.construct ..cannot_parse_phase_inputs [2])))) (org/python/core/PyObject [] (__call__ self {_ org/python/core/ThreadState} {input/0 org/python/core/PyObject} {input/1 org/python/core/PyObject} {input/2 org/python/core/PyObject}) org/python/core/PyObject (case [(..read input/0) (..read input/1) (..read input/2)] [(#try.Success input/0) (#try.Success input/1) (#try.Success input/2)] (..to_host ((:coerce (-> Nat Nat Nat []) phase) (:coerce Nat input/0) (:coerce Nat input/1) (:coerce Nat input/2))) _ (error! (exception.construct ..cannot_parse_phase_inputs [3])))))) (def: extender Extender ## TODO: Stop relying on coercions ASAP. (<| (:coerce Extender) (function (@self handler)) (:coerce Handler) (function (@self name phase)) (:coerce Phase) (function (@self archive parameters)) (:coerce Operation) (function (@self state)) (:coerce Try) try.assume (:coerce Try) (do try.monad [handler (try.from_maybe (..ensure_function handler)) output (org/python/core/PyFunction::__call__ (|> (host.array org/python/core/PyObject 5) (host.array_write 0 (org/python/core/PyString::new name)) (host.array_write 1 (..host_phase phase)) (host.array_write 2 (..to_host archive)) (host.array_write 3 (..to_host parameters)) (host.array_write 4 (..to_host state))) handler)] (..read output))))) @.python (def: (extender handler) Extender (:assume handler))}) (def: (declare_success! _) (-> Any (Promise Any)) (promise.future (\ world/program.default exit +0))) (def: (scope body) (-> (_.Statement Any) (_.Statement Any)) (let [@program (_.var "lux_program") max_recursion (|> (_.int +10) (_.** (_.int +6))) ; _.statement] (<| (_.comment "-*- coding: utf-8 -*-") ($_ _.then (; (|> (_.__import__/1 (_.unicode "sys")) (_.do "setrecursionlimit" (list max_recursion)))) (_.def @program (list) body) (; (_.apply/* @program (list))) )))) (program: [{service /cli.service}] (let [extension ".py"] (exec (do promise.monad [_ (/.compiler {#/static.host @.python #/static.host_module_extension extension #/static.target (/cli.target service) #/static.artifact_extension extension} ..expander analysis.bundle ..platform generation.bundle extension/bundle.empty ..program [Register (type (_.Expression Any)) (type (_.Statement Any))] ..extender service [(packager.package (: (_.Statement Any) (_.manual "")) _.code _.then ..scope) (format (/cli.target service) (\ file.default separator) "program" extension)])] (..declare_success! [])) (io.io []))))