-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tokens.hs
167 lines (136 loc) · 3.76 KB
/
Tokens.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
module Tokens
( Pos(..)
, Token(..)
, isTokenError
, printError
, tp
) where
data Pos = Pos Int Int deriving Eq
instance Show Pos where
show (Pos l c) = "(Line " ++ show l ++ ", Col " ++ show c ++ ")"
data Token
-- language --
= TokenProgram Pos | TokenUsing Pos | TokenIn Pos
| TokenAssign Pos
-- brackets --
| TokenCurlyOpen Pos | TokenCurlyClose Pos
| TokenParenOpen Pos | TokenParenClose Pos
-- types --
| TokenIntT Pos | TokenBoolT Pos | TokenSetT Pos
-- boolean constants --
| TokenTrue Pos | TokenFalse Pos
-- separators --
| TokenComma Pos | TokenSemicolon Pos
-- operators --
-- -- int --
| TokenPlus Pos | TokenMinus Pos | TokenTimes Pos
| TokenDiv Pos | TokenMod Pos
-- -- set --
| TokenSetUnion Pos | TokenSetMinus Pos | TokenSetInter Pos
| TokenSetMax Pos | TokenSetMin Pos | TokenSetSize Pos
-- -- map --
| TokenMapPlus Pos | TokenMapMinus Pos | TokenMapTimes Pos
| TokenMapDiv Pos | TokenMapMod Pos
-- -- bool --
| TokenAnd Pos | TokenOr Pos | TokenNot Pos
-- -- relational --
| TokenLT Pos | TokenLE Pos | TokenGT Pos | TokenGE Pos
| TokenEQ Pos | TokenNE Pos | TokenAt Pos
-- control statements --
| TokenIf Pos | TokenElse Pos | TokenFor Pos
| TokenMin Pos | TokenMax Pos | TokenRepeat Pos
| TokenWhile Pos | TokenDo Pos
-- IO functions --
| TokenScan Pos | TokenPrint Pos | TokenPrintln Pos
-- variables --
| TokenInt Int Pos
| TokenString String Pos
| TokenIdent String Pos
-- errors --
| TokenError String Pos
| TokenIntError String Pos
deriving (Eq, Show)
tp :: Token -> Pos
tp t = case t of
-- language --
(TokenProgram p) -> p
(TokenUsing p) -> p
(TokenIn p) -> p
(TokenAssign p) -> p
-- brackets --
(TokenCurlyOpen p) -> p
(TokenCurlyClose p) -> p
(TokenParenOpen p) -> p
(TokenParenClose p) -> p
-- types --
(TokenIntT p) -> p
(TokenBoolT p) -> p
(TokenSetT p) -> p
-- boolean constants --
(TokenTrue p) -> p
(TokenFalse p) -> p
-- separators --
(TokenComma p) -> p
(TokenSemicolon p) -> p
-- operators --
-- -- int --
(TokenPlus p) -> p
(TokenMinus p) -> p
(TokenTimes p) -> p
(TokenDiv p) -> p
(TokenMod p) -> p
-- -- set --
(TokenSetUnion p) -> p
(TokenSetMinus p) -> p
(TokenSetInter p) -> p
(TokenSetMax p) -> p
(TokenSetMin p) -> p
(TokenSetSize p) -> p
-- -- map --
(TokenMapPlus p) -> p
(TokenMapMinus p) -> p
(TokenMapTimes p) -> p
(TokenMapDiv p) -> p
(TokenMapMod p) -> p
-- -- bool --
(TokenAnd p) -> p
(TokenOr p) -> p
(TokenNot p) -> p
---- -- relational --
(TokenLT p) -> p
(TokenLE p) -> p
(TokenGT p) -> p
(TokenGE p) -> p
(TokenEQ p) -> p
(TokenNE p) -> p
(TokenAt p) -> p
-- control statements --
(TokenIf p) -> p
(TokenElse p) -> p
(TokenFor p) -> p
(TokenMin p) -> p
(TokenMax p) -> p
(TokenRepeat p) -> p
(TokenWhile p) -> p
(TokenDo p) -> p
-- IO functions --
(TokenScan p) -> p
(TokenPrint p) -> p
(TokenPrintln p) -> p
-- variables --
(TokenInt _ p) -> p
(TokenString _ p) -> p
(TokenIdent _ p) -> p
-- errors --
(TokenError _ p) -> p
(TokenIntError _ p) -> p
printError :: Token -> IO ()
printError (TokenError s p) = do
putStrLn $ "Error: unexpected token \"" ++ s ++ "\" " ++ show p
printError (TokenIntError s p) = do
putStrLn $ "Error: integer out of range (-2^31 .. 2^31-1) \"" ++ s ++
"\" " ++ show p
isTokenError :: Token -> Bool
isTokenError (TokenError _ _) = True
isTokenError (TokenIntError _ _) = True
isTokenError _ = False