diff options
author | Eduardo Julian | 2019-03-26 21:23:30 -0400 |
---|---|---|
committer | Eduardo Julian | 2019-03-26 21:23:30 -0400 |
commit | 8df108600f6791237d0079af6b582e6cb306906d (patch) | |
tree | d47d2d0d8cccb58b2baa758057670c35123a187e | |
parent | e76fd0e68475f65e75c1339cfcef6a8114707472 (diff) |
Got the Python compiler running.
Diffstat (limited to '')
-rw-r--r-- | .gitignore | 19 | ||||
-rw-r--r-- | commands | 10 | ||||
-rw-r--r-- | lux-python/project.clj | 29 | ||||
-rw-r--r-- | lux-python/source/program.lux | 270 | ||||
-rw-r--r-- | new-luxc/project.clj | 2 | ||||
-rw-r--r-- | new-luxc/source/luxc/lang/translation/python.lux | 211 | ||||
-rw-r--r-- | new-luxc/source/luxc/lang/translation/python/eval.jvm.lux | 149 | ||||
-rw-r--r-- | new-luxc/source/luxc/lang/translation/python/statement.jvm.lux | 48 | ||||
-rw-r--r-- | stdlib/source/lux/tool/compiler/phase/generation.lux | 11 | ||||
-rw-r--r-- | stdlib/source/lux/tool/compiler/phase/generation/python/function.lux | 9 | ||||
-rw-r--r-- | stdlib/source/lux/tool/compiler/phase/generation/python/runtime.lux | 2 |
11 files changed, 336 insertions, 424 deletions
diff --git a/.gitignore b/.gitignore index 4d1e95d31..d55202a8b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,22 +1,31 @@ -/luxc/target -/luxc/classes -/luxc/checkouts +/docs pom.xml pom.xml.asc *.jar *.class /.lein-* /.nrepl-port + +/luxc/target +/luxc/classes +/luxc/checkouts /luxc/jbe + /stdlib/target + /lux-lein/target + /new-luxc/target /new-luxc/source/lux.lux /new-luxc/source/lux + /lux-js/target /lux-js/source/lux.lux /lux-js/source/lux /lux-js/source/program -/docs -/commands + +/lux-python/target +/lux-python/source/lux.lux +/lux-python/source/lux +/lux-python/source/program @@ -35,6 +35,16 @@ # Try cd ~/lux/lux-js/ && java -jar target/program.jar build --source ~/lux/stdlib/source --source ~/lux/stdlib/test --target ~/lux/stdlib/target --module test/lux +# Python compiler + # Test + cd ~/lux/lux-python/ && lein_2_7_1 lux auto test + cd ~/lux/lux-python/ && lein clean && lein_2_7_1 lux auto test + # Build + cd ~/lux/lux-python/ && lein_2_7_1 lux auto build + cd ~/lux/lux-python/ && lein clean && lein_2_7_1 lux auto build + # Try + cd ~/lux/lux-python/ && java -jar target/program.jar build --source ~/lux/stdlib/source --source ~/lux/stdlib/test --target ~/lux/stdlib/target --module test/lux + # Run compiler test suite cd ~/lux/new-luxc/ && lein_2_7_1 lux auto test cd ~/lux/new-luxc/ && lein clean && lein_2_7_1 lux auto test diff --git a/lux-python/project.clj b/lux-python/project.clj new file mode 100644 index 000000000..cb7be3c12 --- /dev/null +++ b/lux-python/project.clj @@ -0,0 +1,29 @@ +(def version "0.6.0-SNAPSHOT") +(def repo "https://github.com/LuxLang/lux") +(def sonatype-releases "https://oss.sonatype.org/service/local/staging/deploy/maven2/") +(def sonatype-snapshots "https://oss.sonatype.org/content/repositories/snapshots/") + +(defproject com.github.luxlang/lux-python #=(identity version) + :description "A Python compiler for Lux." + :url ~repo + :license {:name "Lux License v0.1" + :url ~(str repo "/blob/master/license.txt")} + :scm {:name "git" + :url ~(str repo ".git")} + :pom-addition [:developers [:developer + [:name "Eduardo Julian"] + [:url "https://github.com/eduardoejp"]]] + + :repositories [["releases" ~sonatype-releases] + ["snapshots" ~sonatype-snapshots]] + :deploy-repositories [["releases" {:url ~sonatype-releases :creds :gpg}] + ["snapshots" {:url ~sonatype-snapshots :creds :gpg}]] + + :plugins [[com.github.luxlang/lein-luxc ~version]] + :dependencies [[com.github.luxlang/luxc-jvm ~version] + [com.github.luxlang/stdlib ~version] + [org.python/jython-standalone "2.7.1"]] + + :source-paths ["source"] + :lux {:program "program"} + ) diff --git a/lux-python/source/program.lux b/lux-python/source/program.lux new file mode 100644 index 000000000..21dac4f04 --- /dev/null +++ b/lux-python/source/program.lux @@ -0,0 +1,270 @@ +(.module: + [lux #* + [cli (#+ program:)] + ["." io (#+ IO io)] + [control + [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] + [phase + [macro (#+ Expander)] + ["." generation + ["." python + ["." runtime] + ["." extension]]]] + [default + ["." platform (#+ Platform)]]]]] + [program + ["/" compositor + ["/." cli]]]) + +(import: #long java/lang/String) + +(import: #long java/lang/Object + (toString [] java/lang/String)) + +(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/PyString + (new [java/lang/String])) + +(import: #long org/python/core/PyObject + (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/util/PythonInterpreter + (new []) + (exec [String] #try void) + (eval [String] #try PyObject)) + +(type: Translator + (-> org/python/core/PyObject (Error Any))) + +(def: (tuple lux-object 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 (lux-object 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: tag-field (org/python/core/PyString::new runtime.variant-tag-field)) +(def: flag-field (org/python/core/PyString::new runtime.variant-flag-field)) +(def: value-field (org/python/core/PyString::new runtime.variant-value-field)) + +(def: (variant lux-object host-object) + (-> Translator Translator) + (case [(org/python/core/PyObject::__getitem__dict tag-field host-object) + (org/python/core/PyObject::__getitem__dict flag-field host-object) + (org/python/core/PyObject::__getitem__dict value-field 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)] + [(lux-object tag) + (#error.Success tag)] + [(lux-object value) + (#error.Success value)]) + (#error.Success [(java/lang/Long::intValue (:coerce java/lang/Long tag)) + (: Any + (case (python-type (:coerce org/python/core/PyObject flag)) + "NoneType" + (host.null) + + _ + "")) + value]) + + _ + (exception.throw ..unknown-kind-of-object host-object))) + +(def: (lux-object host-object) + Translator + (case (python-type host-object) + "str" + (#error.Success (org/python/core/PyObject::asString host-object)) + + "bool" + (#error.Success (org/python/core/PyObject::__nonzero__ host-object)) + + "float" + (#error.Success (org/python/core/PyObject::asDouble host-object)) + + (^or "int" "long") + (#error.Success (org/python/core/PyObject::asLong host-object)) + + "tuple" + (..tuple lux-object host-object) + + "dict" + (..variant lux-object host-object) + + "NoneType" + (#error.Success []) + + type + (exception.throw ..unknown-kind-of-object host-object))) + +(def: (expander macro inputs lux) + Expander + (#error.Failure "YOLO")) + +(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)] + (..lux-object 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)) diff --git a/new-luxc/project.clj b/new-luxc/project.clj index 182673452..b78f662fa 100644 --- a/new-luxc/project.clj +++ b/new-luxc/project.clj @@ -29,8 +29,6 @@ ;; [net.sandius.rembulan/rembulan-compiler "0.1-SNAPSHOT"] ;; ;; Ruby ;; [org.jruby/jruby-complete "9.1.16.0"] - ;; ;; Python - ;; [org.python/jython-standalone "2.7.1"] ;; ;; Scheme ;; [kawa-scheme/kawa-core "2.4"] ;; ;; Common Lisp diff --git a/new-luxc/source/luxc/lang/translation/python.lux b/new-luxc/source/luxc/lang/translation/python.lux deleted file mode 100644 index f466a6f29..000000000 --- a/new-luxc/source/luxc/lang/translation/python.lux +++ /dev/null @@ -1,211 +0,0 @@ -(.module: - lux - (lux (control ["ex" exception #+ exception:] - pipe - [monad #+ do]) - (data [bit] - [maybe] - ["e" error #+ Error] - [text "text/" Eq<Text>] - text/format - (coll [array])) - [macro] - [io #+ IO Process io] - [host #+ class: interface: object] - (world [file #+ File])) - (luxc [lang] - (lang [".L" variable #+ Register] - (host [python #+ Expression Statement])) - [".C" io])) - -(do-template [<name>] - [(exception: #export (<name> {message Text}) - message)] - - [No-Active-Module-Buffer] - [Cannot-Execute] - - [No-Anchor] - ) - -(host.import: java/lang/Object) - -(host.import: java/lang/String - (getBytes [String] #try (Array byte))) - -(host.import: java/lang/CharSequence) - -(host.import: java/lang/Appendable - (append [CharSequence] Appendable)) - -(host.import: java/lang/StringBuilder - (new []) - (toString [] String)) - -(host.import: org/python/core/PyObject) - -(host.import: org/python/util/PythonInterpreter - (new []) - (exec [String] void) - (eval [String] PyObject)) - -(type: #export Anchor [Text Register]) - -(type: #export Host - {#context [Text Nat] - #anchor (Maybe Anchor) - #loader (-> Statement (Error Any)) - #interpreter (-> Expression (Error PyObject)) - #module-buffer (Maybe StringBuilder) - #program-buffer StringBuilder}) - -(def: #export init - (IO Host) - (io (let [interpreter (PythonInterpreter::new [])] - {#context ["" +0] - #anchor #.None - #loader (function (_ code) - ("lux try" (io (PythonInterpreter::exec [(python.statement code)] interpreter)))) - #interpreter (function (_ code) - ("lux try" (io (PythonInterpreter::eval [(python.expression code)] interpreter)))) - #module-buffer #.None - #program-buffer (StringBuilder::new [])}))) - -(def: #export python-module-name Text "module.py") - -(def: #export init-module-buffer - (Meta Any) - (function (_ compiler) - (#e.Success [(update@ #.host - (|>> (:coerce Host) - (set@ #module-buffer (#.Some (StringBuilder::new []))) - (:coerce Nothing)) - compiler) - []]))) - -(def: #export (with-sub-context expr) - (All [a] (-> (Meta a) (Meta [Text a]))) - (function (_ compiler) - (let [old (:coerce Host (get@ #.host compiler)) - [old-name old-sub] (get@ #context old) - new-name (format old-name "___" (%i (.int old-sub)))] - (case (expr (set@ #.host - (:coerce Nothing (set@ #context [new-name +0] old)) - compiler)) - (#e.Success [compiler' output]) - (#e.Success [(update@ #.host - (|>> (:coerce Host) - (set@ #context [old-name (inc old-sub)]) - (:coerce Nothing)) - compiler') - [new-name output]]) - - (#e.Error error) - (#e.Error error))))) - -(def: #export context - (Meta Text) - (function (_ compiler) - (#e.Success [compiler - (|> (get@ #.host compiler) - (:coerce Host) - (get@ #context) - (let> [name sub] - name))]))) - -(def: #export (with-anchor anchor expr) - (All [a] (-> Anchor (Meta a) (Meta a))) - (function (_ compiler) - (let [old (:coerce Host (get@ #.host compiler))] - (case (expr (set@ #.host - (:coerce Nothing (set@ #anchor (#.Some anchor) old)) - compiler)) - (#e.Success [compiler' output]) - (#e.Success [(update@ #.host - (|>> (:coerce Host) - (set@ #anchor (get@ #anchor old)) - (:coerce Nothing)) - compiler') - output]) - - (#e.Error error) - (#e.Error error))))) - -(def: #export anchor - (Meta Anchor) - (function (_ compiler) - (case (|> compiler (get@ #.host) (:coerce Host) (get@ #anchor)) - (#.Some anchor) - (#e.Success [compiler anchor]) - - #.None - ((lang.throw No-Anchor "") compiler)))) - -(def: #export module-buffer - (Meta StringBuilder) - (function (_ compiler) - (case (|> compiler (get@ #.host) (:coerce Host) (get@ #module-buffer)) - #.None - ((lang.throw No-Active-Module-Buffer "") compiler) - - (#.Some module-buffer) - (#e.Success [compiler module-buffer])))) - -(def: #export program-buffer - (Meta StringBuilder) - (function (_ compiler) - (#e.Success [compiler (|> compiler (get@ #.host) (:coerce Host) (get@ #program-buffer))]))) - -(do-template [<name> <field> <inputT> <outputT>] - [(def: (<name> code) - (-> <inputT> (Meta <outputT>)) - (function (_ compiler) - (let [runner (|> compiler (get@ #.host) (:coerce Host) (get@ <field>))] - (case (runner code) - (#e.Error error) - ((lang.throw Cannot-Execute error) compiler) - - (#e.Success output) - (#e.Success [compiler output])))))] - - [load! #loader Statement Any] - [interpret #interpreter Expression PyObject] - ) - -(def: #export variant-tag-field "_lux_tag") -(def: #export variant-flag-field "_lux_flag") -(def: #export variant-value-field "_lux_value") - -(def: #export unit Text "") - -(def: #export (definition-name [module name]) - (-> Name Text) - (lang.normalize-name (format module "$" name))) - -(do-template [<name> <eval> <un-wrap> <inputT> <outputT>] - [(def: #export (<name> code) - (-> <inputT> (Meta <outputT>)) - (do macro.Monad<Meta> - [module-buffer module-buffer - #let [_ (Appendable::append [(:coerce CharSequence (<un-wrap> code))] - module-buffer)]] - (<eval> code)))] - - [save load! python.statement Statement Any] - [run interpret python.expression Expression PyObject] - ) - -(def: #export (save-module! target) - (-> File (Meta (Process Any))) - (do macro.Monad<Meta> - [module macro.current-module-name - module-buffer module-buffer - program-buffer program-buffer - #let [module-code (StringBuilder::toString [] module-buffer) - _ (Appendable::append [(:coerce CharSequence (format module-code "\n"))] - program-buffer)]] - (wrap (ioC.write target - (format (lang.normalize-name module) "/" python-module-name) - (|> module-code - (String::getBytes ["UTF-8"]) - e.assume))))) diff --git a/new-luxc/source/luxc/lang/translation/python/eval.jvm.lux b/new-luxc/source/luxc/lang/translation/python/eval.jvm.lux deleted file mode 100644 index 1ffcc5a1f..000000000 --- a/new-luxc/source/luxc/lang/translation/python/eval.jvm.lux +++ /dev/null @@ -1,149 +0,0 @@ -(.module: - lux - (lux (control ["ex" exception #+ exception:]) - (data [bit] - [maybe] - ["e" error #+ Error] - text/format - (coll [array])) - [host]) - (luxc [lang] - (lang (host [python #+ Expression Statement]))) - [//]) - -(do-template [<name>] - [(exception: #export (<name> {message Text}) - message)] - - [Not-A-Variant] - [Unknown-Kind-Of-Host-Object] - [Null-Has-No-Lux-Representation] - [Cannot-Evaluate] - ) - -(host.import: java/lang/Object - (toString [] String) - (getClass [] (Class Object))) - -(host.import: java/lang/Long - (intValue [] Integer)) - -(host.import: org/python/core/PyType - (getName [] String)) - -(host.import: org/python/core/PyString - (new [String])) - -(host.import: org/python/core/PyObject - (asLong [] long) - (asDouble [] double) - (asString [] String) - (__nonzero__ [] boolean) - (__getitem__ [int] #try PyObject) - (__getitem__ #as __getitem__dict [PyObject] #try PyObject) - (__len__ [] int) - (getType [] PyType)) - -(def: (tuple lux-object host-object) - (-> (-> PyObject (Error Any)) PyObject (Error Any)) - (let [size (:coerce Nat (PyObject::__len__ [] host-object))] - (loop [idx +0 - output (:coerce (Array Any) (array.new size))] - (if (n/< size idx) - (case (PyObject::__getitem__ [(:coerce Int idx)] host-object) - (#e.Error error) - (#e.Error error) - - (#e.Success value) - (case (lux-object value) - (#e.Error error) - (#e.Error error) - - (#e.Success lux-value) - (recur (inc idx) (array.write idx lux-value output)))) - (#e.Success output))))) - -(def: python-type - (-> PyObject Text) - (|>> (PyObject::getType []) (PyType::getName []) (:coerce Text))) - -(def: tag-field (PyString::new [//.variant-tag-field])) -(def: flag-field (PyString::new [//.variant-flag-field])) -(def: value-field (PyString::new [//.variant-value-field])) - -(def: (variant lux-object host-object) - (-> (-> PyObject (Error Any)) PyObject (Error Any)) - (case [(PyObject::__getitem__dict [tag-field] host-object) - (PyObject::__getitem__dict [flag-field] host-object) - (PyObject::__getitem__dict [value-field] host-object)] - (^or [(#e.Error error) _ _] [_ (#e.Error error) _] [_ _ (#e.Error error)]) - (#e.Error error) - - (^multi [(#e.Success tag) (#e.Success flag) (#e.Success value)] - [(lux-object tag) - (#e.Success tag)] - [(lux-object value) - (#e.Success value)]) - (#e.Success [(Long::intValue [] (:coerce Long tag)) - (: Any - (case (python-type (:coerce PyObject flag)) - "NoneType" - (host.null) - - _ - "")) - value]) - - _ - (ex.throw Not-A-Variant (Object::toString [] host-object)))) - -(def: (lux-object host-object) - (-> PyObject (Error Any)) - (case (python-type host-object) - "str" - (#e.Success (PyObject::asString [] host-object)) - - "bool" - (#e.Success (PyObject::__nonzero__ [] host-object)) - - "float" - (#e.Success (PyObject::asDouble [] host-object)) - - (^or "int" "long") - (#e.Success (PyObject::asLong [] host-object)) - - "tuple" - (tuple lux-object host-object) - - "dict" - (variant lux-object host-object) - - "NoneType" - (#e.Success []) - - type - (ex.throw Unknown-Kind-Of-Host-Object (format type " " (Object::toString [] host-object))))) - -(def: #export (eval code) - (-> Expression (Meta Any)) - (function (_ compiler) - (let [interpreter (|> compiler (get@ #.host) (:coerce //.Host) (get@ #//.interpreter))] - (case (interpreter code) - (#e.Error error) - (exec (log! (format "eval #e.Error\n" - "<< " (python.expression code) "\n" - error)) - ((lang.throw Cannot-Evaluate error) compiler)) - - (#e.Success output) - (case (lux-object output) - (#e.Success parsed-output) - (exec ## (log! (format "eval #e.Success\n" - ## "<< " (python.expression code))) - (#e.Success [compiler parsed-output])) - - (#e.Error error) - (exec (log! (format "eval #e.Error\n" - "<< " (python.expression code) "\n" - error)) - ((lang.throw Cannot-Evaluate error) compiler))))))) diff --git a/new-luxc/source/luxc/lang/translation/python/statement.jvm.lux b/new-luxc/source/luxc/lang/translation/python/statement.jvm.lux deleted file mode 100644 index bf34f3f2f..000000000 --- a/new-luxc/source/luxc/lang/translation/python/statement.jvm.lux +++ /dev/null @@ -1,48 +0,0 @@ -(.module: - lux - (lux (control [monad #+ do]) - [macro] - (data text/format)) - (luxc (lang [".L" module] - (host [python #+ Expression Statement @@]))) - [//] - (// [".T" runtime] - [".T" reference] - [".T" eval])) - -(def: #export (translate-def name expressionT expressionO metaV) - (-> Text Type Expression Code (Meta Any)) - (do macro.Monad<Meta> - [current-module macro.current-module-name - #let [def-name [current-module name]]] - (case (macro.get-identifier-ann (name-of #.alias) metaV) - (#.Some real-def) - (do @ - [[realT realA realV] (macro.find-def real-def) - _ (moduleL.define def-name [realT metaV realV])] - (wrap [])) - - _ - (do @ - [#let [def-name (referenceT.global def-name)] - _ (//.save (python.set! (list def-name) expressionO)) - expressionV (evalT.eval (@@ def-name)) - _ (moduleL.define def-name [expressionT metaV expressionV]) - _ (if (macro.type? metaV) - (case (macro.declared-tags metaV) - #.Nil - (wrap []) - - tags - (moduleL.declare-tags tags (macro.export? metaV) (:coerce Type expressionV))) - (wrap [])) - #let [_ (log! (format "DEF " (%name def-name)))]] - (wrap [])) - ))) - -(def: #export (translate-program programO) - (-> Expression (Meta Statement)) - (macro.fail "translate-program NOT IMPLEMENTED YET") - ## (hostT.save (format "var " (referenceT.variable +0) " = " runtimeT.lux//program-args "();" - ## "(" programO ")(null);")) - ) diff --git a/stdlib/source/lux/tool/compiler/phase/generation.lux b/stdlib/source/lux/tool/compiler/phase/generation.lux index a22077df4..882e1127f 100644 --- a/stdlib/source/lux/tool/compiler/phase/generation.lux +++ b/stdlib/source/lux/tool/compiler/phase/generation.lux @@ -52,7 +52,7 @@ (signature: #export (Host expression statement) (: (-> Text expression (Error Any)) evaluate!) - (: (-> Text statement (Error Any)) + (: (-> Text statement (Error Nothing)) execute!) (: (-> Name expression (Error [Text Any statement])) define!)) @@ -179,6 +179,11 @@ _ (extension.update (update@ #counter inc))] (wrap count))) +(def: #export (gensym prefix) + (All [anchor expression statement] + (-> Text (Operation anchor expression statement Text))) + (:: //.monad map (|>> %n (format prefix)) ..next)) + (do-template [<name> <inputT>] [(def: #export (<name> label code) (All [anchor expression statement] @@ -210,8 +215,8 @@ (All [anchor expression statement] (-> Name statement (Operation anchor expression statement Any))) (do //.monad - [count ..next - _ (execute! (format "save" (%n count)) code) + [label (..gensym "save") + _ (execute! label code) ?buffer (extension.read (get@ #buffer))] (case ?buffer (#.Some buffer) diff --git a/stdlib/source/lux/tool/compiler/phase/generation/python/function.lux b/stdlib/source/lux/tool/compiler/phase/generation/python/function.lux index c92f6dd37..3ba95e0f3 100644 --- a/stdlib/source/lux/tool/compiler/phase/generation/python/function.lux +++ b/stdlib/source/lux/tool/compiler/phase/generation/python/function.lux @@ -44,7 +44,7 @@ _ (do ////.monad - [@closure (:: @ map (|>> %n (format "closure") _.var) ///.next) + [@closure (:: @ map _.var (///.gensym "closure")) _ (///.save! ["" (_.code @closure)] (_.def @closure (|> (list.enumerate inits) @@ -54,8 +54,6 @@ (_.return (_.var function-name)))))] (wrap (_.apply/* @closure inits))))) -(def: @curried (_.var "curried")) - (def: input (|>> inc //case.register)) @@ -69,7 +67,8 @@ (generate bodyS)))) closureO+ (: (Operation (List (Expression Any))) (monad.map @ (:: //reference.system variable) environment)) - #let [arityO (|> arity .int _.int) + #let [@curried (_.var "curried") + arityO (|> arity .int _.int) @num-args (_.var "num_args") @self (_.var function-name) apply-poly (.function (_ args func) @@ -82,7 +81,7 @@ initialize-self! (list.indices arity))]] (with-closure function-name closureO+ - (_.def @self (list) + (_.def @self (list (_.poly @curried)) ($_ _.then (_.set (list @num-args) (_.len/1 @curried)) (_.cond (list [(|> @num-args (_.= arityO)) diff --git a/stdlib/source/lux/tool/compiler/phase/generation/python/runtime.lux b/stdlib/source/lux/tool/compiler/phase/generation/python/runtime.lux index fd847af16..e3a8a4537 100644 --- a/stdlib/source/lux/tool/compiler/phase/generation/python/runtime.lux +++ b/stdlib/source/lux/tool/compiler/phase/generation/python/runtime.lux @@ -78,7 +78,7 @@ (def: runtime-name (-> Text SVar) (|>> /////name.normalize - (format ..prefix "$") + (format ..prefix "_") _.var)) (def: (feature name definition) |