aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2023-11-24 19:58:11 -0500
committerClyne Sullivan <clyne@bitgloo.com>2023-11-24 19:58:11 -0500
commit649785d46603182132209b64791f6c996497110e (patch)
treed8df15aca9e95fa75ab59772feeee7a8b69c2897
parent6e6b6841ec509549dcb5ed621342cbf21ae1ec94 (diff)
add text processing words
-rw-r--r--core.fth21
-rw-r--r--source/core.cpp24
-rw-r--r--source/core.hpp4
-rw-r--r--source/state.hpp16
-rw-r--r--sprit.cpp1
5 files changed, 49 insertions, 17 deletions
diff --git a/core.fth b/core.fth
index a7336ca..d55f71a 100644
--- a/core.fth
+++ b/core.fth
@@ -93,5 +93,22 @@
: 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"
diff --git a/source/core.cpp b/source/core.cpp
index c97203e..0b520e8 100644
--- a/source/core.cpp
+++ b/source/core.cpp
@@ -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;
diff --git a/source/core.hpp b/source/core.hpp
index d85cccc..914c39b 100644
--- a/source/core.hpp
+++ b/source/core.hpp
@@ -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. */
diff --git a/source/state.hpp b/source/state.hpp
index 2a4668e..17d8e69 100644
--- a/source/state.hpp
+++ b/source/state.hpp
@@ -22,15 +22,16 @@
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;
}
/**
diff --git a/sprit.cpp b/sprit.cpp
index 263766b..3da1423 100644
--- 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
}>()),