ob-C.el 6.8 KB

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