From 425148d29846ba507599b220d4df05c805e8d38a Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sat, 11 Aug 2018 19:46:17 -0400 Subject: Fixed various JVM translation tests. --- new-luxc/source/luxc/lang/host/jvm.lux | 21 +- new-luxc/source/luxc/lang/translation/jvm.lux | 4 + .../lang/translation/jvm/procedure/common.jvm.lux | 513 +++++++++------------ new-luxc/test/test/luxc/common.lux | 58 +-- .../test/test/luxc/lang/translation/common.lux | 114 +++-- .../test/test/luxc/lang/translation/primitive.lux | 15 +- .../test/test/luxc/lang/translation/reference.lux | 9 +- new-luxc/test/tests.lux | 12 +- 8 files changed, 335 insertions(+), 411 deletions(-) (limited to 'new-luxc') diff --git a/new-luxc/source/luxc/lang/host/jvm.lux b/new-luxc/source/luxc/lang/host/jvm.lux index 6f56f9e0e..cb5bb46fb 100644 --- a/new-luxc/source/luxc/lang/host/jvm.lux +++ b/new-luxc/source/luxc/lang/host/jvm.lux @@ -96,17 +96,16 @@ (type: #export Host (translation.Host Inst Definition)) -(type: #export State - (translation.State ..Anchor Inst Definition)) - -(type: #export Operation - (translation.Operation ..Anchor Inst Definition)) - -(type: #export Phase - (translation.Phase ..Anchor Inst Definition)) - -(type: #export Bundle - (translation.Bundle ..Anchor Inst Definition)) +(do-template [ ] + [(type: #export + ( ..Anchor Inst Definition))] + + [State translation.State] + [Operation translation.Operation] + [Phase translation.Phase] + [Handler translation.Handler] + [Bundle translation.Bundle] + ) ## [Values] (syntax: (config: {type s.local-identifier} diff --git a/new-luxc/source/luxc/lang/translation/jvm.lux b/new-luxc/source/luxc/lang/translation/jvm.lux index 2aa46e050..f9b081972 100644 --- a/new-luxc/source/luxc/lang/translation/jvm.lux +++ b/new-luxc/source/luxc/lang/translation/jvm.lux @@ -214,3 +214,7 @@ (def: #export function-class "LuxFunction") (def: #export runnable-class "LuxRunnable") (def: #export unit "") + +(def: #export $Variant jvm.Type (type.array 1 ..$Object)) +(def: #export $Tuple jvm.Type (type.array 1 ..$Object)) +(def: #export $Function jvm.Type (type.class ..function-class (list))) diff --git a/new-luxc/source/luxc/lang/translation/jvm/procedure/common.jvm.lux b/new-luxc/source/luxc/lang/translation/jvm/procedure/common.jvm.lux index 809a13bb9..7ce1d6fda 100644 --- a/new-luxc/source/luxc/lang/translation/jvm/procedure/common.jvm.lux +++ b/new-luxc/source/luxc/lang/translation/jvm/procedure/common.jvm.lux @@ -1,180 +1,122 @@ (.module: - lux - (lux (control [monad #+ do] - ["p" parser] - ["ex" exception #+ exception:]) - (data ["e" error] - [text] - text/format - (coll [list "list/" Functor] - (dictionary ["dict" unordered #+ Dict]))) - [macro #+ with-gensyms] - (macro [code] - ["s" syntax #+ syntax:]) - [host] - ["//" lang] - (lang ["//." reference #+ Register] - ["//." synthesis #+ Synthesis] - ["//." extension])) - (luxc (lang [".L" host] - (host ["$" jvm] - (jvm ["$t" type] - ["$d" def] - ["_" inst])))) - (/// [".T" runtime] - [".T" case] - [".T" function] - [".T" loop])) - -(host.import: java/lang/Double + [lux #* + [control + ["." monad (#+ do)] + ["p" parser] + ["ex" exception (#+ exception:)]] + [data + ["." text + format] + [collection + ["." list ("list/." Functor)] + ["." dictionary]]] + ["." macro (#+ with-gensyms) + ["." code] + ["s" syntax (#+ syntax:)]] + [compiler + [default + ["." phase + [synthesis (#+ Synthesis)] + ["." extension + ["." bundle]]]]] + [host (#+ import:)]] + [luxc + [lang + [host + ["$" jvm (#+ Label Inst Method Handler Bundle) + ["_t" type] + ["_" inst]]]]] + ["." /// + ["." runtime]]) + +(import: java/lang/Double (#static MIN_VALUE Double) (#static MAX_VALUE Double)) ## [Types] -(type: #export Translator - (-> Synthesis (Meta $.Inst))) - -(type: #export Proc - (-> Translator (List Synthesis) (Meta $.Inst))) - -(type: #export Bundle - (Dict Text Proc)) - (syntax: (Vector {size s.nat} elemT) (wrap (list (` [(~+ (list.repeat size elemT))])))) -(type: #export Nullary (-> (Vector +0 $.Inst) $.Inst)) -(type: #export Unary (-> (Vector +1 $.Inst) $.Inst)) -(type: #export Binary (-> (Vector +2 $.Inst) $.Inst)) -(type: #export Trinary (-> (Vector +3 $.Inst) $.Inst)) -(type: #export Variadic (-> (List $.Inst) $.Inst)) +(type: #export Nullary (-> (Vector 0 Inst) Inst)) +(type: #export Unary (-> (Vector 1 Inst) Inst)) +(type: #export Binary (-> (Vector 2 Inst) Inst)) +(type: #export Trinary (-> (Vector 3 Inst) Inst)) +(type: #export Variadic (-> (List Inst) Inst)) ## [Utils] -(def: $Object $.Type ($t.class "java.lang.Object" (list))) -(def: $Object-Array $.Type ($t.array +1 $Object)) -(def: $Variant $.Type ($t.array +1 $Object)) -(def: $String $.Type ($t.class "java.lang.String" (list))) -(def: $CharSequence $.Type ($t.class "java.lang.CharSequence" (list))) -(def: $Function $.Type ($t.class hostL.function-class (list))) - -(def: #export (install name unnamed) - (-> Text (-> Text Proc) - (-> Bundle Bundle)) - (dict.put name (unnamed name))) - -(def: #export (prefix prefix bundle) - (-> Text Bundle Bundle) - (|> bundle - dict.entries - (list/map (function (_ [key val]) [(format prefix " " key) val])) - (dict.from-list text.Hash))) - -(def: (wrong-arity proc expected actual) - (-> Text Nat Nat Text) - (format "Wrong number of arguments for " (%t proc) "\n" - "Expected: " (|> expected .int %i) "\n" - " Actual: " (|> actual .int %i))) +(def: $Object-Array $.Type (_t.array 1 ///.$Object)) +(def: $String $.Type (_t.class "java.lang.String" (list))) +(def: $CharSequence $.Type (_t.class "java.lang.CharSequence" (list))) (syntax: (arity: {name s.local-identifier} {arity s.nat}) - (with-gensyms [g!_ g!proc g!name g!translate g!inputs] + (with-gensyms [g!_ g!extension g!extension-name g!phase g!inputs] (do @ - [g!input+ (monad.seq @ (list.repeat arity (macro.gensym "input")))] - (wrap (list (` (def: #export ((~ (code.local-identifier name)) (~ g!proc)) - (-> (-> (..Vector (~ (code.nat arity)) $.Inst) $.Inst) - (-> Text ..Proc)) - (function ((~ g!_) (~ g!name)) - (function ((~ g!_) (~ g!translate) (~ g!inputs)) - (case (~ g!inputs) - (^ (list (~+ g!input+))) - (do macro.Monad - [(~+ (|> g!input+ - (list/map (function (_ g!input) - (list g!input (` ((~ g!translate) (~ g!input)))))) - list.concat))] - ((~' wrap) ((~ g!proc) [(~+ g!input+)]))) - - (~' _) - (macro.fail (wrong-arity (~ g!name) +1 (list.size (~ g!inputs)))))))))))))) - -(arity: nullary +0) -(arity: unary +1) -(arity: binary +2) -(arity: trinary +3) - -(def: #export (variadic proc) - (-> Variadic (-> Text Proc)) - (function (_ proc-name) - (function (_ translate inputsS) - (do macro.Monad - [inputsI (monad.map @ translate inputsS)] - (wrap (proc inputsI)))))) + [g!inputC+ (monad.seq @ (list.repeat arity (macro.gensym "input"))) + #let [arityC (code.nat arity)]] + (wrap (list (` (def: #export ((~ (code.local-identifier name)) (~ g!extension)) + (-> (-> (..Vector (~ arityC) Inst) Inst) ..Handler) + (function ((~ g!_) (~ g!extension-name) (~ g!phase) (~ g!inputs)) + (case (~ g!inputs) + (^ (list (~+ g!inputC+))) + (do phase.Monad + [(~+ (|> g!inputC+ + (list/map (function (_ g!input) + (list g!input (` ((~ g!phase) (~ g!input)))))) + list.concat))] + ((~' wrap) ((~ g!extension) [(~+ g!inputC+)]))) + + (~ g!_) + (phase.fail (ex.construct extension.incorrect-arity + [(~ g!extension-name) (~ arityC) (list.size (~ g!inputs))]))))))))))) + +(arity: nullary 0) +(arity: unary 1) +(arity: binary 2) +(arity: trinary 3) + +(def: #export (variadic extension) + (-> Variadic Handler) + (function (_ extension-name phase inputsS) + (do phase.Monad + [inputsH (monad.map @ phase inputsS)] + (wrap (extension inputsH))))) ## [Instructions] -(def: lux-intI $.Inst (|>> _.I2L (_.wrap #$.Long))) -(def: jvm-intI $.Inst (|>> (_.unwrap #$.Long) _.L2I)) +(def: lux-intI Inst (|>> _.I2L (_.wrap #$.Long))) +(def: jvm-intI Inst (|>> (_.unwrap #$.Long) _.L2I)) (def: (predicateI tester) - (-> (-> $.Label $.Inst) - $.Inst) + (-> (-> Label Inst) + Inst) (<| _.with-label (function (_ @then)) _.with-label (function (_ @end)) (|>> (tester @then) - (_.GETSTATIC "java.lang.Boolean" "FALSE" ($t.class "java.lang.Boolean" (list))) + (_.GETSTATIC "java.lang.Boolean" "FALSE" (_t.class "java.lang.Boolean" (list))) (_.GOTO @end) (_.label @then) - (_.GETSTATIC "java.lang.Boolean" "TRUE" ($t.class "java.lang.Boolean" (list))) + (_.GETSTATIC "java.lang.Boolean" "TRUE" (_t.class "java.lang.Boolean" (list))) (_.label @end) ))) -(def: unitI $.Inst (_.string hostL.unit)) +(def: unitI Inst (_.string ///.unit)) -## [Procedures] -## [[Lux]] -(def: (lux//is [leftI rightI]) +## Extensions +### Lux +(def: (lux::is [leftI rightI]) Binary (|>> leftI rightI (predicateI _.IF_ACMPEQ))) -(def: (lux//if [testI thenI elseI]) - Trinary - (caseT.translate-if testI thenI elseI)) - -(def: (lux//try riskyI) +(def: (lux::try riskyI) Unary (|>> riskyI - (_.CHECKCAST hostL.function-class) - (_.INVOKESTATIC hostL.runtime-class "try" - ($t.method (list $Function) (#.Some $Object-Array) (list)) + (_.CHECKCAST ///.function-class) + (_.INVOKESTATIC ///.runtime-class "try" + (_t.method (list ///.$Function) (#.Some $Object-Array) (list)) #0))) -(exception: #export (Wrong-Syntax {message Text}) - message) - -(def: #export (wrong-syntax procedure args) - (-> Text (List Synthesis) Text) - (format "Procedure: " procedure "\n" - "Arguments: " (%code (code.tuple args)))) - -(def: lux//loop - (-> Text Proc) - (function (_ proc-name) - (function (_ translate inputsS) - (case (s.run inputsS ($_ p.seq s.nat (s.tuple (p.many s.any)) s.any)) - (#e.Success [offset initsS+ bodyS]) - (loopT.translate-loop translate offset initsS+ bodyS) - - (#e.Error error) - (//.throw Wrong-Syntax (wrong-syntax proc-name inputsS))) - ))) - -(def: lux//recur - (-> Text Proc) - (function (_ proc-name) - (function (_ translate inputsS) - (loopT.translate-recur translate inputsS)))) - -## [[Bits]] +### Bits (do-template [ ] [(def: ( [inputI maskI]) Binary @@ -182,9 +124,9 @@ maskI (_.unwrap #$.Long) (_.wrap #$.Long)))] - [bit//and _.LAND] - [bit//or _.LOR] - [bit//xor _.LXOR] + [bit::and _.LAND] + [bit::or _.LOR] + [bit::xor _.LXOR] ) (do-template [ ] @@ -195,24 +137,20 @@ (_.wrap #$.Long)))] - [bit//left-shift _.LSHL] - [bit//arithmetic-right-shift _.LSHR] - [bit//logical-right-shift _.LUSHR] + [bit::left-shift _.LSHL] + [bit::arithmetic-right-shift _.LSHR] + [bit::logical-right-shift _.LUSHR] ) -## [[Numbers]] -(def: nat-method - $.Method - ($t.method (list $t.long $t.long) (#.Some $t.long) (list))) - +### Numbers (do-template [ ] [(def: ( _) Nullary (|>> (_.wrap )))] - [frac//smallest (_.double Double::MIN_VALUE) #$.Double] - [frac//min (_.double (f/* -1.0 Double::MAX_VALUE)) #$.Double] - [frac//max (_.double Double::MAX_VALUE) #$.Double] + [f64::smallest (_.double Double::MIN_VALUE) #$.Double] + [f64::min (_.double (f/* -1.0 Double::MAX_VALUE)) #$.Double] + [f64::max (_.double Double::MAX_VALUE) #$.Double] ) (do-template [ ] @@ -223,17 +161,17 @@ (_.wrap )))] - [int//add #$.Long _.LADD] - [int//sub #$.Long _.LSUB] - [int//mul #$.Long _.LMUL] - [int//div #$.Long _.LDIV] - [int//rem #$.Long _.LREM] + [i64::add #$.Long _.LADD] + [i64::sub #$.Long _.LSUB] + [i64::mul #$.Long _.LMUL] + [i64::div #$.Long _.LDIV] + [i64::rem #$.Long _.LREM] - [frac//add #$.Double _.DADD] - [frac//sub #$.Double _.DSUB] - [frac//mul #$.Double _.DMUL] - [frac//div #$.Double _.DDIV] - [frac//rem #$.Double _.DREM] + [f64::add #$.Double _.DADD] + [f64::sub #$.Double _.DSUB] + [f64::mul #$.Double _.DMUL] + [f64::div #$.Double _.DDIV] + [f64::rem #$.Double _.DREM] ) (do-template [ ] @@ -245,11 +183,11 @@ (_.int ) (predicateI _.IF_ICMPEQ)))] - [ 0] + [ +0] [ -1])] - [int//eq int//lt (_.unwrap #$.Long) _.LCMP] - [frac//eq frac//lt (_.unwrap #$.Double) _.DCMPG] + [i64::eq i64::lt (_.unwrap #$.Long) _.LCMP] + [f64::eq f64::lt (_.unwrap #$.Double) _.DCMPG] ) (do-template [ ] @@ -257,28 +195,24 @@ Unary (|>> inputI ))] - [int//to-frac (_.unwrap #$.Long) (<| (_.wrap #$.Double) _.L2D)] - [int//char (_.unwrap #$.Long) - ((|>> _.L2I _.I2C (_.INVOKESTATIC "java.lang.Character" "toString" ($t.method (list $t.char) (#.Some $String) (list)) #0)))] + [i64::to-f64 (_.unwrap #$.Long) (<| (_.wrap #$.Double) _.L2D)] + [i64::char (_.unwrap #$.Long) + ((|>> _.L2I _.I2C (_.INVOKESTATIC "java.lang.Character" "toString" (_t.method (list _t.char) (#.Some $String) (list)) #0)))] - [frac//to-int (_.unwrap #$.Double) (<| (_.wrap #$.Long) _.D2L)] - [frac//encode (_.unwrap #$.Double) - (_.INVOKESTATIC "java.lang.Double" "toString" ($t.method (list $t.double) (#.Some $String) (list)) #0)] - [frac//decode (_.CHECKCAST "java.lang.String") - (_.INVOKESTATIC hostL.runtime-class "decode_frac" ($t.method (list $String) (#.Some $Object-Array) (list)) #0)] + [f64::to-i64 (_.unwrap #$.Double) (<| (_.wrap #$.Long) _.D2L)] + [f64::encode (_.unwrap #$.Double) + (_.INVOKESTATIC "java.lang.Double" "toString" (_t.method (list _t.double) (#.Some $String) (list)) #0)] + [f64::decode (_.CHECKCAST "java.lang.String") + (_.INVOKESTATIC ///.runtime-class "decode_frac" (_t.method (list $String) (#.Some $Object-Array) (list)) #0)] ) -## [[Text]] -(do-template [ ] - [(def: ( inputI) - Unary - (|>> inputI - (_.CHECKCAST "java.lang.String") - (_.INVOKEVIRTUAL ($t.method (list) (#.Some ) (list)) #0) - ))] - - [text//size "java.lang.String" "length" lux-intI $t.int] - ) +### Text +(def: (text::size inputI) + Unary + (|>> inputI + (_.CHECKCAST "java.lang.String") + (_.INVOKEVIRTUAL "java.lang.String" "length" (_t.method (list) (#.Some _t.int) (list)) #0) + lux-intI)) (do-template [ ] [(def: ( [subjectI paramI]) @@ -287,17 +221,17 @@ paramI ))] - [text//eq id id - (_.INVOKEVIRTUAL "java.lang.Object" "equals" ($t.method (list $Object) (#.Some $t.boolean) (list)) #0) + [text::eq id id + (_.INVOKEVIRTUAL "java.lang.Object" "equals" (_t.method (list ///.$Object) (#.Some _t.boolean) (list)) #0) (_.wrap #$.Boolean)] - [text//lt (_.CHECKCAST "java.lang.String") (_.CHECKCAST "java.lang.String") - (_.INVOKEVIRTUAL "java.lang.String" "compareTo" ($t.method (list $String) (#.Some $t.int) (list)) #0) + [text::lt (_.CHECKCAST "java.lang.String") (_.CHECKCAST "java.lang.String") + (_.INVOKEVIRTUAL "java.lang.String" "compareTo" (_t.method (list $String) (#.Some _t.int) (list)) #0) (<| (predicateI _.IF_ICMPEQ) (_.int -1))] - [text//concat (_.CHECKCAST "java.lang.String") (_.CHECKCAST "java.lang.String") - (_.INVOKEVIRTUAL "java.lang.String" "concat" ($t.method (list $String) (#.Some $String) (list)) #0) + [text::concat (_.CHECKCAST "java.lang.String") (_.CHECKCAST "java.lang.String") + (_.INVOKEVIRTUAL "java.lang.String" "concat" (_t.method (list $String) (#.Some $String) (list)) #0) id] - [text//char (_.CHECKCAST "java.lang.String") jvm-intI - (_.INVOKESTATIC hostL.runtime-class "text_char" ($t.method (list $String $t.int) (#.Some $Variant) (list)) #0) + [text::char (_.CHECKCAST "java.lang.String") jvm-intI + (_.INVOKESTATIC ///.runtime-class "text_char" (_t.method (list $String _t.int) (#.Some ///.$Variant) (list)) #0) id] ) @@ -309,13 +243,13 @@ extraI ))] - [text//clip (_.CHECKCAST "java.lang.String") jvm-intI jvm-intI - (_.INVOKESTATIC hostL.runtime-class "text_clip" - ($t.method (list $String $t.int $t.int) (#.Some $Variant) (list)) #0)] + [text::clip (_.CHECKCAST "java.lang.String") jvm-intI jvm-intI + (_.INVOKESTATIC ///.runtime-class "text_clip" + (_t.method (list $String _t.int _t.int) (#.Some ///.$Variant) (list)) #0)] ) -(def: index-method $.Method ($t.method (list $String $t.int) (#.Some $t.int) (list))) -(def: (text//index [textI partI startI]) +(def: index-method Method (_t.method (list $String _t.int) (#.Some _t.int) (list))) +(def: (text::index [textI partI startI]) Trinary (<| _.with-label (function (_ @not-found)) _.with-label (function (_ @end)) @@ -327,24 +261,24 @@ (_.int -1) (_.IF_ICMPEQ @not-found) lux-intI - runtimeT.someI + runtime.someI (_.GOTO @end) (_.label @not-found) - _.POP - runtimeT.noneI + ## _.POP + runtime.noneI (_.label @end)))) -## [[IO]] -(def: string-method $.Method ($t.method (list $String) #.None (list))) -(def: (io//log messageI) +### I/O +(def: string-method Method (_t.method (list $String) #.None (list))) +(def: (io::log messageI) Unary - (|>> (_.GETSTATIC "java.lang.System" "out" ($t.class "java.io.PrintStream" (list))) + (|>> (_.GETSTATIC "java.lang.System" "out" (_t.class "java.io.PrintStream" (list))) messageI (_.CHECKCAST "java.lang.String") (_.INVOKEVIRTUAL "java.io.PrintStream" "println" string-method #0) unitI)) -(def: (io//error messageI) +(def: (io::error messageI) Unary (|>> (_.NEW "java.lang.Error") _.DUP @@ -353,101 +287,94 @@ (_.INVOKESPECIAL "java.lang.Error" "" string-method #0) _.ATHROW)) -(def: (io//exit codeI) +(def: (io::exit codeI) Unary (|>> codeI jvm-intI - (_.INVOKESTATIC "java.lang.System" "exit" ($t.method (list $t.int) #.None (list)) #0) + (_.INVOKESTATIC "java.lang.System" "exit" (_t.method (list _t.int) #.None (list)) #0) _.NULL)) -(def: (io//current-time []) +(def: (io::current-time []) Nullary - (|>> (_.INVOKESTATIC "java.lang.System" "currentTimeMillis" ($t.method (list) (#.Some $t.long) (list)) #0) + (|>> (_.INVOKESTATIC "java.lang.System" "currentTimeMillis" (_t.method (list) (#.Some _t.long) (list)) #0) (_.wrap #$.Long))) -## [Bundles] -(def: lux-procs +## Bundles +(def: bundle::lux Bundle - (|> (dict.new text.Hash) - (install "is" (binary lux//is)) - (install "try" (unary lux//try)) - (install "if" (trinary lux//if)) - (install "loop" lux//loop) - (install "recur" lux//recur) - )) - -(def: bit-procs + (|> (: Bundle bundle.empty) + (bundle.install "is" (binary lux::is)) + (bundle.install "try" (unary lux::try)))) + +(def: bundle::bit Bundle - (<| (prefix "bit") - (|> (dict.new text.Hash) - (install "and" (binary bit//and)) - (install "or" (binary bit//or)) - (install "xor" (binary bit//xor)) - (install "left-shift" (binary bit//left-shift)) - (install "logical-right-shift" (binary bit//logical-right-shift)) - (install "arithmetic-right-shift" (binary bit//arithmetic-right-shift)) - ))) - -(def: int-procs + (<| (bundle.prefix "bit") + (|> (: Bundle bundle.empty) + (bundle.install "and" (binary bit::and)) + (bundle.install "or" (binary bit::or)) + (bundle.install "xor" (binary bit::xor)) + (bundle.install "left-shift" (binary bit::left-shift)) + (bundle.install "logical-right-shift" (binary bit::logical-right-shift)) + (bundle.install "arithmetic-right-shift" (binary bit::arithmetic-right-shift))))) + +(def: bundle::i64 Bundle - (<| (prefix "int") - (|> (dict.new text.Hash) - (install "+" (binary int//add)) - (install "-" (binary int//sub)) - (install "*" (binary int//mul)) - (install "/" (binary int//div)) - (install "%" (binary int//rem)) - (install "=" (binary int//eq)) - (install "<" (binary int//lt)) - (install "to-frac" (unary int//to-frac)) - (install "char" (unary int//char))))) - -(def: frac-procs + (<| (bundle.prefix "i64") + (|> (: Bundle bundle.empty) + (bundle.install "+" (binary i64::add)) + (bundle.install "-" (binary i64::sub)) + (bundle.install "*" (binary i64::mul)) + (bundle.install "/" (binary i64::div)) + (bundle.install "%" (binary i64::rem)) + (bundle.install "=" (binary i64::eq)) + (bundle.install "<" (binary i64::lt)) + (bundle.install "to-f64" (unary i64::to-f64)) + (bundle.install "char" (unary i64::char))))) + +(def: bundle::f64 Bundle - (<| (prefix "frac") - (|> (dict.new text.Hash) - (install "+" (binary frac//add)) - (install "-" (binary frac//sub)) - (install "*" (binary frac//mul)) - (install "/" (binary frac//div)) - (install "%" (binary frac//rem)) - (install "=" (binary frac//eq)) - (install "<" (binary frac//lt)) - (install "smallest" (nullary frac//smallest)) - (install "min" (nullary frac//min)) - (install "max" (nullary frac//max)) - (install "to-int" (unary frac//to-int)) - (install "encode" (unary frac//encode)) - (install "decode" (unary frac//decode))))) - -(def: text-procs + (<| (bundle.prefix "f64") + (|> (: Bundle bundle.empty) + (bundle.install "+" (binary f64::add)) + (bundle.install "-" (binary f64::sub)) + (bundle.install "*" (binary f64::mul)) + (bundle.install "/" (binary f64::div)) + (bundle.install "%" (binary f64::rem)) + (bundle.install "=" (binary f64::eq)) + (bundle.install "<" (binary f64::lt)) + (bundle.install "smallest" (nullary f64::smallest)) + (bundle.install "min" (nullary f64::min)) + (bundle.install "max" (nullary f64::max)) + (bundle.install "to-i64" (unary f64::to-i64)) + (bundle.install "encode" (unary f64::encode)) + (bundle.install "decode" (unary f64::decode))))) + +(def: bundle::text Bundle - (<| (prefix "text") - (|> (dict.new text.Hash) - (install "=" (binary text//eq)) - (install "<" (binary text//lt)) - (install "concat" (binary text//concat)) - (install "index" (trinary text//index)) - (install "size" (unary text//size)) - (install "char" (binary text//char)) - (install "clip" (trinary text//clip)) - ))) - -(def: io-procs + (<| (bundle.prefix "text") + (|> (: Bundle bundle.empty) + (bundle.install "=" (binary text::eq)) + (bundle.install "<" (binary text::lt)) + (bundle.install "concat" (binary text::concat)) + (bundle.install "index" (trinary text::index)) + (bundle.install "size" (unary text::size)) + (bundle.install "char" (binary text::char)) + (bundle.install "clip" (trinary text::clip))))) + +(def: bundle::io Bundle - (<| (prefix "io") - (|> (dict.new text.Hash) - (install "log" (unary io//log)) - (install "error" (unary io//error)) - (install "exit" (unary io//exit)) - (install "current-time" (nullary io//current-time))))) - -(def: #export procedures + (<| (bundle.prefix "io") + (|> (: Bundle bundle.empty) + (bundle.install "log" (unary io::log)) + (bundle.install "error" (unary io::error)) + (bundle.install "exit" (unary io::exit)) + (bundle.install "current-time" (nullary io::current-time))))) + +(def: #export bundle Bundle - (<| (prefix "lux") - (|> lux-procs - (dict.merge bit-procs) - (dict.merge int-procs) - (dict.merge frac-procs) - (dict.merge text-procs) - (dict.merge io-procs) - ))) + (<| (bundle.prefix "lux") + (|> bundle::lux + (dictionary.merge bundle::bit) + (dictionary.merge bundle::i64) + (dictionary.merge bundle::f64) + (dictionary.merge bundle::text) + (dictionary.merge bundle::io)))) diff --git a/new-luxc/test/test/luxc/common.lux b/new-luxc/test/test/luxc/common.lux index 7b370ab21..f694d81bd 100644 --- a/new-luxc/test/test/luxc/common.lux +++ b/new-luxc/test/test/luxc/common.lux @@ -5,15 +5,10 @@ ["." io (#+ IO)] [data [error (#+ Error)]] - ["." macro - ["." code]] [compiler - ["." default + [default ["." reference] - ["." init] ["." phase - ["." analysis - ["." module]] ["." synthesis (#+ Synthesis)] ["." translation] [extension @@ -26,40 +21,32 @@ ["." jvm ["._jvm" runtime] ["._jvm" expression] - ## ["._jvm" statement] - ] + [procedure + ["._jvm" common]]] ## [js] ## (js ["._js" expression] - ## ["._js" runtime] - ## ["._js" statement]) + ## ["._js" runtime]) ## [lua] ## (lua ["._lua" expression] - ## ["._lua" runtime] - ## ["._lua" statement]) + ## ["._lua" runtime]) ## [ruby] ## (ruby ["._ruby" expression] - ## ["._ruby" runtime] - ## ["._ruby" statement]) + ## ["._ruby" runtime]) ## [python] ## (python ["._python" expression] - ## ["._python" runtime] - ## ["._python" statement]) + ## ["._python" runtime]) ## [r] ## (r ["._r" expression] - ## ["._r" runtime] - ## ["._r" statement]) + ## ["._r" runtime]) ## [scheme] ## (scheme ["._scheme" expression] - ## ["._scheme" runtime] - ## ["._scheme" statement]) + ## ["._scheme" runtime]) ## [common-lisp] ## (common-lisp ["._common-lisp" expression] - ## ["._common-lisp" runtime] - ## ["._common-lisp" statement]) + ## ["._common-lisp" runtime]) ## [php] ## (php ["._php" expression] - ## ["._php" runtime] - ## ["._php" statement]) + ## ["._php" runtime]) ]]]) (type: #export Runner (-> Synthesis (Error Any))) @@ -84,26 +71,29 @@ (def: (runner generate-runtime translate bundle state) (-> (Operation Any) Phase Bundle (IO State) Runner) - (function (_ synthesis) + (function (_ valueS) (|> (do phase.Monad [_ generate-runtime - program (translate synthesis)] - (translation.evaluate! program)) + program (translate valueS)] + (translation.evaluate! "runner" program)) + translation.with-buffer (phase.run [bundle (io.run state)])))) (def: (definer generate-runtime translate bundle state) (-> (Operation Any) Phase Bundle (IO State) Definer) - (function (_ name synthesis) + (function (_ lux-name valueS) (|> (do phase.Monad [_ generate-runtime - valueS (translate synthesis) - _ (translation.define! name valueS) - program (translate (synthesis.constant name))] - (translation.evaluate! program)) + valueH (translate valueS) + [host-name host-value] (translation.define! lux-name valueH) + _ (translation.learn lux-name host-name) + program (translate (synthesis.constant lux-name))] + (translation.evaluate! "definer" program)) + translation.with-buffer (phase.run [bundle (io.run state)])))) -(def: #export run-jvm (runner runtime_jvm.translate expression_jvm.translate bundle.empty init-jvm)) -(def: #export def-jvm (definer runtime_jvm.translate expression_jvm.translate bundle.empty init-jvm)) +(def: #export run-jvm (runner runtime_jvm.translate expression_jvm.translate common_jvm.bundle init-jvm)) +(def: #export def-jvm (definer runtime_jvm.translate expression_jvm.translate common_jvm.bundle init-jvm)) ## (def: #export run-js (runner runtime_js.translate expression_js.translate bundle.empty init-js)) ## (def: #export def-js (definer runtime_js.translate expression_js.translate bundle.empty init-js)) diff --git a/new-luxc/test/test/luxc/lang/translation/common.lux b/new-luxc/test/test/luxc/lang/translation/common.lux index f03965de2..246598072 100644 --- a/new-luxc/test/test/luxc/lang/translation/common.lux +++ b/new-luxc/test/test/luxc/lang/translation/common.lux @@ -4,7 +4,7 @@ [monad (#+ do)] pipe] [data - ["e" error] + ["." error (#+ Error)] [bit ("bit/." Equivalence)] [number ("frac/." Number Interval) ["." i64]] @@ -33,10 +33,10 @@ [(test (|> (run (#synthesis.Extension (list (synthesis.i64 subject) (synthesis.i64 param)))) - (case> (#e.Success valueT) + (case> (#error.Success valueT) (n/= ( param subject) (:coerce Nat valueT)) - (#e.Error error) + (#error.Error error) (exec (log! error) #0)) (let [param ])))] @@ -53,12 +53,12 @@ (|> (run (#synthesis.Extension "lux bit arithmetic-right-shift" (list (synthesis.i64 subject) (synthesis.i64 param)))) - (case> (#e.Success valueT) + (case> (#error.Success valueT) ("lux i64 =" (i64.arithmetic-right-shift param subject) (:coerce I64 valueT)) - (#e.Error error) + (#error.Error error) (exec (log! error) #0)) (let [param (n/% 64 param)]))) @@ -73,31 +73,30 @@ (~~ (do-template [ ] [(test (|> (run (#synthesis.Extension (list (synthesis.i64 subject)))) - (case> (#e.Success valueT) + (case> (#error.Success valueT) ( ( subject) (:coerce valueT)) - (#e.Error error) + (#error.Error error) (exec (log! error) #0)) (let [subject ])))] - ["lux i64 to-frac" Frac int-to-frac f/= subject] - ["lux i64 char" Text (|>> (:coerce Nat) text.from-code) text/= (|> subject - (:coerce Nat) - (n/% (i64.left-shift 8 1)) - (:coerce Int))] + ["lux i64 to-f64" Frac int-to-frac f/= subject] + ["lux i64 char" Text (|>> (:coerce Nat) text.from-code) text/= (|> subject + (:coerce Nat) + (n/% (i64.left-shift 8 1)) + (:coerce Int))] )) (~~ (do-template [ ] [(test - (exec (log! ) - (|> (run (#synthesis.Extension (list (synthesis.i64 subject) - (synthesis.i64 param)))) - (case> (#e.Success valueT) - ( ( param subject) (:coerce valueT)) + (|> (run (#synthesis.Extension (list (synthesis.i64 subject) + (synthesis.i64 param)))) + (case> (#error.Success valueT) + ( ( param subject) (:coerce valueT)) - (#e.Error error) - (exec (log! error) - #0)))))] + (#error.Error error) + (exec (log! error) + #0))))] ["lux i64 +" i/+ Int i/=] ["lux i64 -" i/- Int i/=] @@ -118,7 +117,7 @@ [(test (|> (run (#synthesis.Extension (list (synthesis.f64 subject) (synthesis.f64 param)))) - (case> (#e.Success valueT) + (case> (#error.Success valueT) ( ( param subject) (:coerce valueT)) _ @@ -139,12 +138,12 @@ (def: (f64-spec/1 run) (-> Runner Test) (do r.Monad - [subject r.frac] + [subject (|> r.nat (:: @ map (|>> (n/% 1000) .int int-to-frac)))] (`` ($_ seq (~~ (do-template [ ] [(test (|> (run (#synthesis.Extension (list))) - (case> (#e.Success valueT) + (case> (#error.Success valueT) ( (:coerce Frac valueT)) _ @@ -154,19 +153,16 @@ ["lux f64 max" (f/= frac/top)] ["lux f64 smallest" (f/= ("lux frac smallest"))] )) - (~~ (do-template [ ] - [(test - (|> (run (|> subject synthesis.f64 - (list) (#synthesis.Extension ) - (list) (#synthesis.Extension ))) - (case> (#e.Success valueT) - (|> valueT (:coerce Frac) (f/- subject) frac/abs ) - - (#e.Error error) - (exec (log! error) - #0))))] - - ["lux f64 to-int" "lux i64 to-frac" (f/< +1.0)])) + (test "\"lux f64 to-i64\" && \"lux i64 to-f64\"" + (|> (run (|> subject synthesis.f64 + (list) (#synthesis.Extension "lux f64 to-i64") + (list) (#synthesis.Extension "lux i64 to-f64"))) + (case> (#error.Success valueT) + (f/= subject (:coerce Frac valueT)) + + (#error.Error error) + (exec (log! error) + #0)))) )))) (def: (f64-spec run) @@ -195,34 +191,35 @@ ($_ seq (test "Can compare texts for equality." (and (|> (run (#synthesis.Extension "lux text =" (list sample0S sample0S))) - (case> (#e.Success valueV) + (case> (#error.Success valueV) (:coerce Bit valueV) _ #0)) (|> (run (#synthesis.Extension "lux text =" (list sample0S sample1S))) - (case> (#e.Success valueV) + (case> (#error.Success valueV) (not (:coerce Bit valueV)) _ #0)))) (test "Can compare texts for order." (|> (run (#synthesis.Extension "lux text <" (list sample1S sample0S))) - (case> (#e.Success valueV) + (case> (#error.Success valueV) (:coerce Bit valueV) - _ - #0))) + (#error.Error error) + (exec (log! error) + #0)))) (test "Can get length of text." (|> (run (#synthesis.Extension "lux text size" (list sample0S))) - (case> (#e.Success valueV) + (case> (#error.Success valueV) (n/= sample-size (:coerce Nat valueV)) _ #0))) (test "Can concatenate text." (|> (run (#synthesis.Extension "lux text size" (list concatenatedS))) - (case> (#e.Success valueV) + (case> (#error.Success valueV) (n/= (n/* 2 sample-size) (:coerce Nat valueV)) _ @@ -230,8 +227,8 @@ (test "Can find index of sub-text." (and (|> (run (#synthesis.Extension "lux text index" (list concatenatedS sample0S - (synthesis.i64 0)))) - (case> (^multi (#e.Success valueV) + (synthesis.i64 +0)))) + (case> (^multi (#error.Success valueV) [(:coerce (Maybe Nat) valueV) (#.Some valueV)]) (n/= 0 valueV) @@ -239,8 +236,8 @@ #0)) (|> (run (#synthesis.Extension "lux text index" (list concatenatedS sample1S - (synthesis.i64 0)))) - (case> (^multi (#e.Success valueV) + (synthesis.i64 +0)))) + (case> (^multi (#error.Success valueV) [(:coerce (Maybe Nat) valueV) (#.Some valueV)]) (n/= sample-size valueV) @@ -252,7 +249,7 @@ (list concatenatedS (synthesis.i64 from) (synthesis.i64 to)))) - (case> (^multi (#e.Success valueV) + (case> (^multi (#error.Success valueV) [(:coerce (Maybe Text) valueV) (#.Some valueV)]) (text/= expected valueV) @@ -265,7 +262,7 @@ (|> (run (#synthesis.Extension "lux text char" (list sample0S (synthesis.i64 char-idx)))) - (case> (^multi (#e.Success valueV) + (case> (^multi (#error.Success valueV) [(:coerce (Maybe Int) valueV) (#.Some valueV)]) (text.contains? ("lux int char" valueV) sample0) @@ -282,10 +279,10 @@ (test "Can log messages." (|> (run (#synthesis.Extension "lux io log" (list (synthesis.text (format "LOG: " message))))) - (case> (#e.Success valueV) + (case> (#error.Success valueV) #1 - (#e.Error error) + (#error.Error error) (exec (log! error) #0)))) (test "Can throw runtime errors." @@ -295,8 +292,8 @@ #synthesis.arity 1 #synthesis.body (#synthesis.Extension "lux io error" (list (synthesis.text message)))})))) - (case> (^multi (#e.Success valueV) - [(:coerce (e.Error Text) valueV) (#e.Error error)]) + (case> (^multi (#error.Success valueV) + [(:coerce (Error Text) valueV) (#error.Error error)]) (text.contains? message error) _ @@ -305,10 +302,9 @@ (list (synthesis.function/abstraction {#synthesis.environment (list) #synthesis.arity 1 - #synthesis.body (#synthesis.Extension "lux io error" - (list (synthesis.text message)))})))) - (case> (^multi (#e.Success valueV) - [(:coerce (e.Error Text) valueV) (#e.Success valueV)]) + #synthesis.body (synthesis.text message)})))) + (case> (^multi (#error.Success valueV) + [(:coerce (Error Text) valueV) (#error.Success valueV)]) (text/= message valueV) _ @@ -316,11 +312,11 @@ (test "Can obtain current time in milli-seconds." (|> (run (synthesis.tuple (list (#synthesis.Extension "lux io current-time" (list)) (#synthesis.Extension "lux io current-time" (list))))) - (case> (#e.Success valueV) + (case> (#error.Success valueV) (let [[pre post] (:coerce [Nat Nat] valueV)] (n/>= pre post)) - (#e.Error error) + (#error.Error error) (exec (log! error) #0)))) ))) diff --git a/new-luxc/test/test/luxc/lang/translation/primitive.lux b/new-luxc/test/test/luxc/lang/translation/primitive.lux index 08fab78aa..ee8e53d5e 100644 --- a/new-luxc/test/test/luxc/lang/translation/primitive.lux +++ b/new-luxc/test/test/luxc/lang/translation/primitive.lux @@ -6,6 +6,7 @@ [data ["." error] [bit ("bit/." Equivalence)] + ["." number] [text ("text/." Equivalence) format]] [math @@ -19,6 +20,12 @@ [luxc common]]) +(def: (f/=' reference subject) + (-> Frac Frac Bit) + (or (f/= reference subject) + (and (number.not-a-number? reference) + (number.not-a-number? subject)))) + (def: (spec run) (-> Runner Test) (do r.Monad @@ -38,13 +45,13 @@ ["bit" Bit synthesis.bit |bit| bit/=] ["int" Int synthesis.i64 |i64| i/=] - ["frac" Frac synthesis.f64 |f64| f/=] - ["text" Text synthesis.text |text| text/=])) + ["frac" Frac synthesis.f64 |f64| f/='] + ["text" Text synthesis.text |text| text/=] + )) )))) (context: "[JVM] Primitives." - (<| (seed 7147645721729046766) - ## (times 100) + (<| (times 100) (spec run-jvm))) ## (context: "[JS] Primitives." diff --git a/new-luxc/test/test/luxc/lang/translation/reference.lux b/new-luxc/test/test/luxc/lang/translation/reference.lux index a10e98ae6..c1a348f76 100644 --- a/new-luxc/test/test/luxc/lang/translation/reference.lux +++ b/new-luxc/test/test/luxc/lang/translation/reference.lux @@ -20,14 +20,15 @@ [// ["&" function]]) -(def: name-part - (r.Random Text) - (r.ascii/alpha 5)) +(def: name^ + (r.Random Name) + (let [name-part (r.ascii/upper-alpha 5)] + [(r.and name-part name-part)])) (def: (definitions-spec define) (-> Definer Test) (do r.Monad - [name (r.and name-part name-part) + [name name^ value r.frac] (test "Can refer to definitions." (|> (define name (synthesis.f64 value)) diff --git a/new-luxc/test/tests.lux b/new-luxc/test/tests.lux index 09b95c6b2..04362d4d1 100644 --- a/new-luxc/test/tests.lux +++ b/new-luxc/test/tests.lux @@ -1,17 +1,17 @@ (.module: [lux [cli (#+ program:)] - [test]] + ["." test]] [test [luxc [lang [translation ["_.T" primitive] - ## ["_.T" structure] - ## ["_.T" function] - ## ["_.T" reference] - ## ["_.T" case] - ## ["_.T" common] + ["_.T" structure] + ["_.T" function] + ["_.T" reference] + ["_.T" case] + ["_.T" common] ## ["_.T" jvm] ## ["_.T" js] ## ["_.T" lua] -- cgit v1.2.3