Last active
August 29, 2015 14:12
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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