Skip to content

Commit

Permalink
Merge c90e08b into c503a24
Browse files Browse the repository at this point in the history
  • Loading branch information
Anabra authored Nov 30, 2019
2 parents c503a24 + c90e08b commit 6c198f2
Show file tree
Hide file tree
Showing 21 changed files with 817 additions and 390 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,10 @@ codeGen e = flip evalState emptyCGState $ para folder e >> mkCByProgramM where
-}
AltF cpat (_, cgAlt) -> pure $ A cpat cgAlt
-- NOTE: Currently, the names of the alternatives are ignored by the analysis.
NAltF cpat n (_, cgAlt) -> pure $ A cpat cgAlt
NAltF cpat n (_, cgAlt) -> do
altNameReg <- newReg
addReg n altNameReg
pure $ A cpat cgAlt

SAppF name args -> getExternal name >>= \case
Just ext -> do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ codeGenM = cata folder where

AltF _ exp -> exp
-- NOTE: Currently, the names of the alternatives are ignored by the analysis.
NAltF _ _ exp -> exp
NAltF _ n exp -> do
altNameReg <- newReg
addReg n altNameReg
exp

SAppF name args -> getExternal name >>= \case
Just ext -> do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,10 @@ codeGenM = cata folder where

AltF cpat exp -> pure $ A cpat exp
-- NOTE: Currently, the names of the alternatives are ignored by the analysis.
NAltF cpat n exp -> pure $ A cpat exp
NAltF cpat n exp -> do
altNameReg <- newReg
addReg n altNameReg
pure $ A cpat exp

SAppF name args -> do
-- copy args to definition's variables ; read function result register
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,10 @@ codeGenM e = (cata folder >=> const setMainLive) e

AltF cpat exp -> pure $ A cpat exp
-- NOTE: Currently, the names of the alternatives are ignored by the analysis.
NAltF cpat n exp -> pure $ A cpat exp
NAltF cpat n exp -> do
altNameReg <- newReg
addReg n altNameReg
pure $ A cpat exp

SAppF name args -> do
appReg <- newReg
Expand Down
4 changes: 2 additions & 2 deletions grin/src/Grin/ExtendedSyntax/Parse/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ifThenElse i = do

simpleExp :: Pos -> Parser SimpleExp
simpleExp i = SReturn <$ kw "pure" <*> value <|>
ECase <$ kw "case" <*> var <* kw "of" <*> (L.indentGuard sc GT i >>= some . (\pos -> try (alternative pos) <|> nAlternative pos)) <|>
ECase <$ kw "case" <*> var <* kw "of" <*> (L.indentGuard sc GT i >>= some . nAlternative) <|>
SStore <$ kw "store" <*> var <|>
SFetch <$ kw "fetch" <*> var <|>
SUpdate <$ kw "update" <*> var <*> var <|>
Expand All @@ -71,7 +71,7 @@ nAlternative i = NAlt <$> try (L.indentGuard sc EQ i *> altPat) <*> (op "@" *> v
-- and we don't want to parenthesize variables, literals and units.
bindingPat :: Parser BPat
bindingPat =
try (AsPat <$> (var <* char '@') <*> {- parens -} value) <|>
try (flip AsPat <$> value <*> (op "@" *> var)) <|>
VarPat <$> var


Expand Down
2 changes: 1 addition & 1 deletion grin/src/Grin/ExtendedSyntax/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ instance Pretty Lit where
instance Pretty BPat where
pretty = \case
VarPat v -> pretty v
AsPat v val -> pretty v <> pretty '@' <> pretty '(' <> pretty val <> pretty ')'
AsPat v pat -> pretty pat <> pretty '@' <> pretty v

instance Pretty CPat where
pretty = \case
Expand Down
20 changes: 7 additions & 13 deletions grin/src/Grin/ExtendedSyntax/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ data CPat


-- * Binding pattern

data BPat
= VarPat { _bPatVar :: Name }
-- TODO: swap the fields so that it is consistent with the concrete syntax
| AsPat { _bPatVar :: Name
, _bPatVal :: Val
}
Expand Down Expand Up @@ -137,20 +137,14 @@ deriving instance Ord a => Ord (ExpF a)
pattern BoolPat b = LitPat (LBool b)

_AltCPat :: Traversal' Exp CPat
_AltCPat f (Alt p e) = (`Alt` e) <$> f p
_AltCPat _ other = pure other
_AltCPat f (Alt p e) = (`Alt` e) <$> f p
_AltCPat f (NAlt p n e) = NAlt <$> f p <*> pure n <*> pure e
_AltCPat _ other = pure other

_AltFCPat :: Traversal' (ExpF a) CPat
_AltFCPat f (AltF p e) = (`AltF` e) <$> f p
_AltFCPat _ other = pure other

_NAltCPat :: Traversal' Exp CPat
_NAltCPat f (NAlt p n e) = NAlt <$> f p <*> pure n <*> pure e
_NAltCPat _ other = pure other

_NAltFCPat :: Traversal' (ExpF a) CPat
_NAltFCPat f (NAltF p n e) = NAltF <$> f p <*> pure n <*> pure e
_NAltFCPat _ other = pure other
_AltFCPat f (AltF p e) = (`AltF` e) <$> f p
_AltFCPat f (NAltF p n e) = NAltF <$> f p <*> pure n <*> pure e
_AltFCPat _ other = pure other

_ValVar :: Traversal' Val Name
_ValVar f (Var name) = Var <$> f name
Expand Down
5 changes: 3 additions & 2 deletions grin/src/Transformations/ExtendedSyntax/Conversion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ convertToNew :: Exp -> New.Exp
convertToNew = convert . nameEverything

nameEverything :: Exp -> Exp
nameEverything = nodeArgumentNaming
nameEverything = bindNormalisation
. nodeArgumentNaming
. bindNormalisation
. appArgumentNaming
. bindNormalisation
Expand All @@ -284,7 +285,7 @@ appArgumentNaming e = fst . evalNameM e . cata alg $ e where
newArgName :: NameM Name
newArgName = deriveNewName "x"

-- NOTE: we can ssume tha Producer Name Introduction
-- NOTE: we can assume that Producer Name Introduction
-- & Binding Pattern Simplification has already been run
-- ConstTagNodes can only appear in SReturns
nodeArgumentNaming :: Exp -> Exp
Expand Down
1 change: 1 addition & 0 deletions grin/src/Transformations/ExtendedSyntax/Names.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ mapNameDefExpM f = \case
EBind leftExp (VarPat var) rightExp -> do EBind leftExp <$> (VarPat <$> f var) <*> pure rightExp
EBind leftExp (AsPat var val) rightExp -> EBind leftExp <$> (AsPat <$> f var <*> mapNamesValM f val) <*> pure rightExp
Alt cpat body -> Alt <$> mapNamesCPatM f cpat <*> pure body
NAlt cpat n body -> NAlt <$> mapNamesCPatM f cpat <*> f n <*> pure body
exp -> pure exp

mapNamesCPatM :: Monad m => (Name -> m Name) -> CPat -> m CPat
Expand Down
Loading

0 comments on commit 6c198f2

Please sign in to comment.