#include <algorithm>
#include <cstring>
+Error (*Parser::customParse)(State&, Word) = nullptr;
+
Error Parser::parse(State& state, const char *str)
{
auto addr = Dictionary::Input;
auto cw = CoreWords::findi(state, word);
if (cw < 0) {
- return parseNumber(state, word);
+ auto r = parseNumber(state, word);
+ if (r != Error::none)
+ return customParse ? customParse(state, word) : r;
+ else
+ return r;
} else {
ins = cw;
imm = ins == CoreWords::Semicolon;
if (inv)
result *= -1;
- auto value = static_cast<Cell>(result);
+ processLiteral(state, static_cast<Cell>(result));
+ return Error::none;
+}
+
+void Parser::processLiteral(State& state, Cell value)
+{
if (state.compiling()) {
- auto ins = CoreWords::findi("_lit");
+ constexpr auto ins = CoreWords::findi("_lit");
const Cell maxlit = Dictionary::Begin - CoreWords::WordCount;
if (value >= 0 && value < maxlit)
} else {
state.push(value);
}
-
- return Error::none;
}
class Parser
{
public:
+ static Error (*customParse)(State&, Word);
+
/**
* Parses (and evaluates) the given string using the given state.
* The string is stored in the state's input buffer, then parseSource()
*/
static Error parseSource(State&);
+ static void processLiteral(State&, Cell);
+
private:
/**
* Parses the given word using the given state.