From 5dbf134346424602b0104d1f749c1a9eac6f21af Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Tue, 12 Jan 2021 17:31:48 -0400 Subject: Compiler now shows suggestions when encountering unknown definitions. --- lux-python/project.clj | 4 +- lux-python/source/program.lux | 410 ++++++++++++++++++++++++++---------------- 2 files changed, 258 insertions(+), 156 deletions(-) (limited to 'lux-python') diff --git a/lux-python/project.clj b/lux-python/project.clj index 9722945b5..887cd2b89 100644 --- a/lux-python/project.clj +++ b/lux-python/project.clj @@ -20,9 +20,9 @@ ["snapshots" {:url ~sonatype-snapshots :creds :gpg}]] :plugins [[com.github.luxlang/lein-luxc ~version]] - :dependencies [[com.github.luxlang/luxc-jvm ~version] + :dependencies [[com.github.luxlang/lux-bootstrapper ~version] [com.github.luxlang/stdlib ~version] - [org.python/jython-standalone "2.7.1"]] + [org.python/jython-standalone "2.7.2"]] :manifest {"lux" ~version} :source-paths ["source"] diff --git a/lux-python/source/program.lux b/lux-python/source/program.lux index 756b27b9c..2acae70d3 100644 --- a/lux-python/source/program.lux +++ b/lux-python/source/program.lux @@ -1,144 +1,169 @@ (.module: [lux #* + [program (#+ program:)] + ["." host (#+ import: interface: do_to object)] [abstract [monad (#+ do)]] [control - pipe - [cli (#+ program:)] + [pipe (#+ new>)] + ["." try (#+ Try)] ["." exception (#+ exception:)] - ["." io (#+ IO io)]] + ["." io (#+ IO io)] + [concurrency + ["." promise (#+ Promise)]]] [data ["." maybe] - ["." error (#+ Error)] - [number - ["." i64]] - ["." text ("#@." hash) - format] + ["." text ("#\." hash) + ["%" format (#+ format)] + ["." encoding]] [collection - ["." array (#+ Array)] - ["." list ("#@." functor)]]] + ["." array (#+ Array)]]] [macro ["." template]] - [world - ["." file]] - ["." host (#+ import: interface: do-to object) + [math + [number + ["n" nat] + ["." i64]]] + ["." world #_ + ["." file] + ["#/." program]] + ["@" target ["_" python]] [tool [compiler - ["." name] - ["." synthesis] - [phase - [macro (#+ Expander)] - ["." generation - ["." python - ["." runtime] - ["." extension]]]] + [phase (#+ Operation Phase)] + [language + [lux + [program (#+ Program)] + [generation (#+ Context Host)] + ["." synthesis] + [analysis + [macro (#+ Expander)]] + [phase + ["." extension (#+ Extender Handler) + ["#/." bundle] + ["." generation #_ + ["#" python]]] + [generation + ["." reference] + ["." python + ["." runtime]]]]]] [default - ["." platform (#+ Platform)]]]]] + ["." platform (#+ Platform)]] + [meta + ["." packager #_ + ["#" script]]]]]] [program ["/" compositor - ["/." cli]]]) - -(import: #long java/lang/String) - -(import: #long (java/lang/Class a)) - -(import: #long java/lang/Object - (new []) - (toString [] java/lang/String) - (getClass [] (java/lang/Class java/lang/Object))) - -(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)) - -(import: #long org/python/core/PyFunction - (__call__ [[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)) + ["/." cli] + ["/." static]]]) + +(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]] org/python/core/PyObject)]) + +(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 (Error Any))) + (-> org/python/core/PyObject (Try Any))) -(def: (read-tuple read host-object) +(def: (read_tuple read host_object) (-> Translator Translator) - (let [size (|> host-object org/python/core/PyObject::__len__ .nat)] + (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) + (if (n.< size idx) + (case (org/python/core/PyObject::__getitem__ (.int idx) host_object) + (#try.Failure try) + (#try.Failure try) - (#error.Success value) + (#try.Success value) (case (read value) - (#error.Failure error) - (#error.Failure error) + (#try.Failure try) + (#try.Failure try) - (#error.Success lux-value) - (recur (inc idx) (array.write idx lux-value output)))) - (#error.Success output))))) + (#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: (unknown_kind_of_object {object java/lang/Object}) (exception.report ["Object" (java/lang/Object::toString object)])) -(def: (read-variant read host-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) + (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 [(#error.Success tag) (#error.Success flag) (#error.Success value)] + (^multi [(#try.Success tag) (#try.Success flag) (#try.Success value)] [(read tag) - (#error.Success tag)] + (#try.Success tag)] [(read value) - (#error.Success value)]) - (#error.Success [tag - (: Any - (case (host.check org/python/core/PyNone flag) - (#.Some _) - (host.null) + (#try.Success value)]) + (#try.Success [tag + (: Any + (case (host.check org/python/core/PyNone flag) + (#.Some _) + (host.null) - #.None - synthesis.unit)) - value]) + #.None + synthesis.unit)) + value]) _ - (exception.throw ..unknown-kind-of-object host-object))) + (exception.throw ..unknown_kind_of_object host_object))) -(def: (read host-object) +(def: (read host_object) Translator (`` (<| (~~ (template [ ] - [(case (host.check host-object) - (#.Some host-object) - (#error.Success (<| host-object)) + [(case (host.check host_object) + (#.Some host_object) + (#try.Success (<| host_object)) _)] @@ -153,115 +178,192 @@ [[java/lang/Object] (|>)] )) (~~ (template [ ] - [(case (host.check host-object) - (#.Some host-object) - (<| host-object) + [(case (host.check host_object) + (#.Some host_object) + (<| host_object) _)] - [org/python/core/PyTuple (..read-variant read)] - [org/python/core/PyList (..read-tuple read)] + [org/python/core/PyTuple (..read_variant read)] + [org/python/core/PyList (..read_tuple read)] )) - (exception.throw ..unknown-kind-of-object host-object)))) + (exception.throw ..unknown_kind_of_object host_object)))) -(exception: (cannot-apply-a-non-function {object java/lang/Object}) +(exception: (cannot_apply_a_non_function {object java/lang/Object}) (exception.report ["Non-function" (java/lang/Object::toString object)])) -(def: (ensure-macro macro) +(def: (ensure_macro macro) (-> Macro (Maybe org/python/core/PyFunction)) (host.check org/python/core/PyFunction (:coerce java/lang/Object macro))) -(def: object-class +(def: object_class (java/lang/Class java/lang/Object) (java/lang/Object::getClass (java/lang/Object::new))) -(def: to-host +(def: to_host (-> Any org/python/core/PyObject) - (|>> (:coerce java/lang/Object) (org/python/core/PyArray::new ..object-class))) + (|>> (: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)]))) +(def: ensure_function + (-> Any (Maybe org/python/core/PyFunction)) + (|>> (:coerce java/lang/Object) + (host.check org/python/core/PyFunction))) + +(def: (call_macro inputs lux macro) + (-> (List Code) Lux org/python/core/PyFunction (Try (Try [Lux (List Code)]))) (<| :assume ..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))) + (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) + (case (ensure_macro macro) (#.Some macro) - (case (call-macro inputs lux macro) - (#error.Success output) + (case (call_macro inputs lux macro) + (#try.Success output) (|> output (:coerce org/python/core/PyObject) ..read - (:coerce (Error (Error [Lux (List Code)])))) + (:coerce (Try (Try [Lux (List Code)])))) - (#error.Failure error) - (#error.Failure error)) + (#try.Failure try) + (#try.Failure try)) #.None - (exception.throw cannot-apply-a-non-function (:coerce java/lang/Object macro))) + (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 (Host (_.Expression Any) (_.Statement Any))) (io (let [interpreter (org/python/util/PythonInterpreter::new) - evaluate! (: (-> Text (_.Expression Any) (Error Any)) - (function (evaluate! alias input) - (do error.monad + 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! (: (-> Text (_.Statement Any) (Error Any)) - (function (execute! alias input) - (do error.monad + execute! (: (-> (_.Statement Any) (Try Any)) + (function (execute! input) + (do try.monad [_ (org/python/util/PythonInterpreter::exec (_.code input) interpreter)] (wrap []))))] - (: Host + (: (Host (_.Expression Any) (_.Statement Any)) (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))) + (def: (define! context input) + (let [global (reference.artifact context) @global (_.var global)] - (do error.monad + (do try.monad [#let [definition (_.set (list @global) input)] - _ (execute! global definition) - value (evaluate! global @global)] - (wrap [global value definition]))))))))) + _ (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)))))))))) (def: platform - (IO (Platform IO _.SVar (_.Expression Any) (_.Statement Any))) + (IO (Platform _.SVar (_.Expression Any) (_.Statement Any))) (do io.monad [host ..host] - (wrap {#platform.&monad io.monad - #platform.&file-system file.system + (wrap {#platform.&file_system (file.async file.default) #platform.host host #platform.phase python.generate - #platform.runtime runtime.generate}))) + #platform.runtime runtime.generate + #platform.write (|>> _.code (\ encoding.utf8 encode))}))) -(def: (program program) - (-> (_.Expression Any) (_.Statement Any)) +(def: (program context 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"))) + (runtime.lux//program_args (|> (_.var "sys") (_.the "argv"))) _.none))))) +(for {@.old + (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 (host.try + (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 (..to_host 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")] + ($_ _.then + (_.def @program (list) body) + (_.statement (_.apply/* @program (list))) + ))) + (program: [{service /cli.service}] - (/.compiler ..expander - ..platform - extension.bundle - ..program - 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 + extension/bundle.empty + ..platform + generation.bundle + extension/bundle.empty + ..program + [_.SVar (type (_.Expression Any)) (type (_.Statement Any))] + ..extender + service + [(packager.package (<| (_.comment "-*- coding: utf-8 -*-") + (: (_.Statement Any) (_.manual ""))) + _.code + _.then + ..scope) + (format (/cli.target service) + (\ file.default separator) + "program" + extension)])] + (..declare_success! [])) + (io.io [])))) + -- cgit v1.2.3