forth-riscv

My forth
git clone git://git.electrosoup.com/forth-riscv
Log | Files | Refs

commit 4999fdd21730919a8723fd4bf8a9bebc737651ea
parent 6bc69c7f6e2b45bea9f15ebbc98bdba576999790
Author: Christian Ermann <christianermann@gmail.com>
Date:   Tue,  5 Nov 2024 10:50:03 -0800

Add string literals

Diffstat:
Mforth.f | 21+++++++++++++++++++++
Mforth.s | 27+++++++++++++++++++++++----
2 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/forth.f b/forth.f @@ -19,3 +19,24 @@ : \ begin parse-char while repeat ; immediate : ( begin parse-char dup while ')' = if exit then repeat ; immediate +: '"' [ parse-char " ] literal ; + +: parse-string + here + begin + parse-char dup dup '"' <> and + while + over c! 1 + + repeat + drop here - here swap ; + +: " + compiling? if + postpone lit-string ahead + then + parse-string + compiling? if + swap over + dp ! align + swap ! + then ; immediate + diff --git a/forth.s b/forth.s @@ -540,7 +540,19 @@ defcode "lit", 3, 0, 0x404CD5B6, lit, latest push w next -defword "allot", 5, 0, 0xADB1A69F, allot, lit +defcode "lit-string", 10, 0, 0xC7BE567C, lit_string, lit + load_cell x, 0(ip) # len + addi ip, ip, cell + mv w, ip # addr + add ip, ip, x + # re-align ip + addi ip, ip, cell - 1 + andi ip, ip, -cell + push w + push x + next + +defword "allot", 5, 0, 0xADB1A69F, allot, lit_string .int dp, add_store .int exit @@ -554,7 +566,7 @@ defword "c,", 2, 0, 0xA32A0A5A, char_comma, comma .int lit, 1, allot # increment 'here' .int exit -defcode "align", 5, 0, 0xC73174DF, align, char_comma +defcode "align", 5, 0, 0x602C63DE, align, char_comma la w, _here load_cell x, 0(w) addi x, x, 3 @@ -634,7 +646,7 @@ defcode "2dup", 3, 0, 0x5E46F4D6, two_dup, nip # math and logic # ----------------------------------------------------------------------------- -defcode "+", 1, 0, 0xC4ADC675, plus, two_dup +defcode "+", 1, 0, 0x2E0C9DAA, plus, two_dup pop w pop x add w, w, x @@ -655,7 +667,14 @@ defcode "or", 2, 0, 0x5D342984, or_, minus push w next -defcode "aligned", 7, 0, 0xC73174DF, aligned, or_ +defcode "and", 3, 0, 0x0F29C2A6, and_, or_ + pop w + pop x + and w, w, x + push w + next + +defcode "aligned", 7, 0, 0xC73174DF, aligned, and_ pop w addi w, w, 3 andi w, w, 0xFFFFFFFC