aboutsummaryrefslogtreecommitdiff
path: root/lib/Language/Elna/SymbolTable.hs
blob: b8892c9656cdedb5bf9ae0cc7c7bacef5cbe2783 (plain)
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
module Language.Elna.SymbolTable
    ( Info(..)
    , ParameterInfo(..)
    , SymbolTable
    , empty
    , enter
    , fromList
    , lookup
    , builtInSymbolTable
    ) where

import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Vector (Vector)
import Language.Elna.Location (Identifier(..))
import Language.Elna.Types (Type(..), intType, booleanType)
import Prelude hiding (lookup)

newtype SymbolTable = SymbolTable (HashMap Identifier Info)
    deriving (Eq, Show)

instance Semigroup SymbolTable
  where
    (SymbolTable lhs) <> (SymbolTable rhs) = SymbolTable $ rhs <> lhs

instance Monoid SymbolTable
  where
    mempty = SymbolTable HashMap.empty

builtInSymbolTable :: SymbolTable
builtInSymbolTable = SymbolTable $ HashMap.fromList
    [ ("boolean", TypeInfo booleanType)
    , ("int", TypeInfo intType)
    ]

empty :: SymbolTable
empty = SymbolTable HashMap.empty

enter :: Identifier -> Info -> SymbolTable -> SymbolTable
enter identifier info (SymbolTable table) = SymbolTable
    $ HashMap.insert identifier info table

lookup :: Identifier -> SymbolTable -> Maybe Info
lookup identifier (SymbolTable table) = HashMap.lookup identifier table

fromList :: [(Identifier, Info)] -> SymbolTable
fromList = SymbolTable . HashMap.fromList

data ParameterInfo = ParameterInfo
    { name :: Identifier
    , type' :: Type
    , isReferenceParameter :: Bool
    } deriving (Eq, Show)

data Info
    = TypeInfo Type
    | VariableInfo Type Bool
    | ProcedureInfo SymbolTable (Vector ParameterInfo)
    deriving (Eq, Show)