git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #570
commit4d31abb5351d7466b8cb7d540ca13e31b680ba3c authorPhil Freeman <paf31@cantab.net> dateSun 24 Aug 2014, 21:18 (12 years ago) parent2e0559b
2 files changed+32−7
examples/passing/DeepArrayBinder.purs+16 src/Language/PureScript/CodeGen/JS.hs+16−7
examples/passing/DeepArrayBinder.pursnew file+16
@@ -0,0 +1,16 @@
1+module Main where
2+
3+import Control.Monad.Eff
4+
5+match2 :: [Number] -> Number
6+match2 (x : y : xs) = x * y + match2 xs
7+match2 _ = 0
8+
9+foreign import explode
10+ "function explode() {\
11+ \ throw new Error('Incorrect result');\
12+ \}" :: forall eff a. Eff eff a
13+
14+main = case match2 [1, 2, 3, 4, 5, 6, 7, 8, 9] of
15+ 100 -> Debug.Trace.trace "Done"
16+ _ -> explode
src/Language/PureScript/CodeGen/JS.hs+16−7
@@ -27,7 +27,7 @@ import Data.Maybe (catMaybes, fromJust)
2727import Data.Function (on)
2828import Data.List (nub, (\\), delete, sortBy)
2929
30−import Control.Monad (replicateM, forM)
30+import Control.Monad (foldM, replicateM, forM)
3131import Control.Applicative
3232
3333import Language.PureScript.Names
@@ -392,18 +392,27 @@ binderToJs m e varName done (ArrayBinder bs) = do
392392 done'' <- go done' (index + 1) bs'
393393 js <- binderToJs m e elVar done'' binder
394394 return (JSVariableIntroduction elVar (Just (JSIndexer (JSNumericLiteral (Left index)) (JSVar varName))) : js)
395−binderToJs m e varName done (ConsBinder headBinder tailBinder) = do
396− headVar <- freshName
395+binderToJs m e varName done binder@(ConsBinder _ _) = do
396+ let (headBinders, tailBinder) = uncons [] binder
397+ numberOfHeadBinders = fromIntegral $ length headBinders
398+ js1 <- foldM (\done' (headBinder, index) -> do
399+ headVar <- freshName
400+ jss <- binderToJs m e headVar done' headBinder
401+ return (JSVariableIntroduction headVar (Just (JSIndexer (JSNumericLiteral (Left index)) (JSVar varName))) : jss)) done (zip headBinders [0..])
397402 tailVar <- freshName
398− js1 <- binderToJs m e headVar done headBinder
399403 js2 <- binderToJs m e tailVar js1 tailBinder
400− return [JSIfElse (JSBinary GreaterThan (JSAccessor "length" (JSVar varName)) (JSNumericLiteral (Left 0))) (JSBlock
401− ( JSVariableIntroduction headVar (Just (JSIndexer (JSNumericLiteral (Left 0)) (JSVar varName))) :
402− JSVariableIntroduction tailVar (Just (JSApp (JSAccessor "slice" (JSVar varName)) [JSNumericLiteral (Left 1)])) :
404+ return [JSIfElse (JSBinary GreaterThanOrEqualTo (JSAccessor "length" (JSVar varName)) (JSNumericLiteral (Left numberOfHeadBinders))) (JSBlock
405+ ( JSVariableIntroduction tailVar (Just (JSApp (JSAccessor "slice" (JSVar varName)) [JSNumericLiteral (Left numberOfHeadBinders)])) :
403406 js2
404407 )) Nothing]
408+ where
409+ uncons :: [Binder] -> Binder -> ([Binder], Binder)
410+ uncons acc (ConsBinder h t) = uncons (h : acc) t
411+ uncons acc (PositionedBinder _ b) = uncons acc b
412+ uncons acc tailBinder = (reverse acc, tailBinder)
405413binderToJs m e varName done (NamedBinder ident binder) = do
406414 js <- binderToJs m e varName done binder
407415 return (JSVariableIntroduction (identToJs ident) (Just (JSVar varName)) : js)
408416binderToJs m e varName done (PositionedBinder _ binder) =
409417 binderToJs m e varName done binder
418+