2 name: (handle array byte)
3 posts: (handle array int) # item indices
8 id: (handle array byte)
9 name: (handle array byte)
10 real-name: (handle array byte)
11 avatar: (handle image)
15 id: (handle array byte)
16 channel: (handle array byte)
18 text: (handle array byte)
19 parent: int # item index
20 comments: (handle array int)
21 comments-first-free: int
25 data: (handle array item)
30 # users: (handle array user)
31 # channels: (handle array channel)
32 # items: (handle array item)
36 # user -> posts|comments
38 # comment -> post|comments
39 # keywords -> posts|comments
41 # static buffer sizes in this program:
43 # data-size-in-sectors
51 fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk) {
52 # load entire disk contents to a single enormous stream
53 var s-h: (handle stream byte) # the stream is too large to put on the stack
54 var s-ah/eax: (addr handle stream byte) <- address s-h
55 populate-stream s-ah, 0x4000000/data-size
56 var _s/eax: (addr stream byte) <- lookup *s-ah
57 var s/ebx: (addr stream byte) <- copy _s
58 var sector-count/eax: int <- copy 0x400 # test_data
59 #? var sector-count/eax: int <- copy 0x20000 # largest size tested; slow
60 set-cursor-position 0/screen, 0x20/x 0/y # aborts clobber the screen starting x=0
61 draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, "loading ", 3/fg 0/bg
62 draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen screen, sector-count, 3/fg 0/bg
63 draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, " sectors from data disk..", 3/fg 0/bg
64 load-sectors data-disk, 0/lba, sector-count, s
65 draw-text-wrapping-right-then-down-from-cursor-over-full-screen screen, "done", 3/fg 0/bg
66 # parse global data structures out of the stream
67 var users-h: (handle array user)
68 var users-ah/eax: (addr handle array user) <- address users-h
69 populate users-ah, 0x800/num-users
70 var _users/eax: (addr array user) <- lookup *users-ah
71 var users/edi: (addr array user) <- copy _users
72 var channels-h: (handle array channel)
73 var channels-ah/eax: (addr handle array channel) <- address channels-h
74 populate channels-ah, 0x20/num-channels
75 var _channels/eax: (addr array channel) <- lookup *channels-ah
76 var channels/esi: (addr array channel) <- copy _channels
77 var items-storage: item-list
78 var items/edx: (addr item-list) <- address items-storage
79 var items-data-ah/eax: (addr handle array item) <- get items, data
80 populate items-data-ah, 0x10000/num-items
81 parse s, users, channels, items
83 var env-storage: environment
84 var env/ebx: (addr environment) <- address env-storage
85 initialize-environment env, items
87 render-environment screen, env, users, channels, items
89 var key/eax: byte <- read-key keyboard
92 update-environment env, key, users, channels, items
98 fn parse in: (addr stream byte), users: (addr array user), channels: (addr array channel), _items: (addr item-list) {
99 var items/esi: (addr item-list) <- copy _items
100 var items-data-ah/eax: (addr handle array item) <- get items, data
101 var _items-data/eax: (addr array item) <- lookup *items-data-ah
102 var items-data/edi: (addr array item) <- copy _items-data
103 # 'in' consists of a long, flat sequence of records surrounded by parens
104 var record-storage: (stream byte 0x18000)
105 var record/ecx: (addr stream byte) <- address record-storage
106 var user-idx/edx: int <- copy 0
107 var item-idx/ebx: int <- copy 0
109 var done?/eax: boolean <- stream-empty? in
110 compare done?, 0/false
112 var c/eax: byte <- peek-byte in
115 set-cursor-position 0/screen, 0x20/x 1/y
116 draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "parsed " 3/fg 0/bg
117 draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, user-idx, 3/fg 0/bg
118 draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, " users, " 3/fg 0/bg
119 draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, item-idx, 3/fg 0/bg
120 draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, " posts/comments" 3/fg 0/bg
122 parse-record in, record
123 var user?/eax: boolean <- user-record? record
125 compare user?, 0/false
127 parse-user record, users, user-idx
128 user-idx <- increment
131 compare user?, 0/false
133 parse-item record, channels, items-data, item-idx
134 item-idx <- increment
138 var dest/eax: (addr int) <- get items, data-first-free
139 copy-to *dest, item-idx
142 fn parse-record in: (addr stream byte), out: (addr stream byte) {
143 var paren/eax: byte <- read-byte in
144 compare paren, 0x28/open-paren
147 set-cursor-position 0/screen, 0x20 0x10
148 var c/eax: int <- copy paren
149 draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen c, 5/fg 0/bg
150 abort "parse-record: ("
152 var paren-int/eax: int <- copy paren
153 append-byte out, paren-int
156 var eof?/eax: boolean <- stream-empty? in
157 compare eof?, 0/false
159 abort "parse-record: truncated; increase the sector-count to load from disk"
161 var c/eax: byte <- read-byte in
163 var c-int/eax: int <- copy c
164 append-byte out, c-int
166 compare c, 0x29/close-paren
168 compare c, 0x22/double-quote
171 slurp-json-string in, out
175 skip-chars-matching-whitespace in
178 fn user-record? record: (addr stream byte) -> _/eax: boolean {
180 var c/eax: byte <- read-byte record # skip paren
181 var c/eax: byte <- read-byte record # skip double quote
182 var c/eax: byte <- read-byte record
192 fn parse-user record: (addr stream byte), _users: (addr array user), user-idx: int {
193 var users/esi: (addr array user) <- copy _users
194 var offset/eax: (offset user) <- compute-offset users, user-idx
195 var user/esi: (addr user) <- index users, offset
197 var s-storage: (stream byte 0x100)
198 var s/ecx: (addr stream byte) <- address s-storage
201 var paren/eax: byte <- read-byte record
202 compare paren, 0x28/open-paren
205 abort "parse-user: ("
208 skip-chars-matching-whitespace record
209 var double-quote/eax: byte <- read-byte record
210 compare double-quote, 0x22/double-quote
213 abort "parse-user: id"
215 next-json-string record, s
216 var dest/eax: (addr handle array byte) <- get user, id
217 stream-to-array s, dest
219 skip-chars-matching-whitespace record
220 var double-quote/eax: byte <- read-byte record
221 compare double-quote, 0x22/double-quote
224 abort "parse-user: name"
227 next-json-string record, s
228 var dest/eax: (addr handle array byte) <- get user, name
229 stream-to-array s, dest
231 skip-chars-matching-whitespace record
232 var double-quote/eax: byte <- read-byte record
233 compare double-quote, 0x22/double-quote
236 abort "parse-user: real-name"
239 next-json-string record, s
240 var dest/eax: (addr handle array byte) <- get user, real-name
241 stream-to-array s, dest
243 skip-chars-matching-whitespace record
244 var open-bracket/eax: byte <- read-byte record
245 compare open-bracket, 0x5b/open-bracket
248 abort "parse-user: avatar"
250 skip-chars-matching-whitespace record
251 var c/eax: byte <- peek-byte record
253 compare c, 0x5d/close-bracket
255 var dest-ah/eax: (addr handle image) <- get user, avatar
257 var dest/eax: (addr image) <- lookup *dest-ah
258 initialize-image dest, record
262 fn parse-item record: (addr stream byte), _channels: (addr array channel), _items: (addr array item), item-idx: int {
263 var items/esi: (addr array item) <- copy _items
264 var offset/eax: (offset item) <- compute-offset items, item-idx
265 var item/edi: (addr item) <- index items, offset
267 var s-storage: (stream byte 0x40)
268 var s/ecx: (addr stream byte) <- address s-storage
271 var paren/eax: byte <- read-byte record
272 compare paren, 0x28/open-paren
275 abort "parse-item: ("
278 skip-chars-matching-whitespace record
279 var double-quote/eax: byte <- read-byte record
280 compare double-quote, 0x22/double-quote
283 abort "parse-item: id"
285 next-json-string record, s
286 var dest/eax: (addr handle array byte) <- get item, id
287 stream-to-array s, dest
290 var word-slice-storage: slice
291 var word-slice/ecx: (addr slice) <- address word-slice-storage
292 next-word record, word-slice
293 var src/eax: int <- parse-decimal-int-from-slice word-slice
296 var dest/edx: (addr int) <- get item, parent
298 # cross-link to parent
299 var parent-offset/eax: (offset item) <- compute-offset items, src
300 var parent-item/esi: (addr item) <- index items, parent-offset
301 var parent-comments-ah/ebx: (addr handle array int) <- get parent-item, comments
302 var parent-comments/eax: (addr array int) <- lookup *parent-comments-ah
303 compare parent-comments, 0
306 populate parent-comments-ah, 0x200/num-comments
307 parent-comments <- lookup *parent-comments-ah
309 var parent-comments-first-free-addr/edi: (addr int) <- get parent-item, comments-first-free
310 var parent-comments-first-free/edx: int <- copy *parent-comments-first-free-addr
311 var dest/eax: (addr int) <- index parent-comments, parent-comments-first-free
312 var src/ecx: int <- copy item-idx
314 increment *parent-comments-first-free-addr
317 skip-chars-matching-whitespace record
318 var double-quote/eax: byte <- read-byte record
319 compare double-quote, 0x22/double-quote
322 abort "parse-item: channel"
325 next-json-string record, s
326 var dest/eax: (addr handle array byte) <- get item, channel
327 stream-to-array s, dest
328 #? set-cursor-position 0/screen, 0x70 item-idx
329 # cross-link to channels
331 var channels/esi: (addr array channel) <- copy _channels
332 var channel-index/eax: int <- find-or-insert channels, s
333 #? draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, channel-index, 5/fg 0/bg
334 var channel-offset/eax: (offset channel) <- compute-offset channels, channel-index
335 var channel/eax: (addr channel) <- index channels, channel-offset
337 #? var foo/eax: int <- copy channel
338 #? draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, foo, 4/fg 0/bg
340 var channel-posts-ah/ecx: (addr handle array int) <- get channel, posts
341 var channel-posts-first-free-addr/edx: (addr int) <- get channel, posts-first-free
343 #? var foo/eax: int <- copy channel-posts-first-free-addr
344 #? draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, foo, 4/fg 0/bg
346 var channel-posts-first-free/ebx: int <- copy *channel-posts-first-free-addr
347 #? draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, channel-posts-first-free, 3/fg 0/bg
348 var channel-posts/eax: (addr array int) <- lookup *channel-posts-ah
349 var dest/eax: (addr int) <- index channel-posts, channel-posts-first-free
350 var src/ecx: int <- copy item-idx
352 increment *channel-posts-first-free-addr
356 var word-slice-storage: slice
357 var word-slice/ecx: (addr slice) <- address word-slice-storage
358 next-word record, word-slice
359 var src/eax: int <- parse-decimal-int-from-slice word-slice
360 var dest/edx: (addr int) <- get item, by
364 var s-storage: (stream byte 0x4000) # message-text-limit
365 var s/ecx: (addr stream byte) <- address s-storage
366 skip-chars-matching-whitespace record
367 var double-quote/eax: byte <- read-byte record
368 compare double-quote, 0x22/double-quote
371 abort "parse-item: text"
373 next-json-string record, s
374 var dest/eax: (addr handle array byte) <- get item, text
375 stream-to-array s, dest
378 fn find-or-insert _channels: (addr array channel), name: (addr stream byte) -> _/eax: int {
379 var channels/esi: (addr array channel) <- copy _channels
380 var i/ecx: int <- copy 0
381 var max/edx: int <- length channels
385 var offset/eax: (offset channel) <- compute-offset channels, i
386 var curr/ebx: (addr channel) <- index channels, offset
387 var curr-name-ah/edi: (addr handle array byte) <- get curr, name
388 var curr-name/eax: (addr array byte) <- lookup *curr-name-ah
393 stream-to-array name, curr-name-ah
394 var posts-ah/eax: (addr handle array int) <- get curr, posts
395 populate posts-ah, 0x8000/channel-capacity
398 var found?/eax: boolean <- stream-data-equal? name, curr-name
400 compare found?, 0/false
407 abort "out of channels"
411 # includes trailing double quote
412 fn slurp-json-string in: (addr stream byte), out: (addr stream byte) {
413 # open quote is already slurped
416 var eof?/eax: boolean <- stream-empty? in
417 compare eof?, 0/false
419 abort "slurp-json-string: truncated"
421 var c/eax: byte <- read-byte in
423 var c-int/eax: int <- copy c
424 append-byte out, c-int
426 compare c, 0x22/double-quote
428 compare c, 0x5c/backslash
433 var c-int/eax: int <- copy c
434 append-byte out, c-int
440 # drops trailing double quote
441 fn next-json-string in: (addr stream byte), out: (addr stream byte) {
442 # open quote is already read
445 var eof?/eax: boolean <- stream-empty? in
446 compare eof?, 0/false
448 abort "next-json-string: truncated"
450 var c/eax: byte <- read-byte in
451 compare c, 0x22/double-quote
454 var c-int/eax: int <- copy c
455 append-byte out, c-int
457 compare c, 0x5c/backslash
462 var c-int/eax: int <- copy c
463 append-byte out, c-int