@@ -6,6 +6,7 @@ import Data.Argonaut as Json
66import Data.Array as Array
77import Data.Array.NonEmpty as NEA
88import Data.Array.NonEmpty as NonEmptyArray
9+import Data.Bifunctor (bimap)
910import Data.Char (toCharCode)
1011import Data.Int as Int
1112import Data.Maybe (Maybe(..))
@@ -88,9 +89,7 @@ codegenModule { name, bindings, imports, foreign: foreign_ } =
8889
8990definitionIdentifiers :: ChezDefinition -> Array String
9091definitionIdentifiers = case _ of
91− DefineValue i _ -> [ i ]
92− DefineCurriedFunction i _ _ -> [ i ]
93− DefineUncurriedFunction i _ _ -> [ i ]
92+ Define i _ -> [ i ]
9493 DefineRecordType i [ x ] ->
9594 [ recordTypeCurriedConstructor i
9695 , recordTypePredicate i
@@ -118,11 +117,8 @@ codegenTopLevelBindingGroup
118117 :: CodegenEnv
119118 -> BackendBindingGroup Ident NeutralExpr
120119 -> Array ChezDefinition
121−codegenTopLevelBindingGroup codegenEnv { recursive, bindings }
122− | recursive, Just bindings' <- NonEmptyArray.fromArray bindings =
123− [ DefineValue "rtlbg" $ S.Identifier "recursive-top-level-binding-group" ]
124− | otherwise =
125− Array.concatMap (codegenTopLevelBinding codegenEnv) bindings
120+codegenTopLevelBindingGroup codegenEnv { bindings } =
121+ Array.concatMap (codegenTopLevelBinding codegenEnv) bindings
126122
127123codegenTopLevelBinding
128124 :: CodegenEnv
@@ -130,25 +126,12 @@ codegenTopLevelBinding
130126 -> Array ChezDefinition
131127codegenTopLevelBinding codegenEnv (Tuple (Ident i) n) =
132128 case unwrap n of
133− Abs a e ->
134− [ DefineCurriedFunction
135− i
136− (uncurry toChezIdent <$> a)
137− (codegenExpr codegenEnv e)
138− ]
139− UncurriedAbs a e ->
140− [ DefineUncurriedFunction
141− i
142− (uncurry toChezIdent <$> a)
143− (codegenExpr codegenEnv e)
144− ]
145129 CtorDef _ _ _ ss ->
146130 case NonEmptyArray.fromArray ss of
147131 Nothing ->
148− [ DefineValue i (S.quote $ S.Identifier i)
149− , DefineUncurriedFunction
150− (S.recordTypePredicate i)
151− [ "v" ]
132+ [ Define i (S.quote $ S.Identifier i)
133+ , Define (S.recordTypePredicate i)
134+ $ S.mkUncurriedFn [ "v" ]
152135 $ S.eqQ (S.quote $ S.Identifier i) (S.Identifier "v")
153136 ]
154137 Just xs
@@ -156,13 +139,13 @@ codegenTopLevelBinding codegenEnv (Tuple (Ident i) n) =
156139 [ DefineRecordType i ss ]
157140 | otherwise ->
158141 [ DefineRecordType i ss
159− , DefineCurriedFunction i xs
142+ , Define i $ S.mkCurriedFn xs
160143 $ S.runUncurriedFn
161144 (S.Identifier $ S.recordTypeUncurriedConstructor i)
162145 (map S.Identifier ss)
163146 ]
164147 _ ->
165− [ DefineValue i $ codegenExpr codegenEnv n ]
148+ [ Define i $ codegenExpr codegenEnv n ]
166149
167150codegenExpr :: CodegenEnv -> NeutralExpr -> ChezExpr
168151codegenExpr codegenEnv@{ currentModule } s = case unwrap s of
@@ -209,9 +192,9 @@ codegenExpr codegenEnv@{ currentModule } s = case unwrap s of
209192 (map (codegenExpr codegenEnv <<< Tuple.snd) xs)
210193 CtorDef _ _ _ _ ->
211194 unsafeCrashWith "codegenExpr:CtorDef - handled by codegenTopLevelBinding!"
212−
213− LetRec _ _ _ ->
214− S.Identifier "let-rec"
195+ LetRec lvl bindings expr ->
196+ S.Let true (map (bimap (flip toChezIdent lvl <<< Just) (codegenExpr codegenEnv)) bindings) $
197+ codegenExpr codegenEnv expr
215198 Let _ _ _ _ ->
216199 codegenPureChain codegenEnv s
217200 Branch b o -> do