|
|
@ -26,83 +26,84 @@ class Parser
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
ParseStatus parse(State& state, std::string_view& str) {
|
|
|
|
ParseStatus parse(State& state, std::string_view& str) {
|
|
|
|
const auto end = str.find_first_of(" \t\n\r");
|
|
|
|
const auto end = str.find_first_of(" \t\n\r");
|
|
|
|
const auto sub = str.substr(0, end);
|
|
|
|
const auto sub = str.substr(0, end);
|
|
|
|
if (sub.empty())
|
|
|
|
if (sub.empty())
|
|
|
|
return ParseStatus::Finished;
|
|
|
|
return ParseStatus::Finished;
|
|
|
|
|
|
|
|
|
|
|
|
if (state.pass != Pass::None) {
|
|
|
|
if (state.pass != Pass::None) {
|
|
|
|
switch (state.pass) {
|
|
|
|
switch (state.pass) {
|
|
|
|
case Pass::Comment:
|
|
|
|
case Pass::Comment:
|
|
|
|
if (str.front() == ')')
|
|
|
|
if (str.front() == ')')
|
|
|
|
state.pass = Pass::None;
|
|
|
|
state.pass = Pass::None;
|
|
|
|
|
|
|
|
|
|
|
|
str = str.substr(1);
|
|
|
|
str = str.substr(1);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case Pass::Colon:
|
|
|
|
case Pass::Colon:
|
|
|
|
state.pass = Pass::None;
|
|
|
|
state.pass = Pass::None;
|
|
|
|
state.compiling = true;
|
|
|
|
state.compiling = true;
|
|
|
|
state.dict.addDefinition(sub);
|
|
|
|
state.dict.addDefinition(sub);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case Pass::Constant:
|
|
|
|
case Pass::Constant:
|
|
|
|
state.pass = Pass::None;
|
|
|
|
state.pass = Pass::None;
|
|
|
|
state.compiling = true;
|
|
|
|
state.compiling = true;
|
|
|
|
state.dict.addDefinition(sub);
|
|
|
|
state.dict.addDefinition(sub);
|
|
|
|
state.dict.add(CoreWords::HiddenWordLiteral);
|
|
|
|
state.dict.add(CoreWords::HiddenWordLiteral);
|
|
|
|
state.dict.add(state.pop());
|
|
|
|
state.dict.add(state.pop());
|
|
|
|
state.dict.add(CoreWords::findi(";"));
|
|
|
|
state.dict.add(CoreWords::findi(";"));
|
|
|
|
CoreWords::run(CoreWords::findi(";"), state);
|
|
|
|
CoreWords::run(CoreWords::findi(";"), state);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if (auto i = CoreWords::findi(sub); i >= 0) {
|
|
|
|
if (auto i = CoreWords::findi(sub); i >= 0) {
|
|
|
|
if (state.compiling)
|
|
|
|
if (state.compiling)
|
|
|
|
state.dict.add(i);
|
|
|
|
state.dict.add(i);
|
|
|
|
if (!state.compiling || sub.front() == ';')
|
|
|
|
if (!state.compiling || sub.front() == ';')
|
|
|
|
CoreWords::run(i, state);
|
|
|
|
CoreWords::run(i, state);
|
|
|
|
} else if (auto j = state.dict.find(sub); j > 0) {
|
|
|
|
} else if (auto j = state.dict.find(sub); j > 0) {
|
|
|
|
auto e = state.dict.getexec(j);
|
|
|
|
auto e = state.dict.getexec(j);
|
|
|
|
if (state.compiling) {
|
|
|
|
|
|
|
|
if (state.dict.read(j) & CoreWords::Immediate) {
|
|
|
|
if (state.compiling) {
|
|
|
|
state.compiling = false;
|
|
|
|
if (state.dict.read(j) & CoreWords::Immediate) {
|
|
|
|
Executor::fullexec(state, e);
|
|
|
|
state.compiling = false;
|
|
|
|
state.compiling = true;
|
|
|
|
Executor::fullexec(state, e);
|
|
|
|
} else {
|
|
|
|
state.compiling = true;
|
|
|
|
state.dict.add(CoreWords::HiddenWordJump);
|
|
|
|
} else {
|
|
|
|
state.dict.add(e);
|
|
|
|
state.dict.add(CoreWords::HiddenWordJump);
|
|
|
|
}
|
|
|
|
state.dict.add(e);
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
Executor::fullexec(state, e);
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
Executor::fullexec(state, e);
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
char *p;
|
|
|
|
} else {
|
|
|
|
const auto l = static_cast<Cell>(std::strtol(sub.data(), &p, 10));
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
const auto l = static_cast<Cell>(std::strtol(sub.data(), &p, 10));
|
|
|
|
|
|
|
|
|
|
|
|
if (p != sub.data()) {
|
|
|
|
if (p != sub.data()) {
|
|
|
|
if (state.compiling) {
|
|
|
|
if (state.compiling) {
|
|
|
|
state.dict.add(CoreWords::HiddenWordLiteral);
|
|
|
|
state.dict.add(CoreWords::HiddenWordLiteral);
|
|
|
|
state.dict.add(l);
|
|
|
|
state.dict.add(l);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
state.push(l);
|
|
|
|
state.push(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return ParseStatus::Error;
|
|
|
|
return ParseStatus::Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (end == std::string_view::npos)
|
|
|
|
if (end == std::string_view::npos)
|
|
|
|
return ParseStatus::Finished;
|
|
|
|
return ParseStatus::Finished;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const auto next = str.find_first_not_of(" \t\n\r", end);
|
|
|
|
const auto next = str.find_first_not_of(" \t\n\r", end);
|
|
|
|
|
|
|
|
|
|
|
|
if (next == std::string_view::npos) {
|
|
|
|
if (next == std::string_view::npos) {
|
|
|
|
return ParseStatus::Finished;
|
|
|
|
return ParseStatus::Finished;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
str = str.substr(next);
|
|
|
|
str = str.substr(next);
|
|
|
|
return ParseStatus::Continue;
|
|
|
|
return ParseStatus::Continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|