Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strings lib #338

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/long-strings.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require ./formatted-output.fs

\ override place, count and +place
\ with a cell-counted version that
\ allows for 255+ character strings

: place ( c-addr1 u1 c-addr2 -- )
over >r rot over cell+ r> move ! ;

: count ( c-addr1 -- c-addr2 u )
dup cell+ swap @ ;

: +place ( c-addr1 u1 c-addr2 -- )
over over >r >r
count chars +
swap chars move
r> r@ @ + r> ! ;

\ override additional words from strings lib
\ to work with redefined place and +place

: c+place ( c c-addr )
dup @ char+ over !
count 1- chars + ! ;

: #+place ( u c-addr -- )
swap
dup abs <# #s swap sign #>
rot +place ;

\ define an additional word wrap. to display
\ long text wrapped within the screen. returns
\ the remaining addr' u' portion if the text
\ does not fit on one page, or 0 0 if all text
\ was written to the screen

: wrap. ( addr u -- addr' u' )
begin
dup SCRN_X_B >
while
over SCRN_X_B
begin 1- 2dup + c@ bl = until
dup 1+ >r
begin 1- 2dup + c@ bl <> until
1+ type cr
r> /string
cursor-y @ SCRN_Y_B = if exit then
repeat type cr 0 0 ;
15 changes: 15 additions & 0 deletions lib/strings.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require ./formatted-output.fs

: toupper ( c -- c )
dup
[ char a ]L [ char z 1+ ]L within
if 32 - then ;

: c+place ( c c-addr -- )
dup c@ char+ over c!
count 1- chars + c! ;

: #+place ( u c-addr -- )
swap
dup abs <# #s swap sign #>
rot +place ;
11 changes: 11 additions & 0 deletions test/libs/test-strings.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require strings.fs

create note 100 chars allot

: main
s" Forth " note place
4 note #+place
s" the " note +place
[char] g toupper note c+place
[char] B toupper note c+place
note ;
13 changes: 13 additions & 0 deletions test/libs/test-strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const gb = require("../gbtest")(__filename);

const readCountedString = (addr) => {
const len = gb.memory[addr]
const chars = gb.memory.slice(addr + 1, addr + 1 + len)
return String.fromCharCode(...chars)
}

test("strings-ext", () => {
gb.run();
expect(gb.stack.length).toBe(1);
expect(readCountedString(gb.stack[0])).toBe("Forth 4 the GB")
});