Created
December 18, 2020 21:27
-
-
Save larsen/15ef1d8174fb36f5ff4e9f3a38f9ee6b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| ;; As a memento, here the code I wrote for the first part | |
| ;; Not malleable enough for the second part. | |
| (defun funky-eval (tokens) | |
| (let ((stack nil)) | |
| (loop while (not (null tokens)) | |
| do (let ((token (pop tokens))) | |
| (if (scan "\\)" token) | |
| (return-from funky-eval (values (pop stack) | |
| tokens))) | |
| (if (scan "\\(" token) | |
| (multiple-value-bind (res new-tokens) | |
| (funky-eval tokens) | |
| (push res stack) | |
| (setf tokens new-tokens))) | |
| (if (scan "\\d+" token) | |
| (push (parse-integer token) stack)) | |
| (if (scan "\\+" token) | |
| (push (+ (pop stack) | |
| (if (scan "\\(" (car tokens)) | |
| (multiple-value-bind (res new-tokens) | |
| (funky-eval (cdr tokens)) | |
| (setf tokens new-tokens) | |
| res) | |
| (parse-integer (pop tokens)))) | |
| stack)) | |
| (if (scan "\\*" token) | |
| (push (* (pop stack) | |
| (if (scan "\\(" (car tokens)) | |
| (multiple-value-bind (res new-tokens) | |
| (funky-eval (cdr tokens)) | |
| (setf tokens new-tokens) | |
| res) | |
| (parse-integer (pop tokens)))) | |
| stack))) | |
| finally (return (pop stack))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment