org-babel-C.el 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ;;; org-babel-C.el --- org-babel functions for C and similar languages
  2. ;; Copyright (C) 2010 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating C code.
  24. ;;
  25. ;; very limited implementation:
  26. ;; - currently only support :results output
  27. ;; - not much in the way of error feedback
  28. ;;; Code:
  29. (require 'org-babel)
  30. (require 'cc-mode)
  31. (org-babel-add-interpreter "C")
  32. (add-to-list 'org-babel-tangle-langs '("C" "c" nil))
  33. (org-babel-add-interpreter "c++")
  34. (add-to-list 'org-babel-tangle-langs '("c++" "cpp" nil))
  35. (defvar org-babel-C-compiler "gcc"
  36. "Command used to compile a C source code file into an
  37. executable.")
  38. (defvar org-babel-c++-compiler "g++"
  39. "Command used to compile a c++ source code file into an
  40. executable.")
  41. (defun org-babel-execute:cpp (body params)
  42. (org-babel-execute:C body params))
  43. (defun org-babel-execute:c++ (body params)
  44. "Execute a block of C++ code with org-babel. This function is
  45. called by `org-babel-execute-src-block'."
  46. (let ((c-variant 'cpp)) (org-babel-C-execute body params)))
  47. (defun org-babel-expand-body:c++ (body params &optional processed-params)
  48. "Expand a block of C++ code with org-babel according to it's
  49. header arguments (calls `org-babel-C-expand')."
  50. (let ((c-variant 'cpp)) (org-babel-C-expand body params processed-params)))
  51. (defun org-babel-execute:C (body params)
  52. "Execute a block of C code with org-babel. This function is
  53. called by `org-babel-execute-src-block'."
  54. (let ((c-variant 'c)) (org-babel-C-execute body params)))
  55. (defun org-babel-expand-body:c (body params &optional processed-params)
  56. "Expand a block of C code with org-babel according to it's
  57. header arguments (calls `org-babel-C-expand')."
  58. (let ((c-variant 'c)) (org-babel-C-expand body params processed-params)))
  59. (defun org-babel-C-execute (body params)
  60. "This should only be called by `org-babel-execute:C' or
  61. `org-babel-execute:c++'."
  62. (message "executing C source code block")
  63. (let* ((processed-params (org-babel-process-params params))
  64. (tmp-src-file (make-temp-file "org-babel-C-src" nil
  65. (case c-variant
  66. ('c ".c")
  67. ('cpp ".cpp"))))
  68. (tmp-bin-file (make-temp-file "org-babel-C-bin"))
  69. (tmp-out-file (make-temp-file "org-babel-C-out"))
  70. (cmdline (cdr (assoc :cmdline params)))
  71. (flags (cdr (assoc :flags params)))
  72. (full-body (org-babel-C-expand body params))
  73. (error-buf (get-buffer-create "*Org-Babel Error Output*"))
  74. (compile
  75. (progn
  76. (with-temp-file tmp-src-file (insert full-body))
  77. (with-temp-buffer
  78. (org-babel-shell-command-on-region
  79. (point-min) (point-max)
  80. (format "%s -o %s %s %s"
  81. (case c-variant
  82. ('c org-babel-C-compiler)
  83. ('cpp org-babel-c++-compiler))
  84. tmp-bin-file
  85. (mapconcat 'identity
  86. (if (listp flags) flags (list flags)) " ")
  87. tmp-src-file)
  88. (current-buffer) 'replace error-buf)))))
  89. (if (= compile 0)
  90. (org-babel-reassemble-table
  91. (org-babel-read
  92. (org-babel-trim
  93. (with-temp-buffer
  94. (org-babel-shell-command-on-region
  95. (point-min) (point-max)
  96. (concat tmp-bin-file (if cmdline (concat " " cmdline) ""))
  97. (current-buffer) 'replace)
  98. (buffer-string))))
  99. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  100. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params))))
  101. (progn
  102. (with-current-buffer error-buf
  103. (goto-char (point-max))
  104. (insert (concat "\n\n--body--\n" full-body))
  105. (goto-char (point-min)))
  106. (display-buffer error-buf) nil))))
  107. (defun org-babel-C-expand (body params &optional processed-params)
  108. "Expand a block of C or C++ code with org-babel according to
  109. it's header arguments."
  110. (let ((vars (second (or processed-params
  111. (org-babel-process-params params))))
  112. (main-p (not (string= (cdr (assoc :main params)) "no")))
  113. (includes (or (cdr (assoc :includes params))
  114. (org-babel-read (org-entry-get nil "includes" t))))
  115. (defines (org-babel-read
  116. (or (cdr (assoc :defines params))
  117. (org-babel-read (org-entry-get nil "defines" t))))))
  118. (mapconcat 'identity
  119. (list
  120. ;; includes
  121. (mapconcat
  122. (lambda (inc) (format "#include %s" inc))
  123. (if (listp includes) includes (list includes)) "\n")
  124. ;; defines
  125. (mapconcat
  126. (lambda (inc) (format "#define %s" inc))
  127. (if (listp defines) defines (list defines)) "\n")
  128. ;; variables
  129. (mapconcat 'org-babel-C-var-to-C vars "\n")
  130. ;; body
  131. (if main-p
  132. (org-babel-C-ensure-main-wrap body)
  133. body) "\n") "\n")))
  134. (defun org-babel-C-ensure-main-wrap (body)
  135. "Wrap body in a \"main\" function call if none exists."
  136. (if (string-match "^[ \t]*[intvod]+[ \t]*main[ \t]*(.*)" body)
  137. body
  138. (format "int main() {\n%s\n}\n" body)))
  139. (defun org-babel-prep-session:C (session params)
  140. "C is a compiled languages -- no support for sessions"
  141. (error "C is a compiled languages -- no support for sessions"))
  142. (defun org-babel-load-session:C (session body params)
  143. "C is a compiled languages -- no support for sessions"
  144. (error "C is a compiled languages -- no support for sessions"))
  145. ;; helper functions
  146. (defun org-babel-C-var-to-C (pair)
  147. "Convert an elisp val into a string of C code specifying a var
  148. of the same value. TODO list support."
  149. (let ((var (car pair))
  150. (val (cdr pair)))
  151. (when (symbolp val)
  152. (setq val (symbol-name val))
  153. (when (= (length val) 1)
  154. (setq val (string-to-char val))))
  155. (cond
  156. ((integerp val)
  157. (format "int %S = %S;" var val))
  158. ((floatp val)
  159. (format "double %S = %S;" var val))
  160. ((or (characterp val))
  161. (format "char %S = '%S';" var val))
  162. ((stringp val)
  163. (format "char %S[%d] = \"%s\";"
  164. var (+ 1 (length val)) val))
  165. (t
  166. (format "u32 %S = %S;" var val)))))
  167. (provide 'org-babel-C)
  168. ;;; org-babel-C.el ends here