ob-C.el 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. ;;; ob-C.el --- org-babel functions for C and similar languages
  2. ;; Copyright (C) 2010-2013 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating C code.
  19. ;;
  20. ;; very limited implementation:
  21. ;; - currently only support :results output
  22. ;; - not much in the way of error feedback
  23. ;;; Code:
  24. (require 'ob)
  25. (require 'cc-mode)
  26. (eval-when-compile
  27. (require 'cl))
  28. (declare-function org-entry-get "org"
  29. (pom property &optional inherit literal-nil))
  30. (declare-function org-remove-indentation "org" (code &optional n))
  31. (defvar org-babel-tangle-lang-exts)
  32. (add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp"))
  33. (defvar org-babel-default-header-args:C '())
  34. (defvar org-babel-C-compiler "gcc"
  35. "Command used to compile a C source code file into an
  36. executable.")
  37. (defvar org-babel-C++-compiler "g++"
  38. "Command used to compile a C++ source code file into an
  39. executable.")
  40. (defvar org-babel-c-variant nil
  41. "Internal variable used to hold which type of C (e.g. C or C++)
  42. is currently being evaluated.")
  43. (defun org-babel-execute:cpp (body params)
  44. "Execute BODY according to PARAMS.
  45. This function calls `org-babel-execute:C++'."
  46. (org-babel-execute:C++ body params))
  47. (defun org-babel-execute:C++ (body params)
  48. "Execute a block of C++ code with org-babel.
  49. This function is called by `org-babel-execute-src-block'."
  50. (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params)))
  51. (defun org-babel-expand-body:C++ (body params)
  52. "Expand a block of C++ code with org-babel according to it's
  53. header arguments (calls `org-babel-C-expand')."
  54. (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand body params)))
  55. (defun org-babel-execute:C (body params)
  56. "Execute a block of C code with org-babel.
  57. This function is called by `org-babel-execute-src-block'."
  58. (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
  59. (defun org-babel-expand-body:c (body params)
  60. "Expand a block of C code with org-babel according to it's
  61. header arguments (calls `org-babel-C-expand')."
  62. (let ((org-babel-c-variant 'c)) (org-babel-C-expand body params)))
  63. (defun org-babel-C-execute (body params)
  64. "This function should only be called by `org-babel-execute:C'
  65. or `org-babel-execute:C++'."
  66. (let* ((tmp-src-file (org-babel-temp-file
  67. "C-src-"
  68. (cond
  69. ((equal org-babel-c-variant 'c) ".c")
  70. ((equal org-babel-c-variant 'cpp) ".cpp"))))
  71. (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext))
  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. (org-babel-process-file-name tmp-bin-file)
  84. (mapconcat 'identity
  85. (if (listp flags) flags (list flags)) " ")
  86. (org-babel-process-file-name tmp-src-file)) ""))))
  87. (let ((results
  88. (org-babel-trim
  89. (org-remove-indentation
  90. (org-babel-eval
  91. (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
  92. (org-babel-reassemble-table
  93. (org-babel-result-cond (cdr (assoc :result-params params))
  94. (org-babel-read results t)
  95. (let ((tmp-file (org-babel-temp-file "c-")))
  96. (with-temp-file tmp-file (insert results))
  97. (org-babel-import-elisp-from-file tmp-file)))
  98. (org-babel-pick-name
  99. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  100. (org-babel-pick-name
  101. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
  102. ))
  103. (defun org-babel-C-expand (body params)
  104. "Expand a block of C or C++ code with org-babel according to
  105. it's header arguments."
  106. (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  107. (main-p (not (string= (cdr (assoc :main params)) "no")))
  108. (includes (or (cdr (assoc :includes params))
  109. (org-babel-read (org-entry-get nil "includes" t))))
  110. (defines (org-babel-read
  111. (or (cdr (assoc :defines params))
  112. (org-babel-read (org-entry-get nil "defines" t))))))
  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\nreturn 0;\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-format-val (type val)
  144. "Handle the FORMAT part of TYPE with the data from VAL."
  145. (let ((format-data (cadr type)))
  146. (if (stringp format-data)
  147. (cons "" (format format-data val))
  148. (funcall format-data val))))
  149. (defun org-babel-C-val-to-C-type (val)
  150. "Determine the type of VAL.
  151. Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type.
  152. FORMAT can be either a format string or a function which is called with VAL."
  153. (cond
  154. ((integerp val) '("int" "%d"))
  155. ((floatp val) '("double" "%f"))
  156. ((or (listp val) (vectorp val))
  157. (lexical-let ((type (org-babel-C-val-to-C-list-type val)))
  158. (list (car type)
  159. (lambda (val)
  160. (cons
  161. (format "[%d]%s"
  162. (length val)
  163. (car (org-babel-C-format-val type (elt val 0))))
  164. (concat "{ "
  165. (mapconcat (lambda (v)
  166. (cdr (org-babel-C-format-val type v)))
  167. val
  168. ", ")
  169. " }"))))))
  170. (t ;; treat unknown types as string
  171. '("char" (lambda (val)
  172. (let ((s (format "%s" val))) ;; convert to string for unknown types
  173. (cons (format "[%d]" (1+ (length s)))
  174. (concat "\"" s "\""))))))))
  175. (defun org-babel-C-val-to-C-list-type (val)
  176. "Determine the C array type of a VAL."
  177. (let (type)
  178. (mapc
  179. #'(lambda (i)
  180. (let* ((tmp-type (org-babel-C-val-to-C-type i))
  181. (type-name (car type))
  182. (tmp-type-name (car tmp-type)))
  183. (when (and type (not (string= type-name tmp-type-name)))
  184. (if (and (member type-name '("int" "double" "int32_t"))
  185. (member tmp-type-name '("int" "double" "int32_t")))
  186. (setq tmp-type '("double" "" "%f"))
  187. (error "Only homogeneous lists are supported by C. You can not mix %s and %s"
  188. type-name
  189. tmp-type-name)))
  190. (setq type tmp-type)))
  191. val)
  192. type))
  193. (defun org-babel-C-var-to-C (pair)
  194. "Convert an elisp val into a string of C code specifying a var
  195. of the same value."
  196. ;; TODO list support
  197. (let ((var (car pair))
  198. (val (cdr pair)))
  199. (when (symbolp val)
  200. (setq val (symbol-name val))
  201. (when (= (length val) 1)
  202. (setq val (string-to-char val))))
  203. (let* ((type-data (org-babel-C-val-to-C-type val))
  204. (type (car type-data))
  205. (formated (org-babel-C-format-val type-data val))
  206. (suffix (car formated))
  207. (data (cdr formated)))
  208. (format "%s %s%s = %s;"
  209. type
  210. var
  211. suffix
  212. data))))
  213. (provide 'ob-C)
  214. ;;; ob-C.el ends here