Horizon
program.hpp
1 #pragma once
2 #include "set.hpp"
3 #include <deque>
4 #include <functional>
5 #include <memory>
6 #include <string>
7 
8 namespace horizon {
9 // using json = nlohmann::json;
10 
12  friend class ParameterCommands;
13 
14 public:
15  ParameterProgram(const std::string &s);
16  ParameterProgram(const ParameterProgram &other);
17  ParameterProgram &operator=(const ParameterProgram &other);
18  std::pair<bool, std::string> get_init_error();
19  const std::string &get_code() const;
20  std::pair<bool, std::string> set_code(const std::string &s);
21 
22  std::pair<bool, std::string> run(const ParameterSet &pset = {});
23 
24 protected:
25  class Token {
26  public:
27  enum class Type { INT, CMD, STR, UUID };
28  Token(Type ty) : type(ty)
29  {
30  }
31 
32  const Type type;
33 
34  virtual ~Token()
35  {
36  }
37  };
38 
39  class TokenInt : public Token {
40  public:
41  TokenInt(int64_t v) : Token(Token::Type::INT), value(v)
42  {
43  }
44 
45  const int64_t value;
46  };
47 
48  class TokenCommand : public Token {
49  public:
50  TokenCommand(const std::string &cmd) : Token(Token::Type::CMD), command(cmd)
51  {
52  }
53 
54  const std::string command;
55  std::deque<std::unique_ptr<Token>> arguments;
56  };
57 
58  class TokenString : public Token {
59  public:
60  TokenString(const std::string &str) : Token(Token::Type::STR), string(str)
61  {
62  }
63 
64  const std::string string;
65  };
66 
67  class TokenUUID : public Token {
68  public:
69  TokenUUID(const std::string &str) : Token(Token::Type::UUID), string(str)
70  {
71  }
72 
73  const std::string string;
74  };
75  using CommandHandler =
76  std::function<std::pair<bool, std::string>(const TokenCommand *cmd, std::deque<int64_t> &stack)>;
77  virtual CommandHandler get_command(const std::string &cmd);
78 
79 private:
80  std::string code;
81  std::pair<bool, std::string> compile();
82  std::pair<bool, std::string> init_error = {false, ""};
83 
84  std::deque<std::unique_ptr<Token>> tokens;
85 };
86 } // namespace horizon
Definition: program.hpp:25
Definition: program.hpp:58
Definition: program.hpp:11
Definition: program.hpp:39
Definition: program.cpp:54
Definition: program.hpp:67
This class encapsulates a UUID and allows it to be uses as a value type.
Definition: uuid.hpp:16
Definition: block.cpp:7
Definition: program.hpp:48