Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/Language/PureScript/Backend/Lua.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{-# LANGUAGE QuasiQuotes #-}

module Language.PureScript.Backend.Lua
( fromUberModule
, fromIR
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions test/ps/golden/Golden/ArrayPatternMatch/Test.purs
Original file line number Diff line number Diff line change
@@ -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 ])
1 change: 1 addition & 0 deletions test/ps/output/Golden.ArrayPatternMatch.Test/corefn.json
Original file line number Diff line number Diff line change
@@ -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]}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
actual.txt
4 changes: 4 additions & 0 deletions test/ps/output/Golden.ArrayPatternMatch.Test/eval/golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
30
-1
-1
9
Loading
Loading