berto
Posts: 20708
Joined: 3/13/2002 From: metro Chicago, Illinois, USA Status: offline
|
checking data values Beyond syntactic and semantic analysis, we can also add logical analysis to the agelint lexer/parser combo. For example, instead of using the generic int all the time: int: intpos { $$ = $1; }
| intneg { $$ = $1; }
; we could instead use chance: _ZERO { $$ = 0; }
| _ONE { $$ = 1; }
| _INTPOS { $$ = $1; CHKINT($1, 0, 0, 100, 100); }
;
pct: _ZERO { $$ = 0; }
| _ONE { $$ = 1; }
| _INTPOS { $$ = $1; CHKINT($1, 0, 0, 100, 100); }
; So, wherever we specify the Probability statement: probability: _PROBABILITY eq chance
| _PROBABILITY eq chance sc _ESVINTVAR sc int
{
/*
http://www.ageod.net/agewiki/Probability
Syntax: Probability = Value; <esvIntVar(x)>; <VarCoeff>
*/
}
; rather than the more generic probability: _PROBABILITY eq int
...
; the parser will catch as errors probabilities > 100% or < 0%. Another way we can check numerical values is via the CHKINT() C macro: #define CHKINT(val, min, low, high, max) if ((val < min) || (val > max)) { \
txterrmsg(_ERROR, TRUE, linenorhs, "illegal value: %ld", val); \
} else if (isshow_warn && ((val < low) || (val > high))) { \
txterrmsg(_WARNING, TRUE, linenorhs, "suspicious value: %ld", val); \
} Sample usage: assault: _ASSAULT eq intpos
{ CHKINT($3, 0, 0, 100, 999);
/*
Syntax: Assault = Value
*/
}
; Anything outside the range 0 to 100 will generate a warning message, and anything beyond 999 will generate an error message. Still more data values we can check are date values, as in: datestuff: datethings {
if (maxdateval && mindateval) {
if (date2days(maxdateval) < date2days(mindateval)) {
txterrmsg(_ERROR, FALSE, linenorhs, "MaxDate %s precede\
s MinDate %s", maxdateval, mindateval);
} else if (date2days(maxdateval) == date2days(mindateval)) \
{
txterrmsg(_WARNING, FALSE, linenorhs, "MinDate %s same \
as MaxDate %s", mindateval, maxdateval);
}
}
}
;
datethings: datething
| datethings datething
;
datething: maxdate
| mindate
; (For the record, using AGElint, I found four instances in three different AGEOD games where MaxDate < MinDate.) There are many more data values we can check. I have fully fleshed out the logical date analysis, and added a fair amount of CHKINT() checks, but barely scratched the surface of other kinds of data value checks. So much to do, so little time!
< Message edited by berto -- 12/21/2011 12:01:42 PM >
_____________________________
|