Skip to content

Instantly share code, notes, and snippets.

@abeaumont
Last active August 29, 2015 14:12
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 abeaumont/7f8f6ecfced9fdef14fc to your computer and use it in GitHub Desktop.
Save abeaumont/7f8f6ecfced9fdef14fc to your computer and use it in GitHub Desktop.
Solution for TLang problem, (ab)using reader macros https://contest.tuenti.net/resources/2011/Question_2.html
(defun transform-expr (expr)
(if (symbolp expr)
(let ((s (symbol-name expr)))
(cond ((string-equal s "=") '+)
((string-equal s "@") '-)
(t expr)))
expr))
(defun read-next-object (delimiter &optional (stream *standard-input*))
(if (and delimiter (char= (peek-char t stream t nil t) delimiter))
(and (read-char stream t nil t) nil)
(read stream t nil t)))
(defun caret-reader (stream char)
(declare (ignore char))
(loop for object = (read-next-object #\$ stream)
while object
collect (transform-expr object) into objects
finally (return `(format t "~a~%" (,@objects)))))
(defun read-delimiter (stream char)
(declare (ignore stream char)))
(defun sharp-reader (stream char)
(declare (ignore stream char))
'*)
(set-macro-character #\^ #'caret-reader)
(set-macro-character #\$ #'read-delimiter)
(set-macro-character #\# #'sharp-reader)
(load *standard-input*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment