|
|
@ -20,47 +20,47 @@
|
|
|
|
#include "executor.hpp"
|
|
|
|
#include "executor.hpp"
|
|
|
|
#include "parser.hpp"
|
|
|
|
#include "parser.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <cctype>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
|
|
|
|
ParseStatus Parser::parse(State& state, std::string_view& str)
|
|
|
|
ParseStatus Parser::parse(State& state, std::string_view& str)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const auto end = str.find_first_of(" \t\n\r");
|
|
|
|
auto addr = Dictionary::Input;
|
|
|
|
const auto sub = str.substr(0, end);
|
|
|
|
state.dict.write(addr, str.size() + 1);
|
|
|
|
if (sub.empty())
|
|
|
|
|
|
|
|
return ParseStatus::Finished;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (state.pass != Pass::None) {
|
|
|
|
addr += sizeof(Cell) + Dictionary::InputCells - str.size() - 1;
|
|
|
|
switch (state.pass) {
|
|
|
|
for (char c : str)
|
|
|
|
case Pass::Comment:
|
|
|
|
state.dict.writebyte(addr++, c);
|
|
|
|
if (str.front() == ')')
|
|
|
|
state.dict.writebyte(addr, '\0');
|
|
|
|
state.pass = Pass::None;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
str = str.substr(1);
|
|
|
|
return parseSource(state);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Pass::Colon:
|
|
|
|
|
|
|
|
state.pass = Pass::None;
|
|
|
|
|
|
|
|
state.compiling(true);
|
|
|
|
|
|
|
|
state.dict.addDefinition(sub);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Pass::Constant:
|
|
|
|
|
|
|
|
state.pass = Pass::None;
|
|
|
|
|
|
|
|
state.compiling(true);
|
|
|
|
|
|
|
|
state.dict.addDefinition(sub);
|
|
|
|
|
|
|
|
state.dict.add(CoreWords::HiddenWordLiteral);
|
|
|
|
|
|
|
|
state.dict.add(state.pop());
|
|
|
|
|
|
|
|
state.dict.add(CoreWords::findi(";"));
|
|
|
|
|
|
|
|
CoreWords::run(CoreWords::findi(";"), state);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (auto i = CoreWords::findi(sub); i >= 0) {
|
|
|
|
ParseStatus Parser::parseSource(State& state)
|
|
|
|
if (state.compiling())
|
|
|
|
{
|
|
|
|
|
|
|
|
auto word = state.dict.input();
|
|
|
|
|
|
|
|
while (word.size() > 0) {
|
|
|
|
|
|
|
|
if (auto ret = parseWord(state, word); ret == ParseStatus::Error)
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
word = state.dict.input();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ParseStatus::Finished;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ParseStatus Parser::parseWord(State& state, Word word)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (auto i = CoreWords::findi(state, word); i >= 0) {
|
|
|
|
|
|
|
|
if (state.compiling()) {
|
|
|
|
state.dict.add(i & ~CoreWords::CoreImmediate);
|
|
|
|
state.dict.add(i & ~CoreWords::CoreImmediate);
|
|
|
|
|
|
|
|
} else if (state.dict.equal(word, ":")) {
|
|
|
|
|
|
|
|
state.compiling(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!state.compiling() || (i & CoreWords::CoreImmediate))
|
|
|
|
if (!state.compiling() || (i & CoreWords::CoreImmediate))
|
|
|
|
CoreWords::run(i & ~CoreWords::CoreImmediate, state);
|
|
|
|
CoreWords::run(i & ~CoreWords::CoreImmediate, state);
|
|
|
|
} else if (auto j = state.dict.find(sub); j > 0) {
|
|
|
|
} else if (auto j = state.dict.find(word); j > 0) {
|
|
|
|
auto e = state.dict.getexec(j);
|
|
|
|
auto e = state.dict.getexec(j);
|
|
|
|
|
|
|
|
|
|
|
|
if (state.compiling()) {
|
|
|
|
if (state.compiling()) {
|
|
|
@ -76,11 +76,15 @@ ParseStatus Parser::parse(State& state, std::string_view& str)
|
|
|
|
Executor::fullexec(state, e);
|
|
|
|
Executor::fullexec(state, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
char buf[word.size()];
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < word.size(); ++i)
|
|
|
|
|
|
|
|
buf[i] = state.dict.readbyte(word.start + i);
|
|
|
|
|
|
|
|
|
|
|
|
char *p;
|
|
|
|
char *p;
|
|
|
|
const auto base = state.dict.read(0);
|
|
|
|
const auto base = state.dict.read(0);
|
|
|
|
const auto l = static_cast<Cell>(std::strtol(sub.data(), &p, base));
|
|
|
|
const auto l = static_cast<Cell>(std::strtol(buf, &p, base));
|
|
|
|
|
|
|
|
|
|
|
|
if (p != sub.data()) {
|
|
|
|
if (p != buf) {
|
|
|
|
if (state.compiling()) {
|
|
|
|
if (state.compiling()) {
|
|
|
|
state.dict.add(CoreWords::HiddenWordLiteral);
|
|
|
|
state.dict.add(CoreWords::HiddenWordLiteral);
|
|
|
|
state.dict.add(l);
|
|
|
|
state.dict.add(l);
|
|
|
@ -92,17 +96,23 @@ ParseStatus Parser::parse(State& state, std::string_view& str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (end == std::string_view::npos)
|
|
|
|
|
|
|
|
return ParseStatus::Finished;
|
|
|
|
return ParseStatus::Finished;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const auto next = str.find_first_not_of(" \t\n\r", end);
|
|
|
|
//case Pass::Colon:
|
|
|
|
|
|
|
|
// state.pass = Pass::None;
|
|
|
|
if (next == std::string_view::npos) {
|
|
|
|
// state.compiling(true);
|
|
|
|
return ParseStatus::Finished;
|
|
|
|
// state.dict.addDefinition(sub);
|
|
|
|
} else {
|
|
|
|
// break;
|
|
|
|
str = str.substr(next);
|
|
|
|
//case Pass::Constant:
|
|
|
|
return ParseStatus::Continue;
|
|
|
|
// state.pass = Pass::None;
|
|
|
|
}
|
|
|
|
// state.compiling(true);
|
|
|
|
}
|
|
|
|
// state.dict.addDefinition(sub);
|
|
|
|
|
|
|
|
// state.dict.add(CoreWords::HiddenWordLiteral);
|
|
|
|
|
|
|
|
// state.dict.add(state.pop());
|
|
|
|
|
|
|
|
// state.dict.add(CoreWords::findi(";"));
|
|
|
|
|
|
|
|
// CoreWords::run(CoreWords::findi(";"), state);
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
//default:
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
//}
|
|
|
|