A programming language for manipulation of streams.
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.

107 lines
1.8 KiB

5 years ago
  1. #pragma once
  2. #include "tool.h"
  3. #include <deque>
  4. #include <regex>
  5. class filter_numbers : public tool
  6. {
  7. std::optional<std::shared_ptr<tool>> n;
  8. tool_env me;
  9. std::deque<std::string> queue;
  10. bool filter_in = true;
  11. public:
  12. filter_numbers(tool_env t)
  13. : me{t}
  14. {}
  15. virtual void init(std::optional<std::shared_ptr<tool>> next)
  16. {
  17. n = next;
  18. if(me.find("reverse")!=me.end())
  19. filter_in = false;
  20. }
  21. virtual void execute()
  22. {
  23. if(has_queue())
  24. {
  25. std::string v = queue.front();
  26. queue.pop_front();
  27. bool point_sigil = false;
  28. auto it = v.begin();
  29. if(*it=='-' || *it=='+')
  30. {
  31. ++it;
  32. }
  33. for(;it!=v.end();++it)
  34. {
  35. if(!isdigit(*it))
  36. {
  37. if(*it == '.')
  38. {
  39. if(point_sigil)
  40. {
  41. if(n.has_value() && !filter_in)
  42. n.value()->enqueue(v);
  43. return;
  44. }
  45. else
  46. {
  47. point_sigil = true;
  48. }
  49. }
  50. else
  51. {
  52. if(n.has_value() && !filter_in)
  53. n.value()->enqueue(v);
  54. return;
  55. }
  56. }
  57. }
  58. if(n.has_value() && filter_in)
  59. n.value()->enqueue(v);
  60. }
  61. }
  62. virtual void enqueue(std::string v)
  63. {
  64. queue.push_back(v);
  65. }
  66. virtual bool has_queue()
  67. {
  68. return queue.size()!=0;
  69. }
  70. };
  71. class filter_in : public tool
  72. {
  73. std::optional<std::shared_ptr<tool>> n;
  74. tool_env me;
  75. std::deque<std::string> queue;
  76. std::regex pattern;
  77. public:
  78. filter_in(tool_env t)
  79. : me{t}
  80. {}
  81. virtual void init(std::optional<std::shared_ptr<tool>> next)
  82. {
  83. n = next;
  84. pattern = std::regex(me["pattern"]);
  85. }
  86. virtual void execute()
  87. {
  88. if(has_queue())
  89. {
  90. if(n.has_value() && std::regex_match(queue.front(), pattern))
  91. n.value()->enqueue(queue.front());
  92. queue.pop_front();
  93. }
  94. }
  95. virtual void enqueue(std::string v)
  96. {
  97. queue.push_back(v);
  98. }
  99. virtual bool has_queue()
  100. {
  101. return queue.size()!=0;
  102. }
  103. };