We won't need this dynamic scope thing, thankfully.
Chris Pressey
1 year, 7 months ago
7 | 7 |
data ParseState = Parsing String Store
|
8 | 8 |
| Failure
|
9 | 9 |
deriving (Show, Ord, Eq)
|
10 | |
|
11 | |
newScope (Parsing s st) = Parsing s (pushScope st)
|
12 | |
newScope other = other
|
13 | |
|
14 | |
discardScope (Parsing s st) = Parsing s (popScope st)
|
15 | |
discardScope other = other
|
16 | 10 |
|
17 | 11 |
|
18 | 12 |
expectTerminal :: Char -> ParseState -> ParseState
|
|
50 | 44 |
parse g st (Terminal c) = expectTerminal c st
|
51 | 45 |
parse g st (NonTerminal nt actuals) =
|
52 | 46 |
let
|
53 | |
st' = newScope st
|
54 | |
st'' = parse g st (production nt g)
|
55 | |
st''' = discardScope st''
|
|
47 |
st' = parse g st (production nt g)
|
56 | 48 |
in
|
57 | |
st'''
|
|
49 |
st'
|
58 | 50 |
|
59 | 51 |
parse g st@(Parsing text store) (Constraint cstr) =
|
60 | 52 |
case applyConstraint cstr store of
|
6 | 6 |
|
7 | 7 |
|
8 | 8 |
data Store = Store {
|
9 | |
scopes :: [Map.Map Variable Integer],
|
|
9 |
table :: Map.Map Variable Integer,
|
10 | 10 |
events :: [String]
|
11 | 11 |
} deriving (Show, Ord, Eq)
|
12 | 12 |
|
13 | 13 |
|
14 | |
empty = Store{ scopes=[Map.empty], events=[] }
|
15 | |
|
16 | |
fetch k st = lookup k $ scopes st where
|
17 | |
lookup k [] = Nothing
|
18 | |
lookup k (table:rest) =
|
19 | |
case Map.lookup k table of
|
20 | |
Just v -> Just v
|
21 | |
Nothing -> lookup k rest
|
22 | |
|
23 | |
insert k v st = st{ scopes=insertTop k v $ scopes st, events=("insert":events st) }
|
24 | |
where insertTop k v (table:rest) = (Map.insert k v table:rest)
|
25 | |
|
26 | |
-- TODO: this needs to update the layer where the variable is found
|
27 | |
update f k st = st{ scopes=updateTop f k $ scopes st, events=("update":events st) }
|
28 | |
where updateTop f k (table:rest) = (Map.update f k table:rest)
|
29 | |
|
30 | |
pushScope st = st{ scopes=(Map.empty:scopes st) }
|
31 | |
popScope st = st{ scopes=tail $ scopes st }
|
|
14 |
empty = Store{ table=Map.empty, events=[] }
|
|
15 |
fetch k st = Map.lookup k (table st)
|
|
16 |
insert k v st = st{ table=Map.insert k v (table st), events=("insert":events st) }
|
|
17 |
update f k st = st{ table=Map.update f k (table st), events=("update":events st) }
|
32 | 18 |
|
33 | 19 |
constructStore :: [String] -> Store
|
34 | 20 |
constructStore [] = empty
|