Commits

Anonymous committed 4af7b6e

Hash encoding implemented.

  • Participants
  • Parent commits 8530943

Comments (0)

Files changed (1)

File src/encode.ss

-(module encode scheme
+(module encode racket
   (require "types.ss")
   
   (define (write-1 byte)
         (write-1 0))
     (wr-b (abs num)))
   
+  (define (write-hash hash)
+    (write-any-raw 
+     (vector
+      'bert       
+      'dict 
+      (reverse (hash-map
+                hash
+                (lambda (k v) (vector (convert k) (convert v))))))))
+  
   (define (write-any-raw obj)
     (cond
+      [(hash? obj)
+       (write-hash obj)]
       [(symbol? obj)
        (write-symbol obj)]
       [(list? obj)
       [(integer? obj)
        (write-fixnum obj)]
       [(vector? obj)
-       (write-vector obj)]      
+       (write-vector obj)]
       [else (error "write-any-raw: Not implemented.")]))
   
   (define (write-any obj)
       [(void? obj)
        (vector 'bert 'nil)]
       [else obj]))
-      
+  
   (define (encode obj)
     (with-output-to-bytes (lambda() (write-any (convert obj)))))
   
   (define (encode-pretty obj)
     (bytes->list (encode obj)))
   
-  (define (test)
+  (define (test-1)
     ; {foo,42,666,{12,[],{255,'Bar'},111222333444555666}}
     (equal?
      (encode (vector 'foo 42 666 (vector 12 '() (vector 255 'bar) 111222333444555666)))
-     (bytes 131 104 4 100 0 3 102 111 111 97 42 98 0 0 2 154 104 4 97 12 106 104 2 97 255 100 0 3 98 97 114 110 8 0 146 131 13 124 31 36 139 1))))
+     (bytes 131 104 4 100 0 3 102 111 111 97 42 98 0 0 2 154 104 4 97 12 106 104 2 97 255 100 0 3 98 97 114 110 8 0 146 131 13 124 31 36 139 1)))
+  
+  (define (test-2)
+    ; {bert,dict,[{one,1},{two,2},{three,3}]}
+    (equal?
+     (encode (hash 'one 1 'two 2 'three 3))
+     (bytes 131 104 4 100 0 3 102 111 111 97 42 98 0 0 2 154 104 4 97 12 106 104 2 97 255 100 0 3 98 97 114 110 8 0 146 131 13 124 31 36 139 1)))
+  
+  (define (tests)
+    (test-1))