Commits

Anonymous committed ba5d3ec

Fix read-list (it didn't reverse the list after constructing it). Rename read-hash to read-dict, rewrite it to make ensure 3rd element is a(n empty) list.

Comments (0)

Files changed (1)

                  (rename 
                   (rnrs arithmetic bitwise) 
                   (bitwise-and band)
+                  (bitwise-ior bor)
                   (bitwise-arithmetic-shift-left bsl)
-                  (bitwise-arithmetic-shift-right bsr)
-                  (bitwise-ior bor))
+                  (bitwise-arithmetic-shift-right bsr))
                  (rnrs arithmetic fixnums)                 
                  (rnrs arithmetic flonums)
                  (rnrs r5rs)
                             (vector 'bert 'dict
                                     (vector->list
                                      (vector-map
-                                      (lambda (k v) (vector (convert k) (convert v)))
+                                      (lambda (k v) 
+                                        (vector (convert k) (convert v)))
                                       a b))))))
          
          (define (write-any-raw outpr obj)
                  (string-append (if (< float 0) "-" "")                                
                                 (insert-decimal-exp place strdigits)))))
          
-         ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+         ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
          ;; These are routines from
          ;;  "Printing Floating-Point Numbers Quickly and Accurately"
          ;; by Robert G. Burger and R. Kent Dybvig.
          ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
          ;; Decoder part
          
-         (define (decode bytes)
-           (read-any (open-bytevector-input-port bytes)))
+         (define (decode bytevector)
+           (read-any (open-bytevector-input-port bytevector)))
          
          (define (read-any inp)
            (if (eq? (read-small-int inp) MAGIC)
          (define (read-short inp)
            (bytevector-u16-ref (read-2 inp) 0 (endianness big)))
          
+         (define (read-small-int inp)
+           (bytevector-u8-ref (read-1 inp) 0))
+         
+         (define (read-int inp)
+           (bytevector-s32-ref (read-4 inp) 0 (endianness big)))
+         
          (define (read-str inp length)
            (bytevector->string (read-n inp length) 
                                (native-transcoder)))
          
          (define (read-atom inp)
-           (let ((len (read-short inp)))
-             (string->symbol (read-str inp len))))
-         
-         (define (read-small-int inp)
-           (bytevector-u8-ref (read-1 inp) 0))
-         
-         (define (read-int inp)
-           (bytevector-s32-ref (read-4 inp) 0 (endianness big)))
+           (string->symbol (read-str inp (read-short inp))))
          
          (define (read-bignum-t inp type)
            (let* ((size (if (eq? type 'small) 
                       (nanosecond (* (read-any-raw inp) 1000)))
                   (make-time time-utc nanosecond second)))
                ((eq? obj 'dict)
-                (read-hash inp))
-               (else 42))))
+                (read-dict inp))
+               (else 
+                'nil))))
          
-         (define (read-hash inp)
-           (let ((keyvallst (read-any-raw inp))
+         (define (read-dict inp)
+           (let ((type (read-small-int inp))
                  (h (make-eq-hashtable)))
-             (for-each (lambda (pair)
-                         (let ((k (vector-ref pair 0))
-                               (v (vector-ref pair 1)))
-                           (hashtable-set! h k v)))
-                       keyvallst)
-             h))
+             (cond
+               ((eq? type LIST)
+                (let recur ((len (read-int inp)))
+                  (if (zero? len)
+                      (begin
+                        (read-1 inp)     ;; NIL
+                        h)
+                      (let* ((pair (read-any-raw inp))
+                             (k (vector-ref pair 0))
+                             (v (vector-ref pair 1)))
+                        (hashtable-set! h k v)
+                        (recur (- len 1))))))
+               ((eq? type NIL)
+                h)
+               (else
+                (error "read-dict"
+                       "Invalid dict spec, not an erlang list"))))
+           )
          
          (define (read-list inp)
            (let ((len (read-int inp)))
                (if (zero? i)
                    (begin
                      (read-1 inp)
-                     lst)
+                     (reverse lst))
                    (recur (- i 1) (cons (read-any-raw inp) lst))))))
          
          (define (read-nil inp)
            (read-str inp (read-short inp)))
          
          (define (read-bin inp)
-           (read-str inp (read-int inp)))
-         
-         )
+           (read-str inp (read-int inp))))