Created
June 15, 2011 03:19
-
-
Save miyamuko/1026413 to your computer and use it in GitHub Desktop.
#xyzzy の calc.l を使って中置記法から前置記法(ポーランド記法)への変換
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
| ;; xyzzy の calc.l を使って中置記法から前置記法(ポーランド記法)への変換 | |
| ;; | |
| ;; 参考: | |
| ;; | |
| ;; 中置記法から前置記法(ポーランド記法)への変換 - sketch code | |
| ;; http://d.hatena.ne.jp/ibaza/20070602/1180762871 | |
| ;; based on ed::calc-string | |
| (defun infix->prefix (string) | |
| (let ((*read-default-float-format* 'double-float) | |
| (ed::*calc-token* (let ((token nil)) | |
| (with-input-from-string (s string) | |
| (do ((tok (ed::calc-next-token s) (ed::calc-next-token s))) | |
| ((null tok) (nreverse token)) | |
| (push tok token)))))) | |
| (let ((expr (and ed::*calc-token* (ed::calc-expr)))) | |
| (when ed::*calc-token* | |
| (error "unexpected ~A." (ed::calc-token-string (car ed::*calc-token*)))) | |
| expr))) | |
| ;; test | |
| (infix->prefix "(1 + 2 + 3)") | |
| ;=> (+ (+ 1 2) 3) | |
| (infix->prefix "(1 + 2 * 3)") | |
| ;=> (+ 1 (* 2 3)) | |
| (infix->prefix "(1 & 3 | 2 & 4)") | |
| ;=> (logior (logand 1 3) (logand 2 4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment