fix indentation

llvm
Clyne 2 years ago
parent 59ecb61463
commit 92680120c6
Signed by: clyne
GPG Key ID: 4C835D6F5F30690A

@ -35,7 +35,7 @@ int main(int argc, char *argv[])
std::vector args (argv + 1, argv + argc); std::vector args (argv + 1, argv + argc);
for (const auto& a : args) { for (const auto& a : args) {
std::ifstream file (a); std::ifstream file (a);
parseFile(parser, state, file); parseFile(parser, state, file);
} }
//std::cout << state.size() << ' ' << state.compiling << "> "; //std::cout << state.size() << ' ' << state.compiling << "> ";
@ -49,7 +49,7 @@ int user_sys(State& state)
switch (state.pop()) { switch (state.pop()) {
case 0: case 0:
std::cout << state.pop() << std::endl; std::cout << state.pop() << std::endl;
break; break;
} }
return 0; return 0;

@ -193,13 +193,13 @@ int CoreWords::op_shr(State& state) {
} }
int CoreWords::op_comment(State& state) { int CoreWords::op_comment(State& state) {
state.pass = Pass::Comment; state.pass = Pass::Comment;
return 0; return 0;
} }
int CoreWords::op_colon(State& state) { int CoreWords::op_colon(State& state) {
state.pass = Pass::Colon; state.pass = Pass::Colon;
state.pushr(state.dict.here); // this is for EXIT state.pushr(state.dict.here);
return 0; return 0;
} }
@ -207,13 +207,13 @@ int CoreWords::op_semic(State& state) {
if (!state.compiling) { if (!state.compiling) {
state.ip = state.popr(); state.ip = state.popr();
} else { } else {
auto begin = state.popr(); auto begin = state.popr();
state.dict.write(begin, state.dict.write(begin,
(state.dict.read(begin) & 0x1F) | (state.dict.read(begin) & 0x1F) |
((begin - state.dict.latest) << 6)); ((begin - state.dict.latest) << 6));
state.dict.latest = begin; state.dict.latest = begin;
state.compiling = false; state.compiling = false;
} }
@ -221,7 +221,7 @@ int CoreWords::op_semic(State& state) {
} }
int CoreWords::op_here(State& state) { int CoreWords::op_here(State& state) {
state.push(state.dict.here); state.push(state.dict.here);
return 0; return 0;
} }
@ -266,7 +266,7 @@ int CoreWords::findi(std::string_view str)
std::string_view words (wordsarr, sizeof(wordsarr)); std::string_view words (wordsarr, sizeof(wordsarr));
for (i = 0; i < words.size();) { for (i = 0; i < words.size();) {
const auto end = words.find('\0', i); const auto end = words.find('\0', i);
if (words.compare(i, end - i, str) == 0) if (words.compare(i, end - i, str) == 0)

@ -41,11 +41,11 @@ public:
private: private:
constexpr static char wordsarr[] = constexpr static char wordsarr[] =
"drop\0dup\0swap\0pick\0sys\0" "drop\0dup\0swap\0pick\0sys\0"
"+\0-\0*\0/\0%\0" "+\0-\0*\0/\0%\0"
"@\0!\0rot\0>r\0r>\0" "@\0!\0rot\0>r\0r>\0"
"=\0<\0allot\0&\0|\0" "=\0<\0allot\0&\0|\0"
"^\0<<\0>>\0(\0:\0" "^\0<<\0>>\0(\0:\0"
";\0here\0exit\0imm\0const\0"; ";\0here\0exit\0imm\0const\0";
// lit, jmp, jmp0, ', lits // lit, jmp, jmp0, ', lits
static Func get(int index); static Func get(int index);

@ -33,58 +33,57 @@ struct Dictionary
virtual int write(Addr, Cell) = 0; virtual int write(Addr, Cell) = 0;
Addr allot(Cell amount) { Addr allot(Cell amount) {
Addr old = here; Addr old = here;
here += amount; here += amount;
return old; return old;
} }
void add(Cell value) { void add(Cell value) {
write(here++, value); write(here++, value);
} }
void addDefinition(std::string_view str) { void addDefinition(std::string_view str) {
add(str.size()); add(str.size());
for (char c : str) for (char c : str)
add(c); add(c);
if (here & 1) if (here & 1)
allot(1); allot(1);
} }
bool issame(Addr addr, std::string_view str, std::size_t n) { bool issame(Addr addr, std::string_view str, std::size_t n) {
if (str.size() != n) if (str.size() != n)
return false; return false;
for (char c : str) { for (char c : str) {
if (read(addr++) != c) if (read(addr++) != c)
return false; return false;
} }
return true; return true;
} }
Addr find(std::string_view str) { Addr find(std::string_view str) {
if (latest == 0) if (latest == 0)
return 0; return 0;
auto lt = latest; auto lt = latest;
do { do {
const auto l = read(lt); const auto l = read(lt);
const auto len = l & 0x1F; const auto len = l & 0x1F;
if (issame(lt + 1, str, len)) { if (issame(lt + 1, str, len))
return lt; return lt;
} else { else
lt -= l >> 6; lt -= l >> 6;
} } while (lt);
} while (lt);
return 0; return 0;
} }
Addr getexec(Addr addr) { Addr getexec(Addr addr) {
const auto len = read(addr) & 0x1F; const auto len = read(addr) & 0x1F;
return ((addr + 1 + len) + 1) & ~1; return ((addr + 1 + len) + 1) & ~1;
} }
}; };

@ -21,22 +21,19 @@
#include "corewords.hpp" #include "corewords.hpp"
//#include <iostream>
class Executor class Executor
{ {
public: public:
static int fullexec(State& state, Addr addr) { static int fullexec(State& state, Addr addr) {
state.pushr(0); state.pushr(0);
state.ip = addr - 1; state.ip = addr - 1;
do { do {
++state.ip; ++state.ip;
//std::cout << "-- " << state.rsize() << "e " << state.ip << std::endl; CoreWords::run(state.dict.read(state.ip), state);
CoreWords::run(state.dict.read(state.ip), state); } while (state.ip);
} while (state.ip);
return 0;
return 0;
} }
}; };

@ -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;
} }
} }
}; };

@ -44,51 +44,51 @@ public:
constexpr State(Dictionary& d): dict(d) {} constexpr State(Dictionary& d): dict(d) {}
Cell beyondip() const { Cell beyondip() const {
return dict.read(ip + 1); return dict.read(ip + 1);
} }
void pushr(Cell value) { void pushr(Cell value) {
if (rsize() == ReturnStackSize) if (rsize() == ReturnStackSize)
throw; throw;
*++rsp = value; *++rsp = value;
} }
Cell popr() { Cell popr() {
if (rsize() == 0) if (rsize() == 0)
throw; throw;
return *rsp--; return *rsp--;
} }
void push(Cell value) { void push(Cell value) {
if (size() == DataStackSize) if (size() == DataStackSize)
throw; throw;
*++dsp = value; *++dsp = value;
} }
Cell pop() { Cell pop() {
if (size() == 0) if (size() == 0)
throw; throw;
return *dsp--; return *dsp--;
} }
Cell& top() { Cell& top() {
if (size() == 0) if (size() == 0)
throw; throw;
return *dsp; return *dsp;
} }
Cell& pick(std::size_t i) { Cell& pick(std::size_t i) {
if (i >= size()) if (i >= size())
throw; throw;
return *(dsp - i); return *(dsp - i);
} }
std::size_t size() const noexcept { std::size_t size() const noexcept {
return std::distance(dstack, static_cast<const Cell *>(dsp)) + 1; return std::distance(dstack, static_cast<const Cell *>(dsp)) + 1;
} }
std::size_t rsize() const noexcept { std::size_t rsize() const noexcept {
return std::distance(rstack, static_cast<const Cell *>(rsp)) + 1; return std::distance(rstack, static_cast<const Cell *>(rsp)) + 1;
} }
}; };

Loading…
Cancel
Save