~jojo/Carth

ref: 26203a95273481f4fca841dfded00016948bfdf3 Carth/src/AnnotAst.hs -rw-r--r-- 1.8 KiB
26203a95JoJo Don't use NonEmpty in Ast 2 years 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
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
-- | Type annotated AST as a result of typechecking
module AnnotAst
    ( WithPos(..)
    , TVar(..)
    , TPrim(..)
    , TConst
    , Type(..)
    , Scheme(..)
    , Id
    , TypedVar(..)
    , Const(..)
    , VariantIx
    , Access(..)
    , Span
    , Con(..)
    , Pat'(..)
    , Pat
    , Cases(..)
    , DecisionTree(..)
    , VarBindings
    , Expr
    , Expr'(..)
    , Defs
    , TypeDefs
    , Ctors
    , Externs
    , startType
    )
where

import Data.Map.Strict (Map)
import Data.Word

import Ast
    (TVar(..), TPrim(..), TConst, Type(..), Scheme(..), Const(..), startType)
import SrcPos


type Id = WithPos String

data TypedVar = TypedVar Id Type
    deriving (Show, Eq, Ord)

type VariantIx = Integer

data Access
    = Obj
    | As Access Span [Type]
    | Sel Word32 Span Access
    | ADeref Access
    deriving (Show, Eq, Ord)

type Span = Integer

data Con = Con
    { variant :: VariantIx
    , span :: Span
    , argTs :: [Type]
    }
    deriving Show

data Pat'
    = PVar TypedVar
    | PWild
    | PCon Con [Pat]
    | PBox Pat
    deriving Show
type Pat = WithPos Pat'

newtype Cases = Cases [(Pat, Expr Cases)]
    deriving Show

data DecisionTree
    = DLeaf (VarBindings, Expr DecisionTree)
    | DSwitch Access (Map VariantIx DecisionTree) DecisionTree
    deriving Show

type VarBindings = Map TypedVar Access

data Expr' m
    = Lit Const
    | Var TypedVar
    | App (Expr m) (Expr m) Type
    | If (Expr m) (Expr m) (Expr m)
    | Let (Defs m) (Expr m)
    | Match (Expr m) m Type Type
    | FunMatch m Type Type
    | Ctor VariantIx Span TConst [Type]
    | Box (Expr m)
    | Deref (Expr m)
    deriving (Show)

type Expr m = WithPos (Expr' m)

type Defs m = Map String (Scheme, Expr m)
type TypeDefs = Map String ([TVar], [(String, [Type])])
type Ctors = Map String (VariantIx, (String, [TVar]), [Type], Span)
type Externs = Map String Type