@@ -1,4 +1,4 @@
1−-----------------------------------------------------------------------------
1+---------------------------------------------------------------------------------------------------
3-- Module : Language.PureScript.CodeGen.Cpp
3-- Module : Language.PureScript.CodeGen.Cpp
4-- Copyright : (c) 2013-15 Phil Freeman, Andy Arvanitis, and other contributors
4-- Copyright : (c) 2013-15 Phil Freeman, Andy Arvanitis, and other contributors
@@ -9,10 +9,10 @@
9-- Portability :
9-- Portability :
12−-- This module generates code in the simplified C++1x intermediate representation from Purescript code
12+-- This module generates code in the simplified C++1x intermediate representation from
14−-----------------------------------------------------------------------------
15+---------------------------------------------------------------------------------------------------
16{-# LANGUAGE GADTs #-}
16{-# LANGUAGE GADTs #-}
17{-# LANGUAGE ViewPatterns #-}
17{-# LANGUAGE ViewPatterns #-}
18{-# LANGUAGE FlexibleContexts #-}
18{-# LANGUAGE FlexibleContexts #-}
@@ -63,35 +63,39 @@ moduleToCpp :: forall m. (Applicative m, Monad m, MonadReader Options m, MonadSu
63 => E.Environment -> Module Ann -> m [Cpp]
63 => E.Environment -> Module Ann -> m [Cpp]
64---------------------------------------------------------------------------------------------------
64---------------------------------------------------------------------------------------------------
65moduleToCpp env (Module _ mn imps _ foreigns decls) = do
65moduleToCpp env (Module _ mn imps _ foreigns decls) = do
67+ traverse (pure . runModuleName) .
66− cppImports <- traverse (pure . runModuleName) . delete (ModuleName [ProperName C.prim]) . (\\ [mn]) $ imps
68+ delete (ModuleName [ProperName C.prim]) . (\\ [mn]) $
67− let cppImports' = "PureScript" : cppImports
70+ let cppImports' = "PureScript" : cppImports
68− cppDecls <- concat <$> mapM bindToCpp decls
71+ cppDecls <- concat <$> mapM bindToCpp decls
69− optimized <- traverse optimize cppDecls
72+ optimized <- traverse optimize cppDecls
70− let moduleHeader = fileBegin mn "HH"
73+ let moduleHeader =
71− ++ P.linebreak
75+ P.linebreak ++
72− ++ ((\i -> CppInclude i i) <$> cppImports')
76+ ((\i -> CppInclude i i) <$> cppImports') ++
73− ++ (if not (null foreigns)
77+ (if not (null foreigns)
74− then [CppInclude [] (runModuleName mn ++ "_ffi")]
78+ then [CppInclude [] (runModuleName mn ++ "_ffi")]
75− else []
79+ else []) ++
77− ++ P.linebreak
80+ P.linebreak ++
78− ++ [CppNamespace (runModuleName mn) $
81+ [ CppNamespace (runModuleName mn) $
79− (CppUseNamespace <$> cppImports') ++ P.linebreak
82+ (CppUseNamespace <$> cppImports') ++
80− ++ toHeader optimized
81− ++ toHeaderFns optimized
83+ P.linebreak ++ toHeader optimized ++ toHeaderFns optimized ] ++
84− ++ fileEnd mn "HH"
84+ P.linebreak ++ fileEnd mn "HH"
85− let bodyCpps = toBodyDecl optimized ++ toBody optimized
85+ let bodyCpps = toBodyDecl optimized ++ toBody optimized
86− moduleBody = CppInclude (runModuleName mn) (runModuleName mn) : P.linebreak
87+ CppInclude (runModuleName mn) (runModuleName mn) :
87− ++ (if null bodyCpps
89+ (if null bodyCpps
89− else [CppNamespace (runModuleName mn) $
91+ else [ CppNamespace (runModuleName mn) $
90− (CppUseNamespace <$> cppImports') ++ P.linebreak ++ bodyCpps])
92+ (CppUseNamespace <$> cppImports') ++
93+ P.linebreak ++ bodyCpps ]) ++
91− ++ P.linebreak
94+ P.linebreak ++
92− ++ (if isMain mn then [nativeMain] ++ P.linebreak else [])
96+ then [nativeMain] ++ P.linebreak
93− return $ moduleHeader ++ CppEndOfHeader : moduleBody
98+ return $ moduleHeader ++ CppEndOfHeader : moduleBody
97 -- Generate code in the simplified C++1x intermediate representation for a declaration
101 -- Generate code in the simplified C++1x intermediate representation for a declaration
@@ -115,37 +119,47 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
115 body@(Accessor _ _ (Var _ (Qualified Nothing arg'))))
119 body@(Accessor _ _ (Var _ (Qualified Nothing arg'))))
116 | arg' == arg = do
120 | arg' == arg = do
117 block <- asReturnBlock <$> valueToCpp body
121 block <- asReturnBlock <$> valueToCpp body
118− let fn' = CppFunction (identToCpp ident)
122+ let fn' = CppFunction
119− [(identToCpp arg, Just $ CppAny [CppConst, CppRef])]
124+ [(identToCpp arg, Just $ CppAny [CppConst, CppRef])]
120− (Just $ CppAny [])
125+ (Just $ CppAny [])
123 return fn'
128 return fn'
125 declToCpp vqs ident (Abs (_, com, _, _) arg body) = do
130 declToCpp vqs ident (Abs (_, com, _, _) arg body) = do
126 block <- asReturnBlock <$> valueToCpp body
131 block <- asReturnBlock <$> valueToCpp body
127 let block' = convertNestedLambdas block
132 let block' = convertNestedLambdas block
128− fn' = CppFunction (identToCpp ident) [(identToCpp arg, Just $ CppAny [CppConst, CppRef])]
135+ [(identToCpp arg, Just $ CppAny [CppConst, CppRef])]
129− (Just $ CppAny [])
136+ (Just $ CppAny [])
132 return (CppComment com fn')
139 return (CppComment com fn')
134− declToCpp _ ident (Constructor _ _ (ProperName _) []) = return $
141+ declToCpp _ ident (Constructor _ _ (ProperName _) []) =
135− CppFunction (identToCpp ident)
144+ (identToCpp ident)
136− [] (Just $ CppAny [])
146+ (Just $ CppAny [])
137− [CppInline]
147+ [CppInline]
138− (CppBlock [CppReturn (CppDataLiteral [CppStringLiteral (identToCpp ident)])])
148+ (CppBlock [CppReturn (CppDataLiteral [CppStringLiteral (identToCpp ident)])])
140 declToCpp _ ident (Constructor _ _ (ProperName _) fields) = return . CppNamespace [] $
150 declToCpp _ ident (Constructor _ _ (ProperName _) fields) = return . CppNamespace [] $
141− [ CppFunction ('$' : identToCpp ident) (farg <$> fields')
152+ ('$' : identToCpp ident)
142− (Just $ CppAny [])
154+ (Just $ CppAny [])
143− [CppInline]
155+ [CppInline]
144− (CppBlock fullyConstructed)
156+ (CppBlock fullyConstructed)
145− , CppFunction (identToCpp ident) (farg <$> f)
158+ (identToCpp ident)
146− (Just $ CppAny [])
160+ (Just $ CppAny [])
148− (CppBlock (fieldLambdas fs))
162+ (CppBlock (fieldLambdas fs))
151 name = identToCpp ident
165 name = identToCpp ident
@@ -154,24 +168,31 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
154 | otherwise = ([], [])
168 | otherwise = ([], [])
155 farg = \x -> (x, Just $ CppAny [CppConst, CppRef])
169 farg = \x -> (x, Just $ CppAny [CppConst, CppRef])
156 fieldLambdas flds
170 fieldLambdas flds
157− | (f' : fs') <- flds = [CppReturn . maybeRemCaps $
171+ | (f' : fs') <- flds = [ CppReturn . maybeRemCaps $
158− CppLambda [CppCaptureAll] (farg <$> [f'])
173+ [CppCaptureAll]
159− (Just $ CppAny [])
175+ (Just $ CppAny [])
160− (CppBlock $ fieldLambdas fs')]
176+ (CppBlock $ fieldLambdas fs') ]
161 | otherwise = fullyConstructed
177 | otherwise = fullyConstructed
162 fullyConstructed = [CppReturn (CppDataLiteral (CppStringLiteral name : (CppVar <$> fields')))]
178 fullyConstructed = [CppReturn (CppDataLiteral (CppStringLiteral name : (CppVar <$> fields')))]
164 declToCpp _ ident val = do
180 declToCpp _ ident val = do
165 val' <- valueToCpp val
181 val' <- valueToCpp val
183+ CppVariableIntroduction
166− return $ CppVariableIntroduction (identToCpp ident, Just $ CppAny [CppConst]) [] (Just val')
184+ (identToCpp ident, Just $ CppAny [CppConst])
168 -------------------------------------------------------------------------------------------------
188 -------------------------------------------------------------------------------------------------
169 fnToLambda :: Cpp -> Cpp
189 fnToLambda :: Cpp -> Cpp
170 -------------------------------------------------------------------------------------------------
190 -------------------------------------------------------------------------------------------------
171 fnToLambda (CppFunction name args _ qs body) =
191 fnToLambda (CppFunction name args _ qs body) =
172− CppVariableIntroduction (name, Just $ CppAny [CppConst])
192+ CppVariableIntroduction
193+ (name, Just $ CppAny [CppConst])
173− (filter (==CppStatic) qs)
194+ (filter (==CppStatic) qs)
174− (Just (CppLambda cs args (Just $ CppAny []) body))
195+ (Just $ CppLambda cs args (Just $ CppAny []) body)
176 cs | (not . null) (qs `intersect` [CppInline, CppStatic]) = []
197 cs | (not . null) (qs `intersect` [CppInline, CppStatic]) = []
177 | otherwise = [CppCaptureAll]
198 | otherwise = [CppCaptureAll]
@@ -211,26 +232,36 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
211 removeRec cpp' = cpp'
232 removeRec cpp' = cpp'
213 dict :: Cpp
234 dict :: Cpp
214− dict = CppVariableIntroduction (topdict, Just $ CppAny [CppConst]) []
235+ dict = CppVariableIntroduction
236+ (topdict, Just $ CppAny [CppConst])
215− (Just $ CppObjectLiteral fns)
238+ (Just $ CppObjectLiteral fns)
217 accessor :: String -> (String, Cpp) -> Cpp
240 accessor :: String -> (String, Cpp) -> Cpp
241+ accessor dictname (name, _) =
218− accessor dictname (name, _) = CppVariableIntroduction (name, Just $ CppAny [CppConst]) [] (Just accessed)
242+ CppVariableIntroduction
243+ (name, Just $ CppAny [CppConst])
220 dict' = CppVar dictname
247 dict' = CppVar dictname
221 accessed = CppApp (CppIndexer (CppStringLiteral name) dict') [dict']
248 accessed = CppApp (CppIndexer (CppStringLiteral name) dict') [dict']
223 toelem :: Cpp -> (String, Cpp)
250 toelem :: Cpp -> (String, Cpp)
224 toelem (CppFunction name args rtyp _ (CppBlock body)) =
251 toelem (CppFunction name args rtyp _ (CppBlock body)) =
225− (name, withdict . maybeRemCaps $ CppLambda [CppCaptureAll]
252+ (name, withdict . maybeRemCaps $ CppLambda
226− args rtyp (CppBlock $ (accessor localdict <$> fns) ++ body))
256+ (CppBlock $ (accessor localdict <$> fns) ++ body))
227 toelem _ = error "not a function"
257 toelem _ = error "not a function"
229 withdict :: Cpp -> Cpp
259 withdict :: Cpp -> Cpp
230− withdict cpp' = maybeRemCaps $
260+ withdict cpp' = maybeRemCaps $ CppLambda
231− CppLambda [CppCaptureAll] [(localdict, Just $ CppAny [CppConst, CppRef])]
262+ [(localdict, Just $ CppAny [CppConst, CppRef])]
232− (Just $ CppAny [])
263+ (Just $ CppAny [])
233− (CppBlock [CppReturn cpp'])
264+ (CppBlock [CppReturn cpp'])
234 topdict :: String
265 topdict :: String
235 topdict = "__dict__"
266 topdict = "__dict__"
236 localdict :: String
267 localdict :: String
@@ -292,7 +323,12 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
292 valueToCpp (Abs _ arg body) = do
323 valueToCpp (Abs _ arg body) = do
293 cpp <- valueToCpp body
324 cpp <- valueToCpp body
294 let cpp' = convertNestedLambdas cpp
325 let cpp' = convertNestedLambdas cpp
295− return $ CppLambda [] [(identToCpp arg, Just $ CppAny [CppConst, CppRef])] (Just $ CppAny []) (asReturnBlock cpp')
329+ [(identToCpp arg, Just $ CppAny [CppConst, CppRef])]
331+ (asReturnBlock cpp')
297 -- valueToCpp (App _ e (Var _ (Qualified (Just (ModuleName [ProperName "Prim"])) (Ident "undefined")))) =
333 -- valueToCpp (App _ e (Var _ (Qualified (Just (ModuleName [ProperName "Prim"])) (Ident "undefined")))) =
298 -- valueToCpp e
334 -- valueToCpp e
@@ -307,7 +343,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
307 Var (_, _, _, Just IsTypeClassConstructor) (Qualified mn' (Ident classname)) ->
343 Var (_, _, _, Just IsTypeClassConstructor) (Qualified mn' (Ident classname)) ->
308 let Just (_, constraints, fns) = findClass (Qualified mn' (ProperName classname)) in
344 let Just (_, constraints, fns) = findClass (Qualified mn' (ProperName classname)) in
309 return . CppObjectLiteral $ zip ((sort $ superClassDictionaryNames constraints) ++ (fst <$> fns)) args'
345 return . CppObjectLiteral $ zip ((sort $ superClassDictionaryNames constraints) ++ (fst <$> fns)) args'
310− _ -> -- TODO: verify this
346+ _ ->
311 flip (foldl (\fn a -> CppApp fn [a])) args' <$> valueToCpp f
347 flip (foldl (\fn a -> CppApp fn [a])) args' <$> valueToCpp f
313 valueToCpp (Case (maybeSpan, _, ty, _) values binders) = do
349 valueToCpp (Case (maybeSpan, _, ty, _) values binders) = do
@@ -322,7 +358,9 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
322 then convertRecursiveFns ds''
358 then convertRecursiveFns ds''
323 else ds''
359 else ds''
324 let cpps = convertNestedLambdas <$> rs
360 let cpps = convertNestedLambdas <$> rs
325− return $ CppApp (CppLambda [] [] Nothing (CppBlock (cpps ++ [CppReturn ret]))) []
362+ (CppLambda [] [] Nothing (CppBlock (cpps ++ [CppReturn ret])))
327 hasRecursion :: [Cpp] -> Bool
365 hasRecursion :: [Cpp] -> Bool
328 hasRecursion cpps' = any (everythingOnCpp (||) hasRecursiveRef) cpps'
366 hasRecursion cpps' = any (everythingOnCpp (||) hasRecursiveRef) cpps'
@@ -367,10 +405,14 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
367 cpps <- forM binders $ \(CaseAlternative bs result) -> do
405 cpps <- forM binders $ \(CaseAlternative bs result) -> do
368 ret <- guardsToCpp result
406 ret <- guardsToCpp result
369 go valNames ret bs
407 go valNames ret bs
370− return $ CppApp (CppLambda []
410+ (CppLambda
372− (Just $ CppAny [])
413+ (Just $ CppAny [])
373− (CppBlock (assignments ++ concat cpps ++ [failedPatternError valNames]))) []
414+ (CppBlock (assignments ++ concat cpps ++ [failedPatternError valNames])))
375 mkVarDecl :: String -> Maybe Cpp -> Cpp
417 mkVarDecl :: String -> Maybe Cpp -> Cpp
376 mkVarDecl name = CppVariableIntroduction (name, Nothing) []
418 mkVarDecl name = CppVariableIntroduction (name, Nothing) []
@@ -386,7 +428,9 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
386 CppThrow $ CppApp (CppVar "runtime_error") [CppStringLiteral errorMessage]
428 CppThrow $ CppApp (CppVar "runtime_error") [CppStringLiteral errorMessage]
388 errorMessage :: String
430 errorMessage :: String
431+ errorMessage = "Failed pattern match" ++
389− errorMessage = "Failed pattern match" ++ maybe "" (((" at " ++ runModuleName mn ++ " ") ++) . displayStartEndPos) maybeSpan ++ ": "
432+ maybe "" (((" at " ++ runModuleName mn ++ " ") ++) . displayStartEndPos) maybeSpan ++
391 guardsToCpp :: Either [(Guard Ann, Expr Ann)] (Expr Ann) -> m [Cpp]
435 guardsToCpp :: Either [(Guard Ann, Expr Ann)] (Expr Ann) -> m [Cpp]
392 guardsToCpp (Left gs) = forM gs $ \(cond, val) -> do
436 guardsToCpp (Left gs) = forM gs $ \(cond, val) -> do
@@ -408,7 +452,12 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
408 literalToBinderCpp varName done l
452 literalToBinderCpp varName done l
410 binderToCpp varName done (VarBinder _ ident) =
454 binderToCpp varName done (VarBinder _ ident) =
456+ CppVariableIntroduction
411− return (CppVariableIntroduction (identToCpp ident, Just $ CppAny [CppConst]) [] (Just (CppVar varName)) : done)
457+ (identToCpp ident, Just $ CppAny [CppConst])
459+ (Just $ CppVar varName)
413 binderToCpp varName done (ConstructorBinder (_, _, _, Just IsNewtype) _ _ [b]) =
462 binderToCpp varName done (ConstructorBinder (_, _, _, Just IsNewtype) _ _ [b]) =
414 binderToCpp varName done b
463 binderToCpp varName done b
@@ -418,13 +467,19 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
418 (Qualified _ (ProperName ctor))
467 (Qualified _ (ProperName ctor))
420 cpps <- go (zip fields bs) done
469 cpps <- go (zip fields bs) done
421− return $ case ctorType of
471+ case ctorType of
422− ProductType -> cpps
472+ ProductType -> cpps
423− SumType ->
473+ SumType ->
424− [ CppIfElse (CppBinary Equal (CppIndexer (CppVar ctorKey) (CppCast dataType $ CppVar varName))
479+ (CppCast dataType $ CppVar varName))
425− (CppStringLiteral ctor))
480+ (CppStringLiteral ctor))
426− (CppBlock cpps)
481+ (CppBlock cpps)
427− Nothing ]
482+ Nothing]
429 go :: [(Ident, Binder Ann)] -> [Cpp] -> m [Cpp]
484 go :: [(Ident, Binder Ann)] -> [Cpp] -> m [Cpp]
430 go [] done' = return done'
485 go [] done' = return done'
@@ -432,10 +487,14 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
432 argVar <- freshName
487 argVar <- freshName
433 done'' <- go remain done'
488 done'' <- go remain done'
434 cpps <- binderToCpp argVar done'' binder
489 cpps <- binderToCpp argVar done'' binder
435− return (CppVariableIntroduction (argVar, Nothing)
491+ CppVariableIntroduction
495+ (fieldToIndex field)
437− (Just (CppIndexer (fieldToIndex field) (CppCast dataType $ CppVar varName)))
496+ (CppCast dataType $ CppVar varName))
439 fieldToIndex :: Ident -> Cpp
498 fieldToIndex :: Ident -> Cpp
440 fieldToIndex = CppNumericLiteral . Left . (+1) . read . dropWhile isLetter . runIdent
499 fieldToIndex = CppNumericLiteral . Left . (+1) . read . dropWhile isLetter . runIdent
@@ -444,31 +503,47 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
445 binderToCpp varName done (NamedBinder _ ident binder) = do
504 binderToCpp varName done (NamedBinder _ ident binder) = do
446 cpp <- binderToCpp varName done binder
505 cpp <- binderToCpp varName done binder
507+ CppVariableIntroduction
447− return (CppVariableIntroduction (identToCpp ident, Just $ CppAny [CppConst]) [] (Just (CppVar varName)) : cpp)
508+ (identToCpp ident, Just $ CppAny [CppConst])
510+ (Just $ CppVar varName)
449 -------------------------------------------------------------------------------------------------
513 -------------------------------------------------------------------------------------------------
450 literalToBinderCpp :: String -> [Cpp] -> Literal (Binder Ann) -> m [Cpp]
514 literalToBinderCpp :: String -> [Cpp] -> Literal (Binder Ann) -> m [Cpp]
451 -------------------------------------------------------------------------------------------------
515 -------------------------------------------------------------------------------------------------
452 literalToBinderCpp varName done (NumericLiteral num) =
516 literalToBinderCpp varName done (NumericLiteral num) =
453− return [CppIfElse (CppBinary Equal (CppVar varName) (CppNumericLiteral num)) (CppBlock done) Nothing]
518+ (CppBinary Equal (CppVar varName) (CppNumericLiteral num))
455 literalToBinderCpp varName done (CharLiteral c) =
522 literalToBinderCpp varName done (CharLiteral c) =
456− return [CppIfElse (CppBinary Equal (CppVar varName)
524+ (CppBinary Equal (CppVar varName) (CppCharLiteral c))
458− (CppBlock done)
525+ (CppBlock done)
459− Nothing]
526+ Nothing ]
461 literalToBinderCpp varName done (StringLiteral str) =
528 literalToBinderCpp varName done (StringLiteral str) =
462− return [CppIfElse (CppBinary Equal (CppVar varName)
530+ (CppBinary Equal (CppVar varName) (CppStringLiteral str))
463− (CppStringLiteral str))
464− (CppBlock done)
531+ (CppBlock done)
465− Nothing]
532+ Nothing ]
467 literalToBinderCpp varName done (BooleanLiteral True) =
534 literalToBinderCpp varName done (BooleanLiteral True) =
468− return [CppIfElse (CppVar varName) (CppBlock done) Nothing]
535+ return [ CppIfElse
470 literalToBinderCpp varName done (BooleanLiteral False) =
540 literalToBinderCpp varName done (BooleanLiteral False) =
471− return [CppIfElse (CppUnary CppNot (CppVar varName)) (CppBlock done) Nothing]
541+ return [ CppIfElse
473 literalToBinderCpp varName done (ObjectLiteral bs) = go done bs
548 literalToBinderCpp varName done (ObjectLiteral bs) = go done bs
@@ -478,19 +553,28 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
478 propVar <- freshName
553 propVar <- freshName
479 done'' <- go done' bs'
554 done'' <- go done' bs'
480 cpp <- binderToCpp propVar done'' binder
555 cpp <- binderToCpp propVar done'' binder
481− return (CppVariableIntroduction (propVar, Nothing)
557+ CppVariableIntroduction
483− (Just (CppIndexer (CppStringLiteral prop) (CppVar varName)))
561+ (CppStringLiteral prop)
486 literalToBinderCpp varName done (ArrayLiteral bs) = do
565 literalToBinderCpp varName done (ArrayLiteral bs) = do
487 cpp <- go done 0 bs
566 cpp <- go done 0 bs
488− let cond = case length bs of
568+ case length bs of
489− 0 -> CppBinary Dot (CppCast arrayType $ CppVar varName)
571+ (CppCast arrayType $ CppVar varName)
490− (CppApp (CppVar "empty") [])
572+ (CppApp (CppVar "empty") [])
491− n -> let var = CppCast arrayType $ CppVar varName in
573+ n -> let var = CppCast arrayType $ CppVar varName
492− CppBinary Equal (CppBinary Dot var (CppApp (CppVar "size") []))
576+ (CppBinary Dot var (CppApp (CppVar "size") []))
493− (CppNumericLiteral (Left (fromIntegral n)))
577+ (CppNumericLiteral (Left (fromIntegral n)))
494 return [ CppIfElse cond (CppBlock cpp) Nothing ]
578 return [ CppIfElse cond (CppBlock cpp) Nothing ]
496 go :: [Cpp] -> Integer -> [Binder Ann] -> m [Cpp]
580 go :: [Cpp] -> Integer -> [Binder Ann] -> m [Cpp]
@@ -499,28 +583,41 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
499 elVar <- freshName
583 elVar <- freshName
500 done'' <- go done' (index + 1) bs'
584 done'' <- go done' (index + 1) bs'
501 cpp <- binderToCpp elVar done'' binder
585 cpp <- binderToCpp elVar done'' binder
502− return (CppVariableIntroduction (elVar, Nothing) []
587+ CppVariableIntroduction
503− (Just (CppIndexer (CppNumericLiteral (Left index)) (CppVar varName))) : cpp)
591+ (CppNumericLiteral (Left index))
505 -------------------------------------------------------------------------------------------------
595 -------------------------------------------------------------------------------------------------
507 unApp :: Expr Ann -> [Expr Ann] -> (Expr Ann, [Expr Ann])
596 unApp :: Expr Ann -> [Expr Ann] -> (Expr Ann, [Expr Ann])
597+ -------------------------------------------------------------------------------------------------
508 unApp (App _ val arg) args = unApp val (arg : args)
598 unApp (App _ val arg) args = unApp val (arg : args)
509 unApp other args = (other, args)
599 unApp other args = (other, args)
601+ -------------------------------------------------------------------------------------------------
511 qualifiedToCpp :: (a -> Ident) -> Qualified a -> Cpp
602 qualifiedToCpp :: (a -> Ident) -> Qualified a -> Cpp
603+ -------------------------------------------------------------------------------------------------
512− qualifiedToCpp f (Qualified (Just (ModuleName [ProperName mn'])) a) | mn' == C.prim = CppVar . runIdent $ f a
604+ qualifiedToCpp f (Qualified (Just (ModuleName [ProperName mn'])) a)
605+ | mn' == C.prim = CppVar . runIdent $ f a
513 qualifiedToCpp f (Qualified (Just mn') a)
606 qualifiedToCpp f (Qualified (Just mn') a)
514 | mn /= mn' = CppAccessor (CppVar . identToCpp $ f a) (CppVar (moduleNameToCpp mn'))
607 | mn /= mn' = CppAccessor (CppVar . identToCpp $ f a) (CppVar (moduleNameToCpp mn'))
515 qualifiedToCpp f (Qualified _ a) = CppVar $ identToCpp (f a)
608 qualifiedToCpp f (Qualified _ a) = CppVar $ identToCpp (f a)
610+ -------------------------------------------------------------------------------------------------
517 qualifiedToStr' :: (a -> Ident) -> Qualified a -> String
611 qualifiedToStr' :: (a -> Ident) -> Qualified a -> String
612+ -------------------------------------------------------------------------------------------------
518 qualifiedToStr' = qualifiedToStr mn
613 qualifiedToStr' = qualifiedToStr mn
521 -- Find a class in scope by name, retrieving its list of constraints, function names and types.
616 -- Find a class in scope by name, retrieving its list of constraints, function names and types.
618+ -------------------------------------------------------------------------------------------------
523 findClass :: Qualified (ProperName ClassName) -> Maybe ([String], [T.Constraint], [(String, T.Type)])
619 findClass :: Qualified (ProperName ClassName) -> Maybe ([String], [T.Constraint], [(String, T.Type)])
620+ -------------------------------------------------------------------------------------------------
524 findClass name
621 findClass name
525 | Just (params, fns, constraints) <- M.lookup name (E.typeClasses env),
622 | Just (params, fns, constraints) <- M.lookup name (E.typeClasses env),
526 fns' <- (\(i,t) -> (runIdent i, t)) <$> fns
623 fns' <- (\(i,t) -> (runIdent i, t)) <$> fns