parse.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef __PARSE_H__
  2. #define __PARSE_H__
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #define LONG_MAX_STR_LEN 19 // Based on decimal representation of LONG_MAX
  6. typedef enum direction {
  7. neg = -1,
  8. pos = 1
  9. } direction;
  10. struct roll_encoding;
  11. struct roll_encoding {
  12. long ndice;
  13. long nsides;
  14. direction dir;
  15. bool explode;
  16. bool keep;
  17. long discard;
  18. struct roll_encoding *next;
  19. };
  20. struct parse_tree;
  21. struct parse_tree {
  22. bool suppress; // Used to silence output, eg when clearing screen
  23. bool quit;
  24. long nreps;
  25. bool use_threshold;
  26. long threshold;
  27. struct roll_encoding *dice_specs;
  28. struct roll_encoding *last_roll; // Easily find latest entry in dice_specs list
  29. long ndice;
  30. struct parse_tree *next;
  31. struct parse_tree *current;
  32. };
  33. typedef enum token_t {
  34. none = 0,
  35. number,
  36. dice_operator,
  37. rep_operator,
  38. additive_operator,
  39. explode_operator,
  40. threshold_operator,
  41. keep_operator,
  42. command,
  43. statement_delimiter,
  44. eol
  45. } token_t;
  46. typedef enum cmd_t {
  47. unknown = -1,
  48. quit = 0,
  49. clear
  50. } cmd_t;
  51. struct cmd_map {
  52. cmd_t cmd_code;
  53. char cmd_str[8];
  54. };
  55. struct token {
  56. token_t type;
  57. long number;
  58. char op;
  59. cmd_t cmd;
  60. };
  61. typedef enum state_t {
  62. error = -1,
  63. start = 0,
  64. decide_reps_or_rolls,
  65. want_number_of_sides,
  66. check_number_of_dice,
  67. want_roll,
  68. check_dice_operator,
  69. check_modifiers_or_more_rolls,
  70. check_more_rolls,
  71. want_threshold,
  72. want_keep_number,
  73. check_end,
  74. finish
  75. } state_t;
  76. void clear_screen();
  77. void token_init(struct token *t);
  78. int lex(struct token *t, int *tokens_found, const char *buf, const size_t len);
  79. void print_state_name(const state_t s);
  80. void print_parse_tree(const struct parse_tree *t);
  81. int parse(struct parse_tree *t, const char *buf, const size_t len);
  82. void parse_tree_initialise(struct parse_tree *t);
  83. void parse_tree_reset(struct parse_tree *t);
  84. #endif // __PARSE_H__