git @ Cat's Eye Technologies Fountain / a0e541c
Implement <, >, and add example showing why locals would be nice. Chris Pressey 1 year, 10 months ago
6 changed file(s) with 48 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
7373
7474 ### Semantics
7575
76 * Inc, dec, gt, lt, should take either a variable or an integer on the RHS.
77 Really, the RHS could be some kind of simple expression probably.
7678 * Require that variables be declared. (Unless maybe operating in some cavalier mode)
7779 * Check constraints on all branches of an alternation.
7880 * Allow local variables to be declared.
0 Goal ::= ShortSentence;
1 ShortSentence ::= "Hi" Sp "there" "!";
2 LongSentence ::=
3 "The" Sp "quick" Sp "brown" Sp "fox" Sp
4 "jumped" Sp "over" Sp "the" Sp "lazy" Sp "dog" "."
5 ;
6 Sp ::= <. n = 0 .> { " " <. n += 1 .> } <. n > 0 .>;
88 | Arb Variable
99 | Inc Variable Integer
1010 | Dec Variable Integer
11 | GT Variable Integer
12 | LT Variable Integer
11 | GreaterThan Variable Integer
12 | LessThan Variable Integer
1313 deriving (Show, Ord, Eq)
9191 Just $ update (\i -> Just (i + 1)) v st
9292 applyConstraint (Dec v i) st =
9393 Just $ update (\i -> Just (i - 1)) v st
94 applyConstraint (GreaterThan v i) st =
95 case fetch v st of
96 Just value ->
97 if value > i then Just st else Nothing
98 Nothing ->
99 Nothing
100 applyConstraint (LessThan v i) st =
101 case fetch v st of
102 Just value ->
103 if value < i then Just st else Nothing
104 Nothing ->
105 Nothing
94106
95107
96108 constructState :: [String] -> GenState
4545 keyword ".>"
4646 return $ Constraint $ c
4747
48 constrainer = (try arb) <|> (try unifyConst) <|> (try unifyVar) <|> (try inc) <|> (try dec) -- <|> (try gt) <|> (try lt)
48 constrainer = (try arb) <|> (try unifyConst) <|> (try unifyVar) <|> (try inc) <|> (try dec) <|> (try gt) <|> (try lt)
4949
5050 arb = do
5151 keyword "arb"
7575 keyword "-="
7676 n <- intlit
7777 return $ Dec v n
78
79 gt = do
80 v <- variable
81 keyword ">"
82 n <- intlit
83 return $ GreaterThan v n
84
85 lt = do
86 v <- variable
87 keyword "<"
88 n <- intlit
89 return $ LessThan v n
7890
7991 variable = do
8092 s <- lowWord
7373 Just $ update (\i -> Just (i + 1)) v st
7474 applyConstraint (Dec v i) st =
7575 Just $ update (\i -> Just (i - 1)) v st
76 applyConstraint (GreaterThan v i) st =
77 case fetch v st of
78 Just value ->
79 if value > i then Just st else Nothing
80 Nothing ->
81 Nothing
82 applyConstraint (LessThan v i) st =
83 case fetch v st of
84 Just value ->
85 if value < i then Just st else Nothing
86 Nothing ->
87 Nothing
7688
7789
7890 parseFrom :: Grammar -> String -> ParseState