diff --git a/lib/Language/PureScript/Backend/Lua.hs b/lib/Language/PureScript/Backend/Lua.hs index 9ef7321..1a59af8 100644 --- a/lib/Language/PureScript/Backend/Lua.hs +++ b/lib/Language/PureScript/Backend/Lua.hs @@ -1,5 +1,3 @@ -{-# LANGUAGE QuasiQuotes #-} - module Language.PureScript.Backend.Lua ( fromUberModule , fromIR @@ -177,7 +175,10 @@ fromIR foreigns topLevelNames modname ir = case ir of IR.ArrayLength _ann e → Right . Lua.hash <$> goExp e IR.ArrayIndex _ann expr index → - Right . flip Lua.varIndex (Lua.Integer (fromIntegral index)) <$> goExp expr + -- IR array indices are 0-based (de Bruijn-style, like the source language), + -- but Lua tables are 1-based, so shift by one. This mirrors the arrays FFI + -- `indexImpl`, which reads `xs[i + 1]`. See issue #49. + Right . flip Lua.varIndex (Lua.Integer (fromIntegral index + 1)) <$> goExp expr IR.ObjectProp _ann expr propName → Right . flip Lua.varField (fromPropName propName) <$> goExp expr IR.ObjectUpdate _ann expr propValues → do diff --git a/test/ps/golden/Golden/ArrayPatternMatch/Test.purs b/test/ps/golden/Golden/ArrayPatternMatch/Test.purs new file mode 100644 index 0000000..b5de225 --- /dev/null +++ b/test/ps/golden/Golden/ArrayPatternMatch/Test.purs @@ -0,0 +1,26 @@ +module Golden.ArrayPatternMatch.Test where + +import Prelude + +import Effect (Effect) +import Effect.Console (logShow) + +-- Matching an array-literal pattern destructures by index. The binders must +-- read 1-based Lua slots; the regression in #49 read them 0-based, so the +-- first element came back nil and the match crashed at runtime. +firstTwo :: Array Int -> Int +firstTwo = case _ of + [ a, b ] -> a + b + _ -> -1 + +lastOfThree :: Array Int -> Int +lastOfThree = case _ of + [ _, _, c ] -> c + _ -> -1 + +main :: Effect Unit +main = do + logShow (firstTwo [ 10, 20 ]) + logShow (firstTwo [ 1, 2, 3 ]) + logShow (firstTwo []) + logShow (lastOfThree [ 7, 8, 9 ]) diff --git a/test/ps/output/Golden.ArrayPatternMatch.Test/corefn.json b/test/ps/output/Golden.ArrayPatternMatch.Test/corefn.json new file mode 100644 index 0000000..17e6160 --- /dev/null +++ b/test/ps/output/Golden.ArrayPatternMatch.Test/corefn.json @@ -0,0 +1 @@ +{"builtWith":"0.15.16","comments":[],"decls":[{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"bindType":"NonRec","expression":{"abstraction":{"annotation":{"meta":{"metaType":"IsForeign"},"sourceSpan":{"end":[19,10],"start":[19,8]}},"type":"Var","value":{"identifier":"negate","moduleName":["Data","Ring"]}},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[19,10],"start":[19,8]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"ringInt","moduleName":["Data","Ring"]}},"type":"App"},"identifier":"negate"},{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"bindType":"NonRec","expression":{"abstraction":{"annotation":{"meta":{"metaType":"IsForeign"},"sourceSpan":{"end":[13,18],"start":[13,17]}},"type":"Var","value":{"identifier":"add","moduleName":["Data","Semiring"]}},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[13,20],"start":[13,15]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"semiringInt","moduleName":["Data","Semiring"]}},"type":"App"},"identifier":"add"},{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"bindType":"NonRec","expression":{"abstraction":{"abstraction":{"annotation":{"meta":{"metaType":"IsForeign"},"sourceSpan":{"end":[23,32],"start":[23,3]}},"type":"Var","value":{"identifier":"discard","moduleName":["Control","Bind"]}},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[23,32],"start":[23,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"discardUnit","moduleName":["Control","Bind"]}},"type":"App"},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[23,32],"start":[23,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"bindEffect","moduleName":["Effect"]}},"type":"App"},"identifier":"discard"},{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"bindType":"NonRec","expression":{"abstraction":{"annotation":{"meta":{"metaType":"IsForeign"},"sourceSpan":{"end":[23,10],"start":[23,3]}},"type":"Var","value":{"identifier":"logShow","moduleName":["Effect","Console"]}},"annotation":{"meta":{"metaType":"IsSyntheticApp"},"sourceSpan":{"end":[23,32],"start":[23,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"showInt","moduleName":["Data","Show"]}},"type":"App"},"identifier":"logShow"},{"annotation":{"meta":null,"sourceSpan":{"end":[16,32],"start":[16,1]}},"bindType":"NonRec","expression":{"annotation":{"meta":null,"sourceSpan":{"end":[19,10],"start":[17,15]}},"argument":"v","body":{"annotation":{"meta":null,"sourceSpan":{"end":[19,10],"start":[17,15]}},"caseAlternatives":[{"binders":[{"annotation":{"meta":null,"sourceSpan":{"end":[18,14],"start":[18,3]}},"binderType":"LiteralBinder","literal":{"literalType":"ArrayLiteral","value":[{"annotation":{"meta":null,"sourceSpan":{"end":[18,6],"start":[18,5]}},"binderType":"NullBinder"},{"annotation":{"meta":null,"sourceSpan":{"end":[18,9],"start":[18,8]}},"binderType":"NullBinder"},{"annotation":{"meta":null,"sourceSpan":{"end":[18,12],"start":[18,11]}},"binderType":"VarBinder","identifier":"c"}]}}],"expression":{"annotation":{"meta":null,"sourceSpan":{"end":[18,19],"start":[18,18]}},"type":"Var","value":{"identifier":"c","sourcePos":[18,11]}},"isGuarded":false},{"binders":[{"annotation":{"meta":null,"sourceSpan":{"end":[19,4],"start":[19,3]}},"binderType":"NullBinder"}],"expression":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"negate","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[19,10],"start":[19,8]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[19,10],"start":[19,9]}},"type":"Literal","value":{"literalType":"IntLiteral","value":1}},"type":"App"},"isGuarded":false}],"caseExpressions":[{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"v","sourcePos":[0,0]}}],"type":"Case"},"type":"Abs"},"identifier":"lastOfThree"},{"annotation":{"meta":null,"sourceSpan":{"end":[11,29],"start":[11,1]}},"bindType":"NonRec","expression":{"annotation":{"meta":null,"sourceSpan":{"end":[14,10],"start":[12,12]}},"argument":"v","body":{"annotation":{"meta":null,"sourceSpan":{"end":[14,10],"start":[12,12]}},"caseAlternatives":[{"binders":[{"annotation":{"meta":null,"sourceSpan":{"end":[13,11],"start":[13,3]}},"binderType":"LiteralBinder","literal":{"literalType":"ArrayLiteral","value":[{"annotation":{"meta":null,"sourceSpan":{"end":[13,6],"start":[13,5]}},"binderType":"VarBinder","identifier":"a"},{"annotation":{"meta":null,"sourceSpan":{"end":[13,9],"start":[13,8]}},"binderType":"VarBinder","identifier":"b"}]}}],"expression":{"abstraction":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"add","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[13,20],"start":[13,15]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[13,16],"start":[13,15]}},"type":"Var","value":{"identifier":"a","sourcePos":[13,5]}},"type":"App"},"annotation":{"meta":null,"sourceSpan":{"end":[13,20],"start":[13,15]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[13,20],"start":[13,19]}},"type":"Var","value":{"identifier":"b","sourcePos":[13,8]}},"type":"App"},"isGuarded":false},{"binders":[{"annotation":{"meta":null,"sourceSpan":{"end":[14,4],"start":[14,3]}},"binderType":"NullBinder"}],"expression":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"negate","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[14,10],"start":[14,8]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[14,10],"start":[14,9]}},"type":"Literal","value":{"literalType":"IntLiteral","value":1}},"type":"App"},"isGuarded":false}],"caseExpressions":[{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"v","sourcePos":[0,0]}}],"type":"Case"},"type":"Abs"},"identifier":"firstTwo"},{"annotation":{"meta":null,"sourceSpan":{"end":[21,20],"start":[21,1]}},"bindType":"NonRec","expression":{"abstraction":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"discard","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[23,32],"start":[23,3]}},"argument":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"logShow","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[23,32],"start":[23,3]}},"argument":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[23,20],"start":[23,12]}},"type":"Var","value":{"identifier":"firstTwo","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[23,31],"start":[23,12]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[23,31],"start":[23,21]}},"type":"Literal","value":{"literalType":"ArrayLiteral","value":[{"annotation":{"meta":null,"sourceSpan":{"end":[23,25],"start":[23,23]}},"type":"Literal","value":{"literalType":"IntLiteral","value":10}},{"annotation":{"meta":null,"sourceSpan":{"end":[23,29],"start":[23,27]}},"type":"Literal","value":{"literalType":"IntLiteral","value":20}}]}},"type":"App"},"type":"App"},"type":"App"},"annotation":{"meta":null,"sourceSpan":{"end":[23,32],"start":[23,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[23,32],"start":[23,3]}},"argument":"$__unused","body":{"abstraction":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"discard","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[24,33],"start":[24,3]}},"argument":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"logShow","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[24,33],"start":[24,3]}},"argument":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[24,20],"start":[24,12]}},"type":"Var","value":{"identifier":"firstTwo","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[24,32],"start":[24,12]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[24,32],"start":[24,21]}},"type":"Literal","value":{"literalType":"ArrayLiteral","value":[{"annotation":{"meta":null,"sourceSpan":{"end":[24,24],"start":[24,23]}},"type":"Literal","value":{"literalType":"IntLiteral","value":1}},{"annotation":{"meta":null,"sourceSpan":{"end":[24,27],"start":[24,26]}},"type":"Literal","value":{"literalType":"IntLiteral","value":2}},{"annotation":{"meta":null,"sourceSpan":{"end":[24,30],"start":[24,29]}},"type":"Literal","value":{"literalType":"IntLiteral","value":3}}]}},"type":"App"},"type":"App"},"type":"App"},"annotation":{"meta":null,"sourceSpan":{"end":[24,33],"start":[24,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[24,33],"start":[24,3]}},"argument":"$__unused","body":{"abstraction":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"discard","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[25,24],"start":[25,3]}},"argument":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"logShow","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[25,24],"start":[25,3]}},"argument":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[25,20],"start":[25,12]}},"type":"Var","value":{"identifier":"firstTwo","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[25,23],"start":[25,12]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[25,23],"start":[25,21]}},"type":"Literal","value":{"literalType":"ArrayLiteral","value":[]}},"type":"App"},"type":"App"},"type":"App"},"annotation":{"meta":null,"sourceSpan":{"end":[25,24],"start":[25,3]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[25,24],"start":[25,3]}},"argument":"$__unused","body":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[0,0],"start":[0,0]}},"type":"Var","value":{"identifier":"logShow","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[26,36],"start":[26,3]}},"argument":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[26,23],"start":[26,12]}},"type":"Var","value":{"identifier":"lastOfThree","moduleName":["Golden","ArrayPatternMatch","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[26,35],"start":[26,12]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[26,35],"start":[26,24]}},"type":"Literal","value":{"literalType":"ArrayLiteral","value":[{"annotation":{"meta":null,"sourceSpan":{"end":[26,27],"start":[26,26]}},"type":"Literal","value":{"literalType":"IntLiteral","value":7}},{"annotation":{"meta":null,"sourceSpan":{"end":[26,30],"start":[26,29]}},"type":"Literal","value":{"literalType":"IntLiteral","value":8}},{"annotation":{"meta":null,"sourceSpan":{"end":[26,33],"start":[26,32]}},"type":"Literal","value":{"literalType":"IntLiteral","value":9}}]}},"type":"App"},"type":"App"},"type":"Abs"},"type":"App"},"type":"Abs"},"type":"App"},"type":"Abs"},"type":"App"},"identifier":"main"}],"exports":["firstTwo","lastOfThree","main"],"foreign":[],"imports":[{"annotation":{"meta":null,"sourceSpan":{"end":[26,36],"start":[1,1]}},"moduleName":["Control","Bind"]},{"annotation":{"meta":null,"sourceSpan":{"end":[26,36],"start":[1,1]}},"moduleName":["Data","Ring"]},{"annotation":{"meta":null,"sourceSpan":{"end":[26,36],"start":[1,1]}},"moduleName":["Data","Semiring"]},{"annotation":{"meta":null,"sourceSpan":{"end":[26,36],"start":[1,1]}},"moduleName":["Data","Show"]},{"annotation":{"meta":null,"sourceSpan":{"end":[26,36],"start":[1,1]}},"moduleName":["Effect"]},{"annotation":{"meta":null,"sourceSpan":{"end":[26,36],"start":[1,1]}},"moduleName":["Effect","Console"]},{"annotation":{"meta":null,"sourceSpan":{"end":[26,36],"start":[1,1]}},"moduleName":["Golden","ArrayPatternMatch","Test"]},{"annotation":{"meta":null,"sourceSpan":{"end":[3,15],"start":[3,1]}},"moduleName":["Prelude"]},{"annotation":{"meta":null,"sourceSpan":{"end":[26,36],"start":[1,1]}},"moduleName":["Prim"]}],"moduleName":["Golden","ArrayPatternMatch","Test"],"modulePath":"golden/Golden/ArrayPatternMatch/Test.purs","reExports":{},"sourceSpan":{"end":[26,36],"start":[1,1]}} \ No newline at end of file diff --git a/test/ps/output/Golden.ArrayPatternMatch.Test/eval/.gitignore b/test/ps/output/Golden.ArrayPatternMatch.Test/eval/.gitignore new file mode 100644 index 0000000..d2dc29b --- /dev/null +++ b/test/ps/output/Golden.ArrayPatternMatch.Test/eval/.gitignore @@ -0,0 +1 @@ +actual.txt diff --git a/test/ps/output/Golden.ArrayPatternMatch.Test/eval/golden.txt b/test/ps/output/Golden.ArrayPatternMatch.Test/eval/golden.txt new file mode 100644 index 0000000..45bdfe9 --- /dev/null +++ b/test/ps/output/Golden.ArrayPatternMatch.Test/eval/golden.txt @@ -0,0 +1,4 @@ +30 +-1 +-1 +9 diff --git a/test/ps/output/Golden.ArrayPatternMatch.Test/golden.ir b/test/ps/output/Golden.ArrayPatternMatch.Test/golden.ir new file mode 100644 index 0000000..1764425 --- /dev/null +++ b/test/ps/output/Golden.ArrayPatternMatch.Test/golden.ir @@ -0,0 +1,490 @@ +UberModule + { uberModuleBindings = + [ Standalone + ( QName + { qnameModuleName = ModuleName "Data.Semiring", qnameName = Name "foreign" + }, ForeignImport Nothing + ( ModuleName "Data.Semiring" ) ".spago/prelude/v7.2.1/src/Data/Semiring.purs" + [ ( Nothing, Name "intAdd" ), ( Nothing, Name "intMul" ) ] + ), Standalone + ( QName + { qnameModuleName = ModuleName "Effect", qnameName = Name "foreign" + }, ForeignImport Nothing + ( ModuleName "Effect" ) ".spago/effect/v4.1.0/src/Effect.purs" + [ ( Nothing, Name "pureE" ), ( Nothing, Name "bindE" ) ] + ), Standalone + ( QName + { qnameModuleName = ModuleName "Data.Semiring", qnameName = Name "semiringInt" + }, LiteralObject Nothing + [ + ( PropName "add", ObjectProp ( Just Always ) + ( Ref Nothing ( Imported ( ModuleName "Data.Semiring" ) ( Name "foreign" ) ) 0 ) + ( PropName "intAdd" ) + ), + ( PropName "zero", LiteralInt Nothing 0 ), + ( PropName "mul", ObjectProp ( Just Always ) + ( Ref Nothing ( Imported ( ModuleName "Data.Semiring" ) ( Name "foreign" ) ) 0 ) + ( PropName "intMul" ) + ), + ( PropName "one", LiteralInt Nothing 1 ) + ] + ), Standalone + ( QName + { qnameModuleName = ModuleName "Control.Applicative", qnameName = Name "pure" + }, Abs Nothing + ( ParamNamed Nothing ( Name "dict" ) ) + ( ObjectProp Nothing ( Ref Nothing ( Local ( Name "dict" ) ) 0 ) ( PropName "pure" ) ) + ), Standalone + ( QName + { qnameModuleName = ModuleName "Control.Bind", qnameName = Name "bind" }, Abs Nothing + ( ParamNamed Nothing ( Name "dict" ) ) + ( ObjectProp Nothing ( Ref Nothing ( Local ( Name "dict" ) ) 0 ) ( PropName "bind" ) ) + ), RecursiveGroup + ( + ( QName + { qnameModuleName = ModuleName "Effect", qnameName = Name "monadEffect" + }, LiteralObject Nothing + [ + ( PropName "Applicative0", Abs Nothing ( ParamUnused Nothing ) + ( Ref Nothing ( Imported ( ModuleName "Effect" ) ( Name "applicativeEffect" ) ) 0 ) + ), + ( PropName "Bind1", Abs Nothing ( ParamUnused Nothing ) + ( Ref Nothing ( Imported ( ModuleName "Effect" ) ( Name "bindEffect" ) ) 0 ) + ) + ] + ) :| + [ + ( QName + { qnameModuleName = ModuleName "Effect", qnameName = Name "bindEffect" + }, LiteralObject Nothing + [ + ( PropName "bind", ObjectProp ( Just Always ) + ( Ref Nothing ( Imported ( ModuleName "Effect" ) ( Name "foreign" ) ) 0 ) + ( PropName "bindE" ) + ), + ( PropName "Apply0", Abs Nothing ( ParamUnused Nothing ) + ( App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Effect" ) ( Name "Lazy_applyEffect" ) ) 0 + ) + ( LiteralInt Nothing 0 ) + ) + ) + ] + ), + ( QName + { qnameModuleName = ModuleName "Effect", qnameName = Name "applicativeEffect" + }, LiteralObject Nothing + [ + ( PropName "pure", ObjectProp ( Just Always ) + ( Ref Nothing ( Imported ( ModuleName "Effect" ) ( Name "foreign" ) ) 0 ) + ( PropName "pureE" ) + ), + ( PropName "Apply0", Abs Nothing ( ParamUnused Nothing ) + ( App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Effect" ) ( Name "Lazy_applyEffect" ) ) 0 + ) + ( LiteralInt Nothing 0 ) + ) + ) + ] + ), + ( QName + { qnameModuleName = ModuleName "Effect", qnameName = Name "Lazy_functorEffect" + }, App Nothing + ( App Nothing + ( Ref Nothing ( Local ( Name "PSLUA_runtime_lazy" ) ) 0 ) + ( LiteralString Nothing "functorEffect" ) + ) + ( Abs Nothing ( ParamUnused Nothing ) + ( LiteralObject Nothing + [ + ( PropName "map", Abs Nothing + ( ParamNamed Nothing ( Name "f" ) ) + ( Abs Nothing + ( ParamNamed Nothing ( Name "a" ) ) + ( App Nothing + ( App Nothing + ( ObjectProp Nothing + ( App Nothing + ( ObjectProp Nothing + ( Ref Nothing + ( Imported + ( ModuleName "Effect" ) + ( Name "applicativeEffect" ) + ) 0 + ) + ( PropName "Apply0" ) + ) + ( Ref Nothing + ( Imported ( ModuleName "Prim" ) ( Name "undefined" ) ) 0 + ) + ) + ( PropName "apply" ) + ) + ( App Nothing + ( App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Control.Applicative" ) ( Name "pure" ) ) 0 + ) + ( Ref Nothing + ( Imported ( ModuleName "Effect" ) ( Name "applicativeEffect" ) ) 0 + ) + ) + ( Ref Nothing ( Local ( Name "f" ) ) 0 ) + ) + ) + ( Ref Nothing ( Local ( Name "a" ) ) 0 ) + ) + ) + ) + ] + ) + ) + ), + ( QName + { qnameModuleName = ModuleName "Effect", qnameName = Name "Lazy_applyEffect" + }, App Nothing + ( App Nothing + ( Ref Nothing ( Local ( Name "PSLUA_runtime_lazy" ) ) 0 ) + ( LiteralString Nothing "applyEffect" ) + ) + ( Abs Nothing ( ParamUnused Nothing ) + ( LiteralObject Nothing + [ + ( PropName "apply", Let Nothing + ( Standalone + ( Nothing, Name "bind", App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Control.Bind" ) ( Name "bind" ) ) 0 + ) + ( App Nothing + ( ObjectProp Nothing + ( Ref Nothing + ( Imported ( ModuleName "Effect" ) ( Name "monadEffect" ) ) 0 + ) + ( PropName "Bind1" ) + ) + ( Ref Nothing ( Imported ( ModuleName "Prim" ) ( Name "undefined" ) ) 0 ) + ) + ) :| [] + ) + ( Abs Nothing + ( ParamNamed Nothing ( Name "f" ) ) + ( Abs Nothing + ( ParamNamed Nothing ( Name "a" ) ) + ( App Nothing + ( App Nothing + ( Ref Nothing ( Local ( Name "bind" ) ) 0 ) + ( Ref Nothing ( Local ( Name "f" ) ) 0 ) + ) + ( Abs Nothing + ( ParamNamed Nothing ( Name "f'" ) ) + ( App Nothing + ( App Nothing + ( Ref Nothing ( Local ( Name "bind" ) ) 0 ) + ( Ref Nothing ( Local ( Name "a" ) ) 0 ) + ) + ( Abs Nothing + ( ParamNamed Nothing ( Name "a'" ) ) + ( App Nothing + ( App Nothing + ( Ref Nothing + ( Imported + ( ModuleName "Control.Applicative" ) + ( Name "pure" ) + ) 0 + ) + ( App Nothing + ( ObjectProp Nothing + ( Ref Nothing + ( Imported + ( ModuleName "Effect" ) + ( Name "monadEffect" ) + ) 0 + ) + ( PropName "Applicative0" ) + ) + ( Ref Nothing + ( Imported ( ModuleName "Prim" ) ( Name "undefined" ) ) 0 + ) + ) + ) + ( App Nothing + ( Ref Nothing ( Local ( Name "f'" ) ) 0 ) + ( Ref Nothing ( Local ( Name "a'" ) ) 0 ) + ) + ) + ) + ) + ) + ) + ) + ) + ), + ( PropName "Functor0", Abs Nothing ( ParamUnused Nothing ) + ( App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Effect" ) ( Name "Lazy_functorEffect" ) ) 0 + ) + ( LiteralInt Nothing 0 ) + ) + ) + ] + ) + ) + ) + ] + ), Standalone + ( QName + { qnameModuleName = ModuleName "Golden.ArrayPatternMatch.Test", qnameName = Name "negate" + }, Abs Nothing + ( ParamNamed Nothing ( Name "a" ) ) + ( App Nothing + ( App Nothing + ( ObjectProp Nothing + ( LiteralObject Nothing + [ + ( PropName "sub", ObjectProp ( Just Always ) + ( ForeignImport Nothing + ( ModuleName "Data.Ring" ) ".spago/prelude/v7.2.1/src/Data/Ring.purs" + [ ( Nothing, Name "intSub" ) ] + ) + ( PropName "intSub" ) + ), + ( PropName "Semiring0", Abs Nothing ( ParamUnused Nothing ) + ( Ref Nothing + ( Imported ( ModuleName "Data.Semiring" ) ( Name "semiringInt" ) ) 0 + ) + ) + ] + ) + ( PropName "sub" ) + ) + ( ObjectProp Nothing + ( App Nothing + ( ObjectProp Nothing + ( LiteralObject Nothing + [ + ( PropName "sub", ObjectProp ( Just Always ) + ( ForeignImport Nothing + ( ModuleName "Data.Ring" ) ".spago/prelude/v7.2.1/src/Data/Ring.purs" + [ ( Nothing, Name "intSub" ) ] + ) + ( PropName "intSub" ) + ), + ( PropName "Semiring0", Abs Nothing ( ParamUnused Nothing ) + ( Ref Nothing + ( Imported ( ModuleName "Data.Semiring" ) ( Name "semiringInt" ) ) 0 + ) + ) + ] + ) + ( PropName "Semiring0" ) + ) + ( Ref Nothing ( Imported ( ModuleName "Prim" ) ( Name "undefined" ) ) 0 ) + ) + ( PropName "zero" ) + ) + ) + ( Ref Nothing ( Local ( Name "a" ) ) 0 ) + ) + ), Standalone + ( QName + { qnameModuleName = ModuleName "Golden.ArrayPatternMatch.Test", qnameName = Name "discard" + }, App Nothing + ( ObjectProp Nothing + ( LiteralObject Nothing + [ + ( PropName "discard", Abs Nothing + ( ParamNamed Nothing ( Name "dictBind" ) ) + ( App Nothing + ( Ref Nothing ( Imported ( ModuleName "Control.Bind" ) ( Name "bind" ) ) 0 ) + ( Ref Nothing ( Local ( Name "dictBind" ) ) 0 ) + ) + ) + ] + ) + ( PropName "discard" ) + ) + ( Ref Nothing ( Imported ( ModuleName "Effect" ) ( Name "bindEffect" ) ) 0 ) + ), Standalone + ( QName + { qnameModuleName = ModuleName "Golden.ArrayPatternMatch.Test", qnameName = Name "logShow" + }, Abs Nothing + ( ParamNamed Nothing ( Name "a" ) ) + ( App Nothing + ( ObjectProp ( Just Always ) + ( ForeignImport Nothing + ( ModuleName "Effect.Console" ) ".spago/console/v6.1.0/src/Effect/Console.purs" + [ ( Nothing, Name "log" ) ] + ) + ( PropName "log" ) + ) + ( App Nothing + ( ObjectProp Nothing + ( LiteralObject Nothing + [ + ( PropName "show", ObjectProp ( Just Always ) + ( ForeignImport Nothing + ( ModuleName "Data.Show" ) ".spago/prelude/v7.2.1/src/Data/Show.purs" + [ ( Nothing, Name "showIntImpl" ) ] + ) + ( PropName "showIntImpl" ) + ) + ] + ) + ( PropName "show" ) + ) + ( Ref Nothing ( Local ( Name "a" ) ) 0 ) + ) + ) + ), Standalone + ( QName + { qnameModuleName = ModuleName "Golden.ArrayPatternMatch.Test", qnameName = Name "lastOfThree" + }, Abs Nothing + ( ParamNamed Nothing ( Name "v" ) ) + ( IfThenElse Nothing + ( Eq Nothing + ( LiteralInt Nothing 3 ) + ( ArrayLength Nothing ( Ref Nothing ( Local ( Name "v" ) ) 0 ) ) + ) + ( ArrayIndex Nothing ( Ref Nothing ( Local ( Name "v" ) ) 0 ) 2 ) + ( App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Golden.ArrayPatternMatch.Test" ) ( Name "negate" ) ) 0 + ) + ( LiteralInt Nothing 1 ) + ) + ) + ), Standalone + ( QName + { qnameModuleName = ModuleName "Golden.ArrayPatternMatch.Test", qnameName = Name "firstTwo" + }, Abs Nothing + ( ParamNamed Nothing ( Name "v" ) ) + ( IfThenElse Nothing + ( Eq Nothing + ( LiteralInt Nothing 2 ) + ( ArrayLength Nothing ( Ref Nothing ( Local ( Name "v" ) ) 0 ) ) + ) + ( App Nothing + ( App Nothing + ( ObjectProp Nothing + ( Ref Nothing + ( Imported ( ModuleName "Data.Semiring" ) ( Name "semiringInt" ) ) 0 + ) + ( PropName "add" ) + ) + ( ArrayIndex Nothing ( Ref Nothing ( Local ( Name "v" ) ) 0 ) 0 ) + ) + ( ArrayIndex Nothing ( Ref Nothing ( Local ( Name "v" ) ) 0 ) 1 ) + ) + ( App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Golden.ArrayPatternMatch.Test" ) ( Name "negate" ) ) 0 + ) + ( LiteralInt Nothing 1 ) + ) + ) + ) + ], uberModuleForeigns = [], uberModuleExports = + [ + ( Name "firstTwo", Ref Nothing + ( Imported ( ModuleName "Golden.ArrayPatternMatch.Test" ) ( Name "firstTwo" ) ) 0 + ), + ( Name "lastOfThree", Ref Nothing + ( Imported ( ModuleName "Golden.ArrayPatternMatch.Test" ) ( Name "lastOfThree" ) ) 0 + ), + ( Name "main", App Nothing + ( App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Golden.ArrayPatternMatch.Test" ) ( Name "discard" ) ) 0 + ) + ( App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Golden.ArrayPatternMatch.Test" ) ( Name "logShow" ) ) 0 + ) + ( App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Golden.ArrayPatternMatch.Test" ) ( Name "firstTwo" ) ) 0 + ) + ( LiteralArray Nothing [ LiteralInt Nothing 10, LiteralInt Nothing 20 ] ) + ) + ) + ) + ( Abs Nothing ( ParamUnused Nothing ) + ( App Nothing + ( App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Golden.ArrayPatternMatch.Test" ) ( Name "discard" ) ) 0 + ) + ( App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Golden.ArrayPatternMatch.Test" ) ( Name "logShow" ) ) 0 + ) + ( App Nothing + ( Ref Nothing + ( Imported + ( ModuleName "Golden.ArrayPatternMatch.Test" ) + ( Name "firstTwo" ) + ) 0 + ) + ( LiteralArray Nothing + [ LiteralInt Nothing 1, LiteralInt Nothing 2, LiteralInt Nothing 3 ] + ) + ) + ) + ) + ( Abs Nothing ( ParamUnused Nothing ) + ( App Nothing + ( App Nothing + ( Ref Nothing + ( Imported ( ModuleName "Golden.ArrayPatternMatch.Test" ) ( Name "discard" ) ) 0 + ) + ( App Nothing + ( Ref Nothing + ( Imported + ( ModuleName "Golden.ArrayPatternMatch.Test" ) + ( Name "logShow" ) + ) 0 + ) + ( App Nothing + ( Ref Nothing + ( Imported + ( ModuleName "Golden.ArrayPatternMatch.Test" ) + ( Name "firstTwo" ) + ) 0 + ) + ( LiteralArray Nothing [] ) + ) + ) + ) + ( Abs Nothing ( ParamUnused Nothing ) + ( App Nothing + ( Ref Nothing + ( Imported + ( ModuleName "Golden.ArrayPatternMatch.Test" ) + ( Name "logShow" ) + ) 0 + ) + ( App Nothing + ( Ref Nothing + ( Imported + ( ModuleName "Golden.ArrayPatternMatch.Test" ) + ( Name "lastOfThree" ) + ) 0 + ) + ( LiteralArray Nothing + [ LiteralInt Nothing 7, LiteralInt Nothing 8, LiteralInt Nothing 9 ] + ) + ) + ) + ) + ) + ) + ) + ) + ) + ] + } \ No newline at end of file diff --git a/test/ps/output/Golden.ArrayPatternMatch.Test/golden.lua b/test/ps/output/Golden.ArrayPatternMatch.Test/golden.lua new file mode 100644 index 0000000..84b8d23 --- /dev/null +++ b/test/ps/output/Golden.ArrayPatternMatch.Test/golden.lua @@ -0,0 +1,128 @@ +local function PSLUA_runtime_lazy(name) + return function(init) + return function() + local state = 0 + local val = nil + if state == 2 then + return val + else + if state == 1 then + return error(name .. " was needed before it finished initializing") + else + state = 1 + val = init() + state = 2 + return val + end + end + end + end +end +local M = {} +M.Data_Semiring_foreign = { + intAdd = function(x) return function(y) return x + y end end, + intMul = function(x) return function(y) return x * y end end +} +M.Effect_foreign = { + pureE = function(a) + return function() + return a + end + end, + bindE = function(a) + return function(f) + return function() + return f(a())() + end + end + end +} +M.Data_Semiring_semiringInt = { + add = M.Data_Semiring_foreign.intAdd, + zero = 0, + mul = M.Data_Semiring_foreign.intMul, + one = 1 +} +M.Control_Applicative_pure = function(dict) return dict.pure end +M.Control_Bind_bind = function(dict) return dict.bind end +M.Effect_monadEffect = { + Applicative0 = function() return M.Effect_applicativeEffect end, + Bind1 = function() return M.Effect_bindEffect end +} +M.Effect_bindEffect = { + bind = M.Effect_foreign.bindE, + Apply0 = function() return M.Effect_Lazy_applyEffect(0) end +} +M.Effect_applicativeEffect = { + pure = M.Effect_foreign.pureE, + Apply0 = function() return M.Effect_Lazy_applyEffect(0) end +} +M.Effect_Lazy_functorEffect = PSLUA_runtime_lazy("functorEffect")(function() + return { + map = function(f) + return function(a) + return (M.Effect_applicativeEffect.Apply0()).apply(M.Control_Applicative_pure(M.Effect_applicativeEffect)(f))(a) + end + end + } +end) +M.Effect_Lazy_applyEffect = PSLUA_runtime_lazy("applyEffect")(function() + return { + apply = (function() + return function(f) + local bind = M.Control_Bind_bind(M.Effect_monadEffect.Bind1()) + return function(a) + return bind(f)(function(fPrime) + return bind(a)(function(aPrime) + return M.Control_Applicative_pure(M.Effect_monadEffect.Applicative0())(fPrime(aPrime)) + end) + end) + end + end + end)(), + Functor0 = function() return M.Effect_Lazy_functorEffect(0) end + } +end) +M.Golden_ArrayPatternMatch_Test_negate = function(a) + return (function(x) return function(y) return x - y end end)(((function() + return M.Data_Semiring_semiringInt + end)()).zero)(a) +end +M.Golden_ArrayPatternMatch_Test_discard = (function(dictBind) + return M.Control_Bind_bind(dictBind) +end)(M.Effect_bindEffect) +M.Golden_ArrayPatternMatch_Test_logShow = function(a) + return (function(s) return function() print(s) end end)((function(n) return tostring(n) end)(a)) +end +M.Golden_ArrayPatternMatch_Test_lastOfThree = function(v) + if 3 == #(v) then + return v[3] + else + return M.Golden_ArrayPatternMatch_Test_negate(1) + end +end +M.Golden_ArrayPatternMatch_Test_firstTwo = function(v) + if 2 == #(v) then + return M.Data_Semiring_semiringInt.add(v[1])(v[2]) + else + return M.Golden_ArrayPatternMatch_Test_negate(1) + end +end +return M.Golden_ArrayPatternMatch_Test_discard(M.Golden_ArrayPatternMatch_Test_logShow(M.Golden_ArrayPatternMatch_Test_firstTwo({ + [1] = 10, + [2] = 20 +})))(function() + return M.Golden_ArrayPatternMatch_Test_discard(M.Golden_ArrayPatternMatch_Test_logShow(M.Golden_ArrayPatternMatch_Test_firstTwo({ + [1] = 1, + [2] = 2, + [3] = 3 + })))(function() + return M.Golden_ArrayPatternMatch_Test_discard(M.Golden_ArrayPatternMatch_Test_logShow(M.Golden_ArrayPatternMatch_Test_firstTwo({})))(function( ) + return M.Golden_ArrayPatternMatch_Test_logShow(M.Golden_ArrayPatternMatch_Test_lastOfThree({ + [1] = 7, + [2] = 8, + [3] = 9 + })) + end) + end) +end)()