|
|
@ -31,11 +31,12 @@ constexpr unsigned ReturnStackSize = 16;
|
|
|
|
|
|
|
|
|
|
|
|
class State
|
|
|
|
class State
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
friend class CoreWords;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
Addr ip = 0;
|
|
|
|
Addr ip = 0;
|
|
|
|
Dictionary& dict;
|
|
|
|
Dictionary& dict;
|
|
|
|
void (*input)(State&); // User-provided function to collect "stdin" input.
|
|
|
|
void (*input)(State&); // User-provided function to collect "stdin" input.
|
|
|
|
std::jmp_buf jmpbuf = {}; // Used when catching execution errors.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constexpr State(Dictionary& d, void (*i)(State&)):
|
|
|
|
constexpr State(Dictionary& d, void (*i)(State&)):
|
|
|
|
dict(d), input(i) {}
|
|
|
|
dict(d), input(i) {}
|
|
|
@ -43,17 +44,6 @@ public:
|
|
|
|
bool compiling() const;
|
|
|
|
bool compiling() const;
|
|
|
|
void compiling(bool);
|
|
|
|
void compiling(bool);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Saves execution state so that a new execution can begin.
|
|
|
|
|
|
|
|
* Used for EVALUATE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
std::pair<Addr, std::jmp_buf> save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Reloads the given execution state.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void load(const std::pair<Addr, std::jmp_buf>&);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Begins execution at the given execution token.
|
|
|
|
* Begins execution at the given execution token.
|
|
|
|
* If the token is a CoreWord, this function exits after its execution.
|
|
|
|
* If the token is a CoreWord, this function exits after its execution.
|
|
|
@ -72,38 +62,32 @@ public:
|
|
|
|
std::size_t rsize() const noexcept;
|
|
|
|
std::size_t rsize() const noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
inline void push(Cell value) {
|
|
|
|
inline void push(Cell value) {
|
|
|
|
if (dsp == dstack + DataStackSize)
|
|
|
|
verify(dsp < dstack + DataStackSize, Error::push);
|
|
|
|
std::longjmp(jmpbuf, static_cast<int>(Error::push));
|
|
|
|
|
|
|
|
*dsp++ = value;
|
|
|
|
*dsp++ = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline Cell pop() {
|
|
|
|
inline Cell pop() {
|
|
|
|
if (dsp == dstack)
|
|
|
|
verify(dsp > dstack, Error::pop);
|
|
|
|
std::longjmp(jmpbuf, static_cast<int>(Error::pop));
|
|
|
|
|
|
|
|
return *--dsp;
|
|
|
|
return *--dsp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void pushr(Cell value) {
|
|
|
|
inline void pushr(Cell value) {
|
|
|
|
if (rsp == rstack + ReturnStackSize)
|
|
|
|
verify(rsp < rstack + ReturnStackSize, Error::pushr);
|
|
|
|
std::longjmp(jmpbuf, static_cast<int>(Error::pushr));
|
|
|
|
|
|
|
|
*rsp++ = value;
|
|
|
|
*rsp++ = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline Cell popr() {
|
|
|
|
inline Cell popr() {
|
|
|
|
if (rsp == rstack)
|
|
|
|
verify(rsp > rstack, Error::popr);
|
|
|
|
std::longjmp(jmpbuf, static_cast<int>(Error::popr));
|
|
|
|
|
|
|
|
return *--rsp;
|
|
|
|
return *--rsp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline Cell& top() {
|
|
|
|
inline Cell& top() {
|
|
|
|
if (dsp == dstack)
|
|
|
|
verify(dsp > dstack, Error::top);
|
|
|
|
std::longjmp(jmpbuf, static_cast<int>(Error::top));
|
|
|
|
|
|
|
|
return *(dsp - 1);
|
|
|
|
return *(dsp - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline Cell& pick(std::size_t i) {
|
|
|
|
inline Cell& pick(std::size_t i) {
|
|
|
|
if (dsp - i == dstack)
|
|
|
|
verify(dsp - i > dstack, Error::pick);
|
|
|
|
std::longjmp(jmpbuf, static_cast<int>(Error::pick));
|
|
|
|
|
|
|
|
return *(dsp - i - 1);
|
|
|
|
return *(dsp - i - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -118,6 +102,23 @@ private:
|
|
|
|
Cell rstack[ReturnStackSize] = {};
|
|
|
|
Cell rstack[ReturnStackSize] = {};
|
|
|
|
Cell *dsp = dstack;
|
|
|
|
Cell *dsp = dstack;
|
|
|
|
Cell *rsp = rstack;
|
|
|
|
Cell *rsp = rstack;
|
|
|
|
|
|
|
|
std::jmp_buf jmpbuf = {}; // Used when catching execution errors.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline void verify(bool condition, Error error) {
|
|
|
|
|
|
|
|
if (!condition)
|
|
|
|
|
|
|
|
std::longjmp(jmpbuf, static_cast<int>(error));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Saves execution state so that a new execution can begin.
|
|
|
|
|
|
|
|
* Used for EVALUATE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
std::pair<Addr, std::jmp_buf> save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Reloads the given execution state.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void load(const std::pair<Addr, std::jmp_buf>&);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // ALEEFORTH_STATE_HPP
|
|
|
|
#endif // ALEEFORTH_STATE_HPP
|
|
|
|