]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
pull exit check out of State::execute
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 26 Feb 2023 13:10:43 +0000 (08:10 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 26 Feb 2023 13:10:43 +0000 (08:10 -0500)
corewords.cpp
corewords.hpp
state.cpp
state.hpp

index 6aec5ab2e7979b031ee5a1de7e16682da5ca360e..97f53dc669490ba5307848b9a58b5d1f06d616cf 100644 (file)
@@ -168,19 +168,23 @@ execute:
         break;
     case 25: // exit
         state.ip = state.popr();
+        if (state.ip == 0) {
+            std::longjmp(state.jmpbuf,
+                static_cast<int>(State::Error::exit));
+        }
         break;
     case 26: // semic
         state.dict.add(findi("exit"));
         state.compiling(false);
         break;
-    case 27: // _jmp
-        state.ip = state.beyondip() - sizeof(Cell);
-        break;
-    case 28: // _jmp0
-        if (state.pop())
+    case 27: // _jmp0
+        if (state.pop()) {
             state.beyondip();
-        else
-            state.ip = state.beyondip() - sizeof(Cell);
+            break;
+        }
+        [[fallthrough]];
+    case 28: // _jmp
+        state.ip = state.beyondip() - sizeof(Cell);
         break;
     case 29: // depth
         state.push(state.size());
index fcb00e889b5a6b55b9cefa86283d410c23048ec7..020694f7878783274a5a8a7dbe134d4dccbbb45e 100644 (file)
@@ -44,8 +44,8 @@ private:
         "_@\0_!\0>r\0r>\0=\0"
         "<\0&\0|\0^\0"
         "<<\0>>\0:\0'\0execute\0"
-        "exit\0;\1_jmp\0"
-        "_jmp0\0depth\0_rdepth\0key\0";
+        "exit\0;\1_jmp0\0_jmp\0"
+        "depth\0_rdepth\0key\0";
 };
 
 #endif // ALEEFORTH_COREWORDS_HPP
index 56ad8cddd5a293f798f397a053a783a1be980346..c27d95b9d68eb29eba60aaf66ee259da45a01a83 100644 (file)
--- a/state.cpp
+++ b/state.cpp
@@ -35,20 +35,20 @@ State::Error State::execute(Addr addr)
 {
     auto stat = setjmp(jmpbuf);
     if (!stat) {
-        ip = 0;
+        if (addr < CoreWords::WordCount) {
+            CoreWords::run(addr, *this);
+        } else {
+            auto ins = addr;
 
-        auto ins = addr;
-        for (;;) {
-            CoreWords::run(ins, *this);
-
-            if (!ip)
-                break;
-
-            ip += sizeof(Cell);
-            ins = dict.read(ip);
+            for (;;) {
+                CoreWords::run(ins, *this);
+                ip += sizeof(Cell);
+                ins = dict.read(ip);
+            }
         }
     } else {
-        return static_cast<State::Error>(stat);
+        auto err = static_cast<State::Error>(stat);
+        return err == State::Error::exit ? State::Error::none : err;
     }
 
     return State::Error::none;
index 69db2f62d560c8406d2ee9ed1886f1ae59846ca4..76b3c677e9344b7e73f868117dd071bdaed8d9a8 100644 (file)
--- a/state.hpp
+++ b/state.hpp
@@ -37,7 +37,8 @@ struct State
         pushr,
         popr,
         top,
-        pick
+        pick,
+        exit
     };
 
     Addr ip = 0;