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 /new-luxc/source/luxc/lang | |
parent | e76fd0e68475f65e75c1339cfcef6a8114707472 (diff) |
Got the Python compiler running.
Diffstat (limited to 'new-luxc/source/luxc/lang')
3 files changed, 0 insertions, 408 deletions
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);")) - ) |