add text processing words

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

@ -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"

@ -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;

@ -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. */

@ -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;
}
/**

@ -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
}>()),

Loading…
Cancel
Save