Code/comment cleanup

This commit is contained in:
achmizs 2021-12-04 19:15:10 -05:00
parent e5184f50e9
commit 285b2a9a6b

View File

@ -41,7 +41,7 @@
return _maxDieCount; return _maxDieCount;
} }
-(void) setMaxDieCount:(NSUInteger)maxDieCount { -(void) setMaxDieCount:(NSUInteger)maxDieCount {
if (maxDieCount > NSUIntegerMax / _maxDieSize) { if (maxDieCount > (NSUIntegerMax / _maxDieSize)) {
_maxDieCount = NSUIntegerMax / _maxDieSize; _maxDieCount = NSUIntegerMax / _maxDieSize;
} else { } else {
_maxDieCount = maxDieCount; _maxDieCount = maxDieCount;
@ -52,9 +52,11 @@
return _maxDieSize; return _maxDieSize;
} }
-(void) setMaxDieSize:(NSUInteger)maxDieSize { -(void) setMaxDieSize:(NSUInteger)maxDieSize {
if (maxDieSize > [_diceBag biggestPossibleDieSize] || if ( maxDieSize > [_diceBag biggestPossibleDieSize]
maxDieSize > NSIntegerMax / _maxDieCount) { || maxDieSize > (NSIntegerMax / _maxDieCount)) {
_maxDieSize = ([_diceBag biggestPossibleDieSize] < (NSIntegerMax / _maxDieCount)) ? [_diceBag biggestPossibleDieSize] : NSIntegerMax / _maxDieCount; _maxDieSize = (([_diceBag biggestPossibleDieSize] < (NSIntegerMax / _maxDieCount))
? [_diceBag biggestPossibleDieSize]
: (NSIntegerMax / _maxDieCount));
} else { } else {
_maxDieSize = maxDieSize; _maxDieSize = maxDieSize;
} }
@ -65,12 +67,14 @@
/**************************/ /**************************/
-(instancetype) init { -(instancetype) init {
if (self = [super init]) { if (!(self = [super init]))
_maxDieCount = DEFAULT_MAX_DIE_COUNT; return nil;
_maxDieSize = DEFAULT_MAX_DIE_SIZE;
_maxDieCount = DEFAULT_MAX_DIE_COUNT;
_diceBag = [SA_DiceBag new]; _maxDieSize = DEFAULT_MAX_DIE_SIZE;
}
_diceBag = [SA_DiceBag new];
return self; return self;
} }
@ -78,6 +82,8 @@
#pragma mark - Public methods #pragma mark - Public methods
/****************************/ /****************************/
// TODO: Possibly refuse to evaluate an expression thats already evaluated?
// (i.e., it has a ... .result?? .value??)
-(SA_DiceExpression *) resultOfExpression:(SA_DiceExpression *)expression { -(SA_DiceExpression *) resultOfExpression:(SA_DiceExpression *)expression {
// Check to see if the expression is erroneous (i.e. the parser has judged // Check to see if the expression is erroneous (i.e. the parser has judged
// that it is malformed, etc.). If so, decline to evaluate the expression; // that it is malformed, etc.). If so, decline to evaluate the expression;
@ -87,26 +93,27 @@
} }
/* /*
Even if an expression is not erroneous (i.e. if it has no syntax errors), NOTE: Even if an expression is not erroneous (i.e. if it has no syntax
it may still not be possible to evaluate it. For example, 5d0 is a errors), it may still not be possible to evaluate it. For example, 5d0
perfectly well-formed die string, and will yield an expression tree as follows: is a perfectly well-formed die string, and will yield an expression tree
as follows:
@{SA_DB_TERM_TYPE : SA_DB_TERM_TYPE_ROLL_COMMAND, [ type : SA_DiceExpressionTerm_ROLL_COMMAND,
SA_DB_ROLL_COMMAND : SA_DB_ROLL_COMMAND_SUM, rollCommand : SA_DiceExpressionRollCommand_SUM,
SA_DB_ROLL_DIE_COUNT : @{SA_DB_TERM_TYPE : SA_DB_TERM_TYPE_VALUE, dieCount : [ type : SA_DiceExpressionTerm_VALUE,
SA_DB_VALUE : @(5) value : @(5)
}, ],
SA_DB_ROLL_DIE_SIZE : @{SA_DB_TERM_TYPE : SA_DB_TERM_TYPE_VALUE, dieSize : [ type : SA_DiceExpressionTerm_VALUE,
SA_DB_VALUE : @(0) value : @(0)
} ]
} ]
This is, of course, an illegal expression; we cant roll a die of size 0 This is, of course, an illegal expression; we cant roll a die of size 0
(a die with zero sides?). (a die with zero sides?).
If we encounter such an illegal expression, we add an appropriate error to If we encounter such an illegal expression, we add an appropriate error to
the term. We are not required to set a value for the SA_DB_RESULT key in the -[errorBitMask]. We are not required to set a value (-[value] property)
such a case. in such a case.
*/ */
switch (expression.type) { switch (expression.type) {
@ -146,7 +153,7 @@
SA_DiceExpression *result = [expression copy]; SA_DiceExpression *result = [expression copy];
// For now, only sum and exploding sum (i.e., sum but with exploding dice) // For now, only sum and exploding sum (i.e., sum but with exploding dice)
// are supported Other sorts of roll commands may be added later. // are supported. Other sorts of roll commands may be added later.
switch (result.rollCommand) { switch (result.rollCommand) {
case SA_DiceExpressionRollCommand_SUM: case SA_DiceExpressionRollCommand_SUM:
case SA_DiceExpressionRollCommand_SUM_EXPLODING: { case SA_DiceExpressionRollCommand_SUM_EXPLODING: {
@ -256,9 +263,10 @@
// If the left-hand operand is not a roll-and-sum, then the KEEP // If the left-hand operand is not a roll-and-sum, then the KEEP
// modifier cannot be applied to it. In that case, we add an error // modifier cannot be applied to it. In that case, we add an error
// and return the result without evaluating. // and return the result without evaluating.
if (result.leftOperand.type != SA_DiceExpressionTerm_ROLL_COMMAND || if ( result.leftOperand.type != SA_DiceExpressionTerm_ROLL_COMMAND
(result.leftOperand.rollCommand != SA_DiceExpressionRollCommand_SUM && || ( result.leftOperand.rollCommand != SA_DiceExpressionRollCommand_SUM
result.leftOperand.rollCommand != SA_DiceExpressionRollCommand_SUM_EXPLODING)) { && result.leftOperand.rollCommand != SA_DiceExpressionRollCommand_SUM_EXPLODING)
) {
result.errorBitMask |= SA_DiceExpressionError_ROLL_MODIFIER_INAPPLICABLE; result.errorBitMask |= SA_DiceExpressionError_ROLL_MODIFIER_INAPPLICABLE;
return result; return result;
} }
@ -347,12 +355,14 @@
switch (result.operator) { switch (result.operator) {
case SA_DiceExpressionOperator_MINUS: { case SA_DiceExpressionOperator_MINUS: {
// First, we check for possible overflow... // First, we check for possible overflow...
if (leftOperand > 0 && rightOperand < 0 && if ( leftOperand > 0
NSIntegerMax + rightOperand < leftOperand) { && rightOperand < 0
&& (NSIntegerMax + rightOperand) < leftOperand) {
result.errorBitMask |= SA_DiceExpressionError_INTEGER_OVERFLOW_SUBTRACTION; result.errorBitMask |= SA_DiceExpressionError_INTEGER_OVERFLOW_SUBTRACTION;
break; break;
} else if (leftOperand < 0 && rightOperand > 0 && } else if ( leftOperand < 0
NSIntegerMin + rightOperand > leftOperand) { && rightOperand > 0
&& (NSIntegerMin + rightOperand) > leftOperand) {
result.errorBitMask |= SA_DiceExpressionError_INTEGER_UNDERFLOW_SUBTRACTION; result.errorBitMask |= SA_DiceExpressionError_INTEGER_UNDERFLOW_SUBTRACTION;
break; break;
} }
@ -363,11 +373,14 @@
} }
case SA_DiceExpressionOperator_PLUS: { case SA_DiceExpressionOperator_PLUS: {
// First, we check for possible overflow... // First, we check for possible overflow...
if (rightOperand > 0 && leftOperand > 0 && if ( rightOperand > 0
NSIntegerMax - rightOperand < leftOperand) { && leftOperand > 0
&& (NSIntegerMax - rightOperand) < leftOperand) {
result.errorBitMask |= SA_DiceExpressionError_INTEGER_OVERFLOW_ADDITION; result.errorBitMask |= SA_DiceExpressionError_INTEGER_OVERFLOW_ADDITION;
break; break;
} else if(rightOperand < 0 && leftOperand < 0 && NSIntegerMin - rightOperand > leftOperand) { } else if ( rightOperand < 0
&& leftOperand < 0
&& (NSIntegerMin - rightOperand) > leftOperand) {
result.errorBitMask |= SA_DiceExpressionError_INTEGER_UNDERFLOW_ADDITION; result.errorBitMask |= SA_DiceExpressionError_INTEGER_UNDERFLOW_ADDITION;
break; break;
} }
@ -378,11 +391,19 @@
} }
case SA_DiceExpressionOperator_TIMES: { case SA_DiceExpressionOperator_TIMES: {
// First, we check for possible overflow... // First, we check for possible overflow...
if (( leftOperand == NSIntegerMin && ( rightOperand != 0 || rightOperand != 1 )) || if ( ( leftOperand == NSIntegerMin
( rightOperand == NSIntegerMin && ( leftOperand != 0 || leftOperand != 1 )) || && ( rightOperand != 0
( leftOperand != 0 && ( (NSIntegerMax / ABS(leftOperand)) < rightOperand ))) { || rightOperand != 1 ))
if ((leftOperand > 0 && rightOperand > 0) || || ( rightOperand == NSIntegerMin
(leftOperand < 0 && rightOperand < 0)) { && ( leftOperand != 0
|| leftOperand != 1 ))
|| ( leftOperand != 0
&& ((NSIntegerMax / ABS(leftOperand)) < rightOperand))
) {
if ( ( leftOperand > 0
&& rightOperand > 0)
|| ( leftOperand < 0
&& rightOperand < 0)) {
result.errorBitMask |= SA_DiceExpressionError_INTEGER_OVERFLOW_MULTIPLICATION; result.errorBitMask |= SA_DiceExpressionError_INTEGER_OVERFLOW_MULTIPLICATION;
} else { } else {
result.errorBitMask |= SA_DiceExpressionError_INTEGER_UNDERFLOW_MULTIPLICATION; result.errorBitMask |= SA_DiceExpressionError_INTEGER_UNDERFLOW_MULTIPLICATION;