]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
reduce built-in word count
authorClyne Sullivan <clyne@bitgloo.com>
Sat, 25 Feb 2023 18:39:04 +0000 (13:39 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sat, 25 Feb 2023 18:39:04 +0000 (13:39 -0500)
alee.cpp
core.fth
corewords.cpp
corewords.hpp
dictionary.cpp
dictionary.hpp
parser.cpp

index f65b700f7b0e6094ba4732d0af552062a6eb3c36..c5b0a95e3a39d470a6e1c79cd075919a5147415a 100644 (file)
--- a/alee.cpp
+++ b/alee.cpp
@@ -36,6 +36,7 @@ int main(int argc, char *argv[])
     Parser parser;
 
     dict.write(Dictionary::Base, 10);
+    dict.write(Dictionary::Here, Dictionary::Begin);
     dict.write(Dictionary::Latest, Dictionary::Begin);
     dict.write(Dictionary::Compiling, 0);
     dict.write(Dictionary::Postpone, 0);
@@ -71,7 +72,7 @@ static void save(State& state)
     std::ofstream file ("alee.dat", std::ios::binary);
 
     if (file.good()) {
-        for (Addr i = 0; i < state.dict.here; ++i)
+        for (Addr i = 0; i < state.dict.here(); ++i)
             file.put(state.dict.readbyte(i));
     }
 }
@@ -83,8 +84,6 @@ static void load(State& state)
     Addr i = 0;
     while (file.good())
         state.dict.writebyte(i++, file.get());
-
-    state.dict.here = i - 1;
 }
 
 void user_sys(State& state)
index bcdd577950414fe7b921d481d49814d63fab910d..24ef53699a7d44d9b60fa00f2bb2613849b32e7a 100644 (file)
--- a/core.fth
+++ b/core.fth
 
 : !        1 _! ;
 : @        1 _@ ;
+: +!       dup >r swap r> @ + swap ! ;
+
+: base     0 ;
+: here     1 cells @ ;
+: allot    1 cells +! ;
+: _latest  2 cells ;
+: imm      _latest @ dup @ 1 5 << | swap ! ;
+: state    3 cells ;
+: postpone 1 4 cells ! ; imm
+: _input   5 cells ;
+
 : ,        here ! 1 cells allot ;
 
 : over     1 pick ;
 : char+    1+ ;
 : chars    ;
 
-: base     0 ;
-: _latest  1 cells ;
-: imm      _latest @ dup @ 1 5 << | swap ! ;
-: state    2 cells ;
-: postpone 1 3 cells ! ; imm
-: _input   4 cells ;
-
 : decimal  10 base ! ;
 : hex      16 base ! ;
 
+: literal  0 , , ; imm
 : [']      ' postpone literal ; imm
 : [        0 state ! ; imm
 : ]        1 state ! ;
@@ -53,7 +58,6 @@
 
 : 2!       swap over ! cell+ ! ;
 : 2@       dup cell+ @ swap @ ;
-: +!       swap over @ + swap ! ;
 
 : 0=       0 = ;
 : 0<       0 < ;
index 63ccc7d7b05dc9a3a843d60438310110c874d29a..02fcb5364ddb59daf3e60ac0954577db556b2391 100644 (file)
@@ -43,7 +43,7 @@ void CoreWords::run(unsigned int index, State& state)
         if (auto j = state.dict.find(word); j > 0)
             state.push(state.dict.getexec(j));
         else if (auto i = CoreWords::findi(state, word); i >= 0)
-            state.push(i & ~CoreWords::Compiletime);
+            state.push(i & ~CoreWords::Immediate);
         else
             state.push(0);
     };
@@ -57,77 +57,77 @@ void CoreWords::run(unsigned int index, State& state)
         state.pushr(state.ip);
         state.ip = index - sizeof(Cell);
         break;
-    case 0: // drop
+    case 0: // _lit
+        state.push(state.beyondip());
+        break;
+    case 1: // drop
         state.pop();
         break;
-    case 1: // dup
+    case 2: // dup
         state.push(state.top());
         break;
-    case 2: // swap
+    case 3: // swap
         std::swap(state.top(), state.pick(1));
         break;
-    case 3: // pick
+    case 4: // pick
         state.push(state.pick(state.pop()));
         break;
-    case 4: // sys
+    case 5: // sys
         user_sys(state);
         break;
-    case 5: // add
+    case 6: // add
         cell = state.pop();
         state.top() += cell;
         break;
-    case 6: // sub
+    case 7: // sub
         cell = state.pop();
         state.top() -= cell;
         break;
-    case 7: // mul ( n n -- d )
+    case 8: // mul ( n n -- d )
         cell = state.pop();
         dcell = state.pop() * cell;
         state.push(dcell);
         state.push(dcell >> (sizeof(Cell) * 8));
         break;
-    case 8: // div ( d n -- n )
+    case 9: // div ( d n -- n )
         cell = state.pop();
         dcell = state.pop() << (sizeof(Cell) * 8);
         dcell |= state.pop();
         state.push(dcell / cell);
         break;
-    case 9: // mod ( d n -- n )
+    case 10: // mod ( d n -- n )
         cell = state.pop();
         dcell = state.pop() << (sizeof(Cell) * 8);
         dcell |= state.pop();
         state.push(dcell % cell);
         break;
-    case 10: // peek
+    case 11: // peek
         if (state.pop())
             state.push(state.dict.read(state.pop()));
         else
             state.push(state.dict.readbyte(state.pop()));
         break;
-    case 11: // poke
+    case 12: // poke
         cell = state.pop();
         if (auto addr = state.pop(); cell)
             state.dict.write(addr, state.pop());
         else
             state.dict.writebyte(addr, state.pop());
         break;
-    case 12: // pushr
+    case 13: // pushr
         state.pushr(state.pop());
         break;
-    case 13: // popr
+    case 14: // popr
         state.push(state.popr());
         break;
-    case 14: // equal
+    case 15: // equal
         cell = state.pop();
         state.top() = state.top() == cell;
         break;
-    case 15: // lt
+    case 16: // lt
         cell = state.pop();
         state.top() = state.top() < cell;
         break;
-    case 16: // allot
-        state.dict.allot(state.pop());
-        break;
     case 17: // and
         cell = state.pop();
         state.top() &= cell;
@@ -165,32 +165,22 @@ void CoreWords::run(unsigned int index, State& state)
         state.dict.add(findi("exit"));
         state.compiling(false);
         break;
-    case 27: // here
-        state.push(state.dict.here);
-        break;
-    case 28: // _lit
-        state.push(state.beyondip());
-        break;
-    case 29: // literal
-        state.dict.add(findi("_lit"));
-        state.dict.add(state.pop());
-        break;
-    case 30: // _jmp
+    case 27: // _jmp
         state.ip = state.beyondip() - sizeof(Cell);
         break;
-    case 31: // _jmp0
+    case 28: // _jmp0
         if (state.pop())
             state.beyondip();
         else
             state.ip = state.beyondip() - sizeof(Cell);
         break;
-    case 32: // depth
+    case 29: // depth
         state.push(state.size());
         break;
-    case 33: // _rdepth
+    case 30: // _rdepth
         state.push(state.rsize());
         break;
-    case 34: // key
+    case 31: // key
         cell = state.dict.read(Dictionary::Input);
         while (cell <= 0) {
             state.input(state);
@@ -219,7 +209,7 @@ int CoreWords::findi(const char *word)
             ++end;
 
         if (size == end - i && !std::strncmp(word, wordsarr + i, size))
-            return wordsarr[end] == '\0' ? wordsi : (wordsi | Compiletime);
+            return wordsarr[end] == '\0' ? wordsi : (wordsi | Immediate);
 
         ++wordsi;
         i = end + 1;
@@ -239,7 +229,7 @@ int CoreWords::findi(State& state, Word word)
             ++end;
 
         if (state.dict.equal(word, wordsarr + i, end - i))
-            return wordsarr[end] == '\0' ? wordsi : (wordsi | Compiletime);
+            return wordsarr[end] == '\0' ? wordsi : (wordsi | Immediate);
 
         ++wordsi;
         i = end + 1;
index 4eb9d4202be74796a36e91fd78527ca9549b32b3..fcb00e889b5a6b55b9cefa86283d410c23048ec7 100644 (file)
@@ -27,10 +27,9 @@ void user_sys(State&);
 class CoreWords
 {
 public:
-    constexpr static std::size_t WordCount = 35;
+    constexpr static std::size_t WordCount = 32;
 
-    constexpr static Cell Immediate   = (1 << 5);
-    constexpr static Cell Compiletime = (1 << 6);
+    constexpr static Cell Immediate = (1 << 5);
 
     static int findi(const char *);
     static int findi(State&, Word);
@@ -40,13 +39,13 @@ private:
     // Ends with '\0': regular word
     // Ends with '\1': compile-only word
     constexpr static char wordsarr[] =
-        "drop\0dup\0swap\0pick\0sys\0"
+        "_lit\0drop\0dup\0swap\0pick\0sys\0"
         "+\0-\0m*\0_/\0_%\0"
         "_@\0_!\0>r\0r>\0=\0"
-        "<\0allot\0&\0|\0^\0"
+        "<\0&\0|\0^\0"
         "<<\0>>\0:\0'\0execute\0"
-        "exit\0;\1here\0_lit\0literal\1"
-        "_jmp\0_jmp0\0depth\0_rdepth\0key\0";
+        "exit\0;\1_jmp\0"
+        "_jmp0\0depth\0_rdepth\0key\0";
 };
 
 #endif // ALEEFORTH_COREWORDS_HPP
index cb2aba8523d0ad53ab98bbd2f08a72788b84a1a7..199f3ed16ee28cd163a56b7168623fc82c5af1d7 100644 (file)
@@ -23,8 +23,8 @@
 
 Addr Dictionary::allot(Cell amount) noexcept
 {
-    Addr old = here;
-    here += amount;
+    Addr old = here();
+    here(old + amount);
     return old;
 }
 
@@ -44,8 +44,8 @@ Addr Dictionary::aligned(Addr addr) const noexcept
 
 Addr Dictionary::alignhere() noexcept
 {
-    here = aligned(here);
-    return here;
+    here(aligned(here()));
+    return here();
 }
 
 void Dictionary::addDefinition(Word word) noexcept
index 9987e8f08af1f04d8f8ae88e9fe4f983ccab424e..1321875f8846cf7450ffa9bfad4b3ac8b68545b1 100644 (file)
@@ -28,14 +28,16 @@ class Dictionary
 {
 public:
     constexpr static Addr Base       = 0;
-    constexpr static Addr Latest     = sizeof(Cell);
-    constexpr static Addr Compiling  = sizeof(Cell) * 2;
-    constexpr static Addr Postpone   = sizeof(Cell) * 3;
-    constexpr static Addr Input      = sizeof(Cell) * 4; // len data...
+    constexpr static Addr Here       = sizeof(Cell);
+    constexpr static Addr Latest     = sizeof(Cell) * 2;
+    constexpr static Addr Compiling  = sizeof(Cell) * 3;
+    constexpr static Addr Postpone   = sizeof(Cell) * 4;
+    constexpr static Addr Input      = sizeof(Cell) * 5; // len data...
     constexpr static Addr InputCells = 80; // bytes!
-    constexpr static Addr Begin      = sizeof(Cell) * 5 + InputCells;
+    constexpr static Addr Begin      = sizeof(Cell) * 6 + InputCells;
 
-    Addr here = Begin;
+    Addr here() const noexcept { return read(Here); }
+    void here(Addr l) noexcept { write(Here, l); }
 
     Addr latest() const noexcept { return read(Latest); }
     void latest(Addr l) noexcept { write(Latest, l); }
index af5da6969d552411d4185339b9457f5cd1b80bad..91c4e5c7a76f4ddc8ab739dc77a90c1edc1088eb 100644 (file)
@@ -62,8 +62,8 @@ int Parser::parseWord(State& state, Word word)
         if (ins < 0) {
             return parseNumber(state, word);
         } else {
-            imm = ins & CoreWords::Compiletime;
-            ins &= ~CoreWords::Compiletime;
+            imm = ins & CoreWords::Immediate;
+            ins &= ~CoreWords::Immediate;
         }
     } else {
         imm = state.dict.read(ins) & CoreWords::Immediate;