git @ Cat's Eye Technologies Fountain / cefcc0d
We won't need this dynamic scope thing, thankfully. Chris Pressey 1 year, 7 months ago
2 changed file(s) with 7 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
77 data ParseState = Parsing String Store
88 | Failure
99 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
1610
1711
1812 expectTerminal :: Char -> ParseState -> ParseState
5044 parse g st (Terminal c) = expectTerminal c st
5145 parse g st (NonTerminal nt actuals) =
5246 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)
5648 in
57 st'''
49 st'
5850
5951 parse g st@(Parsing text store) (Constraint cstr) =
6052 case applyConstraint cstr store of
66
77
88 data Store = Store {
9 scopes :: [Map.Map Variable Integer],
9 table :: Map.Map Variable Integer,
1010 events :: [String]
1111 } deriving (Show, Ord, Eq)
1212
1313
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) }
3218
3319 constructStore :: [String] -> Store
3420 constructStore [] = empty