aboutsummaryrefslogtreecommitdiff
path: root/lux-python/source
diff options
context:
space:
mode:
authorEduardo Julian2022-02-11 19:57:00 -0400
committerEduardo Julian2022-02-11 19:57:00 -0400
commit105ab334201646be6b594d3d1215297e3b629a10 (patch)
treed1f972d5fe676f8b93f4efa8fb0a8ce602878903 /lux-python/source
parent469b171e5793422a4dbd27f4f2fab8a261c9ccf9 (diff)
Fixed directive extensions for Lux/Python.
Diffstat (limited to 'lux-python/source')
-rw-r--r--lux-python/source/program.lux406
1 files changed, 223 insertions, 183 deletions
diff --git a/lux-python/source/program.lux b/lux-python/source/program.lux
index 8fb761f0f..1ae02bb2e 100644
--- a/lux-python/source/program.lux
+++ b/lux-python/source/program.lux
@@ -1,75 +1,82 @@
(.using
- [library
- [lux "*"
- [program {"+" program:}]
- ["[0]" ffi {"+" import:}]
- [abstract
- [monad {"+" do}]]
- [control
- [pipe {"+" new>}]
- ["[0]" maybe]
- ["[0]" try {"+" Try}]
- ["[0]" exception {"+" exception:}]
- ["[0]" io {"+" IO io}]
- ["[0]" function]
- [concurrency
- ["[0]" async {"+" Async}]]]
- [data
- ["[0]" text ("[1]#[0]" hash)
- ["%" format {"+" format}]
- [encoding
- ["[0]" utf8]]]
- [collection
- ["[0]" array {"+" Array}]
- ["[0]" list]]]
- [macro
- ["[0]" template]]
- [math
- [number
- ["n" nat]
- ["[0]" i64]]]
- ["[0]" world "_"
- ["[0]" file]
- ["[1]/[0]" program]]
- ["@" target
- ["_" python]]
- [tool
- [compiler
- ["[0]" phase {"+" Operation Phase}]
- [reference
- [variable {"+" Register}]]
- [language
- [lux
- [program {"+" Program}]
- [generation {"+" Context Host}]
- ["[0]" synthesis]
- [analysis
- [macro {"+" Expander}]]
- [phase
- ["[0]" extension {"+" Extender Handler}
- ["[1]/[0]" bundle]
- ["[0]" analysis "_"
- ["[1]" python]]
- ["[0]" generation "_"
- ["[1]" python]]]
- [generation
- ["[0]" reference]
- ["[0]" python
- ["[0]" runtime]]]]]]
- [default
- ["[0]" platform {"+" Platform}]]
- [meta
- [archive {"+" Archive}]
- ["[0]" packager "_"
- ["[1]" script]]]]]]]
- [program
- ["/" compositor
- ["/[0]" cli]
- ["/[0]" static]]])
+ [library
+ [lux "*"
+ [program {"+" program:}]
+ ["[0]" ffi {"+" import:}]
+ ["[0]" debug]
+ ["[0]" static]
+ [abstract
+ ["[0]" monad {"+" do}]]
+ [control
+ [pipe {"+" new>}]
+ ["[0]" maybe]
+ ["[0]" try {"+" Try} ("[1]#[0]" monad)]
+ ["[0]" exception {"+" exception:}]
+ ["[0]" io {"+" IO io}]
+ ["[0]" function]
+ [concurrency
+ ["[0]" async {"+" Async}]]]
+ [data
+ ["[0]" text ("[1]#[0]" hash)
+ ["%" format {"+" format}]
+ [encoding
+ ["[0]" utf8]]]
+ [collection
+ ["[0]" array {"+" Array}]
+ ["[0]" list ("[1]#[0]" functor)]]]
+ ["[0]" macro
+ ["[0]" template]
+ ["[0]" code]]
+ [math
+ [number
+ ["n" nat]
+ ["[0]" i64]]]
+ ["[0]" world "_"
+ ["[0]" file]
+ ["[1]/[0]" program]]
+ ["@" target
+ ["_" python]]
+ [tool
+ [compiler
+ ["[0]" phase {"+" Operation Phase} ("[1]#[0]" monad)]
+ [reference
+ [variable {"+" Register}]]
+ [language
+ [lux
+ [program {"+" Program}]
+ [generation {"+" Host}]
+ ["[0]" synthesis]
+ [analysis
+ [macro {"+" Expander}]]
+ [phase
+ ["[0]" extension {"+" Extender Handler}
+ ["[1]/[0]" bundle]
+ ["[0]" analysis "_"
+ ["[1]" python]]
+ ["[0]" generation "_"
+ ["[1]" python]]]
+ [generation
+ ["[0]" reference]
+ ["[0]" python
+ ["[0]" runtime]]]]]]
+ [default
+ ["[0]" platform {"+" Platform}]]
+ [meta
+ ["[0]" cli]
+ [archive {"+" Archive}
+ ["[0]" unit]]
+ ["[0]" packager "_"
+ ["[1]" script]]]]]]]
+ [program
+ ["/" compositor
+ ["/[0]" static]]])
(with_expansions [<jvm> (as_is (import: java/lang/String)
- (import: (java/lang/Class a))
+ (import: (java/lang/Class a)
+ ["[1]::[0]"
+ ("static" forName [java/lang/String] (java/lang/Class java/lang/Object))
+ (getName [] java/lang/String)])
(import: java/lang/Object
["[1]::[0]"
@@ -78,12 +85,21 @@
(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/PyBoolean
+ ["[1]::[0]"
+ (new [boolean])])
+
+ (import: org/python/core/PyLong
+ ["[1]::[0]"
+ (new [long])])
+
+ (import: org/python/core/PyFloat
+ ["[1]::[0]"
+ (new [double])])
(import: org/python/core/PyString
["[1]::[0]"
@@ -173,6 +189,13 @@
_
(exception.except ..unknown_kind_of_object [(:as java/lang/Object host_object)])))
+ (ffi.interface: LuxValue
+ (value [] java/lang/Object))
+
+ (import: LuxValue
+ ["[1]::[0]"
+ (value [] java/lang/Object)])
+
(def: (read host_object)
Translator
(`` (<| (~~ (template [<class> <processing>]
@@ -182,6 +205,7 @@
_)]
+ [LuxValue [LuxValue::value]]
[org/python/core/PyNone [(new> [] [])]]
[org/python/core/PyBoolean [org/python/core/PyObject::__nonzero__]]
... [org/python/core/PyInteger [(ffi.:as org/python/core/PyObject) org/python/core/PyObject::asInt]]
@@ -209,28 +233,112 @@
... (exception.except ..unknown_kind_of_object [(:as java/lang/Object host_object)])
{try.#Success host_object})))
- (exception: (cannot_apply_a_non_function [object java/lang/Object])
- (exception.report
- ["Non-function" (java/lang/Object::toString object)]))
+ (def: (function/? arity)
+ (-> Nat Code)
+ (` (.-> (~+ (list.repeated arity (` .Any))) .Any)))
- (def: (ensure_macro macro)
- (-> Macro (Maybe org/python/core/PyFunction))
- (ffi.check org/python/core/PyFunction (:as java/lang/Object macro)))
+ (def: (inputs/? arity)
+ (-> Nat (List Text))
+ (|> (list.indices arity)
+ (list#each (|>> %.nat (format "input/")))))
+
+ (def: (pseudo_function to_host it)
+ (-> (-> Any org/python/core/PyObject) Any org/python/core/PyObject)
+ (<| (:as org/python/core/PyObject)
+ (ffi.object [] org/python/core/PyObject [LuxValue]
+ []
+ ... Methods
+ (LuxValue [] (value self []) java/lang/Object (:as java/lang/Object it))
+
+ (org/python/core/PyObject
+ [] (__call__ self [inputs [org/python/core/PyObject]
+ keywords [java/lang/String]])
+ org/python/core/PyObject
+ (try.trusted
+ (do [! try.monad]
+ [inputs (monad.each ! ..read (array.list {.#None} inputs))]
+ (in (loop [it it
+ inputs inputs]
+ (`` (`` (case inputs
+ (^ (list))
+ (:as org/python/core/PyObject self)
+
+ (~~ (template [<arity>]
+ [(^ (list (~~ (static.literals code.local_symbol (inputs/? <arity>)))))
+ (to_host ((:as (~~ (static.literal function.identity (function/? <arity>))) it)
+ (~~ (static.literals code.local_symbol (inputs/? <arity>)))))]
+
+ [1]
+ [2]
+ [3]
+ [4]
+ [5]
+ [6]
+ [7]
+ [8]))
+
+ (^ (list& (~~ (static.literals code.local_symbol (inputs/? 8)))
+ input/+))
+ (again ((:as (~~ (static.literal function.identity (function/? 8))) it)
+ (~~ (static.literals code.local_symbol (inputs/? 8))))
+ input/+))))))))))))
(def: object_class
(java/lang/Class java/lang/Object)
(java/lang/Object::getClass (java/lang/Object::new)))
- (def: to_host
+ (import: library/lux/Function)
+
+ (def: (to_host|array to_host it)
+ (-> (-> Any org/python/core/PyObject) Any org/python/core/PyObject)
+ (:as org/python/core/PyObject
+ (ffi.object [] org/python/core/PyArray [LuxValue]
+ [(java/lang/Class java/lang/Object) ..object_class
+ java/lang/Object (:as java/lang/Object it)]
+ ... Methods
+ (LuxValue [] (value self []) java/lang/Object (:as java/lang/Object it))
+
+ (org/python/core/PyArray
+ [] (pyget self [index' int])
+ org/python/core/PyObject
+ (case (|> it
+ (:as (Array Any))
+ (array.read! (|> index' ffi.int_to_long (:as Nat))))
+ {.#None}
+ (::super! [index'])
+
+ {.#Some it}
+ (<| (case (ffi.check [java/lang/Object] (:as java/lang/Object it))
+ {.#Some it}
+ (to_host it)
+
+ {.#None})
+ (case (ffi.check library/lux/Function (:as java/lang/Object it))
+ {.#Some it}
+ (pseudo_function to_host it)
+
+ {.#None})
+ (::super! [index']))))
+ )))
+
+ (def: (to_host it)
(-> Any org/python/core/PyObject)
- (|>> (:as java/lang/Object)
- (org/python/core/PyArray::new ..object_class)
- (:as org/python/core/PyObject)))
-
- (def: ensure_function
- (-> Any (Maybe org/python/core/PyFunction))
- (|>> (:as java/lang/Object)
- (ffi.check org/python/core/PyFunction)))
+ (`` (<| (~~ (template [<jvm> <python>]
+ [(case (ffi.check <jvm> (:as java/lang/Object it))
+ {.#Some it}
+ (:as org/python/core/PyObject
+ (<python> [(:expected it)]))
+
+ {.#None})]
+
+ [java/lang/Boolean org/python/core/PyBoolean::new]
+ [java/lang/Long org/python/core/PyLong::new]
+ [java/lang/Double org/python/core/PyFloat::new]
+ [java/lang/String org/python/core/PyString::new]
+ [library/lux/Function (pseudo_function to_host)]
+ [[java/lang/Object] (to_host|array to_host)]
+ ))
+ (:as org/python/core/PyObject it))))
)]
(for [@.old (as_is <jvm>)
@.jvm (as_is <jvm>)
@@ -238,17 +346,27 @@
(with_expansions [<jvm> (as_is (def: (call_macro inputs lux macro)
(-> (List Code) Lux org/python/core/PyFunction (Try (Try [Lux (List Code)])))
- (:expected
- (do try.monad
- [expansion (org/python/core/PyFunction::__call__ (|> (ffi.array org/python/core/PyObject 2)
- (ffi.write! 0 (..to_host inputs))
- (ffi.write! 1 (..to_host lux)))
- macro)]
- (..read expansion))))
+ (|> macro
+ (org/python/core/PyFunction::__call__ (|> (ffi.array org/python/core/PyObject 2)
+ (ffi.write! 0 (..to_host inputs))
+ (ffi.write! 1 (..to_host lux))))
+ (try#each ..read)
+ try#conjoint
+ :expected))
+
+ (def: python_function!
+ (-> Any (Maybe org/python/core/PyFunction))
+ (|>> (:as java/lang/Object)
+ (ffi.check org/python/core/PyFunction)))
+
+ (exception: (cannot_apply_a_non_function [object java/lang/Object])
+ (exception.report
+ ["Object" (java/lang/Object::toString object)]
+ ["Class" (java/lang/Class::getName (java/lang/Object::getClass object))]))
(def: (expander macro inputs lux)
Expander
- (case (ensure_macro macro)
+ (case (python_function! macro)
{.#Some macro}
(case (..call_macro inputs lux macro)
{try.#Success output}
@@ -273,7 +391,7 @@
(with_expansions [<jvm> (def: host
(IO (Host (_.Expression Any) (_.Statement Any)))
(io (let [interpreter (org/python/util/PythonInterpreter::new)
- evaluate! (: (-> Context (_.Expression Any) (Try Any))
+ evaluate! (: (-> unit.ID (_.Expression Any) (Try Any))
(function (evaluate! context input)
(do try.monad
[output (org/python/util/PythonInterpreter::eval (_.code input) interpreter)]
@@ -326,13 +444,13 @@
(IO (Host (_.Expression Any) (_.Statement Any)))
(io (: (Host (_.Expression Any) (_.Statement Any))
(let [globals (..dict [])
- evaluate! (: (-> Context (_.Expression Any) (Try Any))
+ evaluate! (: (-> unit.ID (_.Expression Any) (Try Any))
(function (evaluate! context input)
(..eval [(_.code input) globals])))
execute! (: (-> (_.Statement Any) (Try Any))
(function (execute! input)
(ffi.try ("python exec" (_.code input) globals))))
- define! (: (-> Context (_.Expression Any) (Try [Text Any (_.Statement Any)]))
+ define! (: (-> unit.ID (_.Expression Any) (Try [Text Any (_.Statement Any)]))
(function (define! context input)
(let [global (reference.artifact context)
@global (_.var global)]
@@ -357,84 +475,7 @@
[_ (execute! content)]
(evaluate! context (_.var (reference.artifact context)))))))))))]))
-(with_expansions [<jvm> (as_is (exception: .public (invaid_phase_application [partial_application (List Any)
- arity Nat])
- (exception.report
- ["Partial Application" (%.nat (list.size partial_application))]
- ["Arity" (%.nat arity)]))
-
- (def: (host_phase partial_application phase)
- (All (_ s i o)
- (-> (List Any) (Phase [extension.Bundle s] i o)
- org/python/core/PyObject))
- (<| (:as org/python/core/PyObject)
- (ffi.object [] org/python/core/PyObject []
- []
- ... Methods
- (org/python/core/PyObject
- [] (__call__ self [inputs [org/python/core/PyObject]
- keywords [java/lang/String]])
- org/python/core/PyObject
- (try.trusted
- (case (array.list {.#None} inputs)
- (^ (list))
- (# try.monad in (host_phase (list) phase))
-
- (^ (list input/0))
- (do try.monad
- [input/0 (..read input/0)]
- (case partial_application
- (^ (list partial/0 partial/1))
- (in (..to_host ((:as (-> Any Any Any Any) phase)
- partial/0
- partial/1
- input/0)))
-
- (^ (list partial/0))
- (in (host_phase (list partial/0 input/0) phase))
-
- (^ (list))
- (in (host_phase (list input/0) phase))
-
- _
- (exception.except ..invaid_phase_application [partial_application (array.size inputs)])))
-
- (^ (list input/0 input/1))
- (do try.monad
- [input/0 (..read input/0)
- input/1 (..read input/1)]
- (case partial_application
- (^ (list partial/0))
- (in (..to_host ((:as (-> Any Any Any Any) phase)
- partial/0
- input/0
- input/1)))
-
- (^ (list))
- (in (host_phase (list input/0 input/1) phase))
-
- _
- (exception.except ..invaid_phase_application [partial_application (array.size inputs)])))
-
- (^ (list input/0 input/1 input/2))
- (do try.monad
- [input/0 (..read input/0)
- input/1 (..read input/1)
- input/2 (..read input/2)]
- (case partial_application
- (^ (list))
- (in (..to_host ((:as (-> Any Any Any Any) phase)
- input/0
- input/1
- input/2)))
-
- _
- (exception.except ..invaid_phase_application [partial_application (array.size inputs)])))
-
- _
- (exception.except ..invaid_phase_application [partial_application (array.size inputs)])))))))
-
- (def: (extender phase_wrapper)
+(with_expansions [<jvm> (as_is (def: (extender phase_wrapper)
(-> phase.Wrapper Extender)
... TODO: Stop relying on coercions ASAP.
(<| (:as Extender)
@@ -449,7 +490,7 @@
try.trusted
(:as Try)
(do try.monad
- [handler (try.of_maybe (..ensure_function handler))
+ [handler (try.of_maybe (..python_function! handler))
output (org/python/core/PyFunction::__call__ (|> (ffi.array org/python/core/PyObject 5)
(ffi.write! 0 (:as org/python/core/PyObject (org/python/core/PyString::new name)))
(ffi.write! 1 (:as org/python/core/PyObject (phase_wrapper phase)))
@@ -468,10 +509,9 @@
(def: (phase_wrapper archive)
(-> Archive (runtime.Operation phase.Wrapper))
- (do phase.monad
- []
- (in (:as phase.Wrapper
- (..host_phase (list))))))
+ (|> (..pseudo_function ..to_host)
+ (:as phase.Wrapper)
+ phase#in))
(def: platform
(IO (Platform Register (_.Expression Any) (_.Statement Any)))
@@ -514,13 +554,13 @@
(; (_.apply/* @program (list)))
))))
-(program: [service /cli.service]
+(program: [service cli.service]
(let [extension ".py"]
(exec
(do async.monad
[_ (/.compiler [/static.#host @.python
/static.#host_module_extension extension
- /static.#target (/cli.target service)
+ /static.#target (cli.target service)
/static.#artifact_extension extension]
..expander
analysis.bundle
@@ -537,7 +577,7 @@
_.code
_.then
..scope)
- (format (/cli.target service)
+ (format (cli.target service)
(# file.default separator)
"program"
extension)])]