~jojo/Carth

ref: 12cf2a697999acc0ccc7419e498f96349a0a3267 Carth/src/TypeAst.hs -rw-r--r-- 1.3 KiB
12cf2a69JoJo Remove DWARF debugging metadata in LLVM output & make funcs External 3 months ago
                                                                                
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
-- | This module mostly exists to expost the builtin types via convenient variables,
--   instead of requiring redefinitions or manually typing the strings of TConst's, which
--   would be prone to typo errors.
module TypeAst where

import Data.Word

data TPrim
    = TNat Word32
    | TNatSize
    | TInt Word32
    | TIntSize
    | TF16
    | TF32
    | TF64
    | TF128
    deriving (Show, Eq, Ord)

type TConst t = (String, [t])

class TypeAst t where
    tprim :: TPrim -> t
    tconst :: TConst t -> t
    tfun :: t -> t -> t
    tbox :: t -> t

mainType :: TypeAst t => t
mainType = tIO tUnit

tIO :: TypeAst t => t -> t
tIO a = tconst ("IO", [a])

tByte :: TypeAst t => t
tByte = tprim (TNat 8)

tBox' :: t -> TConst t
tBox' t = ("Box", [t])

tStr :: TypeAst t => t
tStr = tconst tStr'

tStr' :: TConst t
tStr' = ("Str", [])

tArray :: TypeAst t => t -> t
tArray a = tconst ("Array", [a])

tTuple :: TypeAst t => [t] -> t
tTuple = foldr tCons tUnit

tCons :: TypeAst t => t -> t -> t
tCons car cdr = tconst ("Cons", [car, cdr])

tRealWorld :: TypeAst t => t
tRealWorld = tconst ("RealWorld", [])

tUnit :: TypeAst t => t
tUnit = tconst tUnit'

tUnit' :: TConst t
tUnit' = ("Unit", [])

tBool :: TypeAst t => t
tBool = tconst ("Bool", [])

tBool' :: TConst t
tBool' = ("Bool", [])