Skip to content

Instantly share code, notes, and snippets.

@rpav
Created October 9, 2014 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpav/06525c5c902938c1ddae to your computer and use it in GitHub Desktop.
Save rpav/06525c5c902938c1ddae to your computer and use it in GitHub Desktop.
Abbreviated parsing example
(defparameter *input-string*
"(tiles 2 2
(tile 0 0 TTERMLTERM EMPTY16X2 0)
(tile 1 1 TL UL 7
(primitive_site RLL_X0Y25 RESERVED_LL internal 8)
(primitive_site DCI7 DCI internal 13)
(primitive_site DCI0 DCI internal 13)
(primitive_site PMV PMV internal 8)
(primitive_site DCIRESET7 DCIRESET internal 1)
(primitive_site DCIRESET0 DCIRESET internal 1)
(primitive_site VCC_X0Y25 VCC internal 1)))")
(defgeneric parse-fpga-expr (type rest)
(:documentation "Parse an expression. Specialize `TYPE` on `(eql
'SYMBOL)`. Meant to be called recursively."))
(defmethod parse-fpga-expr (type rest)
(format t "Unhandled type: ~S~%" type))
(defmethod parse-fpga-expr ((type (eql 'tiles)) rest)
(destructuring-bind (h w &rest exprs) rest
(let ((array (make-array (* h w))))
(loop for expr in exprs
for i from 0
do (setf (aref array i)
(parse-fpga-expr (car expr) (cdr expr))))
(make-array (list h w) :displaced-to array))))
(defmethod parse-fpga-expr ((type (eql 'tile)) rest)
(destructuring-bind (x y a b e &rest exprs) rest
(list a b (length exprs))))
(defun parse-fpga (list)
(parse-fpga-expr (car list) (cdr list)))
(parse-fpga (read-from-string *input-string*))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment