You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/pitfall/restructure/pointer.rkt

49 lines
1.7 KiB
Racket

8 years ago
#lang restructure/racket
(provide (all-defined-out))
8 years ago
#|
approximates
https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
|#
8 years ago
(define-subclass RestructureBase (Pointer offsetType type [scope 'local])
(and (symbol? scope) (caseq scope
[(local parent immediate global) 'yay]
[else (raise-argument-error 'Pointer "local or parent or immediate" scope)]))
8 years ago
(define/augride (decode stream [ctx #f])
(define offset (send offsetType decode stream ctx))
(report scope 'pointer-scope)
(define relative (caseq scope
[(local) (· ctx _startOffset)]
[(parent) (· ctx parent _startOffset)]
[(immediate) (- (· stream pos) (send offsetType size))]
[(global)
(let loop ([c ctx])
(cond
[(· c parent) => loop]
[else (or (· c starting-offset) 0)]))]))
(report* (· this _startOffset) (and ctx (· ctx _startOffset)))
(report* offset relative)
(define ptr (+ offset relative))
(report* ptr)
(report (send offsetType size) 'size)
(cond
[type (define orig-pos (send stream pos))
(send stream pos ptr)
(define val (send type decode stream ctx))
(send stream pos orig-pos)
val]
[else ptr]))
(define/override (encode stream val)
(error 'Pointer-encode-not-done))
(define/override (size [val #f] [ctx #f])
(error 'Pointer-size-not-done)
(report* this offsetType type (send type size)))
)