#!/usr/bin/env racket
#lang racket
(require srfi/48)
(define *table* (hasheq '+ + '- - '* * '/ /))
(let loop
((tokens
(string
-split
exp)) (stack
'())) (if (null? tokens)
(car stack)
(let ((token (car tokens)))
(let ((num (string->number token)))
(loop (cdr tokens)
(cons
(or num (apply (hash-ref *table* (string->symbol token))
(reverse (take stack 2))))
((lambda (x)
(if num
x
(drop x 2))) stack))))))))
(define (repl arg)
(display "rpn > ")
(with-handlers ((exn:fail?
(lambda (v)
(display "不正な入力です\n")
(repl #f))))
(repl
(display
(format "~1,4F~%" (let ((exp (read-line)))
(if (string=? exp "quit")
(exit)
(rpn exp))))))))
(repl #f)
IyEvdXNyL2Jpbi9lbnYgcmFja2V0CgojbGFuZyByYWNrZXQKCihyZXF1aXJlIHNyZmkvNDgpCgooZGVmaW5lICp0YWJsZSogKGhhc2hlcSAnKyArICctIC0gJyogKiAnLyAvKSkKCihkZWZpbmUgKHJwbiBleHApCiAgKGxldCBsb29wICgodG9rZW5zIChzdHJpbmctc3BsaXQgZXhwKSkgKHN0YWNrICcoKSkpCiAgICAoaWYgKG51bGw/IHRva2VucykKICAgICAgICAoY2FyIHN0YWNrKQogICAgICAgIChsZXQgKCh0b2tlbiAoY2FyIHRva2VucykpKQogICAgICAgICAgKGxldCAoKG51bSAoc3RyaW5nLT5udW1iZXIgdG9rZW4pKSkKICAgICAgICAgICAgKGxvb3AgKGNkciB0b2tlbnMpCiAgICAgICAgICAgICAgICAgIChjb25zCiAgICAgICAgICAgICAgICAgICAob3IgbnVtIChhcHBseSAoaGFzaC1yZWYgKnRhYmxlKiAoc3RyaW5nLT5zeW1ib2wgdG9rZW4pKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKHJldmVyc2UgKHRha2Ugc3RhY2sgMikpKSkKICAgICAgICAgICAgICAgICAgICgobGFtYmRhICh4KQogICAgICAgICAgICAgICAgICAgICAgKGlmIG51bQogICAgICAgICAgICAgICAgICAgICAgICAgIHgKICAgICAgICAgICAgICAgICAgICAgICAgICAoZHJvcCB4IDIpKSkgc3RhY2spKSkpKSkpKQoKKGRlZmluZSAocmVwbCBhcmcpCiAgKGRpc3BsYXkgInJwbiA+ICIpCiAgKHdpdGgtaGFuZGxlcnMgKChleG46ZmFpbD8KICAgICAgICAgICAgICAgICAgIChsYW1iZGEgKHYpCiAgICAgICAgICAgICAgICAgICAgIChkaXNwbGF5ICLkuI3mraPjgarlhaXlipvjgafjgZlcbiIpCiAgICAgICAgICAgICAgICAgICAgIChyZXBsICNmKSkpKQogICAgKHJlcGwgCiAgICAgKGRpc3BsYXkKICAgICAgKGZvcm1hdCAifjEsNEZ+JSIgKGxldCAoKGV4cCAocmVhZC1saW5lKSkpCiAgICAgICAgICAgICAgICAgICAgICAgICAgKGlmIChzdHJpbmc9PyBleHAgInF1aXQiKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAoZXhpdCkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKHJwbiBleHApKSkpKSkpKQoKKHJlcGwgI2Yp