-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathSyntax.hs
179 lines (143 loc) · 4.66 KB
/
Syntax.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
{-# LANGUAGE DeriveDataTypeable, DeriveGeneric, DeriveAnyClass, DeriveFunctor, TypeFamilies #-}
{-# LANGUAGE DeriveFoldable, DeriveTraversable, PatternSynonyms #-}
{-# LANGUAGE TemplateHaskell, StandaloneDeriving, OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
module Grin.ExtendedSyntax.Syntax
( module Grin.ExtendedSyntax.Syntax
, module Grin.ExtendedSyntax.SyntaxDefs
) where
import Data.Data
import Control.DeepSeq
import Data.Binary
import Data.Functor.Foldable.TH
import Data.Int
import Data.Text (Text, isPrefixOf)
import Data.Vector
import Data.Word
import GHC.Generics (Generic)
import Lens.Micro.Platform
import qualified Data.ByteString.Short as B
import Grin.ExtendedSyntax.SyntaxDefs
import Grin.ExtendedSyntax.TypeEnvDefs
-- * GRIN Externals, i.e. primops and foreign functions
data Ty
= TyCon Name [Ty]
| TyVar Name
| TySimple SimpleType
deriving (Generic, Data, NFData, Binary, Eq, Ord, Show)
data ExternalKind
= PrimOp -- ^ Implemented in the internal code generator
| FFI -- ^ Implemented in C and linked during the linker phase
deriving (Generic, Data, NFData, Binary, Eq, Ord, Show)
data External
= External
{ eName :: Name
, eRetType :: Ty
, eArgsType :: [Ty]
, eEffectful :: Bool
, eKind :: ExternalKind
}
deriving (Generic, Data, NFData, Binary, Eq, Ord, Show)
isExternalName :: [External] -> Name -> Bool
isExternalName es n = n `Prelude.elem` (eName <$> es)
-- * GRIN Literal
-- QUESTION: Now #undefined can be pattern matched on.
-- Should the linter warn about this?
data Lit
= LInt Word32 Int
| LWord Word32 Word
| LFloat Float
| LBool Bool
| LString Text
| LChar Char
deriving (Generic, Data, NFData, Binary, Eq, Ord, Show)
-- * GRIN Value
data Val
= ConstTagNode Tag [Name]
| Unit
-- simple val
| Lit Lit
| Var Name
| Undefined Type
deriving (Generic, Data, NFData, Binary, Eq, Ord, Show)
-- See: https://github.com/ekmett/recursion-schemes/blob/master/Data/Functor/Foldable/TH.hs#L31
makeBaseFunctor ''Val
-- * Case Pattern
data CPat
= NodePat Tag [Name] -- HIGH level GRIN
| LitPat Lit -- HIGH level GRIN
| DefaultPat -- HIGH level GRIN
deriving (Generic, Data, NFData, Binary, Eq, Show, Ord)
-- * Binding pattern
data BPat
= VarPat { _bPatVar :: Name }
| AsPat { _bPatTag :: Tag
, _bPatFields :: [Name]
, _bPatVar :: Name
}
deriving (Generic, Data, NFData, Binary, Eq, Show, Ord)
makeLenses ''BPat
-- * GRIN Expression
type SimpleExp = Exp
type Alt = Exp
type Def = Exp
type Program = Exp
data Exp
= Program [External] [Def]
| Def Name [Name] Exp
-- Exp
| EBind SimpleExp BPat Exp
| ECase Name [Alt]
-- Simple Exp
| SApp Name [Name]
| SReturn Val
| SStore Name
| SFetch Name
| SUpdate Name Name
| SBlock Exp
-- Alt
| Alt CPat Name Exp
deriving (Generic, Data, NFData, Binary, Eq, Ord, Show)
externals :: Exp -> [External]
externals = \case
Program es _ -> es
_ -> []
-- See: https://github.com/ekmett/recursion-schemes/blob/master/Data/Functor/Foldable/TH.hs#L31
makeBaseFunctor ''Exp
deriving instance Show a => Show (ExpF a)
deriving instance Eq a => Eq (ExpF a)
deriving instance Ord a => Ord (ExpF a)
pattern BoolPat b = LitPat (LBool b)
_AltCPat :: Traversal' Exp CPat
_AltCPat f (Alt p n e) = Alt <$> f p <*> pure n <*> pure e
_AltCPat _ other = pure other
_AltFCPat :: Traversal' (ExpF a) CPat
_AltFCPat f (AltF p n e) = AltF <$> f p <*> pure n <*> pure e
_AltFCPat _ other = pure other
_ValVar :: Traversal' Val Name
_ValVar f (Var name) = Var <$> f name
_ValVar _ other = pure other
_OnlyVarPat :: Traversal' BPat Name
_OnlyVarPat f (VarPat v) = VarPat <$> f v
_OnlyVarPat _ other = pure other
_BPatVar :: Traversal' BPat Name
_BPatVar f (AsPat tag vars v) = AsPat <$> pure tag <*> pure vars <*> f v
_BPatVar f (VarPat v) = VarPat <$> f v
_CPatNodeTag :: Traversal' CPat Tag
_CPatNodeTag f (NodePat tag args) = (`NodePat` args) <$> f tag
_CPatNodeTag _ other = pure other
_CPatLit :: Traversal' CPat Lit
_CPatLit f (LitPat lit) = LitPat <$> f lit
_CPatLit _ other = pure other
_CPatDefault :: Traversal' CPat ()
_CPatDefault f DefaultPat = const DefaultPat <$> f ()
_CPatDefault _ other = pure other
_TyCon :: Traversal' Ty (Name, [Ty])
_TyCon f (TyCon n ts) = uncurry TyCon <$> f (n, ts)
_TyCon _ other = pure other
_TyVar :: Traversal' Ty Name
_TyVar f (TyVar n) = TyVar <$> f n
_TyVar _ other = pure other
_TySimple :: Traversal' Ty SimpleType
_TySimple f (TySimple t) = TySimple <$> f t
_TySimple _ other = pure other