]> code.bitgloo.com Git - clyne/sprit-forth.git/commitdiff
add text processing words
authorClyne Sullivan <clyne@bitgloo.com>
Sat, 25 Nov 2023 00:58:11 +0000 (19:58 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sat, 25 Nov 2023 00:58:11 +0000 (19:58 -0500)
core.fth
source/core.cpp
source/core.hpp
source/state.hpp
sprit.cpp

index a7336cadaf5730f0952b44969cfae6806fa9a49c..d55f71a387d3c8d5ca1f8a2a22831e0e12ab7434 100644 (file)
--- a/core.fth
+++ b/core.fth
 : min      2dup <= if drop else swap drop then ;
 : max      2dup <= if swap drop else drop then ;
 
-5 spaces
-
+: source   _source @ _sourceu @ ;
+: count    dup char+ swap c@ ;
+: char     0 here char+ c! bl word char+ c@ ;
+: [char]   char postpone literal ; imm
+
+: (        begin [char] ) key <> while repeat ; imm
+
+: _type    >r begin dup 0 > while
+           swap dup c@ r@ execute char+ swap 1- repeat 2drop r> drop ;
+: type     [ ' emit ] literal _type ;
+: s"       state @ if ['] _jmp compile, here 0 , then
+           [char] " word count
+           state @ 0= if exit then
+           dup cell+ allot
+           rot here swap !
+           swap postpone literal postpone literal ; imm
+: ."       postpone s" state @ if ['] type compile, else type then ; imm
+
+." hello world"
index c97203e196114c628bb62dbf8cb538f527379a7a..0b520e8131709096feff18982d5d760467e53858 100644 (file)
@@ -19,6 +19,7 @@
 #include "state.hpp"
 
 #include <cctype>
+#include <cstring>
 
 void jump(FuncList ip)
 {
@@ -82,13 +83,12 @@ void align()
     HERE = aligned(HERE);
 }
 
-void word()
+static void word(int ch)
 {
-    // Skip leading whitespace.
     int k;
     do {
         k = key();
-    } while (isspace(k));
+    } while (k == ch);
 
     // Collect the word's text.
     char *ptr;
@@ -101,7 +101,7 @@ void word()
             break;
 
         k = key();
-    } while (!isspace(k));
+    } while (k != ch);
     addkey(k);
 
     // Add a null terminator.
@@ -110,12 +110,24 @@ void word()
     ++HERE;
 }
 
+void wordword()
+{
+    auto here = (char *)HERE;
+    ++HERE;
+
+    word(*SP);
+
+    here[0] = strlen(here + 1);
+    HERE = (Cell)here;
+    *SP = HERE;
+}
+
 void colon()
 {
     // Collect (and store) the word's name.
     align();
     auto name = HERE;
-    word();
+    word(' ');
     align();
 
     // Build the Word structure.
@@ -159,7 +171,7 @@ void tick()
 {
     // Get the name to look up.
     auto name = (char *)HERE;
-    word();
+    word(' ');
 
     // Look up the name and push the result.
     int len = HERE - (Cell)name - 1;
index d85cccc51d5d80bf392b6a710e9d302f5f95f243..914c39bd9a5cdaaca6dbb26c96ad3571b228a58b 100644 (file)
@@ -44,8 +44,8 @@ int key();          /** Gets the next key available from the source buffer. */
 
 Cell *comma(Cell n);     /** Stores `n` to HERE++, returns `n`'s storage. */
 Addr aligned(Addr addr); /** Aligns the given address and returns it. */
-void align(); /** Aligns HERE to the next Cell boundary. */
-void word();  /** Gets word from source buffer, stores and allots from HERE. */
+void align();    /** Aligns HERE to the next Cell boundary. */
+void wordword(); /** Definition of WORD. */
 void colon(); /** Begins definition of a new word. */
 void semic(); /** Ends the current word definition which becomes new LATEST. */
 void tick();  /** Gets the execution token for the source buffer's next word. */
index 2a4668e265506e4d779ed06467b6c8e0252e6a96..17d8e694ddcd293f4913df2f56d7a73b0d0dd376 100644 (file)
 
 constexpr Addr DS = 16; /** Data stack size */
 constexpr Addr RS = 16; /** Return stack size */
-constexpr auto DictSize = 2048u; /** Dictionary size */
+constexpr auto DictSize = 4096u; /** Dictionary size */
 
 constexpr Addr DIdxBase   = 0;
 constexpr Addr DIdxHere   = 1;
 constexpr Addr DIdxLatest = 2;
 constexpr Addr DIdxState  = 3;
-constexpr Addr DIdxSource = 4;
-constexpr Addr DIdxSrcLen = 5;
-constexpr Addr DIdxInBuf  = 6;
+constexpr Addr DIdxCompXt = 4;
+constexpr Addr DIdxSource = 5;
+constexpr Addr DIdxSrcLen = 6;
+constexpr Addr DIdxInBuf  = 7;
 constexpr Addr DIdxBegin  = DIdxInBuf + 80 * sizeof(char);
 
 /**
@@ -52,9 +53,10 @@ extern FuncList IP; /** Instruction pointer */
  */
 inline void initialize(const auto& wordset)
 {
-    LATEST = (Cell)wordset.latest;
-    HERE = (Cell)&DICT[DIdxBegin];
-    STATE = 0;
+    DICT[DIdxBase]   = 10;
+    DICT[DIdxHere]   = (Cell)&DICT[DIdxBegin];
+    DICT[DIdxLatest] = (Cell)wordset.latest;
+    DICT[DIdxState]  = 0;
 }
 
 /**
index 263766b49b5a80d39606c51ac0302f60688e7172..3da1423d1d1d3d7f7fb518b4b3e30ca57ec6bd02 100644 (file)
--- a/sprit.cpp
+++ b/sprit.cpp
@@ -65,6 +65,7 @@ constinit WordSet words (
     Word("_i",        WordWrap<[] { *SP = ((Word *)*SP)->immediate(); }, tobool>()),
     Word("[']",       WordWrap<tick, compileliteral>()).markImmediate(),
     Word("compile,",  WordWrap<peek, commaSP>()),
+    Word("word",      WordWrap<wordword>()),
     Word("_b",        WordWrap<[] {
         std::putchar('#'); // Gives a good breakpoint spot for gdb
     }>()),