retroforth/example/defstruct.forth
crc e71709b303 begin work on 2018.6; this release *will* make some changes that break existing code in small ways
FossilOrigin-Name: 2ea7e4d5f74070041c454af65713a478ebe2a9d71bbc9e6bb6add6c256351765
2018-04-25 16:51:46 +00:00

38 lines
925 B
Forth

LISP provides a function, `defstruct`, which creates a data
structure and functions for accessing various fields in it.
This can be useful, so I'm doing something similar here.
(defstruct book title author subject book-id )
~~~
{{
:make-helper (nsq-) [ d:create , ] dip does ;
:make-struct (ns-) d:create , [ here swap fetch allot ] does ;
---reveal---
:defstruct (sq-)
set:from-results dup set:length
[ n:dec swap
[ 'ab 'aabab reorder
'@ s:append [ fetch + fetch ] make-helper
'! s:append [ fetch + store ] make-helper
n:dec
] set:for-each drop
] sip swap make-struct ;
}}
~~~
~~~
'book [ 'title 'author 'subject 'book-id ] defstruct
book 'A const
'The_Hobbit s:keep &A title!
'J.R.R._Tolkien s:keep &A author!
'Fantasy s:keep &A subject!
:info (a-)
[ subject@ ] [ author@ ] [ title@ ] tri
'%s_by_%s_is_a_%s_book. s:format puts nl ;
&A info
~~~