Skip to content

Instantly share code, notes, and snippets.

@larsen
Created December 18, 2020 21:27
Show Gist options
  • Select an option

  • Save larsen/15ef1d8174fb36f5ff4e9f3a38f9ee6b to your computer and use it in GitHub Desktop.

Select an option

Save larsen/15ef1d8174fb36f5ff4e9f3a38f9ee6b to your computer and use it in GitHub Desktop.
;; 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