diff options
Diffstat (limited to '')
-rw-r--r-- | new-luxc/source/luxc/compiler.lux | 154 | ||||
-rw-r--r-- | new-luxc/source/luxc/compiler/expr.jvm.lux | 15 | ||||
-rw-r--r-- | new-luxc/source/luxc/compiler/runtime.jvm.lux | 7 | ||||
-rw-r--r-- | new-luxc/source/luxc/compiler/statement.jvm.lux | 6 |
4 files changed, 125 insertions, 57 deletions
diff --git a/new-luxc/source/luxc/compiler.lux b/new-luxc/source/luxc/compiler.lux index 205c62df0..4ac865786 100644 --- a/new-luxc/source/luxc/compiler.lux +++ b/new-luxc/source/luxc/compiler.lux @@ -1,14 +1,15 @@ (;module: lux (lux (control monad) - [io #- run] + (concurrency ["P" promise]) (data ["E" error] - [text "T/" Eq<Text>] + [text "T/" Hash<Text>] text/format) [macro #+ Monad<Lux>]) (luxc ["&" base] ["&;" io] ["&;" module] + ["&;" parser] (compiler ["&&;" runtime] ["&&;" statement]) )) @@ -36,60 +37,119 @@ [result action] (exhaust action))) -(def: (compile-module source-dirs module-name compiler-state) - (-> (List &;Path) Text Compiler (IO (Error Compiler))) - (do Monad<IO> - [[file-name file-content] (&io;read-module source-dirs module-name) - #let [file-hash (T/hash file-content)] - #let [result (macro;run compiler-state - (do Monad<Lux> - [module-exists? (&module;exists? module-name)] - (if module-exists? - (&;fail (format "Cannot re-define a module: " module-name)) - (wrap []))))]] - (case result - (#E;Success [compiler-state _]) - (let [result (macro;run compiler-state - (do Monad<Lux> - [_ (&module;create module-name file-hash) - _ (&module;flag-active module-name) - _ (if (T/= "lux" module-name) - &&runtime;compile-runtime - (wrap [])) - _ (exhaust - (do @ - [ast parse] - (compile ast))) - _ (&module;flag-compiled module-name)] - (&module;generate-module file-hash module-name)))] - (case result - (#E;Success [compiler-state module-descriptor]) - (do @ - [_ (&io;write-module module-name module-descriptor)] - (wrap (#E;Success compiler-state))) +(def: (ensure-new-module! file-hash module-name) + (-> Nat Text (Lux Unit)) + (do Monad<Lux> + [module-exists? (macro;module-exists? module-name) + _ (: (Lux Unit) + (if module-exists? + (&;fail (format "Cannot re-define a module: " module-name)) + (wrap []))) + _ (&module;create file-hash module-name)] + (wrap []))) + +(def: prelude Text "lux") + +(def: (with-active-compilation [module-name file-name source-code] action) + (All [a] (-> [Text Text Text] (Lux a) (Lux a))) + (do Monad<Lux> + [_ (ensure-new-module! (T/hash source-code) module-name) + #let [init-cursor [file-name +0 +0]] + output (&;with-source-code [init-cursor source-code] + action) + _ (&module;flag-compiled! module-name)] + (wrap output))) +(def: parse + (Lux AST) + (function [compiler] + (case (&parser;parse (get@ #;source compiler)) + (#E;Error error) + (#E;Error error) + + (#E;Success [source' output]) + (#E;Success [(set@ #;source source' compiler) + output])))) + +(def: (compile-module source-dirs module-name compiler) + (-> (List &;Path) Text Compiler (P;Promise (E;Error Compiler))) + (do P;Monad<Promise> + [?input (&io;read-module source-dirs module-name)] + (case ?input + (#E;Success [file-name file-content]) + (let [compilation (do Monad<Lux> + [_ (with-active-compilation [module-name + file-name + file-content] + (exhaust + (do @ + [ast parse] + (compile ast))))] + (wrap []) + ## (&module;generate-descriptor module-name) + )] + (case (macro;run' compiler compilation) + (#E;Success [compiler module-descriptor]) + (do @ + [## _ (&io;write-module module-name module-descriptor) + ] + (wrap (#E;Success compiler))) + (#E;Error error) (wrap (#E;Error error)))) - + (#E;Error error) (wrap (#E;Error error))))) -(def: (or-crash! action) - (All [a] (-> (IO (E;Error a)) (IO a))) - (do Monad<IO> - [result action] - (case result - (#E;Success output) - (wrap output) +(type: Host Unit) + +(def: init-host Host []) + +(def: init-cursor Cursor ["" +0 +0]) + +(def: init-type-context + Type-Context + {#;ex-counter +0 + #;var-counter +0 + #;var-bindings (list)}) +(def: compiler-version Text "0.6.0") + +(def: init-compiler-info + Compiler-Info + {#;compiler-version compiler-version + #;compiler-mode #;Release}) + +(def: (init-compiler host) + (-> Host Compiler) + {#;info init-compiler-info + #;source [init-cursor ""] + #;cursor init-cursor + #;modules (list) + #;scopes (list) + #;type-context init-type-context + #;expected #;None + #;seed +0 + #;scope-type-vars (list) + #;host (:! Void host)}) + +(def: (or-crash! action) + (All [a] (-> (P;Promise (E;Error a)) (P;Promise a))) + (do P;Monad<Promise> + [?output action] + (case ?output (#E;Error error) - (error! (format "Compilation failed:\n" error))))) + (error! error) + + (#E;Success output) + (wrap output)))) (def: #export (compile-program mode program target sources) - (-> &;Mode &;Path &;Path (List &;Path) (IO Unit)) - (do Monad<IO> - [#let [compiler-state (init-compiler-state mode host-state)] - compiler-state (or-crash! (compile-module source-dirs "lux" compiler-state)) - compiler-state (or-crash! (compile-module source-dirs program compiler-state)) + (-> &;Mode &;Path &;Path (List &;Path) (P;Promise Unit)) + (do P;Monad<Promise> + [#let [compiler (init-compiler init-host)] + _ (or-crash! (&&runtime;compile-runtime [])) + compiler (or-crash! (compile-module sources prelude compiler)) + compiler (or-crash! (compile-module sources program compiler)) #let [_ (log! "Compilation complete!")]] (wrap []))) diff --git a/new-luxc/source/luxc/compiler/expr.jvm.lux b/new-luxc/source/luxc/compiler/expr.jvm.lux index 6655abd5f..138d0d540 100644 --- a/new-luxc/source/luxc/compiler/expr.jvm.lux +++ b/new-luxc/source/luxc/compiler/expr.jvm.lux @@ -2,26 +2,29 @@ lux (lux (control monad) (data text/format) - [macro #+ Monad<Lux>]) + [macro #+ Monad<Lux> "Lux/" Monad<Lux>]) (luxc ["&" base] - ["&;" module] - ["&;" env] + lang ["&;" analyser] - ["&;" synthesizer #+ Synthesis])) + ["&;" synthesizer])) (type: #export JVM-Bytecode Void) -(type: Compiled +(type: #export Compiled JVM-Bytecode) (def: (compile-synthesis synthesis) (-> Synthesis Compiled) (undefined)) +(def: (eval type code) + Eval + (undefined)) + (def: #export (compile input) (-> AST (Lux Compiled)) (|> input - &analyser;analyse + (&analyser;analyse eval) (Lux/map &synthesizer;synthesize) (Lux/map compile-synthesis))) diff --git a/new-luxc/source/luxc/compiler/runtime.jvm.lux b/new-luxc/source/luxc/compiler/runtime.jvm.lux index 2d48b3617..b6cebb193 100644 --- a/new-luxc/source/luxc/compiler/runtime.jvm.lux +++ b/new-luxc/source/luxc/compiler/runtime.jvm.lux @@ -1,6 +1,11 @@ (;module: lux (lux (control monad) - (data text/format)) + (concurrency ["P" promise "P/" Monad<Promise>]) + (data text/format + ["E" error])) (luxc ["&" base])) +(def: #export (compile-runtime _) + (-> Top (P;Promise (E;Error Unit))) + (P/wrap (#E;Success []))) diff --git a/new-luxc/source/luxc/compiler/statement.jvm.lux b/new-luxc/source/luxc/compiler/statement.jvm.lux index c4c23746e..0e53ba37d 100644 --- a/new-luxc/source/luxc/compiler/statement.jvm.lux +++ b/new-luxc/source/luxc/compiler/statement.jvm.lux @@ -11,16 +11,16 @@ ["&;" env] (compiler ["&;" expr]))) -(def: (compile-def def-name def-value def-meta) +(def: #export (compile-def def-name def-value def-meta) (-> Text AST AST (Lux Unit)) (do Monad<Lux> [=def-value (&expr;compile def-value) =def-meta (&expr;compile def-meta)] (undefined))) -(def: (compile-program prog-args prog-body) +(def: #export (compile-program prog-args prog-body) (-> Text AST (Lux Unit)) (do Monad<Lux> [=prog-body (&env;with-local [prog-args (type (List Text))] - (&expr;compile prog-body))] + (&expr;compile prog-body))] (undefined))) |