Skip to content

Instantly share code, notes, and snippets.

@UA3MQJ
Created October 15, 2024 14:05
Show Gist options
  • Save UA3MQJ/d7dec066b3f4b3d06b0dc80acc7cf0b3 to your computer and use it in GitHub Desktop.
Save UA3MQJ/d7dec066b3f4b3d06b0dc80acc7cf0b3 to your computer and use it in GitHub Desktop.
GForth structs
https://gist.github.com/tkurtbond/b629cdff2d70e31b0e1527f1ead14462
https://gforth.org/manual/Gforth-structs.html
\ sllist-less-comments.fs - Реализация односвязного списка в forth с использованием структур gforth, и немного комментариев.
struct
cell% field node-text \ address of counted string - адрес строки
cell% field node-next \ address of next node - адрес следующей ноды.
end-struct node%
: node-init ( text-addr next-addr node-addr -- ) tuck node-next ! node-text ! ;
: node-type ( node-addr -- ) node-text @ count type ;
: node-traverse ( first-addr -- )
begin dup @ 0<> while cr dup @ node-type @ node-next repeat drop
;
: node-text-compare ( node1-addr node2-addr -- flag )
node-text @ count rot node-text @ count 2swap compare
;
: node-insert ( new-node-addr p-addr )
dup @ 0= if ! exit then
2dup @ node-text-compare -1 = if 2dup @ swap node-next ! ! exit then
begin
dup @ node-next @ 0<>
while
2dup @ node-text-compare 1 =
while
@ node-next
repeat then
2dup @ node-next @ swap
node-next !
@ node-next !
;
\ Build the initial list manually.
\ Create some strings.
create s1 ," one"
create s2 ," two"
create s3 ," three"
s1 ok 1
.s <1> 139881123671816 ok 1
count ok 2
.s <2> 139881123671817 3 ok 2
type one ok
\ Create some nodes and initialize them, making links between them.
node% %allot constant n1
node% %allot constant n2
node% %allot constant n3
\ ( text-addr next-addr node-addr -- )
s3 0 n3 node-init
s2 n3 n2 node-init
s1 n2 n1 node-init
\ указатель на список (на первый элемент списка)
variable l1
n1 l1 !
\ создал ноду
node% %allot constant n4
\ : node-init ( text-addr next-addr node-addr -- ) tuck node-next ! node-text ! ;
\ запись данных
0 n4 node-next !
s1 n4 node-text !
\ получение данных
\ ( node-addr -- ) <имя поля> @
n4 node-next @ .
n4 node-text @ count type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment