add parse-line procedure
authorChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 23 Nov 2015 03:50:03 +0000 (21:50 -0600)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 23 Nov 2015 03:50:03 +0000 (21:50 -0600)
demos/irc.scm

index cbd3848378f8d8adb3748e0abe66b18e50921507..a5e9ba4426fb6e9f1e5c5a0b0b8d558584a3f561 100755 (executable)
      (irc-format socket "JOIN ~a" channel))
    channels))
 
+(define (startswith-colon? str)
+  (and (> (string-length str) 0)
+       (eq? (string-ref str 0)
+            #\:)))
+
+(define (parse-line line)
+  (define (parse-params pre-params)
+    ;; This is stupid and imperative but I can't wrap my brain around
+    ;; the right way to do it in a functional way :\
+    (let ((param-list '())
+          (currently-building '()))
+      (for-each
+       (lambda (param-item)
+         (cond
+          ((startswith-colon? param-item)
+           (if (not (eq? currently-building '()))
+               (set! param-list
+                     (cons
+                      (reverse currently-building)
+                      param-list)))
+           (set! currently-building (list param-item)))
+          (else
+           (set! currently-building (cons param-item currently-building)))))
+       pre-params)
+      ;; We're still building something, so tack that on there
+      (if (not (eq? currently-building '()))
+          (set! param-list
+                (cons (reverse currently-building) param-list)))
+      ;; return the reverse of the param list
+      (reverse param-list)))
+
+  (match (string-split line #\space)
+    (((? startswith-colon? prefix)
+      command
+      pre-params ...)
+     (list prefix command
+           (parse-params2 pre-params)))
+    ((command pre-params ...)
+     (list #f command (parse-params2 pre-params)))))
+
+
 (define (handle-line socket line my-username)
   (match (string-split line #\space)
     (("PING" rest ...)