-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.hs
More file actions
75 lines (64 loc) · 1.64 KB
/
types.hs
File metadata and controls
75 lines (64 loc) · 1.64 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
module Types where
data Type = IntType | StrType | BlnType | OpType | HghType | LowType | PtrType
deriving (Eq, Show)
-- OpType
ops :: [String]
ops =
[ "#",
"+",
"-",
"*",
"/",
"%",
"print",
"put",
"dup",
"drop",
"!",
"2dup",
"2drop",
"swap",
"over",
"=",
"|",
"&",
"<",
">",
"<=",
">=",
"run",
"s",
"r",
"else"
]
-- HghType
highers :: [String]
highers = ["if", "while"]
-- LowType
lowers :: [String]
lowers = ["end"]
type Token = (Type, String, Loc)
type Loc = (String, Integer, Integer)
-- Stack [(type, val, generated at)]
type Stack = [Token]
formatLocation :: Loc -> String
formatLocation (file, line, char) = file ++ ":" ++ show line ++ ":" ++ show char
formatLexedInfo :: [Token] -> String
formatLexedInfo [] = []
formatLexedInfo ((typ, val, loc) : rest) = info ++ formatLexedInfo rest
where
info = formatLocation loc ++ "\t" ++ show typ ++ "\t\t" ++ val ++ if null rest then "" else "\n"
getType :: String -> Loc -> Type
getType str loc
| isNumerical str = IntType
| str `elem` ["True", "False"] = BlnType
| str `elem` highers = HghType
| str `elem` lowers = LowType
| str `elem` ops = OpType
| otherwise = libraError ("Can't determine type for \"" ++ str ++ "\"") (Just loc)
where
isNumerical :: String -> Bool
isNumerical = foldr (\x -> (&&) (x `elem` "0123456789")) True
libraError :: String -> Maybe Loc -> a
libraError err Nothing = errorWithoutStackTrace ("\n\n♎️ Libra error:\n" ++ err ++ "\n")
libraError err (Just loc) = errorWithoutStackTrace ("\n\n♎️ Libra error:\n" ++ formatLocation loc ++ " " ++ err ++ "\n")