summaryrefslogtreecommitdiff
path: root/lib/Language/Elna/SymbolTable.hs
blob: f1c65340a7056799e0566dbbebc00c4a9ffb1b51 (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
module Language.Elna.SymbolTable
    ( Info(..)
    , ParameterInfo(..)
    , SymbolTable
    , empty
    , enter
    , lookup
    , symbolTable
    ) 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

symbolTable :: SymbolTable
symbolTable = 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

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)