From ae2d5697d93a45dcbff768c32c4dc8fb291096cd Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sat, 7 Jan 2023 18:55:20 -0400 Subject: Now wrapping C++ values inside a universal box. --- lux-c++/source/program.lux | 65 ++- lux-js/commands.md | 6 +- lux-js/source/program.lux | 3 +- stdlib/source/library/lux/data/color/cmyk.lux | 2 +- stdlib/source/library/lux/data/color/hsb.lux | 52 ++- stdlib/source/library/lux/data/color/hsl.lux | 2 +- stdlib/source/library/lux/ffi.lux | 484 ++++++++++++++------- .../language/lux/phase/translation/c++/runtime.lux | 63 +++ .../language/lux/phase/translation/c++/type.lux | 26 ++ .../language/lux/phase/translation/js/runtime.lux | 4 +- .../language/lux/phase/translation/jvm/debug.lux | 31 -- .../language/lux/phase/translation/jvm/runtime.lux | 4 +- .../language/lux/phase/translation/jvm/type.lux | 5 - stdlib/source/library/lux/meta/target/c++.lux | 284 +++++++++++- stdlib/source/library/lux/meta/target/jvm/type.lux | 47 +- stdlib/source/library/lux/meta/target/python.lux | 18 +- stdlib/source/specification/aedifex/repository.lux | 59 --- stdlib/source/specification/compositor.lux | 69 --- .../specification/compositor/analysis/type.lux | 65 --- stdlib/source/specification/compositor/common.lux | 80 ---- .../specification/compositor/generation/case.lux | 290 ------------ .../specification/compositor/generation/common.lux | 350 --------------- .../compositor/generation/function.lux | 96 ---- .../compositor/generation/primitive.lux | 51 --- .../compositor/generation/reference.lux | 64 --- .../compositor/generation/structure.lux | 93 ---- stdlib/source/test/aedifex.lux | 2 +- stdlib/source/test/aedifex/artifact.lux | 7 +- .../source/test/aedifex/dependency/deployment.lux | 31 +- stdlib/source/test/aedifex/repository.lux | 64 ++- .../test/lux/meta/compiler/language/lux/phase.lux | 4 +- .../language/lux/phase/translation/jvm/type.lux | 45 ++ .../lux/meta/compiler/meta/archive/artifact.lux | 5 +- 33 files changed, 905 insertions(+), 1566 deletions(-) create mode 100644 stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/runtime.lux create mode 100644 stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/type.lux delete mode 100644 stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/debug.lux delete mode 100644 stdlib/source/specification/aedifex/repository.lux delete mode 100644 stdlib/source/specification/compositor.lux delete mode 100644 stdlib/source/specification/compositor/analysis/type.lux delete mode 100644 stdlib/source/specification/compositor/common.lux delete mode 100644 stdlib/source/specification/compositor/generation/case.lux delete mode 100644 stdlib/source/specification/compositor/generation/common.lux delete mode 100644 stdlib/source/specification/compositor/generation/function.lux delete mode 100644 stdlib/source/specification/compositor/generation/primitive.lux delete mode 100644 stdlib/source/specification/compositor/generation/reference.lux delete mode 100644 stdlib/source/specification/compositor/generation/structure.lux create mode 100644 stdlib/source/test/lux/meta/compiler/language/lux/phase/translation/jvm/type.lux diff --git a/lux-c++/source/program.lux b/lux-c++/source/program.lux index cb2b96029..3c3a037fc 100644 --- a/lux-c++/source/program.lux +++ b/lux-c++/source/program.lux @@ -52,6 +52,8 @@ [translation ["[0]" reference] [c++ + ["[0]" runtime] + ["[0]" type] ["[0]" primitive]]]]]] [default ["[0]" platform (.only Platform)]] @@ -70,26 +72,49 @@ ("static" cppdef [Text] "io" Bit) ("static" gbl (ffi.Object Any))) +(def standard_out (_.global [_.standard "cout"] (list))) +(def \n (_.global [_.standard "endl"] (list))) + (def (print it) (-> _.Expression - Text) - (%.format "std::cout << " - (_.code it) - " << std::endl;")) + _.Statement) + (|> standard_out + (_.<< it) + (_.<< \n) + _.;)) + +(with_expansions [ "say_hello"] + (def _ + (program [] + (do io.monad + [? (cppyy::cppdef [(_.code (all _.then + runtime.declaration + + (_.include "iostream") + (_.function (_.local ) + (list) + (list) + _.void + (all _.then + ... (print (primitive.bit true)) + ... (print (primitive.i64 +123)) + ... (print (primitive.f64 -456.789)) + ... (print (primitive.text "YOLO")) + + ... (print (runtime.lux_value type.bit (primitive.bit true))) + ... (print (runtime.lux_value type.i64 (primitive.i64 +123))) + ... (print (runtime.lux_value type.f64 (primitive.f64 -456.789))) + ... (print (runtime.lux_value type.text (primitive.text "YOLO"))) -(def _ - (program [] - (do io.monad - [? (cppyy::cppdef [(%.format "void say_hello() { " - (print (primitive.bit true)) - (print (primitive.i64 +123)) - (print (primitive.f64 -456.789)) - (print (primitive.text "YOLO")) - " }")]) - .let [_ (debug.log! (%.format "BEFORE " (%.bit ?)))] - global (cppyy::gbl) - .let [say_hello (as ffi.Function - (.python_object_get# "say_hello" global)) - _ (debug.log! "AFTER") - _ (.python_apply# say_hello [])]] - (in (debug.log! "Hello, C++"))))) + ... (print (_.deref (runtime.host_value type.bit (runtime.lux_value type.bit (primitive.bit true))))) + (print (_.deref (runtime.host_value type.i64 (runtime.lux_value type.i64 (primitive.i64 +123))))) + ... (print (_.deref (runtime.host_value type.f64 (runtime.lux_value type.f64 (primitive.f64 -456.789))))) + ... (print (_.deref (runtime.host_value type.text (runtime.lux_value type.text (primitive.text "YOLO"))))) + ))))]) + .let [_ (debug.log! (%.format "BEFORE " (%.bit ?)))] + global (cppyy::gbl) + .let [say_hello (as ffi.Function + (.python_object_get# global)) + _ (debug.log! "AFTER") + _ (.python_apply# say_hello [])]] + (in (debug.log! "Hello, C++")))))) diff --git a/lux-js/commands.md b/lux-js/commands.md index 6730dad02..0b14b046b 100644 --- a/lux-js/commands.md +++ b/lux-js/commands.md @@ -8,10 +8,6 @@ cd ~/lux/lux-js/ \ # Build ``` -cd ~/lux/lux-js/ \ -&& lux clean \ -&& lux with js auto build - ## Build JVM-based compiler cd ~/lux/lux-js/ \ && lux clean \ @@ -21,7 +17,7 @@ cd ~/lux/lux-js/ \ ## Use JVM-based compiler to produce a JS/Node-based compiler. cd ~/lux/lux-js/ \ && lux clean \ -&& time java -jar jvm_based_compiler.jar build --source ~/lux/lux-js/source --target ~/lux/lux-js/target --module program --program _ \ +&& time java -Xss2m -jar jvm_based_compiler.jar build --source ~/lux/lux-js/source --target ~/lux/lux-js/target --module program --program _ \ && mv target/program.js node_based_compiler.js ## Use JS/Node-based compiler to produce another JS/Node-based compiler. diff --git a/lux-js/source/program.lux b/lux-js/source/program.lux index 527ca0b73..7754db366 100644 --- a/lux-js/source/program.lux +++ b/lux-js/source/program.lux @@ -37,7 +37,6 @@ ["^" pattern] ["[0]" template]] [compiler - ["[0]" phase (.only Operation Phase)] [reference [variable (.only Register)]] [language @@ -46,7 +45,7 @@ [translation (.only Host)] [analysis [macro (.only Expander)]] - [phase + ["[0]" phase (.only Operation Phase) ["[0]" extension (.only Extender Handler) ["[0]" analysis ["[1]" js]] diff --git a/stdlib/source/library/lux/data/color/cmyk.lux b/stdlib/source/library/lux/data/color/cmyk.lux index dc85a4a97..4f2b46238 100644 --- a/stdlib/source/library/lux/data/color/cmyk.lux +++ b/stdlib/source/library/lux/data/color/cmyk.lux @@ -68,7 +68,7 @@ (def up (-> Frac Nat) - (|>> (f.* rgb_factor) f.int .nat)) + (|>> (f.* rgb_factor) f.round f.int .nat)) (def (opposite it) (-> Frac diff --git a/stdlib/source/library/lux/data/color/hsb.lux b/stdlib/source/library/lux/data/color/hsb.lux index ede1ffd08..5e7216de0 100644 --- a/stdlib/source/library/lux/data/color/hsb.lux +++ b/stdlib/source/library/lux/data/color/hsb.lux @@ -92,7 +92,7 @@ (def up (-> Frac Nat) - (|>> (f.* rgb_factor) f.int .nat)) + (|>> (f.* rgb_factor) f.round f.int .nat)) (def .public (of_rgb it) (-> RGB @@ -101,30 +101,36 @@ green (..down (the rgb.#green it)) blue (..down (the rgb.#blue it)) - max (all f.max red green blue) - min (all f.min red green blue) + brightness (all f.max red green blue) + range (all f.min red green blue) - brightness max - diff (|> max (f.- min)) - saturation (if (f.= +0.0 max) + chroma (|> brightness (f.- range)) + saturation (if (f.= +0.0 brightness) +0.0 - (|> diff (f./ max)))] + (|> chroma (f./ brightness)))] (nominal.abstraction - [#hue (if (f.= max min) - ... Achromatic - +0.0 - ... Chromatic - (cond (f.= max red) - (|> green (f.- blue) (f./ diff) - (f.+ (if (f.< blue green) +6.0 +0.0))) - - (f.= max green) - (|> blue (f.- red) (f./ diff) - (f.+ +2.0)) - - ... (f.= max blue) - (|> red (f.- green) (f./ diff) - (f.+ +4.0)))) + [#hue (cond (f.= +0.0 chroma) + ... Achromatic + +0.0 + ... Chromatic + (and (f.= brightness red) + (not (f.= red blue))) + (|> green (f.- blue) + (f./ chroma) + (f.+ +0.0) + (f./ +6.0)) + + (f.= brightness green) + (|> blue (f.- red) + (f./ chroma) + (f.+ +2.0) + (f./ +6.0)) + + ... (f.= brightness blue) + (|> red (f.- green) + (f./ chroma) + (f.+ +4.0) + (f./ +6.0))) #saturation saturation #brightness brightness]))) @@ -140,7 +146,7 @@ t (|> +1.0 (f.- (|> +1.0 (f.- f) (f.* saturation))) (f.* brightness)) v brightness mod (|> i (f.% +6.0) f.int .nat) - + red (when mod 0 v 1 q 2 p 3 p 4 t 5 v _ (undefined)) green (when mod 0 t 1 v 2 v 3 q 4 p 5 p _ (undefined)) blue (when mod 0 p 1 p 2 t 3 v 4 v 5 q _ (undefined))] diff --git a/stdlib/source/library/lux/data/color/hsl.lux b/stdlib/source/library/lux/data/color/hsl.lux index df8fb8a82..ac0b637c8 100644 --- a/stdlib/source/library/lux/data/color/hsl.lux +++ b/stdlib/source/library/lux/data/color/hsl.lux @@ -28,7 +28,7 @@ (def up (-> Frac Nat) - (|>> (f.* rgb_factor) f.int .nat)) + (|>> (f.* rgb_factor) f.round f.int .nat)) (type .public Value Frac) diff --git a/stdlib/source/library/lux/ffi.lux b/stdlib/source/library/lux/ffi.lux index 6d0f9d390..8afc76f04 100644 --- a/stdlib/source/library/lux/ffi.lux +++ b/stdlib/source/library/lux/ffi.lux @@ -1,13 +1,13 @@ (.require [library - [lux (.except Symbol Alias Global Declaration global function type_of undefined alias) + [lux (.except Symbol Alias Global Declaration Pattern global function type_of undefined alias) [abstract ["[0]" monad (.only do)]] [control ["<>" parser (.use "[1]#[0]" monad)] ["[0]" io] ["[0]" maybe (.use "[1]#[0]" functor)] - ["[0]" try]] + ["[0]" try (.only Try)]] [data ["[0]" product] ["[0]" text (.use "[1]#[0]" equivalence) @@ -15,174 +15,324 @@ [collection ["[0]" list (.use "[1]#[0]" monoid monad mix)]]] ["[0]" meta (.only) + ["[0]" location] ["[0]" code (.only) ["<[1]>" \\parser (.only Parser)]] ["[0]" macro (.only with_symbols) [syntax (.only syntax)] ["[0]" template]] - [type + [type (.only sharing) ["[0]" nominal (.except #name def)]] ["@" target (.only) - ["[0]" js]]]]]) - -... These extensions must be defined this way because importing any of the modules -... normally used when writing extensions would introduce a circular dependency -... because the Archive type depends on Binary, and that module depends on this ffi module. -(def extension_name - (syntax (_ []) - (do meta.monad - [module meta.current_module_name - unique_id meta.seed] - (in (list (code.text (%.format module " " (%.nat unique_id)))))))) - -(def extension_analysis - (template (_ ) - [{5 #1 [ ]}])) - -(def text_analysis - (template (_ ) - [{0 #0 {5 #1 }}])) - -(def analysis - (template (_ ) - [("lux def analysis" - (.function (_ name phase archive inputs) - (.function (_ state) - (let [ [name phase archive state]] - (when (.result inputs) - {try.#Failure error} - {try.#Failure (%.format "Invalid inputs for extension: " (%.text name) - text.\n error)} - - {try.#Success } - )))))])) - -(def translation - (template (_ ) - [("lux def translation" - (.function (_ name phase archive inputs) - (.function (_ state) - (let [ [name phase archive state]] - (when inputs - - - - _ - {try.#Failure (%.format "Invalid inputs for extension: " (%.text name))})))))])) - -(for @.js (with_expansions [ (..extension_name) - (..extension_name) - (..extension_name) - (..extension_name)] - (these (analysis - [name phase archive state] - .end - _ - {try.#Success [state (extension_analysis name (list))]}) - - (translation - [name phase archive state] + ["[0]" js] + ["[0]" python]] + [compiler + [arity (.only Arity)] + [reference (.only Reference) + [variable (.only Register)]] + [language + [lux + ["[0]" analysis + ["[1]/[0]" simple] + [complex (.only Complex)] + [pattern (.only Pattern)]] + ["[0]" synthesis + ["[1]/[0]" simple] + [access (.only Access) + [side (.only Side)] + ["[1]/[0]" member]]]]]]]]]) + +(for @.js (these (type (Analysis_Branch of) + [Pattern of]) + + (type (Analysis_Match of) + [(Analysis_Branch of) (List (Analysis_Branch of))]) + + (type (Environment of) + (List of)) + + (type (Extension of) + [.Symbol (List of)]) + + (with_expansions [@ ($ (Analysis~' $))] + (type (Analysis~' $) + (Or analysis/simple.Simple + (Complex @) + Reference + [@ (Analysis_Match @)] + [(Environment @) @] + [@ @] + (Extension @)))) + + (type Analysis~ + (Ann Location + (Analysis~' (Ann Location)))) + + (def extension_analysis + (template (_ ) + [(is Analysis~ + [location.dummy {5 #1 [ ]}])])) + + (def text_analysis + (template (_ ) + [(is Analysis~ + [location.dummy {0 #0 {5 #1 }}])])) + + (def analysis + (template (_ ) + [(def .public + (<| (as .Analysis) + (.function (_ phase archive inputs)) + (.function (_ state)) + (let [ [phase archive state]] + (when (.result inputs) + {try.#Success } + + + {try.#Failure error} + {try.#Failure (%.format "Invalid inputs for extension..." + text.\n error)}))))])) + + (type (Synthesis_Road value next) + [value next]) + + (type (Synthesis_Fork value next) + [(Synthesis_Road value next) + (List (Synthesis_Road value next))]) + + (type (Synthesis_Path s) + (Or Any + Register + Access + [Bit (Synthesis_Path s) (Maybe (Synthesis_Path s))] + (Synthesis_Fork I64 (Synthesis_Path s)) + (Synthesis_Fork Frac (Synthesis_Path s)) + (Synthesis_Fork Text (Synthesis_Path s)) + [(Synthesis_Path s) (Synthesis_Path s)] + [(Synthesis_Path s) (Synthesis_Path s)] + s)) + + (type (Synthesis_Abstraction s) + [(Environment s) Arity s]) + + (type (Synthesis_Apply s) + [s (List s)]) + + (type (Synthesis_Function s) + (Or (Synthesis_Abstraction s) + (Synthesis_Apply s))) + + (type (Synthesis_Branch s) + (Or [s s] + [s Register s] + [s s s] + [(List synthesis/member.Member) s] + [s (Synthesis_Path s)])) + + (type (Synthesis_Scope s) + [Register (List s) s]) + + (type (Synthesis_Loop s) + (Or (Synthesis_Scope s) + (List s))) + + (type (Synthesis_Control s) + (Or (Synthesis_Branch s) + (Synthesis_Loop s) + (Synthesis_Function s))) + + (with_expansions [@ ($ (Synthesis~' $))] + (type (Synthesis~' $) + (Or synthesis/simple.Simple + (Complex @) + Reference + (Synthesis_Control @) + (Extension @)))) + + (type Synthesis~ + (Ann Location + (Synthesis~' (Ann Location)))) + + (def text_synthesis + (template (_ <@> ) + [[<@> {0 #0 {2 #1 }}]])) + + (def translation + (syntax (_ [ .any + .any + (<>.or .local + .any) + .any]) + (with_symbols ['_ 'phase 'archive 'inputs 'state] + (in (list (` (def .public (, ) + (<| (as .Translation) + (.function ((, '_) (, 'phase) (, 'archive) (, 'inputs))) + (.function ((, '_) (, 'state))) + (let [(, ) [(, 'phase) (, 'archive) (, 'state)]] + (, (when + {.#Left } + (` (when (is (List Synthesis~) (, 'inputs)) + (, (code.local )) + (, ))) + + {.#Right } + (` (when (is (List Synthesis~) (, 'inputs)) + (, ) + (, ) + + (, '_) + {try.#Failure "Invalid inputs for extension."}))))))))))))) + + (translation undefined?|translation + [phase archive state] + (list it) + (do try.monad + [.let [phase (sharing [archive it state] + (is [archive it state] + [archive it state]) + (is (-> archive it state + (Try [state js.Expression])) + (as_expected phase)))] + [state it] (phase archive it state)] + (in [state (js.= js.undefined it)]))) + + (analysis undefined?|analysis + [phase archive state] + .any + it + (do try.monad + [.let [phase (sharing [archive state] + (is [archive state] + [archive state]) + (is (-> archive Code state + (Try [state Analysis~])) + (as_expected phase)))] + [state it] (phase archive (` (.is .Any (, it))) state)] + (in [state (extension_analysis (symbol ..undefined?|translation) + (list it))]))) + + (def .public undefined? + (template (undefined? ) + [(.as .Bit (.is .Any (undefined?|analysis )))])) + + (translation undefined|translation + [phase archive state] + (list) + {try.#Success [state js.undefined]}) + + (analysis undefined|analysis + [phase archive state] + .end + _ + {try.#Success [state (extension_analysis (symbol ..undefined|translation) + (list))]}) + + (def .public undefined + (template (_) + [(.is ..Undefined (undefined|analysis))])) + + (def (pairs it) + (All (_ a) (-> (List a) (List [a a]))) + (when it + (list.partial left right tail) + (list.partial [left right] (pairs tail)) + (list) - {try.#Success [state js.undefined]}) - - (def .public undefined - (template (undefined) - [(.is ..Undefined ())])) - - (analysis - [name phase archive state] - .any - it - (do try.monad - [[state it] (phase archive (` (.is .Any (, it))) state)] - (in [state (extension_analysis name (list it))]))) - - (translation - [name phase archive state] - (list it) - (do try.monad - [[state it] (phase archive it state)] - (in [state (js.= js.undefined it)]))) - - (def .public undefined? - (template (undefined? ) - [(.as .Bit (.is .Any ( )))])) - - (analysis - [name phase archive state] - (<>.some (<>.and .text .any)) - it - (do [! try.monad] - [[state output] (monad.mix ! (.function (_ [key value] [state output]) - (do ! - [[state value] (phase archive (` (.is .Any (, value))) state)] - (in [state (list.partial value (text_analysis key) output)]))) - [state (list)] - it)] - (in [state (extension_analysis name (list.reversed output))]))) - - (def text_synthesis - (template (_ ) - [{0 #0 {2 #1 }}])) - - (def (pairs it) - (All (_ a) (-> (List a) (List [a a]))) - (when it - (list.partial left right tail) - (list.partial [left right] (pairs tail)) - - (list) - (list) - - _ - (.undefined))) - - (translation - [name phase archive state] - (list.partial head_key head_value tail) - (do [! try.monad] - [[state output] (monad.mix ! - (.function (_ [key value] [state output]) - (when key - (text_synthesis key) - (do try.monad - [[state value] (phase archive value state)] - (in [state (list.partial [key value] output)])) - - _ - (.undefined))) - [state (list)] - (pairs (list.partial head_key head_value tail)))] - (in [state (js.object (list.reversed output))]))) - - (def .public object - (syntax (_ [it (<>.some .any)]) - (in (list (` (.as (..Object .Any) - ( (,* it)))))))) - - (analysis - [name phase archive state] - (all <>.and .text .any .any) - [field value object] - (do try.monad - [[state value] (phase archive (` (.is .Any (, value))) state) - [state object] (phase archive (` (.is (..Object .Any) (, object))) state)] - (in [state (extension_analysis name (list (text_analysis field) value object))]))) - - (translation - [name phase archive state] - (list (text_synthesis field) value object) - (do try.monad - [[state value] (phase archive value state) - [state object] (phase archive object state)] - (in [state (js.set (js.the field object) value)]))) - - (def .public set - (syntax (_ [field .any - value .any - object .any]) - (in (list (` (.as .Any ( (, field) (, value) (, object)))))))) - )) + (list) + + _ + (.undefined))) + + (translation object|translation + [phase archive state] + it + (do [! try.monad] + [.let [phase (sharing [archive state] + (is [archive state] + [archive state]) + (is (-> archive Synthesis~ state + (Try [state js.Expression])) + (as_expected phase)))] + [state output] (monad.mix ! + (sharing [state] + (is state + state) + (is (-> [Synthesis~ Synthesis~] [state (List [Text js.Expression])] + (Try [state (List [Text js.Expression])])) + (.function (_ [key value] [state output]) + (when key + (text_synthesis @ key) + (do try.monad + [[state value] (phase archive value state)] + (in [state (list.partial [key value] output)])) + + _ + (.undefined))))) + [state (list)] + (pairs it))] + (in [state (js.object (list.reversed output))]))) + + (analysis object|analysis + [phase archive state] + (<>.some (<>.and .text .any)) + it + (do [! try.monad] + [.let [phase (sharing [archive state] + (is [archive state] + [archive state]) + (is (-> archive Code state + (Try [state Analysis~])) + (as_expected phase)))] + [state output] (monad.mix ! (.function (_ [key value] [state output]) + (do ! + [[state value] (phase archive (` (.is .Any (, value))) state)] + (in [state (list.partial value (text_analysis key) output)]))) + [state (list)] + it)] + (in [state (extension_analysis (symbol ..object|translation) + (list.reversed output))]))) + + (def .public object + (syntax (_ [it (<>.some .any)]) + (in (list (` (.as (..Object .Any) + (object|analysis (,* it)))))))) + + (translation set|translation + [phase archive state] + (list (text_synthesis @ field) value object) + (do try.monad + [.let [phase (sharing [archive state] + (is [archive state] + [archive state]) + (is (-> archive Synthesis~ state + (Try [state js.Expression])) + (as_expected phase)))] + [state value] (phase archive value state) + [state object] (phase archive object state)] + (in [state (js.set (js.the field object) value)]))) + + (analysis set|analysis + [phase archive state] + (all <>.and .text .any .any) + [field value object] + (do try.monad + [.let [phase (sharing [archive state] + (is [archive state] + [archive state]) + (is (-> archive Code state + (Try [state Analysis~])) + (as_expected phase)))] + [state value] (phase archive (` (.is .Any (, value))) state) + [state object] (phase archive (` (.is (..Object .Any) (, object))) state)] + (in [state (extension_analysis (symbol ..set|translation) + (list (text_analysis field) value object))]))) + + (def .public set + (syntax (_ [field .any + value .any + object .any]) + (in (list (` (.as .Any (set|analysis (, field) (, value) (, object)))))))) + ) ... else (these)) @@ -217,7 +367,7 @@ @.python .python_function# @.lua .lua_function# (these))] - (nominal.def .public (Object brand) Any) + (nominal.def .public (Object of) Any) (with_expansions [ (for @.js (these [Symbol] [Null] @@ -229,12 +379,12 @@ @.ruby (these [Nil])) ] (with_template [] - [(with_expansions [ (template.symbol [ "'"])] - (nominal.def + [(with_expansions [ (template.symbol [ "'"])] + (nominal.def Any (type .public - (Object ))))] + (Object ))))] [Function] @@ -510,7 +660,7 @@ (def (input_type input :it:) (-> Input Code Code) (let [:it: (if (the #try? input) - (` (try.Try (, :it:))) + (` (Try (, :it:))) :it:)] (if (the #io? input) (` (io.IO (, :it:))) diff --git a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/runtime.lux b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/runtime.lux new file mode 100644 index 000000000..7e29191ac --- /dev/null +++ b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/runtime.lux @@ -0,0 +1,63 @@ +(.require + [library + [lux (.except) + [data + [text + ["%" \\format]]] + ["[0]" meta (.use "[1]#[0]" functor) + ["[0]" code] + [macro + [syntax (.only syntax)]] + [target + ["_" c++]]]]]) + +(def .public (host_value of it) + (-> _.Type _.Expression + _.Expression) + (|> it + (_.do "get" (list) (list)) + (_.as (_.* of)))) + +(def .public namespace + _.Namespace + "lux") + +(def name + (syntax (_ []) + (|> meta.seed + (meta#each (|>> %.nat + (%.format ..namespace) + code.text + list))))) + +(with_expansions [ (..name)] + (def .public declaration + _.Declaration + (let [clean_up (let [of (_.type_name "Of") + it (_.local "it")] + (_.function (_.local ) + (list of) + (list [(_.* of) it]) + _.void + (_.delete it)))] + (all _.then + (_.include "memory") + + (<| (_.namespace ..namespace) + (all _.then + clean_up + ))))) + + (def .public clean_up + (-> _.Type + _.Expression) + (|>> (list) + (_.global [..namespace ]))) + ) + +(def .public (lux_value of it) + (-> _.Type _.Expression + _.Expression) + (_.on (list (_.new of (list it)) + (clean_up of)) + (_.global [_.standard "shared_ptr"] (list _.void)))) diff --git a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/type.lux b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/type.lux new file mode 100644 index 000000000..f091e288f --- /dev/null +++ b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/c++/type.lux @@ -0,0 +1,26 @@ +(.require + [library + [lux (.except i64) + [meta + [target + ["_" c++]]]]]) + +(def .public bit + _.Type + (_.type ["" "bool"] (list))) + +(def .public i64 + _.Type + (_.type ["" "int64_t"] (list))) + +(def .public f64 + _.Type + (_.type ["" "double"] (list))) + +(def .public text + _.Type + (_.type [_.standard "u32string"] (list))) + +(def .public value + _.Type + (_.type [_.standard "shared_ptr"] (list (_.type ["" "void"] (list))))) diff --git a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/js/runtime.lux b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/js/runtime.lux index dbd9f5c45..13790c910 100644 --- a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/js/runtime.lux +++ b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/js/runtime.lux @@ -258,7 +258,7 @@ (lux//try op) (with_vars [ex] (_.try (_.return (..right (_.apply_1 op ..unit))) - [ex (_.return (..left (|> ex (_.do "toString" (list)))))]))) + [ex (_.return (..left (_.the "stack" ex)))]))) (runtime (lux//program_args inputs) @@ -751,7 +751,7 @@ (runtime (io//error message) - (_.throw message)) + (_.throw (_.new (_.var "Error") (list message)))) (def runtime//io Statement diff --git a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/debug.lux b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/debug.lux deleted file mode 100644 index 1d6adf5f6..000000000 --- a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/debug.lux +++ /dev/null @@ -1,31 +0,0 @@ -(.require - [library - [lux (.except) - [abstract - [monad (.only do)]] - [control - ["[0]" io (.only IO)] - ["[0]" try (.only Try)]] - [data - [binary (.only Binary)] - [text - ["%" \\format (.only format)]]] - [world - ["[0]" file (.only File)]]]]) - -(def extension ".class") - -(def .public (write_class! name bytecode) - (-> Text Binary (IO Text)) - (let [file_path (format name ..extension)] - (do io.monad - [outcome (do (try.with @) - [file (is (IO (Try (File IO))) - (file.get_file io.monad file.default file_path))] - (of file over_write bytecode))] - (in (when outcome - {try.#Success definition} - file_path - - {try.#Failure error} - error))))) diff --git a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/runtime.lux b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/runtime.lux index 297504638..af848be72 100644 --- a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/runtime.lux +++ b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/runtime.lux @@ -375,7 +375,7 @@ _.areturn ))})) -(def projection_type (type.method [(list) (list //type.tuple //type.offset) //type.value (list)])) +(def projection_type (type.method [(list) (list //type.tuple //type.lefts) //type.value (list)])) (def left_projection::name "left") (def .public left_projection (..procedure ..left_projection::name ..projection_type)) @@ -452,7 +452,7 @@ $right $tuple::size (_.invokestatic (type.class "java.util.Arrays" (list)) "copyOfRange" - (type.method [(list) (list //type.tuple //type.index //type.index) //type.tuple (list)])))]] + (type.method [(list) (list //type.tuple //type.lefts //type.lefts) //type.tuple (list)])))]] (all _.composite (_.set_label @loop) $last_right $right diff --git a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/type.lux b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/type.lux index c178701b3..329c0a02f 100644 --- a/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/type.lux +++ b/stdlib/source/library/lux/meta/compiler/language/lux/phase/translation/jvm/type.lux @@ -8,17 +8,12 @@ (def .public frac (type.class "java.lang.Double" (list))) (def .public text (type.class "java.lang.String" (list))) - (def .public value (type.class "java.lang.Object" (list))) (def .public lefts type.int) (def .public right? ..value) (def .public variant (type.array ..value)) - -(def .public offset type.int) -(def .public index ..offset) (def .public tuple (type.array ..value)) (def .public stack (type.array ..value)) - (def .public error (type.class "java.lang.Throwable" (list))) diff --git a/stdlib/source/library/lux/meta/target/c++.lux b/stdlib/source/library/lux/meta/target/c++.lux index b8c2414f4..952cc0c0b 100644 --- a/stdlib/source/library/lux/meta/target/c++.lux +++ b/stdlib/source/library/lux/meta/target/c++.lux @@ -1,10 +1,12 @@ (.require [library - [lux (.except Code Type int) + [lux (.except Code Type Global Declaration int as function template local global type) + [abstract + [equivalence (.only Equivalence)]] [control ["|" pipe]] [data - ["[0]" text (.only) + ["[0]" text (.only \n \t) (.use "[1]#[0]" equivalence) ["%" \\format]] [collection ["[0]" list (.use "[1]#[0]" functor)]]] @@ -17,32 +19,60 @@ [type ["[0]" nominal]]]]]) +(def parameter_separator ", ") +(def term_delimiters ["(" ")"]) +(def type_delimiters ["<" ">"]) + (nominal.def .public (Code of) Text + (def .public equivalence + (All (_ of) + (Equivalence (Code of))) + (implementation + (def (= refererence it) + (text#= (nominal.representation refererence) + (nominal.representation it))))) + (def .public code (-> (Code Any) Text) (|>> nominal.representation)) - (with_template [ +] - [(with_expansions [ (template.symbol [ "'"])] - (nominal.def ( of) - Any) - (`` (type .public - (|> Any (,, (template.spliced +))))))] + (with_template [ +] + [(`` (with_template [ *'] + [(with_expansions [* (template.spliced *') + (template.symbol [ "'"])] + (nominal.def ( *) + Any) + + (.type .public + (Ex (_ *) + ( ( *)))))] - [Type [Code]] - [Expression [Code]] - [Computation [Expression' Code]] - ) + (,, (template.spliced +))))] + + [Code + [[Type [of]] + [Expression [of]] + [Statement [of]]]] + + [Expression + [[Computation [of]] + [Reference [of]]]] + + [Type + [[Type_Name []]]] - (with_template [ +] - [(with_expansions [ (template.symbol [ "'"])] - (nominal.def Any) - (`` (type .public (|> (,, (template.spliced +))))))] + [Computation + [[Literal []]]] - [Literal [Computation' Expression' Code]] + [Reference + [[Local []] + [Global []]]] + + [Statement + [[Declaration []]]] ) (def .public bool @@ -69,7 +99,74 @@ [%.frac]) nominal.abstraction)) - (def .public (cast type term) + (.type .public Namespace + Text) + + (def .public standard + Namespace + "std") + + (def .public local + (-> Text + Local) + (|>> nominal.abstraction)) + + (def instantiation + (-> (List Type) + Text) + (|>> (|.when + (list) + "" + + it + (|> it + (list#each ..code) + (text.interposed ..parameter_separator) + (text.enclosed ..type_delimiters))))) + + (def .public (global [ns name] parameters) + (-> [Namespace Text] (List Type) + Global) + (nominal.abstraction + (let [instance (%.format name (instantiation parameters))] + (when ns + "" instance + _ (%.format ns "::" instance))))) + + (def .public (type name parameters) + (-> [Namespace Text] (List Type) + Type) + (|> (..global name parameters) + nominal.transmutation)) + + (def .public type_name + (-> Text + Type_Name) + (|>> nominal.abstraction)) + + (with_template [ ] + [(def .public + Type + (..type [ (template.text [])] (list)))] + + ["" void] + ) + + (def .public * + (-> Type + Type) + (|>> nominal.representation + (text.suffix "*") + nominal.abstraction)) + + (def .public deref + (-> Expression + Expression) + (|>> nominal.representation + (text.prefix "*") + nominal.abstraction)) + + (def .public (as type term) (-> Type Expression Computation) (nominal.abstraction @@ -82,16 +179,135 @@ (|>> %.int nominal.abstraction)) + (def application + (-> (List Expression) + Text) + (|>> (list#each ..code) + (text.interposed ..parameter_separator) + (text.enclosed ..term_delimiters))) + (def .public (on parameters function) (-> (List Expression) Expression Expression) (nominal.abstraction (%.format (nominal.representation function) - "(" - (|> parameters - (list#each (|>> nominal.representation)) - (text.interposed ", ")) - ")"))) + (application parameters)))) + + (def .public (new of parameters) + (-> Type (List Expression) + Expression) + (nominal.abstraction + (%.format "new " + (nominal.representation of) + (application parameters)))) + + (def .public (do method types parameters object) + (-> Text (List Type) (List Expression) Expression + Expression) + (nominal.abstraction + (%.format (nominal.representation object) + "." method + (instantiation types) + (application parameters)))) + + (def .public (<< it to) + (-> Expression Expression + Expression) + (nominal.abstraction + (%.format (nominal.representation to) + " << " + (nominal.representation it)))) + + (def .public (include it) + (-> Text + Declaration) + (nominal.abstraction + (%.format "#include <" it ">"))) + + (def .public (then before after) + (All (_ of) + (-> (Statement of) (Statement of) + (Statement of))) + (nominal.abstraction + (%.format (nominal.representation before) + \n (nominal.representation after)))) + + (def statement + (-> Text + Statement) + (|>> (text.suffix ";") + nominal.abstraction)) + + (def .public ; + (-> Expression + Statement) + (|>> nominal.representation + ..statement)) + + (def .public delete + (-> Expression + Statement) + (|>> nominal.representation + (%.format "delete ") + ..statement)) + + (def template + (-> (List Type_Name) + Text) + (|>> (|.when + (list) + "" + + it + (%.format "template" + " " (|> it + (list#each (|>> nominal.representation (%.format "typename "))) + (text.interposed ..parameter_separator) + (text.enclosed ..type_delimiters)) + " ")))) + + (.type Argument + [Type Local]) + + (def (argument [type it]) + (-> Argument + Text) + (%.format (nominal.representation type) + " " (nominal.representation it))) + + (def arguments + (-> (List Argument) + Text) + (|>> (list#each ..argument) + (text.interposed ..parameter_separator) + (text.enclosed ..term_delimiters))) + + (def block + (-> Statement + Text) + (let [\n\t (%.format \n \t) + <| (%.format "{" \n) + |> (%.format \n "}")] + (|>> nominal.representation + (text.replaced \n \n\t) + (text.enclosed [<| |>])))) + + (def .public (function name types inputs output body) + (-> Local (List Type_Name) (List Argument) Type Statement + Declaration) + (nominal.abstraction + (%.format (..template types) (nominal.representation output) + " " (nominal.representation name) + (..arguments inputs) + " " (..block body)))) + + (def .public (namespace it body) + (-> Namespace Declaration + Declaration) + (nominal.abstraction + (%.format "namespace" + " " it + " " (..block body)))) ... https://en.cppreference.com/w/cpp/types/integer (with_template [] @@ -104,11 +320,31 @@ [int64_t] ) + (def safe + (-> Text + Text) + (let [\\'' (%.format "\" text.\'')] + (`` (|>> (,, (with_template [ ] + [(text.replaced )] + + ["\" "\\"] + [text.\t "\t"] + [text.\v "\v"] + [text.\0 "\0"] + [text.\b "\b"] + [text.\f "\f"] + [text.\n "\n"] + [text.\r "\r"] + [text.\'' \\''] + )) + )))) + ... https://en.cppreference.com/w/cpp/string/basic_string (def .public u32string (-> Text Literal) - (|>> %.text + (|>> ..safe + %.text (%.format "U") nominal.abstraction)) ) diff --git a/stdlib/source/library/lux/meta/target/jvm/type.lux b/stdlib/source/library/lux/meta/target/jvm/type.lux index 0eff5b048..e1cbb4374 100644 --- a/stdlib/source/library/lux/meta/target/jvm/type.lux +++ b/stdlib/source/library/lux/meta/target/jvm/type.lux @@ -45,7 +45,9 @@ (with_template [