|
|
@ -24,25 +24,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
#include <csetjmp>
|
|
|
|
#include <csetjmp>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constexpr unsigned DataStackSize = 16;
|
|
|
|
constexpr unsigned DataStackSize = 16;
|
|
|
|
constexpr unsigned ReturnStackSize = 16;
|
|
|
|
constexpr unsigned ReturnStackSize = 16;
|
|
|
|
|
|
|
|
|
|
|
|
class State
|
|
|
|
class State
|
|
|
|
{
|
|
|
|
{
|
|
|
|
friend class CoreWords;
|
|
|
|
using InputFunc = void (*)(State&);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Context {
|
|
|
|
|
|
|
|
Addr ip = 0;
|
|
|
|
|
|
|
|
std::jmp_buf jmpbuf = {};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
Addr ip = 0;
|
|
|
|
|
|
|
|
Dictionary& dict;
|
|
|
|
Dictionary& dict;
|
|
|
|
void (*input)(State&); // User-provided function to collect "stdin" input.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constexpr State(Dictionary& d, void (*i)(State&)):
|
|
|
|
constexpr State(Dictionary& d, InputFunc i):
|
|
|
|
dict(d), input(i) {}
|
|
|
|
dict(d), inputfunc(i), context() {}
|
|
|
|
|
|
|
|
|
|
|
|
bool compiling() const;
|
|
|
|
|
|
|
|
void compiling(bool);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Begins execution at the given execution token.
|
|
|
|
* Begins execution at the given execution token.
|
|
|
@ -58,9 +57,31 @@ public:
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void reset();
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Addr& ip() noexcept {
|
|
|
|
|
|
|
|
return context.ip;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void input() noexcept {
|
|
|
|
|
|
|
|
inputfunc(*this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool compiling() const;
|
|
|
|
|
|
|
|
void compiling(bool);
|
|
|
|
|
|
|
|
|
|
|
|
std::size_t size() const noexcept;
|
|
|
|
std::size_t size() const noexcept;
|
|
|
|
std::size_t rsize() const noexcept;
|
|
|
|
std::size_t rsize() const noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Saves execution state so that a new execution can begin.
|
|
|
|
|
|
|
|
* Used for EVALUATE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
Context save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Reloads the given execution state.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void load(const Context&);
|
|
|
|
|
|
|
|
|
|
|
|
inline void push(Cell value) {
|
|
|
|
inline void push(Cell value) {
|
|
|
|
verify(dsp < dstack + DataStackSize, Error::push);
|
|
|
|
verify(dsp < dstack + DataStackSize, Error::push);
|
|
|
|
*dsp++ = value;
|
|
|
|
*dsp++ = value;
|
|
|
@ -93,32 +114,23 @@ public:
|
|
|
|
|
|
|
|
|
|
|
|
// Advances the instruction pointer and returns that cell's contents.
|
|
|
|
// Advances the instruction pointer and returns that cell's contents.
|
|
|
|
inline Cell beyondip() {
|
|
|
|
inline Cell beyondip() {
|
|
|
|
ip += sizeof(Cell);
|
|
|
|
context.ip += sizeof(Cell);
|
|
|
|
return dict.read(ip);
|
|
|
|
return dict.read(context.ip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
Cell dstack[DataStackSize] = {};
|
|
|
|
|
|
|
|
Cell rstack[ReturnStackSize] = {};
|
|
|
|
|
|
|
|
Cell *dsp = dstack;
|
|
|
|
|
|
|
|
Cell *rsp = rstack;
|
|
|
|
|
|
|
|
std::jmp_buf jmpbuf = {}; // Used when catching execution errors.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline void verify(bool condition, Error error) {
|
|
|
|
inline void verify(bool condition, Error error) {
|
|
|
|
if (!condition)
|
|
|
|
if (!condition)
|
|
|
|
std::longjmp(jmpbuf, static_cast<int>(error));
|
|
|
|
std::longjmp(context.jmpbuf, static_cast<int>(error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
private:
|
|
|
|
* Saves execution state so that a new execution can begin.
|
|
|
|
InputFunc inputfunc; // User-provided function to collect "stdin" input.
|
|
|
|
* Used for EVALUATE.
|
|
|
|
Context context;
|
|
|
|
*/
|
|
|
|
|
|
|
|
std::pair<Addr, std::jmp_buf> save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Cell dstack[DataStackSize] = {};
|
|
|
|
* Reloads the given execution state.
|
|
|
|
Cell rstack[ReturnStackSize] = {};
|
|
|
|
*/
|
|
|
|
Cell *dsp = dstack;
|
|
|
|
void load(const std::pair<Addr, std::jmp_buf>&);
|
|
|
|
Cell *rsp = rstack;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // ALEEFORTH_STATE_HPP
|
|
|
|
#endif // ALEEFORTH_STATE_HPP
|
|
|
|