git @ Cat's Eye Technologies Fountain / 7a50111
Make Store store Values. Chris Pressey 1 year, 3 months ago
4 changed file(s) with 21 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
11
22 import Data.Maybe (mapMaybe)
33
4 import Language.Fountain.Value
45 import Language.Fountain.Store
56 import Language.Fountain.Constraint
67 import Language.Fountain.Grammar
78
89
9 data GenState = Generating String (Store Integer)
10 data GenState = Generating String (Store Value)
1011 | Failure
1112 deriving (Show, Ord, Eq)
1213
11
22 import Data.Maybe (mapMaybe)
33
4 import Language.Fountain.Value
45 import Language.Fountain.Store
56 import Language.Fountain.Constraint
67 import Language.Fountain.Grammar
78
89
9 data ParseState = Parsing String (Store Integer)
10 data ParseState = Parsing String (Store Value)
1011 | Failure
1112 deriving (Show, Ord, Eq)
1213
1111
1212 import qualified Data.Map as Map
1313
14 import Language.Fountain.Value
1415 import Language.Fountain.Constraint
1516 import Language.Fountain.Loader (parseConstConstraint)
1617
2627 update f k s = s{ store=Map.update f k $ store s }
2728
2829
29 constructStore :: [String] -> Store Integer
30 constructStore :: [String] -> Store Value
3031 constructStore [] = empty
3132 constructStore (constConstrainer:rest) =
3233 let
3334 (k, v) = parseConstConstraint constConstrainer
3435 in
35 insert k v $ constructStore rest
36 insert k (IntVal v) $ constructStore rest
3637
3738 updateStore :: [Variable] -> [Variable] -> Store a -> Store a -> Store a
3839 updateStore [] [] _sourceStore destStore = destStore
4748 in
4849 updateStore sourceKeys destKeys sourceStore destStore'
4950
50 ceval :: CExpr -> Store Integer -> Maybe Integer
51 ceval :: CExpr -> Store Value -> Maybe Integer
5152 ceval (CInt i) _ = Just i
52 ceval (CVar v) st = fetch v st
53 ceval (CVar v) st = case fetch v st of
54 Just (IntVal i) -> Just i
55 _ -> Nothing
5356
54 applyConstraint :: Constraint -> Store Integer -> Maybe (Store Integer)
57 applyConstraint :: Constraint -> Store Value -> Maybe (Store Value)
5558 applyConstraint (UnifyConst v i) st =
5659 case fetch v st of
57 Just value ->
60 Just (IntVal value) ->
5861 if value == i then Just st else Nothing
5962 Nothing ->
60 Just $ insert v i st
63 Just $ insert v (IntVal i) st
6164 applyConstraint (UnifyVar v w) st =
6265 case (fetch v st, fetch w st) of
6366 (Just vValue, Just wValue) ->
7174 applyConstraint (Inc v e) st =
7275 case ceval e st of
7376 Just delta ->
74 Just $ update (\i -> Just (i + delta)) v st
77 Just $ update (\(IntVal i) -> Just $ IntVal $ i + delta) v st
7578 Nothing ->
7679 Nothing
7780 applyConstraint (Dec v e) st =
7881 case ceval e st of
7982 Just delta ->
80 Just $ update (\i -> Just (i - delta)) v st
83 Just $ update (\(IntVal i) -> Just $ IntVal $ i - delta) v st
8184 Nothing ->
8285 Nothing
8386 applyConstraint (Both c1 c2) st =
9497
9598 applyRelConstraint op v e st =
9699 case (fetch v st, ceval e st) of
97 (Just value, Just target) ->
100 (Just (IntVal value), Just target) ->
98101 if value `op` target then Just st else Nothing
99102 _ ->
100103 Nothing
0 module Language.Fountain.Value where
1
2 data Value = IntVal Integer
3 deriving (Show, Ord, Eq)