Skip to content

Commit

Permalink
Refactor - Lenses for Ann in Oczor.Syntax.Types (#16)
Browse files Browse the repository at this point in the history
* Refactor - Lenses for `Ann` in Oczor.Syntax.Types

* Refactor - Lenses for `Ann` in Oczor.Syntax.Types #2

* Refactor - Lenses for `Ann` in Oczor.Syntax.Types #3

* Fix

* Fix #2

* Apply patch
  • Loading branch information
xgrommx authored and nponeccop committed Mar 28, 2017
1 parent 0670b75 commit 93ff9ca
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
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

0 comments on commit 93ff9ca

Please sign in to comment.