|
|
|
@@ -11,6 +11,7 @@ |
|
|
|
#import "SA_DiceExpressionStringConstants.h" |
|
|
|
#import "SA_DiceErrorHandling.h" |
|
|
|
#import "NSString+SA_NSStringExtensions.h" |
|
|
|
#import "SA_DiceFormatter.h" |
|
|
|
|
|
|
|
/********************************/ |
|
|
|
#pragma mark File-scope variables |
|
|
|
@@ -135,6 +136,63 @@ static NSDictionary *_validCharactersDict; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
- (NSDictionary *)expressionByJoiningExpression:(NSDictionary *)leftHandExpression toExpression:(NSDictionary *)rightHandExpression withOperator:(NSString *)operatorName |
|
|
|
{ |
|
|
|
NSMutableDictionary *expression = [NSMutableDictionary dictionary]; |
|
|
|
|
|
|
|
// First, we check that the operands and operator are not nil. If they are, |
|
|
|
// then the expression is invalid... |
|
|
|
if(leftHandExpression == nil || rightHandExpression == nil || operatorName == nil) |
|
|
|
{ |
|
|
|
expression[SA_DB_TERM_TYPE] = SA_DB_TERM_TYPE_NONE; |
|
|
|
|
|
|
|
addErrorToExpression(SA_DB_ERROR_INVALID_EXPRESSION, expression); |
|
|
|
|
|
|
|
return expression; |
|
|
|
} |
|
|
|
|
|
|
|
// If the operands and operator are present, then the expression is an |
|
|
|
// operation expression... |
|
|
|
expression[SA_DB_TERM_TYPE] = SA_DB_TERM_TYPE_OPERATION; |
|
|
|
|
|
|
|
// ... but does it have a valid operator? |
|
|
|
if([operatorName isEqualToString:SA_DB_OPERATOR_PLUS] || |
|
|
|
[operatorName isEqualToString:SA_DB_OPERATOR_MINUS] || |
|
|
|
[operatorName isEqualToString:SA_DB_OPERATOR_TIMES] |
|
|
|
) |
|
|
|
{ |
|
|
|
expression[SA_DB_OPERATOR] = operatorName; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
addErrorToExpression(SA_DB_ERROR_UNKNOWN_OPERATOR, expression); |
|
|
|
|
|
|
|
return expression; |
|
|
|
} |
|
|
|
|
|
|
|
// The operator is valid. Set the operands... |
|
|
|
expression[SA_DB_OPERAND_LEFT] = leftHandExpression; |
|
|
|
expression[SA_DB_OPERAND_RIGHT] = rightHandExpression; |
|
|
|
|
|
|
|
// And inherit any errors that they may have. |
|
|
|
addErrorsFromExpressionToExpression(expression[SA_DB_OPERAND_LEFT], expression); |
|
|
|
addErrorsFromExpressionToExpression(expression[SA_DB_OPERAND_RIGHT], expression); |
|
|
|
|
|
|
|
// Since this top-level expression was NOT generated by parsing an input |
|
|
|
// string, for completeness and consistency, we have to generate a fake |
|
|
|
// input string ourselves! We do this by wrapping each operand in |
|
|
|
// parentheses and putting the canonical representation of the operator |
|
|
|
// between them. |
|
|
|
NSString *fakeInputString = [NSString stringWithFormat:@"(%@)%@(%@)", |
|
|
|
expression[SA_DB_OPERAND_LEFT][SA_DB_INPUT_STRING], |
|
|
|
[SA_DiceFormatter canonicalRepresentationForOperator:expression[SA_DB_OPERATOR]], |
|
|
|
expression[SA_DB_OPERAND_RIGHT][SA_DB_INPUT_STRING]]; |
|
|
|
expression[SA_DB_INPUT_STRING] = fakeInputString; |
|
|
|
|
|
|
|
// The joining is complete. (Power overwhelming.) |
|
|
|
return expression; |
|
|
|
} |
|
|
|
|
|
|
|
/**********************************************/ |
|
|
|
#pragma mark - "Legacy" behavior implementation |
|
|
|
/**********************************************/ |
|
|
|
@@ -188,7 +246,7 @@ static NSDictionary *_validCharactersDict; |
|
|
|
|
|
|
|
// First, we check for whether there even is anything more to the |
|
|
|
// roll string besides the operator. If not, then the string is |
|
|
|
// definitely malformed... |
|
|
|
// malformed by definition... |
|
|
|
if(dieRollString.length == lastOperatorRange.length) |
|
|
|
{ |
|
|
|
expression = [NSMutableDictionary dictionary]; |