forked from KPGPMC/furlang
feat(furvm): add a thing
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
#define FURVM_EXECUTOR_HPP
|
||||
|
||||
#include "furvm/fwd.hpp"
|
||||
#include "furvm/thing.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <stack>
|
||||
|
||||
namespace furvm {
|
||||
|
||||
@@ -33,6 +35,17 @@ private:
|
||||
struct egzekutor {
|
||||
explicit egzekutor() = default;
|
||||
};
|
||||
public:
|
||||
/**
|
||||
* @brief Executor frame.
|
||||
*
|
||||
* Call frame.
|
||||
*/
|
||||
struct frame {
|
||||
std::shared_ptr<mod> mod; /**< Shared pointer to a module with the bytecode. */
|
||||
std::size_t position; /**< Cursor to a current instruction in the bytecode. */
|
||||
std::size_t stackBase; /**< Snapshot of the stack size before this frame. */
|
||||
};
|
||||
public:
|
||||
/**
|
||||
* @brief Private constructor.
|
||||
@@ -47,12 +60,12 @@ public:
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*/
|
||||
executor(executor&&) = default;
|
||||
executor(executor&&) noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*/
|
||||
executor& operator=(executor&&) = default;
|
||||
executor& operator=(executor&&) noexcept = default;
|
||||
|
||||
executor(const executor&) = delete;
|
||||
executor& operator=(const executor&) = delete;
|
||||
@@ -77,10 +90,56 @@ public:
|
||||
* @return The flags.
|
||||
*/
|
||||
executor_flags flags() const { return m_flags; }
|
||||
public:
|
||||
/**
|
||||
* @brief Pushes a new frame.
|
||||
*
|
||||
* @param mod A shared pointer to a furvm module.
|
||||
* @param position Position in the module's bytecode.
|
||||
*/
|
||||
void push_frame(const std::shared_ptr<mod>& mod, std::size_t position);
|
||||
|
||||
/**
|
||||
* @brief Pops the top frame.
|
||||
*
|
||||
* @return The popped frame.
|
||||
*/
|
||||
frame pop_frame();
|
||||
|
||||
/**
|
||||
* @brief Returns the top frame.
|
||||
*
|
||||
* @return The frame.
|
||||
*/
|
||||
frame frame() const;
|
||||
public:
|
||||
/**
|
||||
* @brief Pushes a thing onto the stack.
|
||||
*
|
||||
* @param thing The thing to push.
|
||||
*/
|
||||
void push_thing(const std::shared_ptr<class thing>& thing);
|
||||
|
||||
/**
|
||||
* @brief Pops a thing from the stack.
|
||||
*
|
||||
* @return The popped thing.
|
||||
*/
|
||||
std::shared_ptr<class thing> pop_thing();
|
||||
|
||||
/**
|
||||
* @brief Returns the top thing on the stack.
|
||||
*
|
||||
* @return The thing.
|
||||
*/
|
||||
std::shared_ptr<class thing> thing() const;
|
||||
private:
|
||||
executor_handle m_id;
|
||||
executor_flags m_flags{}; // NOLINT(bugprone-invalid-enum-default-initialization)
|
||||
std::shared_ptr<context> m_context;
|
||||
|
||||
std::stack<struct frame> m_frames;
|
||||
std::stack<std::shared_ptr<class thing>> m_stack;
|
||||
};
|
||||
|
||||
} // namespace furvm
|
||||
|
||||
@@ -61,6 +61,27 @@ class mod;
|
||||
*/
|
||||
using module_handle = std::uint32_t;
|
||||
|
||||
// thing.hpp
|
||||
|
||||
/**
|
||||
* @enum thing_t
|
||||
* @brief Thing type.
|
||||
*/
|
||||
enum class thing_t : std::uint8_t;
|
||||
|
||||
/**
|
||||
* @class thing
|
||||
* @brief Furvm thing.
|
||||
*
|
||||
* A stack element. Think of it like of a value in C++ or I guess a class in java.
|
||||
*/
|
||||
class thing;
|
||||
|
||||
/**
|
||||
* @brief Furvm thing's index.
|
||||
*/
|
||||
using thing_handle = std::uint32_t;
|
||||
|
||||
// executor.hpp
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
#ifndef FURVM_THING_HPP
|
||||
#define FURVM_THING_HPP
|
||||
|
||||
#include "furvm/fwd.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace furvm {
|
||||
|
||||
enum class thing_t : std::uint8_t {
|
||||
Int,
|
||||
};
|
||||
|
||||
class thing {
|
||||
friend class executor;
|
||||
private:
|
||||
/**
|
||||
* @brief A private token for the private constructor.
|
||||
*
|
||||
* Also `rzecz` in Polish translates to `thing` I think.
|
||||
*/
|
||||
struct rzecz {
|
||||
explicit rzecz() = default;
|
||||
};
|
||||
public:
|
||||
/**
|
||||
* @brief Private constructor.
|
||||
*
|
||||
* @param id
|
||||
* @param type
|
||||
* @param context
|
||||
*/
|
||||
thing(rzecz, thing_handle id, thing_t type, const std::shared_ptr<context>& context);
|
||||
|
||||
~thing();
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*/
|
||||
thing(thing&&) noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*/
|
||||
thing& operator=(thing&&) noexcept = default;
|
||||
|
||||
thing(const thing&) = delete;
|
||||
thing& operator=(const thing&) = delete;
|
||||
private:
|
||||
thing_handle m_id;
|
||||
thing_t m_type;
|
||||
std::shared_ptr<context> m_context;
|
||||
|
||||
void* m_data;
|
||||
};
|
||||
|
||||
} // namespace furvm
|
||||
|
||||
#endif // FURVM_THING_HPP
|
||||
Reference in New Issue
Block a user