1
1
mirror of https://github.com/github/semantic.git synced 2025-01-01 11:46:14 +03:00

Make desugar tail-recur (and we don't need a Stack).

This commit is contained in:
Patrick Thomson 2019-09-30 12:32:28 -04:00
parent f7c4658ee6
commit 696d2fc786

View File

@ -150,14 +150,15 @@ data Located a = Located Loc a
-- assignment, storing the names we encounter as we go and eventually -- assignment, storing the names we encounter as we go and eventually
-- returning a terminal expression. We have to keep track of which -- returning a terminal expression. We have to keep track of which
desugar :: (Member (Reader SourcePath) sig, Carrier sig m, MonadFail m) desugar :: (Member (Reader SourcePath) sig, Carrier sig m, MonadFail m)
=> RHS Span => [Located Name]
-> m ((Stack (Located Name)), Desugared Span) -> RHS Span
desugar = \case -> m ([Located Name], Desugared Span)
desugar acc = \case
Left Py.Assignment { left = OneExpression name, right = Just rhs, ann} -> do Left Py.Assignment { left = OneExpression name, right = Just rhs, ann} -> do
loc <- locFromTSSpan <$> ask <*> pure ann loc <- locFromTSSpan <$> ask <*> pure ann
let cons = (Stack.:> Located loc name) let cons = (Located loc name :)
fmap (first cons) (desugar rhs) desugar (cons acc) rhs
Right (Right any) -> pure (Stack.Nil, any) Right (Right any) -> pure (acc, any)
other -> fail ("desugar: couldn't desugar RHS " <> show other) other -> fail ("desugar: couldn't desugar RHS " <> show other)
-- This is a fold function that is invoked from a left fold but that -- This is a fold function that is invoked from a left fold but that
@ -183,9 +184,8 @@ instance Compile (Py.Assignment Span) where
, ann , ann
} cc = do } cc = do
p <- ask @SourcePath p <- ask @SourcePath
(names, val) <- desugar rhs (names, val) <- desugar [Located (locFromTSSpan p ann) name] rhs
let allNames = names Stack.:> Located (locFromTSSpan p ann) name compile val >>= foldr collapseDesugared (const cc) names >>= locate it
compile val >>= foldr collapseDesugared (const cc) allNames >>= locate it
compileCC other _ = fail ("Unhandled assignment case: " <> show other) compileCC other _ = fail ("Unhandled assignment case: " <> show other)