Implement <, >, and add example showing why locals would be nice.
Chris Pressey
1 year, 10 months ago
73 | 73 |
|
74 | 74 |
### Semantics
|
75 | 75 |
|
|
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.
|
76 | 78 |
* Require that variables be declared. (Unless maybe operating in some cavalier mode)
|
77 | 79 |
* Check constraints on all branches of an alternation.
|
78 | 80 |
* 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 .>;
|
8 | 8 |
| Arb Variable
|
9 | 9 |
| Inc Variable Integer
|
10 | 10 |
| Dec Variable Integer
|
11 | |
| GT Variable Integer
|
12 | |
| LT Variable Integer
|
|
11 |
| GreaterThan Variable Integer
|
|
12 |
| LessThan Variable Integer
|
13 | 13 |
deriving (Show, Ord, Eq)
|
91 | 91 |
Just $ update (\i -> Just (i + 1)) v st
|
92 | 92 |
applyConstraint (Dec v i) st =
|
93 | 93 |
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
|
94 | 106 |
|
95 | 107 |
|
96 | 108 |
constructState :: [String] -> GenState
|
45 | 45 |
keyword ".>"
|
46 | 46 |
return $ Constraint $ c
|
47 | 47 |
|
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)
|
49 | 49 |
|
50 | 50 |
arb = do
|
51 | 51 |
keyword "arb"
|
|
75 | 75 |
keyword "-="
|
76 | 76 |
n <- intlit
|
77 | 77 |
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
|
78 | 90 |
|
79 | 91 |
variable = do
|
80 | 92 |
s <- lowWord
|
73 | 73 |
Just $ update (\i -> Just (i + 1)) v st
|
74 | 74 |
applyConstraint (Dec v i) st =
|
75 | 75 |
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
|
76 | 88 |
|
77 | 89 |
|
78 | 90 |
parseFrom :: Grammar -> String -> ParseState
|