-
-;; Experimental. With an argument, print signed, unsigned, hex and
-;; boolean representations.
-(defun c-macro-eval (start end &optional flag) "\
-Expand region using cpp and evaluate it using calc.
-Interactively print value in minibuffer and push it on the kill ring.
-With a C-u argument shows the evaluation in a variety of formats.
-With two C-u's prompts the user for a string of flags to the preprocessor.
-
-Non interactively returns value of region between START and END
-as a string. Several formats are used if optional FLAG is non-nil."
-
- (interactive "r\nP")
- (or (fboundp 'calc-eval)
- (require 'calc))
- (if (or c-macro-always-prompt-p (equal flag '(16)))
- (setq c-macro-default-cppflags
- (read-string "Preprocessor arguments: "
- c-macro-default-cppflags)))
-
- ;; Expand the region.
- (if (interactive-p) (message (c-macro-default-message)))
- (let ((evaluation
- (c-macro-expansion start end
- (concat c-macro-preprocessor " "
- c-macro-default-cppflags)))
- (evalbuf (get-buffer-create " *Macro Evaluation*")))
- (unwind-protect
- (save-excursion
- (set-buffer evalbuf)
- (setq buffer-read-only nil)
- (erase-buffer)
- (insert evaluation)
-
- ;; Evaluate expression(s).
- (if (interactive-p)
- (message "Invoking calc..."))
- (setq evaluation
- (let ((calc-eval-error t))
- (calc-eval (list (buffer-string) 'calc-language 'c))))
- (erase-buffer)
- (cond
- (flag
- (insert (calc-eval (list evaluation
- 'calc-language 'c
- 'calc-simplify-mode 'binary))
- "(u)" " == "
- (calc-eval (list evaluation
- 'calc-language 'c
- 'calc-word-size (- calc-word-size)
- 'calc-simplify-mode 'binary))
- "(d)" " == "
- (calc-eval (list evaluation
- 'calc-language 'c
- 'calc-number-radix 16
- 'calc-simplify-mode 'binary))
- "(x)")
- (save-excursion
- (insert " == " (calc-eval (list evaluation
- 'calc-language 'c
- 'calc-number-radix 16
- 'calc-simplify-mode 'binary))))
- (while (re-search-forward "0x\\([^,]+\\)\\(, \\|\\'\\)" nil t)
- (if (string= "0"
- (buffer-substring (match-beginning 1)
- (match-end 1)))
- (replace-match "FALSE\\2")
- (replace-match "TRUE\\2"))))
- (t
- (insert evaluation)))
-
- ;; Output the evaluation.
- (if (interactive-p)
- (progn
- (copy-region-as-kill 1 (point-max))
- (message (buffer-string)))
- (buffer-string)))
- (kill-buffer evalbuf))))
-
-(defun c-macro-default-message ()
- (format "Invoking %s%s%s on region..."
- c-macro-preprocessor
- (if (string= "" c-macro-default-cppflags) "" " ")
- c-macro-default-cppflags))
-
-(provide 'cmacexp)
-
-;;; cmacexp.el ends here.