Skip to content

Commit b88f3f4

Browse files
committed
Eval: desugarBinds: refactor
1 parent 7bbe3fc commit b88f3f4

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/Nix/Eval.hs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -262,34 +262,37 @@ attrSetAlter ks' pos m' p' val =
262262
insertVal . ((toValue @(AttrSet v, PositionSet)) <=< ((,mempty) <$>) . sequenceA . snd) <$> go p'' m'' ks
263263

264264
desugarBinds :: forall r . ([Binding r] -> r) -> [Binding r] -> [Binding r]
265-
desugarBinds embed binds = evalState (traverse (findBinding <=< collect) binds) mempty
265+
desugarBinds embed = (`evalState` mempty) . traverse (findBinding <=< collect)
266266
where
267267
collect
268268
:: Binding r
269269
-> State
270-
(HashMap VarName (SourcePos, [Binding r]))
270+
(AttrSet (SourcePos, [Binding r]))
271271
(Either VarName (Binding r))
272-
collect (NamedVar (StaticKey x :| y : ys) val p) =
272+
collect (NamedVar (StaticKey x :| y : ys) val oldPosition) =
273273
do
274-
m <- get
275-
put $
276-
join
277-
(M.insert
278-
x
279-
. maybe
280-
(p, one $ bindValAt p)
281-
(\ (sp, bnd) -> (sp, one (bindValAt sp) <> bnd))
282-
. M.lookup x
283-
)
284-
m
274+
modify updateBindingInformation
285275
pure $ Left x
286276
where
287-
bindValAt = NamedVar (y :| ys) val
277+
updateBindingInformation
278+
:: AttrSet (SourcePos, [Binding r])
279+
-> AttrSet (SourcePos, [Binding r])
280+
updateBindingInformation =
281+
M.insert x
282+
=<< maybe
283+
(mkBindingSingleton oldPosition)
284+
(\ (foundPosition, newBindings) -> second (<> newBindings) $ mkBindingSingleton foundPosition)
285+
. M.lookup x
286+
mkBindingSingleton :: SourcePos -> (SourcePos, [Binding r])
287+
mkBindingSingleton np = (np , one $ bindValAt np)
288+
where
289+
bindValAt :: SourcePos -> Binding r
290+
bindValAt = NamedVar (y :| ys) val
288291
collect x = pure $ pure x
289292

290293
findBinding
291294
:: Either VarName (Binding r)
292-
-> State (HashMap VarName (SourcePos, [Binding r])) (Binding r)
295+
-> State (AttrSet (SourcePos, [Binding r])) (Binding r)
293296
findBinding =
294297
either
295298
(\ x ->

0 commit comments

Comments
 (0)