A set of classes for parsing, evaluating, and formatting die roll strings.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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