add text processing words

main
Clyne 10 months ago
parent 6e6b6841ec
commit 649785d466
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -93,5 +93,22 @@
: min 2dup <= if drop else swap drop then ; : min 2dup <= if drop else swap drop then ;
: max 2dup <= if swap drop else 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"

@ -19,6 +19,7 @@
#include "state.hpp" #include "state.hpp"
#include <cctype> #include <cctype>
#include <cstring>
void jump(FuncList ip) void jump(FuncList ip)
{ {
@ -82,13 +83,12 @@ void align()
HERE = aligned(HERE); HERE = aligned(HERE);
} }
void word() static void word(int ch)
{ {
// Skip leading whitespace.
int k; int k;
do { do {
k = key(); k = key();
} while (isspace(k)); } while (k == ch);
// Collect the word's text. // Collect the word's text.
char *ptr; char *ptr;
@ -101,7 +101,7 @@ void word()
break; break;
k = key(); k = key();
} while (!isspace(k)); } while (k != ch);
addkey(k); addkey(k);
// Add a null terminator. // Add a null terminator.
@ -110,12 +110,24 @@ void word()
++HERE; ++HERE;
} }
void wordword()
{
auto here = (char *)HERE;
++HERE;
word(*SP);
here[0] = strlen(here + 1);
HERE = (Cell)here;
*SP = HERE;
}
void colon() void colon()
{ {
// Collect (and store) the word's name. // Collect (and store) the word's name.
align(); align();
auto name = HERE; auto name = HERE;
word(); word(' ');
align(); align();
// Build the Word structure. // Build the Word structure.
@ -159,7 +171,7 @@ void tick()
{ {
// Get the name to look up. // Get the name to look up.
auto name = (char *)HERE; auto name = (char *)HERE;
word(); word(' ');
// Look up the name and push the result. // Look up the name and push the result.
int len = HERE - (Cell)name - 1; int len = HERE - (Cell)name - 1;

@ -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. */ Cell *comma(Cell n); /** Stores `n` to HERE++, returns `n`'s storage. */
Addr aligned(Addr addr); /** Aligns the given address and returns it. */ Addr aligned(Addr addr); /** Aligns the given address and returns it. */
void align(); /** Aligns HERE to the next Cell boundary. */ void align(); /** Aligns HERE to the next Cell boundary. */
void word(); /** Gets word from source buffer, stores and allots from HERE. */ void wordword(); /** Definition of WORD. */
void colon(); /** Begins definition of a new word. */ void colon(); /** Begins definition of a new word. */
void semic(); /** Ends the current word definition which becomes new LATEST. */ void semic(); /** Ends the current word definition which becomes new LATEST. */
void tick(); /** Gets the execution token for the source buffer's next word. */ void tick(); /** Gets the execution token for the source buffer's next word. */

@ -22,15 +22,16 @@
constexpr Addr DS = 16; /** Data stack size */ constexpr Addr DS = 16; /** Data stack size */
constexpr Addr RS = 16; /** Return 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 DIdxBase = 0;
constexpr Addr DIdxHere = 1; constexpr Addr DIdxHere = 1;
constexpr Addr DIdxLatest = 2; constexpr Addr DIdxLatest = 2;
constexpr Addr DIdxState = 3; constexpr Addr DIdxState = 3;
constexpr Addr DIdxSource = 4; constexpr Addr DIdxCompXt = 4;
constexpr Addr DIdxSrcLen = 5; constexpr Addr DIdxSource = 5;
constexpr Addr DIdxInBuf = 6; constexpr Addr DIdxSrcLen = 6;
constexpr Addr DIdxInBuf = 7;
constexpr Addr DIdxBegin = DIdxInBuf + 80 * sizeof(char); constexpr Addr DIdxBegin = DIdxInBuf + 80 * sizeof(char);
/** /**
@ -52,9 +53,10 @@ extern FuncList IP; /** Instruction pointer */
*/ */
inline void initialize(const auto& wordset) inline void initialize(const auto& wordset)
{ {
LATEST = (Cell)wordset.latest; DICT[DIdxBase] = 10;
HERE = (Cell)&DICT[DIdxBegin]; DICT[DIdxHere] = (Cell)&DICT[DIdxBegin];
STATE = 0; DICT[DIdxLatest] = (Cell)wordset.latest;
DICT[DIdxState] = 0;
} }
/** /**

@ -65,6 +65,7 @@ constinit WordSet words (
Word("_i", WordWrap<[] { *SP = ((Word *)*SP)->immediate(); }, tobool>()), Word("_i", WordWrap<[] { *SP = ((Word *)*SP)->immediate(); }, tobool>()),
Word("[']", WordWrap<tick, compileliteral>()).markImmediate(), Word("[']", WordWrap<tick, compileliteral>()).markImmediate(),
Word("compile,", WordWrap<peek, commaSP>()), Word("compile,", WordWrap<peek, commaSP>()),
Word("word", WordWrap<wordword>()),
Word("_b", WordWrap<[] { Word("_b", WordWrap<[] {
std::putchar('#'); // Gives a good breakpoint spot for gdb std::putchar('#'); // Gives a good breakpoint spot for gdb
}>()), }>()),

Loading…
Cancel
Save