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.

58 regels
1.6 KiB

  1. #pragma once
  2. #include <memory>
  3. #include <string>
  4. #include <variant>
  5. #include <vector>
  6. #include <optional>
  7. namespace scripting {
  8. struct null {};
  9. struct array;
  10. using script_value = std::variant<null, int32_t, std::string, array>;
  11. struct script_variable {
  12. std::string name;
  13. };
  14. struct code_location {
  15. std::shared_ptr<const std::string> line_contents;
  16. int32_t line_number;
  17. int32_t column_number;
  18. };
  19. struct script_error {
  20. std::shared_ptr<const code_location> location;
  21. std::string message;
  22. };
  23. struct array {
  24. std::vector<script_value> value;
  25. operator std::vector<script_value>&() {
  26. return value;
  27. }
  28. };
  29. using argument = std::variant<script_value, script_variable>;
  30. class UserScript;
  31. struct function_impl {
  32. virtual std::optional<script_value> apply(UserScript* self, std::vector<argument>, std::optional<script_error>&) = 0;
  33. virtual ~function_impl() = default;
  34. };
  35. using function = std::unique_ptr<function_impl>;
  36. class UserScript {
  37. public:
  38. virtual std::optional<std::reference_wrapper<script_value>> getValue(const std::string& name) = 0;
  39. virtual bool setValue(const std::string& name, script_value value) = 0;
  40. virtual void registerFunction(std::string name, function fn) = 0;
  41. virtual script_value resolve(const std::string& name) = 0;
  42. virtual std::variant<script_value, std::vector<script_error>> executeAtOnce(std::string code) = 0;
  43. virtual std::vector<script_error> prepare(std::string code) = 0;
  44. virtual std::optional<script_error> stepOnce() = 0;
  45. virtual ~UserScript() = default;
  46. };
  47. std::unique_ptr<UserScript> prepare_interpreter(const std::string& code);
  48. }