-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.hs
More file actions
56 lines (45 loc) · 1.46 KB
/
Token.hs
File metadata and controls
56 lines (45 loc) · 1.46 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
module Token where
import Identifier (Identifier)
import Data.Char (isSpace, isDigit)
data Token = LeftParen Char
| RightParen Char
| Number Integer
| Id Identifier
| Boolean Bool
instance Show Token where
show (LeftParen c) = "'" ++ [c] ++ "'"
show (RightParen c) = "'" ++ [c] ++ "'"
show (Number i) = "<num " ++ show i ++ ">"
show (Id s) = "<ID " ++ show s ++ ">"
show (Boolean False) = "<#f>"
show (Boolean True) = "<#t>"
instance Eq Token where
LeftParen c1 == LeftParen c2 = (c1 == c2)
RightParen c1 == RightParen c2 = (c1 == c2)
Number a == Number b = a == b
Id a == Id b = a == b
Boolean a == Boolean b = a == b
_ == _ = False
isLParen :: Char -> Bool
isLParen c = c `elem` "([{"
isRParen :: Char -> Bool
isRParen c = c `elem` ")]}"
tokenize :: String -> [Token]
tokenize "" = []
tokenize (';' : s) = tokenize (dropWhile (/= '\n') s)
tokenize (c : s)
| isSpace c = tokenize s
| isLParen c = LeftParen c : tokenize s
| isRParen c = RightParen c : tokenize s
tokenize ('#' : 't' : s) = Boolean True : tokenize s
tokenize ('#' : 'f' : s) = Boolean False : tokenize s
tokenize s@(c : _)
| isDigit c = Number (read text) : tokenize rest
| otherwise = Id text : tokenize rest
where (text, rest) = break tokenBoundary s
tokenBoundary :: Char -> Bool
tokenBoundary ch
| isSpace ch = True
| isRParen ch = True
| isLParen ch = True
| otherwise = False