git @ Cat's Eye Technologies Fountain / 79bf574
Check that all choices on the Alt have a precondition. Chris Pressey 1 year, 7 months ago
2 changed file(s) with 29 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
244244 Goal ::= "f" | <. a = 0 .> "o";
245245 ???> No pre-condition
246246
247 (This one's not quite right yet.)
248
249 > Goal ::= (<. a = 0 .> "f") | "o";
250 > ???> No pre-condition
247 Goal ::= (<. a = 0 .> "f") | "o";
248 ???> No pre-condition
251249
252250 But if all choices of the Alt have constraints, we are able to select the one
253251 that fulfills the constraints.
00 module Language.Fountain.Generator (constructState, generateFrom, obtainResult) where
1
2 import Data.Maybe (mapMaybe)
13
24 import Language.Fountain.Grammar
35 import Language.Fountain.Constraint
1921 -- Alt choices need preconditions because in generating, unlike parsing,
2022 -- we need some guidance of which one to pick
2123 --
22 getPreCondition :: Expr -> Expr -> Constraint
23 getPreCondition alts (Seq (x:xs)) = getPreCondition alts x
24 getPreCondition alts (Constraint c) = c
25 getPreCondition alts x = error ("No pre-condition present on this Alt choice: " ++ (show alts) ++ " => " ++ (show x))
24 getPreCondition :: Expr -> Maybe Constraint
25 getPreCondition (Seq (x:xs)) = getPreCondition x
26 getPreCondition (Constraint c) = Just c
27 getPreCondition x = Nothing
28
29 missingPreConditions choices =
30 mapMaybe (\x -> case getPreCondition x of
31 Just _ -> Nothing
32 Nothing -> Just x
33 ) choices
2634
2735
2836 gen :: Grammar -> GenState -> Expr -> GenState
3745 -- We look at all the choices; each should start with a pre-condition
3846 -- determining whether we can select it; and we should narrow down our
3947 -- choices based on that. (Then pick randomly? Or insist deterministic?)
40 gen g st@(Generating str store) alts@(Alt s) =
41 let
42 preConditionedAlts = map (\x -> (getPreCondition alts x, x)) s
43 applicableAlts = filter (\(c, x) -> canApplyConstraint c store) preConditionedAlts
44 in
45 genAlt g st applicableAlts where
46 genAlt g st [] = Failure
47 genAlt g st ((_, e) : rest) =
48 case gen g st e of
49 Failure -> genAlt g st rest
50 st' -> st'
48 gen g st@(Generating str store) (Alt choices) =
49 case missingPreConditions choices of
50 missing@(_:_) ->
51 error ("No pre-condition present on these Alt choices: " ++ (show missing))
52 [] ->
53 let
54 preConditionedChoices = map (\x -> (getPreCondition x, x)) choices
55 applicableChoices = filter (\(Just c, x) -> canApplyConstraint c store) preConditionedChoices
56 in
57 genAlt g st applicableChoices where
58 genAlt g st [] = Failure
59 genAlt g st ((_, e) : rest) =
60 case gen g st e of
61 Failure -> genAlt g st rest
62 st' -> st'
5163
5264 gen g state (Loop l postconditions) =
5365 genLoop g state l (assertThereAreSome postconditions) where