Copying a snippet into HTML is where a documented language stops being the real one. Each block on the page is a program here with its recorded output beside it, and check.sh is what says the page is still true after a change.
18 lines
508 B
Plaintext
18 lines
508 B
Plaintext
(defstruct Cursor
|
|
[src [u8] ; a non-owning slice
|
|
pos i32]) ; no initialiser means zeroed
|
|
|
|
(defn peek [c (Ptr Cursor)] u8
|
|
(if (< (.pos c) (len (.src c)))
|
|
(at (.src c) (.pos c))
|
|
0))
|
|
|
|
(defn advance [c (Ptr Cursor)]
|
|
(set (.pos c) (+ (.pos c) 1))) ; field access derefs one level
|
|
|
|
(defn main []
|
|
(let [c (Cursor {:src (bytes "hi")})] ; pos omitted, so pos is 0
|
|
(print-i64 (i64 (peek (addr c)))) (newline)
|
|
(advance (addr c))
|
|
(print-i64 (i64 (peek (addr c)))) (newline)))
|