@@ -278,12 +278,12 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
278278 aty = Just $ CppAny [CppConst, CppRef]
279279 rty = Just $ CppAny []
280280
281− declToCpp _ (_, ident) val = do
281+ declToCpp qs (_, ident) val = do
282282 val' <- valueToCpp val
283283 return $
284284 CppVariableIntroduction
285285 (identToCpp ident, Just $ CppAny [CppConst])
286− []
286+ qs
287287 (Just val')
288288
289289 -------------------------------------------------------------------------------------------------
@@ -336,35 +336,46 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
336336
337337 recursive :: Cpp -> [Cpp]
338338 recursive cpp'@(CppFunction _ _ _ qs _) | CppRecursive `elem` qs = [cpp']
339+ recursive cpp'@(CppVariableIntroduction _ qs _) | CppRecursive `elem` qs = [cpp']
339340 recursive _ = []
340341
341342 removeRec :: Cpp -> Cpp
342− removeRec(CppFunction _ _ _ qs _) | CppRecursive `elem` qs = CppNoOp
343+ removeRec (CppFunction _ _ _ qs _) | CppRecursive `elem` qs = CppNoOp
344+ removeRec (CppVariableIntroduction _ qs _) | CppRecursive `elem` qs = CppNoOp
343345 removeRec cpp' = cpp'
344346
347+ replaceVar :: [(String, Cpp)] -> Cpp -> Cpp
348+ replaceVar rs = go
349+ where
350+ go (CppVar var) | Just var' <- lookup var rs = var'
351+ go cpp' = cpp'
352+
345353 dict :: Cpp
346354 dict = CppVariableIntroduction
347355 (topdict, Just $ CppAny [CppConst])
348356 []
349357 (Just $ CppDataLiteral ((\(f,cpp) -> CppComment [LineComment f] cpp) <$> fns))
350358
359+ accessed :: String -> String -> (String, Cpp)
360+ accessed dictname name =
361+ (name, CppApp
362+ (CppIndexer
363+ (CppNumericLiteral .
364+ Left .
365+ fromIntegral .
366+ fromJust $ findIndex ((== name) . fst) fns)
367+ dict'')
368+ [dict'])
369+ where
370+ dict' = CppVar dictname
371+ dict'' = CppCast dataType dict'
372+
351373 accessor :: String -> (String, Cpp) -> Cpp
352374 accessor dictname (name, _) =
353375 CppVariableIntroduction
354376 (name, Just $ CppAny [CppConst])
355377 []
356− (Just accessed)
357− where
358− dict' = CppVar dictname
359− dict'' = CppCast dataType dict'
360− accessed = CppApp
361− (CppIndexer
362− (CppNumericLiteral .
363− Left .
364− fromIntegral .
365− fromJust $ findIndex ((== name) . fst) fns)
366− dict'')
367− [dict']
378+ (Just . snd $ accessed dictname name)
368379
369380 toelem :: Cpp -> (String, Cpp)
370381 toelem (CppFunction name args rtyp _ body'@(CppBlock body)) =
@@ -373,6 +384,12 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
373384 args
374385 rtyp
375386 (CppBlock $ (accessor localdict <$> filteredFns body') ++ body))
387+
388+ toelem (CppVariableIntroduction (name, _) _ (Just body')) =
389+ let replacements = accessed localdict . fst <$> filteredFns body'
390+ replaced = everywhereOnCpp (replaceVar replacements) body'
391+ in (name, withdict . maybeRemCaps $ CppBlock [CppReturn replaced])
392+
376393 toelem _ = error "not a function"
377394
378395 withdict :: Cpp -> Cpp
@@ -495,19 +512,21 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
495512 hasRecursion :: [Cpp] -> Bool
496513 hasRecursion cpps' = any (everythingOnCpp (||) hasRecursiveRef) cpps'
497514 where
498− fnames :: [String]
499− fnames = concatMap (everythingOnCpp (++) fname) cpps'
515+ valnames :: [String]
516+ valnames = concatMap (everythingOnCpp (++) valname) cpps'
500517
501− fname :: Cpp -> [String]
502− fname (CppFunction name _ _ _ _) = [name]
503− fname _ = []
518+ valname :: Cpp -> [String]
519+ valname (CppFunction name _ _ _ _) = [name]
520+ valname (CppVariableIntroduction (name, _) _ _) = [name]
521+ valname _ = []
504522
505523 hasRecursiveRef :: Cpp -> Bool
506524 hasRecursiveRef (CppFunction _ _ _ _ body) = everythingOnCpp (||) ref body
525+ hasRecursiveRef (CppVariableIntroduction _ _ (Just value)) = everythingOnCpp (||) ref value
507526 hasRecursiveRef _ = False
508527
509528 ref :: Cpp -> Bool
510− ref (CppVar name) | name `elem` fnames = True
529+ ref (CppVar name) | name `elem` valnames = True
511530 ref _ = False
512531
513532 valueToCpp Constructor{} = return CppNoOp