~ilmu/cxp

f1c00b70baed0508d061fc12ae10141de3acebbd — ilmu a month ago 5d5ae7d master
fixed unbalanced parenthesis in output to collect
1 files changed, 21 insertions(+), 22 deletions(-)

M cxp.lisp
M cxp.lisp => cxp.lisp +21 -22
@@ 266,18 266,19 @@

(defun write-cxp (cxp out)
  (let ((writer
	  (alambda (x)
	  (alambda (x top)
		   (if (consp x)
		       (progn
		       (progn ;; x is a cxp, need parens
			 (unless (atom (car x))
			   (write-byte (char-int #\() out))
			 (self (car x))
			 (self (car x) 'foo)
			 (if (null (cdr x))
			     (write-byte (char-int #\)) out)
			     (self (cdr x)))
		       (write-netstring x out))))))
	(funcall writer cxp)))
    
			     (when (not top) (write-byte (char-int #\)) out))
			     (self (cdr x) 'foo)))
		       ;; x is just a netstring
		       (write-netstring x out)))))
    (funcall writer cxp nil)))


;; -- quests --
;; support various io streams


@@ 320,18 321,14 @@
    (unless (done-mode-p parser) ;; FIXME at least use stderr - use fail function; FIXME: control flow see above.
      (format out "Ambiguous Error: Not enough data for given prefix."))))

;; FIXME: obsolete. use for testing new read-netstring and read-cxp functionality.
;;
(defun collect-stream (in out)
  "Checks input to be a proper cxp and collects it into one cxp."
  (let ((parser (initialize-netstring-parser)))
    (when (not (eof-p in))
      (do-until
	(let ((b (read-byte in)))
	  (netstring-parse-step parser b))
	(or (eof-p in)
	    (done-mode-p parser))))
    (unless (done-mode-p parser) ;; FIXME at least use stderr
      (format out "Ambiguous Error: Not enough data for given prefix."))))
  (block parser-busy
    (let ((the-cxp (read-cxp in (lambda (err)
				  (print err out)
				  (return-from parser-busy)))))
      (write-cxp (list the-cxp) out))))


(require 'ironclad)


@@ 345,6 342,7 @@
(defun append-to-path (path str)
  (pathname (concatenate 'string (namestring path) str)))

;; FIXME when evaluating buffer I get warning about database being undefined.
(defparameter +cxp-cache+
  (when (uiop:os-unix-p)
    (ensure-directories-exist ;; this works with a filepath also, it creates containing dirs.


@@ 373,21 371,22 @@
    (when data (write-sequence data out))))

;; this is the toplevel function that gets exposed as the cli interface.
;; TODO make a proper DSL and pass the args into it rather than this cond.
;; TODO make a proper DSL and parse the args into it rather than this cond.
(defun io ()
  (sqlite:with-open-database (db +cxp-cache+)
    (sqlite:execute-non-query db "CREATE TABLE IF NOT EXISTS cxp_cache (id text primary key, data blob not null)")
    (let ((args (cddr sb-ext:*posix-argv*))
	  (cmd (cadr sb-ext:*posix-argv*))
	  (in (flex:make-flexi-stream *standard-input* :element-type '(UNSIGNED-BYTE 8)))
	  ;; FIXME: compatibility of element-type / stream encoding. **CURRENT ISSUE**
	  (out (flex:make-flexi-stream *standard-output* :element-type '(UNSIGNED-BYTE 8))))
      (progn
	(cond
	  ((string= cmd "quote") (quote-stream in out))
	  ((string= cmd "quote")   (quote-stream in out))
	  ((string= cmd "unquote") (unquote-stream in out))
	  ((string= cmd "cache")   (cache-stream db in out))
	  ((string= cmd "fetch")   (fetch-stream db in out))
	  ((string= cmd "collect") (collect-stream in out))
	  ((string= cmd "cache") (cache-stream db in out))
	  ((string= cmd "fetch") (fetch-stream db in out))
	  ((string= cmd "lines") (print "nye"))
	  ((string= cmd "help") (print "nye")) ;; TODO: make each defun add a line to *help--a-section*
	  (t (print "invalid command")))       ;; the sections then fill in the *help-string* template.