-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTypes.hs
67 lines (54 loc) · 1.88 KB
/
Types.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
module Types where
import Data.List(intercalate)
import Data.Maybe
data VT = VInt | VChr | VList [VT] | VFn [VT] [VT] | AutoType | InvalidType
| OptionYes | OptionNo | ItWasAConstant | StaticInt Integer -- only for code gen, not real types
-- | VMaybe VT | Nothing -- (other ideas)
deriving (Show, Eq)
vstr = VList [VChr]
isNum VInt = True
isNum VChr = True
isNum _ = False
isList (VList _) = True
isList _ = False
ret (VFn from to) = to
elemT :: VT -> [VT]
elemT (VList e) = e
elemT s = error $ "is not a list: " ++ show s
toTuple :: [String] -> String
toTuple [t] = t
toTuple s = "(" ++ intercalate "," s ++ ")"
toHsType :: VT -> Maybe String
toHsType VInt = Just "Integer"
toHsType VChr = toHsType VInt
toHsType (VList ts) =
toHsTypes ts >>= \s -> Just $ "["++s++"]"
toHsType (VFn a b) = Nothing
toHsType (ItWasAConstant) = Nothing
toHsType (AutoType) = Nothing
toHsType (InvalidType) = Nothing
toHsType (StaticInt _) = Nothing
toHsType e = error $ "cant toHsType " ++ show e
toHsTypes ts =
let elems = map toHsType ts in
if all isJust elems then
Just $ toTuple $ catMaybes elems
else Nothing
-- won't work since sometimes its curried and others not?
--toHsType (VFn a b) = "((" ++ (intercalate "->" $ map toHsType a) ++ ")->"++(toTuple $ map toHsType b)++ ")"
-- todo remove and use toHsType when Strings use Char internally instead of Integer
toHsReadType :: VT -> String
toHsReadType VInt = "Integer"
toHsReadType VChr = "Char"
toHsReadType (VList [VChr]) = "String"
toHsReadType (VList ts) = "["++toHsReadTypes ts++"]"
toHsReadType (VFn from to) = toHsReadTypes from ++ " -> " ++ toHsReadTypes to
toHsReadType e = error $ "cant toHsReadType " ++ show e
toHsReadTypes ts = toTuple $ map toHsReadType ts
toParser t = "read::String->"++toHsReadTypes t
xorChr [VInt, VChr] = VChr
xorChr [VChr, VInt] = VChr
xorChr _ = VInt
orChr [_, VChr] = VChr
orChr [VChr, _] = VChr
orChr _ = VInt