-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbsExp.hs
More file actions
221 lines (183 loc) · 5.54 KB
/
Copy pathAbsExp.hs
File metadata and controls
221 lines (183 loc) · 5.54 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
-- File generated by the BNF Converter (bnfc 2.9.4).
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PatternSynonyms #-}
-- | The abstract syntax of language Exp0.
module AbsExp where
import Prelude (Integer, String)
import qualified Prelude as C
( Eq, Ord, Show, Read
, Functor, Foldable, Traversable
, Int, Maybe(..)
)
import qualified Data.String
type Program = Program' BNFC'Position
data Program' a = Program a [TopDef' a]
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
type TopDef = TopDef' BNFC'Position
data TopDef' a = FnDef a (Type' a) Ident [Arg' a] (Block' a)
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
type Arg = Arg' BNFC'Position
data Arg' a = Arg a (Type' a) Ident | ArgRef a (Type' a) Ident
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
type Block = Block' BNFC'Position
data Block' a = Block a [Stmt' a]
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
type Stmt = Stmt' BNFC'Position
data Stmt' a
= Empty a
| BStmt a (Block' a)
| Decl a (Type' a) [Item' a]
| LocFun a (TopDef' a)
| Ass a (Lval' a) (Expr' a)
| AssTup a [Tupl' a] (Expr' a)
| TAss a (Lval' a) [Expr' a]
| Incr a (Lval' a)
| Decr a (Lval' a)
| Ret a (Expr' a)
| Cont a
| Break a
| Cond a (Expr' a) (Stmt' a)
| CondElse a (Expr' a) (Stmt' a) (Stmt' a)
| While a (Expr' a) (Stmt' a)
| SExp a (Expr' a)
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
type Item = Item' BNFC'Position
data Item' a = NoInit a Ident | Init a Ident (Expr' a)
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
type Lval = Lval' BNFC'Position
data Lval' a = SmplVal a Ident | CmplVal a (Lval' a) (Expr' a)
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
type Tupl = Tupl' BNFC'Position
data Tupl' a = Tup a (Lval' a) | ETup a
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
type Type = Type' BNFC'Position
data Type' a
= Int a
| Str a
| Bool a
| Tuple a [Type' a]
| Array a (Type' a) Integer
| Fun a (Type' a) [Type' a]
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
type Expr = Expr' BNFC'Position
data Expr' a
= EVar a (Lval' a)
| ELitInt a Integer
| ELitTrue a
| ELitFalse a
| EApp a Ident [Expr' a]
| EString a String
| Neg a (Expr' a)
| Not a (Expr' a)
| EMul a (Expr' a) (MulOp' a) (Expr' a)
| EAdd a (Expr' a) (AddOp' a) (Expr' a)
| ERel a (Expr' a) (RelOp' a) (Expr' a)
| EAnd a (Expr' a) (Expr' a)
| EOr a (Expr' a) (Expr' a)
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
type AddOp = AddOp' BNFC'Position
data AddOp' a = Plus a | Minus a
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
type MulOp = MulOp' BNFC'Position
data MulOp' a = Times a | Div a | Mod a
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
type RelOp = RelOp' BNFC'Position
data RelOp' a = LTH a | LE a | GTH a | GE a | EQU a | NE a
deriving (C.Eq, C.Ord, C.Show, C.Read, C.Functor, C.Foldable, C.Traversable)
newtype Ident = Ident String
deriving (C.Eq, C.Ord, C.Show, C.Read, Data.String.IsString)
-- | Start position (line, column) of something.
type BNFC'Position = C.Maybe (C.Int, C.Int)
pattern BNFC'NoPosition :: BNFC'Position
pattern BNFC'NoPosition = C.Nothing
pattern BNFC'Position :: C.Int -> C.Int -> BNFC'Position
pattern BNFC'Position line col = C.Just (line, col)
-- | Get the start position of something.
class HasPosition a where
hasPosition :: a -> BNFC'Position
instance HasPosition Program where
hasPosition = \case
Program p _ -> p
instance HasPosition TopDef where
hasPosition = \case
FnDef p _ _ _ _ -> p
instance HasPosition Arg where
hasPosition = \case
Arg p _ _ -> p
ArgRef p _ _ -> p
instance HasPosition Block where
hasPosition = \case
Block p _ -> p
instance HasPosition Stmt where
hasPosition = \case
Empty p -> p
BStmt p _ -> p
Decl p _ _ -> p
LocFun p _ -> p
Ass p _ _ -> p
AssTup p _ _ -> p
TAss p _ _ -> p
Incr p _ -> p
Decr p _ -> p
Ret p _ -> p
Cont p -> p
Break p -> p
Cond p _ _ -> p
CondElse p _ _ _ -> p
While p _ _ -> p
SExp p _ -> p
instance HasPosition Item where
hasPosition = \case
NoInit p _ -> p
Init p _ _ -> p
instance HasPosition Lval where
hasPosition = \case
SmplVal p _ -> p
CmplVal p _ _ -> p
instance HasPosition Tupl where
hasPosition = \case
Tup p _ -> p
ETup p -> p
instance HasPosition Type where
hasPosition = \case
Int p -> p
Str p -> p
Bool p -> p
Tuple p _ -> p
Array p _ _ -> p
Fun p _ _ -> p
instance HasPosition Expr where
hasPosition = \case
EVar p _ -> p
ELitInt p _ -> p
ELitTrue p -> p
ELitFalse p -> p
EApp p _ _ -> p
EString p _ -> p
Neg p _ -> p
Not p _ -> p
EMul p _ _ _ -> p
EAdd p _ _ _ -> p
ERel p _ _ _ -> p
EAnd p _ _ -> p
EOr p _ _ -> p
instance HasPosition AddOp where
hasPosition = \case
Plus p -> p
Minus p -> p
instance HasPosition MulOp where
hasPosition = \case
Times p -> p
Div p -> p
Mod p -> p
instance HasPosition RelOp where
hasPosition = \case
LTH p -> p
LE p -> p
GTH p -> p
GE p -> p
EQU p -> p
NE p -> p