diff options
author | Eduardo Julian | 2021-02-01 04:59:32 -0400 |
---|---|---|
committer | Eduardo Julian | 2021-02-01 04:59:32 -0400 |
commit | 3d457763e34d4dd1992427b3918b351ac684adb7 (patch) | |
tree | 5e6ead8ab0c360d6c3eca5765b6be0be782709e2 /stdlib/source/lux/tool | |
parent | 1797521191746640e761cc1b4973d46b8c403dee (diff) |
Improved compilation of loops and pattern-matching for Python.
Diffstat (limited to 'stdlib/source/lux/tool')
7 files changed, 287 insertions, 256 deletions
diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/python/common.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/python/common.lux index 285499f13..90cafc75b 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/python/common.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/extension/generation/python/common.lux @@ -111,7 +111,7 @@ (/.install "/" (binary (product.uncurry _.//))) (/.install "%" (binary (product.uncurry //runtime.i64//remainder))) (/.install "f64" (unary _.float/1)) - (/.install "char" (unary _.chr/1)) + (/.install "char" (unary //runtime.i64//char)) ))) (def: f64_procs diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux index 5487cc628..9fa7107bb 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/js/runtime.lux @@ -99,57 +99,46 @@ list.concat))] (~ body))))))) -(def: (runtime_name name) - (-> Text [Code Code]) - (let [identifier (format ..prefix - "_" (%.nat $.version) - "_" (%.nat (text\hash name)))] - [(` (_.var (~ (code.text identifier)))) - (code.local_identifier identifier)])) - (syntax: (runtime: {declaration (<>.or <code>.local_identifier (<code>.form (<>.and <code>.local_identifier (<>.some <code>.local_identifier))))} code) - (case declaration - (#.Left name) - (macro.with_gensyms [g!_] - (let [[runtime_nameC runtime_nameC!] (..runtime_name name) - nameC (code.local_identifier name)] - (wrap (list (` (def: (~ runtime_nameC!) - Var - (~ runtime_nameC))) - - (` (def: #export (~ nameC) - (~ runtime_nameC!))) - - (` (def: (~ (code.local_identifier (format "@" name))) - Statement - (..feature (~ runtime_nameC) - (function ((~ g!_) (~ nameC)) - (~ code))))))))) - - (#.Right [name inputs]) - (macro.with_gensyms [g!_] - (let [[runtime_nameC runtime_nameC!] (..runtime_name name) - nameC (code.local_identifier name) - code_nameC (code.local_identifier (format "@" name)) - inputsC (list\map code.local_identifier inputs) - inputs_typesC (list\map (function.constant (` _.Expression)) inputs)] - (wrap (list (` (def: ((~ runtime_nameC!) (~+ inputsC)) - (-> (~+ inputs_typesC) Computation) - (_.apply/* (~ runtime_nameC) (list (~+ inputsC))))) - - (` (def: #export (~ nameC) - (~ runtime_nameC!))) - - (` (def: (~ (code.local_identifier (format "@" name))) - Statement - (..feature (~ runtime_nameC) - (function ((~ g!_) (~ g!_)) - (..with_vars [(~+ inputsC)] - (_.function (~ g!_) (list (~+ inputsC)) - (~ code))))))))))))) + (macro.with_gensyms [g!_ runtime] + (let [runtime_name (` (_.var (~ (code.text (%.code runtime)))))] + (case declaration + (#.Left name) + (let [g!name (code.local_identifier name)] + (wrap (list (` (def: (~ runtime) + Var + (~ runtime_name))) + + (` (def: #export (~ g!name) + (~ runtime))) + + (` (def: (~ (code.local_identifier (format "@" name))) + Statement + (..feature (~ runtime_name) + (function ((~ g!_) (~ g!name)) + (~ code)))))))) + + (#.Right [name inputs]) + (let [g!name (code.local_identifier name) + inputsC (list\map code.local_identifier inputs) + inputs_typesC (list\map (function.constant (` _.Expression)) inputs)] + (wrap (list (` (def: ((~ runtime) (~+ inputsC)) + (-> (~+ inputs_typesC) Computation) + (_.apply/* (~ runtime_name) (list (~+ inputsC))))) + + (` (def: #export (~ g!name) + (~ runtime))) + + (` (def: (~ (code.local_identifier (format "@" name))) + Statement + (..feature (~ runtime_name) + (function ((~ g!_) (~ g!_)) + (..with_vars [(~+ inputsC)] + (_.function (~ g!_) (list (~+ inputsC)) + (~ code)))))))))))))) (def: length (-> Expression Computation) @@ -318,7 +307,9 @@ (|> i64 (_.the ..i64_low_field) (_.+ i64//2^32))))) (runtime: (i64//to_number i64) - (_.return (|> i64 (_.the ..i64_high_field) (_.* i64//2^32) + (_.return (|> i64 + (_.the ..i64_high_field) + (_.* i64//2^32) (_.+ (i64//unsigned_low i64))))) (runtime: i64//zero @@ -364,13 +355,21 @@ (_.define r00 (ll parameter)) (_.define x00 (_.+ l00 r00)) - (_.define x16 (high_16 x00)) + + (_.define x16 (|> (high_16 x00) + (_.+ l16) + (_.+ r16))) (_.set x00 (low_16 x00)) - (_.set x16 (|> x16 (_.+ l16) (_.+ r16))) - (_.define x32 (high_16 x16)) + + (_.define x32 (|> (high_16 x16) + (_.+ l32) + (_.+ r32))) (_.set x16 (low_16 x16)) - (_.set x32 (|> x32 (_.+ l32) (_.+ r32))) - (_.define x48 (|> (high_16 x32) (_.+ l48) (_.+ r48) low_16)) + + (_.define x48 (|> (high_16 x32) + (_.+ l48) + (_.+ r48) + low_16)) (_.set x32 (low_16 x32)) (_.return (..i64 (_.bit_or (up_16 x48) x32) @@ -394,33 +393,33 @@ (_.bit_not (_.the ..i64_low_field value))))) (runtime: (i64//negate value) - (_.if (i64//= i64//min value) - (_.return i64//min) - (_.return (i64//+ i64//one (i64//not value))))) + (_.return (_.? (i64//= i64//min value) + i64//min + (i64//+ i64//one (i64//not value))))) (runtime: i64//-one (i64//negate i64//one)) (runtime: (i64//from_number value) - (_.cond (list [(_.not_a_number? value) - (_.return i64//zero)] - [(_.<= (_.negate i64//2^63) value) - (_.return i64//min)] - [(|> value (_.+ (_.i32 +1)) (_.>= i64//2^63)) - (_.return i64//max)] - [(|> value (_.< (_.i32 +0))) - (_.return (|> value _.negate i64//from_number i64//negate))]) - (_.return (..i64 (|> value (_./ i64//2^32) _.to_i32) - (|> value (_.% i64//2^32) _.to_i32))))) + (_.return (<| (_.? (_.not_a_number? value) + i64//zero) + (_.? (_.<= (_.negate i64//2^63) value) + i64//min) + (_.? (|> value (_.+ (_.i32 +1)) (_.>= i64//2^63)) + i64//max) + (_.? (|> value (_.< (_.i32 +0))) + (|> value _.negate i64//from_number i64//negate)) + (..i64 (|> value (_./ i64//2^32) _.to_i32) + (|> value (_.% i64//2^32) _.to_i32))))) (def: (cap_shift! shift) (-> Var Statement) (_.set shift (|> shift (_.bit_and (_.i32 +63))))) (def: (no_shift! shift input) - (-> Var Var [Expression Statement]) - [(|> shift (_.= (_.i32 +0))) - (_.return input)]) + (-> Var Var (-> Expression Expression)) + (_.? (|> shift (_.= (_.i32 +0))) + input)) (def: small_shift? (-> Var Expression) @@ -429,43 +428,44 @@ (runtime: (i64//left_shift input shift) ($_ _.then (..cap_shift! shift) - (_.cond (list (..no_shift! shift input) - [(..small_shift? shift) - (let [high (_.bit_or (|> input (_.the ..i64_high_field) (_.left_shift shift)) - (|> input (_.the ..i64_low_field) (_.logic_right_shift (_.- shift (_.i32 +32))))) - low (|> input (_.the ..i64_low_field) (_.left_shift shift))] - (_.return (..i64 high low)))]) - (let [high (|> input (_.the ..i64_low_field) (_.left_shift (_.- (_.i32 +32) shift)))] - (_.return (..i64 high (_.i32 +0))))))) + (_.return (<| (..no_shift! shift input) + (_.? (..small_shift? shift) + (let [high (_.bit_or (|> input (_.the ..i64_high_field) (_.left_shift shift)) + (|> input (_.the ..i64_low_field) (_.logic_right_shift (_.- shift (_.i32 +32))))) + low (|> input (_.the ..i64_low_field) (_.left_shift shift))] + (..i64 high low))) + (let [high (|> input (_.the ..i64_low_field) (_.left_shift (_.- (_.i32 +32) shift)))] + (..i64 high (_.i32 +0))))) + )) (runtime: (i64//arithmetic_right_shift input shift) ($_ _.then (..cap_shift! shift) - (_.cond (list (..no_shift! shift input) - [(..small_shift? shift) - (let [high (|> input (_.the ..i64_high_field) (_.arithmetic_right_shift shift)) - low (|> input (_.the ..i64_low_field) (_.logic_right_shift shift) - (_.bit_or (|> input (_.the ..i64_high_field) (_.left_shift (_.- shift (_.i32 +32))))))] - (_.return (..i64 high low)))]) - (let [high (_.? (|> input (_.the ..i64_high_field) (_.>= (_.i32 +0))) - (_.i32 +0) - (_.i32 -1)) - low (|> input (_.the ..i64_high_field) (_.arithmetic_right_shift (_.- (_.i32 +32) shift)))] - (_.return (..i64 high low)))))) + (_.return (<| (..no_shift! shift input) + (_.? (..small_shift? shift) + (let [high (|> input (_.the ..i64_high_field) (_.arithmetic_right_shift shift)) + low (|> input (_.the ..i64_low_field) (_.logic_right_shift shift) + (_.bit_or (|> input (_.the ..i64_high_field) (_.left_shift (_.- shift (_.i32 +32))))))] + (..i64 high low))) + (let [high (_.? (|> input (_.the ..i64_high_field) (_.>= (_.i32 +0))) + (_.i32 +0) + (_.i32 -1)) + low (|> input (_.the ..i64_high_field) (_.arithmetic_right_shift (_.- (_.i32 +32) shift)))] + (..i64 high low)))))) (runtime: (i64//right_shift input shift) ($_ _.then (..cap_shift! shift) - (_.cond (list (..no_shift! shift input) - [(..small_shift? shift) - (let [high (|> input (_.the ..i64_high_field) (_.logic_right_shift shift)) - low (|> input (_.the ..i64_low_field) (_.logic_right_shift shift) - (_.bit_or (|> input (_.the ..i64_high_field) (_.left_shift (_.- shift (_.i32 +32))))))] - (_.return (..i64 high low)))] - [(|> shift (_.= (_.i32 +32))) - (_.return (..i64 (_.i32 +0) (|> input (_.the ..i64_high_field))))]) - (_.return (..i64 (_.i32 +0) - (|> input (_.the ..i64_high_field) (_.logic_right_shift (_.- (_.i32 +32) shift)))))))) + (_.return (<| (..no_shift! shift input) + (_.? (..small_shift? shift) + (let [high (|> input (_.the ..i64_high_field) (_.logic_right_shift shift)) + low (|> input (_.the ..i64_low_field) (_.logic_right_shift shift) + (_.bit_or (|> input (_.the ..i64_high_field) (_.left_shift (_.- shift (_.i32 +32))))))] + (..i64 high low))) + (_.? (|> shift (_.= (_.i32 +32))) + (..i64 (_.i32 +0) (|> input (_.the ..i64_high_field)))) + (..i64 (_.i32 +0) + (|> input (_.the ..i64_high_field) (_.logic_right_shift (_.- (_.i32 +32) shift)))))))) (def: runtime//bit Statement @@ -483,64 +483,67 @@ (_.return (i64//+ (i64//negate parameter) subject))) (runtime: (i64//* parameter subject) - (let [negative? (|>> (_.the ..i64_high_field) (_.< (_.i32 +0)))] - (_.cond (list [(negative? subject) - (_.if (negative? parameter) - ## Both are negative - (_.return (i64//* (i64//negate parameter) (i64//negate subject))) - ## Subject is negative - (_.return (i64//negate (i64//* parameter (i64//negate subject)))))] - [(negative? parameter) - ## Parameter is negative - (_.return (i64//negate (i64//* (i64//negate parameter) subject)))]) - ## Both are positive - (let [up_16 (_.left_shift (_.i32 +16)) - high_16 (_.logic_right_shift (_.i32 +16)) - low_16 (_.bit_and (_.i32 (.int (hex "FFFF")))) - hh (|>> (_.the ..i64_high_field) high_16) - hl (|>> (_.the ..i64_high_field) low_16) - lh (|>> (_.the ..i64_low_field) high_16) - ll (|>> (_.the ..i64_low_field) low_16)] - (with_vars [l48 l32 l16 l00 - r48 r32 r16 r00 - x48 x32 x16 x00] - ($_ _.then - (_.define l48 (hh subject)) - (_.define l32 (hl subject)) - (_.define l16 (lh subject)) - (_.define l00 (ll subject)) - - (_.define r48 (hh parameter)) - (_.define r32 (hl parameter)) - (_.define r16 (lh parameter)) - (_.define r00 (ll parameter)) - - (_.define x00 (_.* l00 r00)) - (_.define x16 (high_16 x00)) - (_.set x00 (low_16 x00)) - - (_.set x16 (|> x16 (_.+ (_.* l16 r00)))) - (_.define x32 (high_16 x16)) (_.set x16 (low_16 x16)) - (_.set x16 (|> x16 (_.+ (_.* l00 r16)))) - (_.set x32 (|> x32 (_.+ (high_16 x16)))) (_.set x16 (low_16 x16)) - - (_.set x32 (|> x32 (_.+ (_.* l32 r00)))) - (_.define x48 (high_16 x32)) (_.set x32 (low_16 x32)) - (_.set x32 (|> x32 (_.+ (_.* l16 r16)))) - (_.set x48 (|> x48 (_.+ (high_16 x32)))) (_.set x32 (low_16 x32)) - (_.set x32 (|> x32 (_.+ (_.* l00 r32)))) - (_.set x48 (|> x48 (_.+ (high_16 x32)))) (_.set x32 (low_16 x32)) - - (_.set x48 (|> x48 - (_.+ (_.* l48 r00)) - (_.+ (_.* l32 r16)) - (_.+ (_.* l16 r32)) - (_.+ (_.* l00 r48)) - low_16)) - - (_.return (..i64 (_.bit_or (up_16 x48) x32) - (_.bit_or (up_16 x16) x00))) - )))))) + (let [up_16 (_.left_shift (_.i32 +16)) + high_16 (_.logic_right_shift (_.i32 +16)) + low_16 (_.bit_and (_.i32 (.int (hex "FFFF")))) + hh (|>> (_.the ..i64_high_field) high_16) + hl (|>> (_.the ..i64_high_field) low_16) + lh (|>> (_.the ..i64_low_field) high_16) + ll (|>> (_.the ..i64_low_field) low_16)] + (with_vars [l48 l32 l16 l00 + r48 r32 r16 r00 + x48 x32 x16 x00] + ($_ _.then + (_.define l48 (hh subject)) + (_.define l32 (hl subject)) + (_.define l16 (lh subject)) + (_.define l00 (ll subject)) + + (_.define r48 (hh parameter)) + (_.define r32 (hl parameter)) + (_.define r16 (lh parameter)) + (_.define r00 (ll parameter)) + + (_.define x00 (_.* l00 r00)) + (_.define x16 (high_16 x00)) + (_.set x00 (low_16 x00)) + + (_.set x16 (|> x16 (_.+ (_.* l16 r00)))) + (_.define x32 (high_16 x16)) (_.set x16 (low_16 x16)) + (_.set x16 (|> x16 (_.+ (_.* l00 r16)))) + (_.set x32 (|> x32 (_.+ (high_16 x16)))) (_.set x16 (low_16 x16)) + + (_.set x32 (|> x32 (_.+ (_.* l32 r00)))) + (_.define x48 (high_16 x32)) (_.set x32 (low_16 x32)) + (_.set x32 (|> x32 (_.+ (_.* l16 r16)))) + (_.set x48 (|> x48 (_.+ (high_16 x32)))) (_.set x32 (low_16 x32)) + (_.set x32 (|> x32 (_.+ (_.* l00 r32)))) + (_.set x48 (|> x48 (_.+ (high_16 x32)))) (_.set x32 (low_16 x32)) + + (_.set x48 (|> x48 + (_.+ (_.* l48 r00)) + (_.+ (_.* l32 r16)) + (_.+ (_.* l16 r32)) + (_.+ (_.* l00 r48)) + low_16)) + + (_.return (..i64 (_.bit_or (up_16 x48) x32) + (_.bit_or (up_16 x16) x00))) + )))) + +## (runtime: (i64//* parameter subject) +## (let [negative? (|>> (_.the ..i64_high_field) (_.< (_.i32 +0)))] +## (_.cond (list [(negative? subject) +## (_.if (negative? parameter) +## ## Both are negative +## (_.return (i64//*' (i64//negate parameter) (i64//negate subject))) +## ## Subject is negative +## (_.return (i64//negate (i64//*' parameter (i64//negate subject)))))] +## [(negative? parameter) +## ## Parameter is negative +## (_.return (i64//negate (i64//*' (i64//negate parameter) subject)))]) +## ## Both are positive +## (_.return (i64//*' parameter subject))))) (runtime: (i64//< parameter subject) (let [negative? (|>> (_.the ..i64_high_field) (_.< (_.i32 +0)))] @@ -548,16 +551,17 @@ ($_ _.then (_.define -subject? (negative? subject)) (_.define -parameter? (negative? parameter)) - (_.cond (list [(_.and -subject? (_.not -parameter?)) - (_.return (_.boolean true))] - [(_.and (_.not -subject?) -parameter?) - (_.return (_.boolean false))]) - (_.return (negative? (i64//- parameter subject)))))))) + (_.return (<| (_.? (_.and -subject? (_.not -parameter?)) + (_.boolean true)) + (_.? (_.and (_.not -subject?) -parameter?) + (_.boolean false)) + (negative? (i64//- parameter subject)))) + )))) (def: (i64//<= param subject) (-> Expression Expression Expression) - (_.or (i64//< param subject) - (i64//= param subject))) + (|> (i64//< param subject) + (_.or (i64//= param subject)))) (runtime: (i64/// parameter subject) (let [negative? (function (_ value) @@ -668,6 +672,7 @@ @i64//to_number @i64//from_number @i64//- + ## @i64//*' @i64//* @i64//< @i64/// @@ -679,9 +684,9 @@ (with_vars [idx] ($_ _.then (_.define idx (|> text (_.do "indexOf" (list part (i64//to_number start))))) - (_.if (_.= (_.i32 -1) idx) - (_.return ..none) - (_.return (..some (i64//from_number idx))))))) + (_.return (_.? (_.= (_.i32 -1) idx) + ..none + (..some (i64//from_number idx))))))) (runtime: (text//clip start end text) (_.return (|> text (_.do "substring" (list (_.the ..i64_low_field start) @@ -735,9 +740,9 @@ (with_vars [temp] ($_ _.then (_.define temp (_.at field object)) - (_.if (_.= _.undefined temp) - (_.return ..none) - (_.return (..some temp)))))) + (_.return (_.? (_.= _.undefined temp) + ..none + (..some temp)))))) (runtime: (js//set object field input) ($_ _.then diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python.lux index 4d6000fbc..2de025059 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python.lux @@ -46,7 +46,7 @@ [#////synthesis.Extension]) (^ (////synthesis.branch/case case)) - (/case.case! statement expression archive case) + (/case.case! false statement expression archive case) (^ (////synthesis.branch/let let)) (/case.let! statement expression archive let) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/case.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/case.lux index a1ae27d5e..62225bb9c 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/case.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/case.lux @@ -35,6 +35,10 @@ [meta [archive (#+ Archive)]]]]]]]) +(def: #export (gensym prefix) + (-> Text (Operation SVar)) + (///////phase\map (|>> %.nat (format prefix) _.var) /////generation.next)) + (def: #export register (-> Register SVar) (|>> (///reference.local //reference.system) :assume)) @@ -147,19 +151,30 @@ [right_choice (_.string "") inc] ) -(def: (alternation pre! post!) - (-> (Statement Any) (Statement Any) (Statement Any)) - ($_ _.then - (_.while (_.bool true) - ($_ _.then - ..save! - pre!)) - ($_ _.then - ..restore! - post!))) - -(def: (pattern_matching' statement expression archive) - (-> Phase! Phase Archive Path (Operation (Statement Any))) +(def: (alternation in_closure? g!once pre! post!) + (-> Bit SVar (Statement Any) (Statement Any) (Statement Any)) + (.if in_closure? + ($_ _.then + (_.while (_.bool true) + ($_ _.then + ..save! + pre!) + #.None) + ..restore! + post!) + ($_ _.then + (_.set (list g!once) (_.bool true)) + (_.while g!once + ($_ _.then + (_.set (list g!once) (_.bool false)) + ..save! + pre!) + (#.Some _.continue)) + ..restore! + post!))) + +(def: (pattern_matching' in_closure? statement expression archive) + (-> Bit Phase! Phase Archive Path (Operation (Statement Any))) (function (recur pathP) (.case pathP (^ (/////synthesis.path/then bodyS)) @@ -249,23 +264,38 @@ (..multi_pop! (n.+ 2 extra_pops)) next!)))) - (^template [<tag> <combinator>] - [(^ (<tag> preP postP)) - (do ///////phase.monad - [pre! (recur preP) - post! (recur postP)] - (wrap (<combinator> pre! post!)))]) - ([/////synthesis.path/seq _.then] - [/////synthesis.path/alt ..alternation])))) - -(def: (pattern_matching statement expression archive pathP) - (-> Phase! Phase Archive Path (Operation (Statement Any))) + (^ (/////synthesis.path/seq preP postP)) + (do ///////phase.monad + [pre! (recur preP) + post! (recur postP)] + (wrap (_.then pre! post!))) + + (^ (/////synthesis.path/alt preP postP)) + (do ///////phase.monad + [pre! (recur preP) + post! (recur postP) + g!once (..gensym "once")] + (wrap (..alternation in_closure? g!once pre! post!)))))) + +(def: (pattern_matching in_closure? statement expression archive pathP) + (-> Bit Phase! Phase Archive Path (Operation (Statement Any))) (do ///////phase.monad - [pattern_matching! (pattern_matching' statement expression archive pathP)] - (wrap ($_ _.then - (_.while (_.bool true) - pattern_matching!) - (_.raise (_.Exception/1 (_.string case.pattern_matching_error))))))) + [pattern_matching! (pattern_matching' in_closure? statement expression archive pathP) + g!once (..gensym "once")] + (wrap (.if in_closure? + ($_ _.then + (_.while (_.bool true) + pattern_matching! + #.None) + (_.raise (_.Exception/1 (_.string case.pattern_matching_error)))) + ($_ _.then + (_.set (list g!once) (_.bool true)) + (_.while g!once + ($_ _.then + (_.set (list g!once) (_.bool false)) + pattern_matching!) + (#.Some _.continue)) + (_.raise (_.Exception/1 (_.string case.pattern_matching_error)))))))) (def: #export dependencies (-> Path (List SVar)) @@ -280,15 +310,11 @@ (#///////variable.Foreign register) (..capture register)))))) -(def: #export (gensym prefix) - (-> Text (Operation SVar)) - (///////phase\map (|>> %.nat (format prefix) _.var) /////generation.next)) - -(def: #export (case! statement expression archive [valueS pathP]) - (Generator! [Synthesis Path]) +(def: #export (case! in_closure? statement expression archive [valueS pathP]) + (-> Bit (Generator! [Synthesis Path])) (do ///////phase.monad [stack_init (expression archive valueS) - pattern_matching! (pattern_matching statement expression archive pathP)] + pattern_matching! (pattern_matching in_closure? statement expression archive pathP)] (wrap ($_ _.then (_.set (list @cursor) (_.list (list stack_init))) (_.set (list @savepoint) (_.list (list))) @@ -299,7 +325,7 @@ (-> Phase! (Generator [Synthesis Path])) (do ///////phase.monad [[[case_module case_artifact] pattern_matching!] (/////generation.with_new_context archive - (case! statement expression archive [valueS pathP])) + (case! true statement expression archive [valueS pathP])) #let [@case (_.var (///reference.artifact [case_module case_artifact])) @dependencies+ (..dependencies (/////synthesis.path/seq (/////synthesis.path/then valueS) pathP)) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/function.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/function.lux index 8ef3446f5..f2c71eae8 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/function.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/function.lux @@ -68,10 +68,8 @@ (def: #export (function statement expression archive [environment arity bodyS]) (-> Phase! (Generator (Abstraction Synthesis))) (do {! ///////phase.monad} - [@expected_exception (//case.gensym "expected_exception") - @actual_exception (//case.gensym "actual_exception") - [[function_module function_artifact] body!] (/////generation.with_new_context archive - (/////generation.with_anchor [1 @expected_exception] + [[[function_module function_artifact] body!] (/////generation.with_new_context archive + (/////generation.with_anchor 1 (statement expression archive bodyS))) environment (monad.map ! (expression archive) environment) #let [@curried (_.var "curried") @@ -93,7 +91,7 @@ (_.set (list @num_args) (_.len/1 @curried)) (_.cond (list [(|> @num_args (_.= arityO)) (<| (_.then initialize!) - (//loop.set_scope @expected_exception @actual_exception) + //loop.set_scope body!)] [(|> @num_args (_.> arityO)) (let [arity_inputs (_.slice (_.int +0) arityO @curried) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/loop.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/loop.lux index c330d1f45..83f093001 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/loop.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/loop.lux @@ -40,18 +40,11 @@ list.reverse (list\fold _.then body))) -(def: #export (set_scope @expected_exception @actual_exception body!) - (-> SVar SVar (Statement Any) (Statement Any)) - (let [exception_class (_.var "Exception")] - ($_ _.then - (_.set (list @expected_exception) (_.apply/* exception_class (list (_.string "")))) - (_.while (_.bool true) - (_.try body! - (list {#_.classes (list exception_class) - #_.exception @actual_exception - #_.handler (_.if (_.is @expected_exception @actual_exception) - _.continue - (_.raise @actual_exception))})))))) +(def: #export (set_scope body!) + (-> (Statement Any) (Statement Any)) + (_.while (_.bool true) + body! + #.None)) (def: #export (scope! statement expression archive [start initsS+ bodyS]) (Generator! (Scope Synthesis)) @@ -64,12 +57,10 @@ _ (do {! ///////phase.monad} [initsO+ (monad.map ! (expression archive) initsS+) - @expected_exception (//case.gensym "expected_exception") - @actual_exception (//case.gensym "actual_exception") - body! (/////generation.with_anchor [start @expected_exception] + body! (/////generation.with_anchor start (statement expression archive bodyS))] (wrap (<| (..setup start initsO+) - (set_scope @expected_exception @actual_exception) + ..set_scope body!))))) (def: #export (scope statement expression archive [start initsS+ bodyS]) @@ -82,18 +73,16 @@ ## true loop _ (do {! ///////phase.monad} - [@expected_exception (//case.gensym "expected_exception") - @actual_exception (//case.gensym "actual_exception") - initsO+ (monad.map ! (expression archive) initsS+) + [initsO+ (monad.map ! (expression archive) initsS+) [[loop_module loop_artifact] body!] (/////generation.with_new_context archive - (/////generation.with_anchor [start @expected_exception] + (/////generation.with_anchor start (statement expression archive bodyS))) #let [@loop (_.var (///reference.artifact [loop_module loop_artifact])) locals (|> initsS+ list.enumeration (list\map (|>> product.left (n.+ start) //case.register))) actual_loop (<| (_.def @loop locals) - (set_scope @expected_exception @actual_exception) + ..set_scope body!) [directive instantiation] (: [(Statement Any) (Expression Any)] (case (|> (synthesis.path/then bodyS) @@ -119,7 +108,7 @@ (def: #export (recur! statement expression archive argsS+) (Generator! (List Synthesis)) (do {! ///////phase.monad} - [[offset @exception] /////generation.anchor + [offset /////generation.anchor @temp (//case.gensym "lux_recur_values") argsO+ (monad.map ! (expression archive) argsS+) #let [re_binds (|> argsO+ @@ -129,4 +118,4 @@ (wrap ($_ _.then (_.set (list @temp) (_.list argsO+)) (..setup offset re_binds - (_.raise @exception)))))) + _.continue))))) diff --git a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux index f32712fc2..132ec3c98 100644 --- a/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux +++ b/stdlib/source/lux/tool/compiler/language/lux/phase/generation/python/runtime.lux @@ -39,7 +39,7 @@ (template [<name> <base>] [(type: #export <name> - (<base> [Register SVar] (Expression Any) (Statement Any)))] + (<base> Register (Expression Any) (Statement Any)))] [Operation /////generation.Operation] [Phase /////generation.Phase] @@ -214,28 +214,29 @@ (_.set (list tuple) (_.nth last_index_right tuple))))] (runtime: (tuple//left lefts tuple) (with_vars [last_index_right] - (<| (_.while (_.bool true)) - ($_ _.then - (_.set (list last_index_right) (..last_index tuple)) - (_.if (_.> lefts last_index_right) - ## No need for recursion - (_.return (_.nth lefts tuple)) - ## Needs recursion - <recur>))))) + (_.while (_.bool true) + ($_ _.then + (_.set (list last_index_right) (..last_index tuple)) + (_.if (_.> lefts last_index_right) + ## No need for recursion + (_.return (_.nth lefts tuple)) + ## Needs recursion + <recur>)) + #.None))) (runtime: (tuple//right lefts tuple) (with_vars [last_index_right right_index] - (<| (_.while (_.bool true)) - ($_ _.then - (_.set (list last_index_right) (..last_index tuple)) - (_.set (list right_index) (_.+ (_.int +1) lefts)) - (_.cond (list [(_.= last_index_right right_index) - (_.return (_.nth right_index tuple))] - [(_.> last_index_right right_index) - ## Needs recursion. - <recur>]) - (_.return (_.slice_from right_index tuple))) - ))))) + (_.while (_.bool true) + ($_ _.then + (_.set (list last_index_right) (..last_index tuple)) + (_.set (list right_index) (_.+ (_.int +1) lefts)) + (_.cond (list [(_.= last_index_right right_index) + (_.return (_.nth right_index tuple))] + [(_.> last_index_right right_index) + ## Needs recursion. + <recur>]) + (_.return (_.slice_from right_index tuple)))) + #.None)))) (runtime: (sum//get sum wantsLast wantedTag) (let [no_match! (_.return _.none) @@ -321,6 +322,17 @@ [i64//xor _.bit_xor] ) +(def: version + (Expression Any) + (|> (_.__import__/1 (_.unicode "sys")) + (_.the "version_info") + (_.the "major"))) + +(runtime: (i64//char value) + (_.return (_.? (_.= (_.int +3) ..version) + (_.chr/1 value) + (_.unichr/1 value)))) + (def: runtime//i64 (Statement Any) ($_ _.then @@ -334,6 +346,7 @@ @i64//and @i64//or @i64//xor + @i64//char )) (runtime: (f64//decode input) |