Horizon
program.hpp
1 #pragma once
2 #include "set.hpp"
3 #include <deque>
4 #include <functional>
5 #include <memory>
6 #include <string>
7 #include <algorithm>
8 
9 namespace horizon {
10 // using json = nlohmann::json;
11 
12 class ParameterProgram {
13  friend class ParameterCommands;
14 
15 public:
16  ParameterProgram(const std::string &s);
17  ParameterProgram(const ParameterProgram &other);
18  ParameterProgram &operator=(const ParameterProgram &other);
19  std::pair<bool, std::string> get_init_error();
20  const std::string &get_code() const;
21  std::pair<bool, std::string> set_code(const std::string &s);
22 
23  std::pair<bool, std::string> run(const ParameterSet &pset = {});
24 
25  static bool stack_pop(std::deque<int64_t> &stack, int64_t &va);
26 
27 protected:
28  class Token {
29  public:
30  enum class Type { INT, CMD, STR, UUID };
31  Token(Type ty) : type(ty)
32  {
33  }
34 
35  const Type type;
36 
37  virtual ~Token()
38  {
39  }
40 
41  virtual std::unique_ptr<Token> clone() const = 0;
42  };
43 
44  class TokenInt : public Token {
45  public:
46  TokenInt(int64_t v) : Token(Token::Type::INT), value(v)
47  {
48  }
49 
50  const int64_t value;
51 
52  std::unique_ptr<Token> clone() const override
53  {
54  return std::make_unique<TokenInt>(*this);
55  }
56  };
57 
58  class TokenCommand : public Token {
59  public:
60  TokenCommand(const std::string &cmd) : Token(Token::Type::CMD), command(cmd)
61  {
62  }
63 
64  TokenCommand(const TokenCommand &other) : Token(Token::Type::CMD), command(other.command)
65  {
66  std::transform(other.arguments.begin(), other.arguments.end(), std::back_inserter(arguments),
67  [](auto &x) { return x->clone(); });
68  }
69 
70  const std::string command;
71  std::deque<std::unique_ptr<Token>> arguments;
72 
73  std::unique_ptr<Token> clone() const override
74  {
75  return std::make_unique<TokenCommand>(*this);
76  }
77  };
78 
79  class TokenString : public Token {
80  public:
81  TokenString(const std::string &str) : Token(Token::Type::STR), string(str)
82  {
83  }
84 
85  const std::string string;
86 
87  std::unique_ptr<Token> clone() const override
88  {
89  return std::make_unique<TokenString>(*this);
90  }
91  };
92 
93  class TokenUUID : public Token {
94  public:
95  TokenUUID(const std::string &str) : Token(Token::Type::UUID), string(str)
96  {
97  }
98 
99  const std::string string;
100 
101  std::unique_ptr<Token> clone() const override
102  {
103  return std::make_unique<TokenUUID>(*this);
104  }
105  };
106  using CommandHandler =
107  std::function<std::pair<bool, std::string>(const TokenCommand *cmd, std::deque<int64_t> &stack)>;
108  virtual CommandHandler get_command(const std::string &cmd);
109 
110 private:
111  std::string code;
112  std::pair<bool, std::string> compile();
113  std::pair<bool, std::string> init_error = {false, ""};
114 
115  std::deque<std::unique_ptr<Token>> tokens;
116 };
117 } // namespace horizon
libzip::int64_t
zip_int64_t int64_t
zip_int64_t typedef.
Definition: zip.hpp:103