-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBkeep.hs
112 lines (90 loc) · 1.98 KB
/
Bkeep.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
--
-- Created: 14 Aug 2007 by tobbe@tornkvist.org
--
-- ADT + Ops. to maintain various kind of information.
--
module Bkeep where
import Data.Bits
type Name = String
type Mod = String
type Fun = Name
type Arity = Int
--
-- Variable counter. For creating internal vaiables.
--
type Vcnt = Int
--
-- Simple symbol table
--
data Stab = Sfa Fun Arity
deriving (Show)
type Symtab = [Stab]
--
-- String (space) prefix when generating code
--
type Prefix = String
--
-- Bit flags field.
--
type Flags = Int
--
-- Data structure for bookeeping
--
data Bkeep = Bkeep Vcnt Symtab Prefix Flags
deriving (Show)
bkNew :: Bkeep
bkNew = Bkeep 0 [] "" 0
--
-- Bump the variable counter
--
bkBump :: Bkeep -> (Int, Bkeep)
bkBump (Bkeep i s p f) =
let j = i + 1
in (j, Bkeep j s p f)
--
-- Increase the prefix
--
bkInc :: Bkeep -> Bkeep
bkInc (Bkeep i s p f) = Bkeep i s (" " ++ p) f
--
-- Decrease the prefix
--
bkDec :: Bkeep -> Bkeep
bkDec (Bkeep i s p f) | length p < 2 = Bkeep i s p f
bkDec (Bkeep i s (_:_:p) f) = Bkeep i s p f
--
-- Return current prefix
--
bkPix :: Bkeep -> String
bkPix (Bkeep _ _ p _) = p
--
-- Remove current prefix
--
bkPix0 :: Bkeep -> Bkeep
bkPix0 (Bkeep i s _ f) = Bkeep i s "" f
--
-- Add entry to Symbol Table
--
bkSyAdd :: Stab -> Bkeep -> Bkeep
bkSyAdd x (Bkeep i syb p f) = Bkeep i (x:syb) p f
--
-- Get entry from Symbol Table
--
bkSyFun :: String -> Bkeep -> Maybe Stab
bkSyFun name (Bkeep _ [] _ _) =
Nothing
bkSyFun name (Bkeep _ ((Sfa n1 arity):xs) _ _) | name == n1 =
Just $ Sfa n1 arity
bkSyFun name (Bkeep i (_:xs) p f) =
bkSyFun name (Bkeep i xs p f)
--
-- Set bit flags
--
fInsideGuard :: Int
fInsideGuard = 1
bkSetInsideGuard :: Bkeep -> Bkeep
bkSetInsideGuard (Bkeep i syb p f) = Bkeep i syb p (f `setBit` fInsideGuard)
bkClrInsideGuard :: Bkeep -> Bkeep
bkClrInsideGuard (Bkeep i syb p f) = Bkeep i syb p (f `clearBit` fInsideGuard)
bkIsInsideGuard :: Bkeep -> Bool
bkIsInsideGuard (Bkeep i syb p f) = f `testBit` fInsideGuard