Anonymous avatar Anonymous committed a26a278

introduce bitconstructing low-level

Comments (0)

Files changed (2)

 ;; bitstirng module implements the subset of Erlang bit syntax.
-;; 
-;; Basic syntax description
-;; (bitmatch somedata 
-;;   ((pattern ...) expression)
-;;   ...
-;;   (else expression))
-;;
-;; *** bitconstruct is the experimental future please notice ***
-;; (bitconstruct   
-;;   (pattern ...)
-;;   ...
-;;   (else expression))
-;;
-;; (bitpacket NAME pattern ...)
-;;
-;; Notes:
-;; - else block is optional.
-;; - default endianes bigendian.
-;; - default count 8 bits.
-;; - if nothing matches and else guard didnt specified 'bitstring-match-failure
-;; - guard condition, continue evaluate only when expression returns #t.
-;; exception will thrown.
-;;
-;; pattern:
-;; (NAME)
-;; (NAME bitpacket)
-;; (NAME bitstring)
-;; (NAME BITS)
-;; (NAME BITS big)
-;; (NAME BITS little)
-;; (NAME BITS float)
-;; (NAME BITS bitstring)
-;; (check EXPRESSION)
-;;
-;;
-;;
 
 (module bitstring
   (bitmatch
       (and
       	condition
       	(bitstring-pattern ("secret" mode stream handler) rest ...)))
+    ; evaluate constructing function
+    ((_ ("secret" "constructing" stream handler) ((VALUE ...) bitstring) rest ...)
+      (and-let* ((tmp (VALUE ...))
+      	         (bits (bitstring-length tmp)))
+	(bitstring-pattern ("secret" "constructing" stream handler)
+	                   (tmp bits bitstring) rest ...)))
     ; bitpacket
     ((_ ("secret" mode stream handler) (NAME bitpacket) rest ...)
       (bitstring-packet-expand mode stream handler NAME rest ...))
 
 (define-syntax bitstring-pattern-expand
   (syntax-rules ()
+    ((_ "constructing" stream name continuation)
+      (and-let* ((tmp (bitstring-of-any name)))
+      	(bitstring-append stream tmp)
+      	continuation))
     ((_ "constructing" stream name bits type continuation)
       (and-let* ((tmp (bitstring-write-expand name bits type)))
       	(bitstring-append stream tmp)

tests/examples.scm

-
 ; Example 1. Tagged data structure.
 ;
 ; struct Tagged {
   (else
     (print "bad datagram")))
 
+; Example 3.1 Using bitconstruct.
+
+(define (construct-fixed-string str)
+  (bitconstruct
+    (( (string-length str) 16) (str bitstring) )))
+
+(bitmatch (construct-fixed-string "qwerty.")
+  (((7 16) ("qwerty."))
+    (print #t))
+  (else 
+    (print #f)))
+
+; Example 3.2 Concatenating bitstrings.
+
+(define (construct-complex-object)
+  (bitconstruct
+    ( ((construct-fixed-string "A") bitstring)
+      (#xAABB 16)
+      ((construct-fixed-string "RRR") bitstring))
+      (#\X)))
+
+(print (construct-complex-object))
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.