A set of classes for parsing, evaluating, and formatting die roll strings.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SA_DiceExpression.h 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // SA_DiceExpression.h
  3. //
  4. // Copyright 2016-2021 Said Achmiz.
  5. // See LICENSE and README.md for more info.
  6. #import <Foundation/Foundation.h>
  7. /***********************/
  8. #pragma mark Definitions
  9. /***********************/
  10. typedef NS_ENUM(NSUInteger, SA_DiceExpressionTermType) {
  11. SA_DiceExpressionTerm_NONE,
  12. SA_DiceExpressionTerm_OPERATION,
  13. SA_DiceExpressionTerm_ROLL_COMMAND,
  14. SA_DiceExpressionTerm_ROLL_MODIFIER,
  15. SA_DiceExpressionTerm_VALUE
  16. };
  17. typedef NS_ENUM(NSUInteger, SA_DiceExpressionOperator) {
  18. SA_DiceExpressionOperator_NONE,
  19. SA_DiceExpressionOperator_MINUS,
  20. SA_DiceExpressionOperator_PLUS,
  21. SA_DiceExpressionOperator_TIMES
  22. };
  23. typedef NS_ENUM(NSUInteger, SA_DiceExpressionRollCommand) {
  24. SA_DiceExpressionRollCommand_NONE,
  25. SA_DiceExpressionRollCommand_SUM,
  26. SA_DiceExpressionRollCommand_SUM_EXPLODING
  27. };
  28. typedef NS_ENUM(NSUInteger, SA_DiceExpressionDieType) {
  29. SA_DiceExpressionDice_STANDARD,
  30. SA_DiceExpressionDice_FUDGE
  31. };
  32. typedef NS_ENUM(NSUInteger, SA_DiceExpressionRollModifier) {
  33. SA_DiceExpressionRollModifier_NONE,
  34. SA_DiceExpressionRollModifier_KEEP_HIGHEST,
  35. SA_DiceExpressionRollModifier_KEEP_LOWEST
  36. };
  37. typedef NS_OPTIONS(NSUInteger, SA_DiceExpressionError) {
  38. // Errors for expression parsing.
  39. SA_DiceExpressionError_NONE,
  40. SA_DiceExpressionError_ROLL_STRING_EMPTY = 1 << 0 ,
  41. SA_DiceExpressionError_ROLL_STRING_HAS_ILLEGAL_CHARACTERS = 1 << 1 ,
  42. // Errors for expression evaluation.
  43. SA_DiceExpressionError_UNKNOWN_ROLL_COMMAND = 1 << 2 ,
  44. SA_DiceExpressionError_ROLL_MODIFIER_INAPPLICABLE = 1 << 3 ,
  45. SA_DiceExpressionError_UNKNOWN_ROLL_MODIFIER = 1 << 4 ,
  46. SA_DiceExpressionError_DIE_COUNT_NEGATIVE = 1 << 5 ,
  47. SA_DiceExpressionError_DIE_COUNT_EXCESSIVE = 1 << 6 ,
  48. SA_DiceExpressionError_DIE_SIZE_INVALID = 1 << 7 ,
  49. SA_DiceExpressionError_DIE_SIZE_EXCESSIVE = 1 << 8 ,
  50. SA_DiceExpressionError_UNKNOWN_OPERATOR = 1 << 9 ,
  51. SA_DiceExpressionError_INVALID_EXPRESSION = 1 << 10 ,
  52. SA_DiceExpressionError_INTEGER_OVERFLOW_NEGATION = 1 << 11 ,
  53. SA_DiceExpressionError_INTEGER_OVERFLOW_ADDITION = 1 << 12 ,
  54. SA_DiceExpressionError_INTEGER_UNDERFLOW_ADDITION = 1 << 13 ,
  55. SA_DiceExpressionError_INTEGER_OVERFLOW_SUBTRACTION = 1 << 14 ,
  56. SA_DiceExpressionError_INTEGER_UNDERFLOW_SUBTRACTION = 1 << 15 ,
  57. SA_DiceExpressionError_INTEGER_OVERFLOW_MULTIPLICATION = 1 << 16 ,
  58. SA_DiceExpressionError_INTEGER_UNDERFLOW_MULTIPLICATION = 1 << 17 ,
  59. SA_DiceExpressionError_KEEP_COUNT_EXCEEDS_ROLL_COUNT = 1 << 18 ,
  60. SA_DiceExpressionError_KEEP_COUNT_NEGATIVE = 1 << 19
  61. };
  62. /***********************/
  63. #pragma mark - Functions
  64. /***********************/
  65. NSString *NSStringFromSA_DiceExpressionOperator(SA_DiceExpressionOperator operator);
  66. NSString *NSStringFromSA_DiceExpressionRollCommand(SA_DiceExpressionRollCommand command);
  67. NSString *NSStringFromSA_DiceExpressionRollModifier(SA_DiceExpressionRollModifier modifier);
  68. NSString *NSStringFromSA_DiceExpressionError(SA_DiceExpressionError error);
  69. @class SA_DiceExpression;
  70. NSComparisonResult compareEvaluatedExpressionsByResult(SA_DiceExpression *expression1,
  71. SA_DiceExpression *expression2);
  72. NSComparisonResult compareEvaluatedExpressionsByAttemptBonus(SA_DiceExpression *expression1,
  73. SA_DiceExpression *expression2);
  74. /*************************************************/
  75. #pragma mark - SA_DiceExpression class declaration
  76. /*************************************************/
  77. @interface SA_DiceExpression : NSObject <NSCopying>
  78. /************************/
  79. #pragma mark - Properties
  80. /************************/
  81. // The expression’s type (operation, roll command, simple value, etc.).
  82. @property SA_DiceExpressionTermType type;
  83. /*==============================================================================
  84. The following four sets of properties pertain to expressions of specific types.
  85. */
  86. // Expressions of type SA_DiceExpressionTerm_OPERATION.
  87. @property SA_DiceExpressionOperator operator;
  88. @property (nonatomic, strong) SA_DiceExpression *leftOperand;
  89. @property (nonatomic, strong) SA_DiceExpression *rightOperand;
  90. // Expressions of type SA_DiceExpressionTerm_ROLL_COMMAND.
  91. @property SA_DiceExpressionRollCommand rollCommand;
  92. @property (nonatomic, strong) SA_DiceExpression *dieCount;
  93. @property (nonatomic, strong) SA_DiceExpression *dieSize;
  94. @property SA_DiceExpressionDieType dieType;
  95. // Expressions of type SA_DiceExpressionTerm_ROLL_MODIFIER.
  96. @property SA_DiceExpressionRollModifier rollModifier;
  97. // Expressions of type SA_DiceExpressionTerm_VALUE.
  98. @property (nonatomic, strong) NSNumber *value;
  99. /*===================================================
  100. The following properties pertain to all expressions.
  101. */
  102. @property SA_DiceExpressionError errorBitMask;
  103. @property (copy, nonatomic) NSString *inputString;
  104. @property (copy, nonatomic) NSAttributedString *attributedInputString;
  105. /*=========================================================================
  106. The following properties pertain to evaluated expressions only.
  107. (They have a nil value for expressions which have not yet been evaluated.)
  108. */
  109. // Evaluated expressions (of any type).
  110. @property (nonatomic, strong) NSNumber *result;
  111. // Evaluated expressions of type SA_DiceExpressionTerm_ROLL_COMMAND.
  112. @property (nonatomic, strong) NSArray <NSNumber *> *rolls;
  113. /****************************/
  114. #pragma mark - Public methods
  115. /****************************/
  116. +(instancetype) expressionByJoiningExpression:(SA_DiceExpression *)leftHandExpression
  117. toExpression:(SA_DiceExpression *)rightHandExpression
  118. withOperator:(SA_DiceExpressionOperator)operator;
  119. @end