A set of classes for parsing, evaluating, and formatting die roll strings.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SA_DiceParser.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // SA_DiceParser.h
  3. // RPGBot
  4. //
  5. // Created by Sandy Achmiz on 12/30/15.
  6. //
  7. //
  8. #import <Foundation/Foundation.h>
  9. /*********************/
  10. #pragma mark Constants
  11. /*********************/
  12. /*
  13. These constants describe one of several behavior modes for roll string parsing.
  14. Each behavior mode defines a set of capabilities - what sorts of expressions,
  15. operators, roll commands, etc. the parser recognizes in that mode, and the
  16. syntax for using those capabilities.
  17. The structure and content of the expression tree generated by the parser from
  18. a given die roll string also depends on the behavior mode that the parser is
  19. set to.
  20. NOTE: While SA_DiceEvaluator is modeless (it correctly evaluates all
  21. expressions generated by the parser in any supported mode),
  22. SA_DiceFormatter is modal. SA_DiceFormatter provides several
  23. formatter behaviors, each of which supports one or more parser modes. Using the
  24. wrong formatter behavior mode for an expression tree (that is, passing an
  25. SA_DiceFormatter instance an expression tree that was was generated
  26. by a parser mode that is not supported by the formatter's currently set
  27. formatter behavior mode) results in undefined behavior.
  28. See SA_DiceFormatter.h for a list of which formatter behavior modes are
  29. appropriate for use with which parser modes. See also SA_DiceBot.h for a
  30. discussion of how the currently set parser mode affects what die-roll-related
  31. bot commands are available. (Together, the parser behavior mode and the results
  32. formatter behavior mode define the behavior and capabilities of an SA_DiceBot.)
  33. Each mode is described below.
  34. ======================
  35. ==== DEFAULT mode ====
  36. ======================
  37. "Default" mode is an alias for whatever default behavior is currently set for
  38. new SA_DiceParser instances. (The "default default" behavior for the current
  39. implementation is "legacy".)
  40. =====================
  41. ==== LEGACY mode ====
  42. =====================
  43. Legacy mode (mostly) emulates DiceBot by Sabin (and Dawn by xthemage before
  44. it). It replicates the parsing and evaluation functions of those venerable
  45. bots, providing the following capabilities:
  46. 1. Arithmetic operations: addition, subtraction, multiplication. Syntax is as
  47. normal for such operations, e.g.:
  48. 2+3
  49. 25*10
  50. 5-3*2
  51. Normal operator precedence and behavior (commutativity, associativity) apply.
  52. 2. Simple roll-and-sum. Roll X dice, each with Y sides, and take the sum of
  53. the rolled values, by inputting 'XdY' where X is a nonnegative integer and Y
  54. is a positive integer, e.g.:
  55. 1d20
  56. 5d6
  57. 8d27
  58. 3. Left-associative recursive roll-and-sum. Roll X dice, each with Y sides,
  59. and take the sum of the rolled values, by inputting 'XdY', where Y is a
  60. positive integer and X may be a nonnegative integer or a recursive roll-and-sum
  61. expression, e.g.:
  62. 5d6d10
  63. 1d20d6
  64. 4d4d4d4d4d4d4
  65. The above capabilities may be used indiscriminately in a single roll string:
  66. 1d20-5
  67. 5d6*10
  68. 1d20+2d4*10
  69. 5d6d10-2*3
  70. 5+3-2*4d6+2d10d3-20+5d4*2
  71. NOTE: The 'd' operator takes precedence over arithmetic operators. (Legacy
  72. mode does not support parentheses.)
  73. NOTE 2: Legacy mode does not support whitespace within roll strings.
  74. =====================
  75. ==== MODERN mode ====
  76. =====================
  77. >>> NOT YET IMPLEMENTED <<<
  78. Modern mode provides a comprehensive range of commands and capabilities.
  79. ======================
  80. ==== FEEPBOT mode ====
  81. ======================
  82. >>> NOT YET IMPLEMENTED <<<
  83. Feepbot mode emulates feepbot by feep.
  84. */
  85. typedef enum
  86. {
  87. SA_DiceParserBehaviorDefault = 0,
  88. SA_DiceParserBehaviorLegacy = 1337,
  89. SA_DiceParserBehaviorModern = 2001,
  90. SA_DiceParserBehaviorFeepbot = 65516
  91. } SA_DiceParserBehavior;
  92. /*********************************************/
  93. #pragma mark - SA_DiceParser class declaration
  94. /*********************************************/
  95. @interface SA_DiceParser : NSObject
  96. /************************/
  97. #pragma mark - Properties
  98. /************************/
  99. @property SA_DiceParserBehavior parserBehavior;
  100. /****************************************/
  101. #pragma mark - "Class property" accessors
  102. /****************************************/
  103. + (void)setDefaultParserBehavior:(SA_DiceParserBehavior)defaultParserBehavior;
  104. + (SA_DiceParserBehavior)defaultParserBehavior;
  105. /********************************************/
  106. #pragma mark - Initializers & factory methods
  107. /********************************************/
  108. - (instancetype)init;
  109. - (instancetype)initWithBehavior:(SA_DiceParserBehavior)parserBehavior NS_DESIGNATED_INITIALIZER;
  110. + (instancetype)defaultParser;
  111. + (instancetype)parserWithBehavior:(SA_DiceParserBehavior)parserBehavior;
  112. /****************************/
  113. #pragma mark - Public methods
  114. /****************************/
  115. - (NSDictionary *)expressionForString:(NSString *)dieRollString;
  116. @end