#lang scheme/base ;; Serialized expression form that supports prefixed byte strings. To ;; keep things simple, other data atoms, including cons cells, are ;; prefixed too. ;; ;; * all tags are newline terminated ;; * supported types: ;; * atom = standard inter-lisp read compatible ;; * cons / null ;; * bytes ;; ;; The 'inter-lisp' thing is not really specified, but it contains ;; decimal numbers and symbols, with extensions depending on the ;; application. (require (lib "match.ss")) (provide flat-read flat-write) (define (flat-read (port (current-input-port))) (case (read port) ((c) (cons (flat-read port) (flat-read port))) ((a) (read port)) ((n) '()) ((b) (let ((n (read port))) (read-bytes 1 port) ;; skip whitespace (read-bytes n port))) (else (raise-user-error 'flat-syntax-error)))) (define (flat-write datum (p (current-output-port))) (define fw flat-write) (define d display) (cond ((pair? datum) (d "c " p) (fw (car datum) p) (fw (cdr datum) p)) ((null? datum) (d "n\n" p)) ((bytes? datum) (d (format "b ~a " (bytes-length datum)) p) (d datum p) (d " " p)) (else (d (format "a ~s " datum) p))))