From 92680120c6e3902644171c37eb7b271cf2d82250 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 9 Feb 2023 14:12:26 -0500 Subject: [PATCH] fix indentation --- alee.cpp | 4 +- corewords.cpp | 18 ++++---- corewords.hpp | 10 ++-- dictionary.hpp | 55 +++++++++++----------- executor.hpp | 19 ++++---- parser.hpp | 123 +++++++++++++++++++++++++------------------------ state.hpp | 28 +++++------ 7 files changed, 127 insertions(+), 130 deletions(-) diff --git a/alee.cpp b/alee.cpp index 265982c..b7858e6 100644 --- a/alee.cpp +++ b/alee.cpp @@ -35,7 +35,7 @@ int main(int argc, char *argv[]) std::vector args (argv + 1, argv + argc); for (const auto& a : args) { std::ifstream file (a); - parseFile(parser, state, file); + parseFile(parser, state, file); } //std::cout << state.size() << ' ' << state.compiling << "> "; @@ -49,7 +49,7 @@ int user_sys(State& state) switch (state.pop()) { case 0: std::cout << state.pop() << std::endl; - break; + break; } return 0; diff --git a/corewords.cpp b/corewords.cpp index 6595e41..8fbcfbd 100644 --- a/corewords.cpp +++ b/corewords.cpp @@ -193,13 +193,13 @@ int CoreWords::op_shr(State& state) { } int CoreWords::op_comment(State& state) { - state.pass = Pass::Comment; + state.pass = Pass::Comment; return 0; } int CoreWords::op_colon(State& state) { state.pass = Pass::Colon; - state.pushr(state.dict.here); // this is for EXIT + state.pushr(state.dict.here); return 0; } @@ -207,13 +207,13 @@ int CoreWords::op_semic(State& state) { if (!state.compiling) { state.ip = state.popr(); } else { - auto begin = state.popr(); + auto begin = state.popr(); - state.dict.write(begin, - (state.dict.read(begin) & 0x1F) | - ((begin - state.dict.latest) << 6)); + state.dict.write(begin, + (state.dict.read(begin) & 0x1F) | + ((begin - state.dict.latest) << 6)); - state.dict.latest = begin; + state.dict.latest = begin; state.compiling = false; } @@ -221,7 +221,7 @@ int CoreWords::op_semic(State& state) { } int CoreWords::op_here(State& state) { - state.push(state.dict.here); + state.push(state.dict.here); return 0; } @@ -266,7 +266,7 @@ int CoreWords::findi(std::string_view str) 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); if (words.compare(i, end - i, str) == 0) diff --git a/corewords.hpp b/corewords.hpp index 08f172b..ff9d84d 100644 --- a/corewords.hpp +++ b/corewords.hpp @@ -41,11 +41,11 @@ public: private: constexpr static char wordsarr[] = "drop\0dup\0swap\0pick\0sys\0" - "+\0-\0*\0/\0%\0" - "@\0!\0rot\0>r\0r>\0" - "=\0<\0allot\0&\0|\0" - "^\0<<\0>>\0(\0:\0" - ";\0here\0exit\0imm\0const\0"; + "+\0-\0*\0/\0%\0" + "@\0!\0rot\0>r\0r>\0" + "=\0<\0allot\0&\0|\0" + "^\0<<\0>>\0(\0:\0" + ";\0here\0exit\0imm\0const\0"; // lit, jmp, jmp0, ', lits static Func get(int index); diff --git a/dictionary.hpp b/dictionary.hpp index c527603..880b8a5 100644 --- a/dictionary.hpp +++ b/dictionary.hpp @@ -33,58 +33,57 @@ struct Dictionary virtual int write(Addr, Cell) = 0; Addr allot(Cell amount) { - Addr old = here; - here += amount; - return old; + Addr old = here; + here += amount; + return old; } void add(Cell value) { - write(here++, value); + write(here++, value); } void addDefinition(std::string_view str) { add(str.size()); - for (char c : str) - add(c); + for (char c : str) + add(c); - if (here & 1) - allot(1); + if (here & 1) + allot(1); } bool issame(Addr addr, std::string_view str, std::size_t n) { - if (str.size() != n) - return false; + if (str.size() != n) + return false; for (char c : str) { - if (read(addr++) != c) - return false; - } - - return true; + if (read(addr++) != c) + return false; + } + + return true; } Addr find(std::string_view str) { if (latest == 0) - return 0; + return 0; - auto lt = latest; - do { + auto lt = latest; + do { const auto l = read(lt); - const auto len = l & 0x1F; + const auto len = l & 0x1F; - if (issame(lt + 1, str, len)) { - return lt; - } else { - lt -= l >> 6; - } - } while (lt); + if (issame(lt + 1, str, len)) + return lt; + else + lt -= l >> 6; + } while (lt); - return 0; + return 0; } Addr getexec(Addr addr) { - const auto len = read(addr) & 0x1F; - return ((addr + 1 + len) + 1) & ~1; + const auto len = read(addr) & 0x1F; + return ((addr + 1 + len) + 1) & ~1; } }; diff --git a/executor.hpp b/executor.hpp index 485e82a..62afe5f 100644 --- a/executor.hpp +++ b/executor.hpp @@ -21,22 +21,19 @@ #include "corewords.hpp" -//#include - class Executor { public: static int fullexec(State& state, Addr addr) { state.pushr(0); - state.ip = addr - 1; - - do { - ++state.ip; - //std::cout << "-- " << state.rsize() << "e " << state.ip << std::endl; - CoreWords::run(state.dict.read(state.ip), state); - } while (state.ip); - - return 0; + state.ip = addr - 1; + + do { + ++state.ip; + CoreWords::run(state.dict.read(state.ip), state); + } while (state.ip); + + return 0; } }; diff --git a/parser.hpp b/parser.hpp index 588315d..c136990 100644 --- a/parser.hpp +++ b/parser.hpp @@ -26,83 +26,84 @@ class Parser public: ParseStatus parse(State& state, std::string_view& str) { 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()) return ParseStatus::Finished; - if (state.pass != Pass::None) { - switch (state.pass) { - case Pass::Comment: + if (state.pass != Pass::None) { + switch (state.pass) { + case Pass::Comment: if (str.front() == ')') - state.pass = Pass::None; + state.pass = Pass::None; - str = str.substr(1); - break; - case Pass::Colon: - state.pass = Pass::None; - state.compiling = true; - state.dict.addDefinition(sub); - break; - case Pass::Constant: - state.pass = Pass::None; + str = str.substr(1); + 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.addDefinition(sub); + state.dict.add(CoreWords::HiddenWordLiteral); + state.dict.add(state.pop()); state.dict.add(CoreWords::findi(";")); - CoreWords::run(CoreWords::findi(";"), state); + CoreWords::run(CoreWords::findi(";"), state); break; - default: - break; - } - } else { - if (auto i = CoreWords::findi(sub); i >= 0) { - if (state.compiling) + default: + break; + } + } else { + if (auto i = CoreWords::findi(sub); i >= 0) { + if (state.compiling) state.dict.add(i); - if (!state.compiling || sub.front() == ';') - CoreWords::run(i, state); - } else if (auto j = state.dict.find(sub); j > 0) { - auto e = state.dict.getexec(j); - if (state.compiling) { - if (state.dict.read(j) & CoreWords::Immediate) { - state.compiling = false; - Executor::fullexec(state, e); - state.compiling = true; - } else { - state.dict.add(CoreWords::HiddenWordJump); - state.dict.add(e); - } - } else { - Executor::fullexec(state, e); - } - } else { - char *p; - const auto l = static_cast(std::strtol(sub.data(), &p, 10)); + if (!state.compiling || sub.front() == ';') + CoreWords::run(i, state); + } else if (auto j = state.dict.find(sub); j > 0) { + auto e = state.dict.getexec(j); + + if (state.compiling) { + if (state.dict.read(j) & CoreWords::Immediate) { + state.compiling = false; + Executor::fullexec(state, e); + state.compiling = true; + } else { + state.dict.add(CoreWords::HiddenWordJump); + state.dict.add(e); + } + } else { + Executor::fullexec(state, e); + } + } else { + char *p; + const auto l = static_cast(std::strtol(sub.data(), &p, 10)); - if (p != sub.data()) { - if (state.compiling) { - state.dict.add(CoreWords::HiddenWordLiteral); - state.dict.add(l); - } else { - state.push(l); - } - } else { - return ParseStatus::Error; - } - } + if (p != sub.data()) { + if (state.compiling) { + state.dict.add(CoreWords::HiddenWordLiteral); + state.dict.add(l); + } else { + state.push(l); + } + } else { + return ParseStatus::Error; + } + } - if (end == std::string_view::npos) + if (end == std::string_view::npos) 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; - } else { - str = str.substr(next); - return ParseStatus::Continue; - } + } else { + str = str.substr(next); + return ParseStatus::Continue; + } } }; diff --git a/state.hpp b/state.hpp index 03d0084..e178877 100644 --- a/state.hpp +++ b/state.hpp @@ -44,51 +44,51 @@ public: constexpr State(Dictionary& d): dict(d) {} Cell beyondip() const { - return dict.read(ip + 1); + return dict.read(ip + 1); } void pushr(Cell value) { - if (rsize() == ReturnStackSize) + if (rsize() == ReturnStackSize) throw; *++rsp = value; } Cell popr() { - if (rsize() == 0) + if (rsize() == 0) throw; - return *rsp--; + return *rsp--; } void push(Cell value) { - if (size() == DataStackSize) + if (size() == DataStackSize) throw; *++dsp = value; } Cell pop() { - if (size() == 0) + if (size() == 0) throw; - return *dsp--; + return *dsp--; } Cell& top() { - if (size() == 0) + if (size() == 0) throw; - return *dsp; + return *dsp; } Cell& pick(std::size_t i) { - if (i >= size()) - throw; - return *(dsp - i); + if (i >= size()) + throw; + return *(dsp - i); } std::size_t size() const noexcept { - return std::distance(dstack, static_cast(dsp)) + 1; + return std::distance(dstack, static_cast(dsp)) + 1; } std::size_t rsize() const noexcept { - return std::distance(rstack, static_cast(rsp)) + 1; + return std::distance(rstack, static_cast(rsp)) + 1; } };