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