Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor - Lenses for Ann in Oczor.Syntax.Types #16

Merged
merged 6 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Oczor/Compiler/Utl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ compileJsPartTxt x = do
inferTxt2 x = either (putStrLn . pack . show) (putStrLn . pack . prettyShow) $ inferTxt x

inferType :: Expr -> Either Error TypeExpr
inferType y = (fst . attr . snd) <$> inferAllExpr baseTypeContext y
inferType y = (attrType . snd) <$> inferAllExpr baseTypeContext y

inferTxt :: String -> Either Error TypeExpr
inferTxt x = normalizeType <$> (Parser.parseExpr x >>= inferType)
Expand Down
3 changes: 2 additions & 1 deletion src/Oczor/Infer/InferAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import Oczor.Utl
type InferExprF = AnnF ExprF (TypeExpr, InferContext)
type InferExpr = Ann ExprF (TypeExpr, InferContext)

attrType = fst . attr
attrType :: Ann a (x, y) -> x
attrType = view (attr . _1)

annType x y = Ann x (y, emptyContext)

Expand Down
19 changes: 7 additions & 12 deletions src/Oczor/Syntax/Ast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
module Oczor.Syntax.Ast (module Oczor.Syntax.Ast, module Oczor.Syntax.Types, Lits(..), Stmts(..)) where

import ClassyPrelude
import Control.Lens
import Data.Functor.Foldable
import Data.Functor.Foldable.TH
import Oczor.Syntax.Types
Expand Down Expand Up @@ -70,8 +71,11 @@ makeBaseFunctor ''Expr

deriving instance Show a => Show (ExprF a)

data Ann f a = Ann (f (Ann f a)) a deriving (Functor, Foldable, Traversable)
data AnnF f a r = AnnF (f r) a deriving (Functor, Foldable, Traversable)
data Ann f a = Ann { _unAnn :: f (Ann f a), _attr :: a } deriving (Functor, Foldable, Traversable)
data AnnF f a r = AnnF { _unAnnF :: f r, _attrF :: a } deriving (Functor, Foldable, Traversable)

makeLenses ''Ann
makeLenses ''AnnF

type instance Base (Ann f a) = AnnF f a

Expand All @@ -85,16 +89,7 @@ instance Show a => Show (Ann ExprF a) where
show (Ann x y) = "(" ++ show x ++ " ANN " ++ show y ++ ")"

stripAnns :: Ann ExprF a -> Expr
stripAnns = cata $ \case AnnF x _ -> embed x

attr :: Ann f a -> a
attr (Ann _ a) = a

unAnn :: Ann f a -> f (Ann f a)
unAnn (Ann a _) = a

changeAttr :: Ann f a -> a -> Ann f a
changeAttr (Ann x a) = Ann x
stripAnns = cata $ embed . view unAnnF

pattern UnAnn x <- Ann x y

Expand Down