ob-C.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. ;;; ob-C.el --- org-babel functions for C and similar languages
  2. ;; Copyright (C) 2010-2014 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte, Thierry Banel
  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, C++, D 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. (add-to-list 'org-babel-tangle-lang-exts '("D" . "d"))
  34. (defvar org-babel-default-header-args:C '())
  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. (defvar org-babel-D-compiler "rdmd"
  42. "Command used to compile and execute a D source code file.")
  43. (defvar org-babel-c-variant nil
  44. "Internal variable used to hold which type of C (e.g. C or C++ or D)
  45. is currently being evaluated.")
  46. (defun org-babel-execute:cpp (body params)
  47. "Execute BODY according to PARAMS.
  48. This function calls `org-babel-execute:C++'."
  49. (org-babel-execute:C++ body params))
  50. (defun org-babel-execute:C++ (body params)
  51. "Execute a block of C++ code with org-babel.
  52. This function is called by `org-babel-execute-src-block'."
  53. (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params)))
  54. ;;(defun org-babel-expand-body:C++ (body params) ;; unused
  55. ;; "Expand a block of C++ code with org-babel according to it's
  56. ;;header arguments (calls `org-babel-C-expand')."
  57. ;; (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand body params)))
  58. (defun org-babel-execute:D (body params)
  59. "Execute a block of D code with org-babel.
  60. This function is called by `org-babel-execute-src-block'."
  61. (let ((org-babel-c-variant 'd)) (org-babel-C-execute body params)))
  62. ;; (defun org-babel-expand-body:D (body params) ;; unused
  63. ;; "Expand a block of D code with org-babel according to it's
  64. ;;header arguments (calls `org-babel-C-expand')."
  65. ;; (let ((org-babel-c-variant 'd)) (org-babel-C-expand body params)))
  66. (defun org-babel-execute:C (body params)
  67. "Execute a block of C code with org-babel.
  68. This function is called by `org-babel-execute-src-block'."
  69. (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
  70. ;; (defun org-babel-expand-body:c (body params) ;; unused
  71. ;; "Expand a block of C code with org-babel according to it's
  72. ;;header arguments (calls `org-babel-C-expand')."
  73. ;; (let ((org-babel-c-variant 'c)) (org-babel-C-expand body params)))
  74. (defun org-babel-C-execute (body params)
  75. "This function should only be called by `org-babel-execute:C'
  76. or `org-babel-execute:C++' or `org-babel-execute:D'."
  77. (let* ((tmp-src-file (org-babel-temp-file
  78. "C-src-"
  79. (cond
  80. ((equal org-babel-c-variant 'c ) ".c" )
  81. ((equal org-babel-c-variant 'cpp) ".cpp")
  82. ((equal org-babel-c-variant 'd ) ".d" ))))
  83. (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext)) ;; not used for D
  84. (cmdline (cdr (assoc :cmdline params)))
  85. (cmdline (if cmdline (concat " " cmdline) ""))
  86. (flags (cdr (assoc :flags params)))
  87. (flags (mapconcat 'identity
  88. (if (listp flags) flags (list flags)) " "))
  89. (full-body
  90. (cond ((equal org-babel-c-variant 'c ) (org-babel-C-expand-C body params))
  91. ((equal org-babel-c-variant 'cpp) (org-babel-C-expand-C++ body params))
  92. ((equal org-babel-c-variant 'd ) (org-babel-C-expand-D body params)))))
  93. (with-temp-file tmp-src-file (insert full-body))
  94. (if (memq org-babel-c-variant '(c cpp)) ;; no separate compilation for D
  95. (org-babel-eval
  96. (format "%s -o %s %s %s"
  97. (cond
  98. ((equal org-babel-c-variant 'c ) org-babel-C-compiler)
  99. ((equal org-babel-c-variant 'cpp) org-babel-C++-compiler))
  100. (org-babel-process-file-name tmp-bin-file)
  101. flags
  102. (org-babel-process-file-name tmp-src-file)) ""))
  103. (let ((results
  104. (org-babel-trim
  105. (org-remove-indentation
  106. (org-babel-eval
  107. (cond ((memq org-babel-c-variant '(c cpp))
  108. (concat tmp-bin-file cmdline))
  109. ((equal org-babel-c-variant 'd)
  110. (format "%s %s %s %s"
  111. org-babel-D-compiler
  112. flags
  113. (org-babel-process-file-name tmp-src-file)
  114. cmdline)))
  115. "")))))
  116. (org-babel-reassemble-table
  117. (org-babel-result-cond (cdr (assoc :result-params params))
  118. (org-babel-read results t)
  119. (let ((tmp-file (org-babel-temp-file "c-")))
  120. (with-temp-file tmp-file (insert results))
  121. (org-babel-import-elisp-from-file tmp-file)))
  122. (org-babel-pick-name
  123. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  124. (org-babel-pick-name
  125. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
  126. ))
  127. (defun org-babel-C-expand-C++ (body params)
  128. "Expand a block of C or C++ code with org-babel according to
  129. it's header arguments."
  130. (org-babel-C-expand-C body params))
  131. (defun org-babel-C-expand-C (body params)
  132. "Expand a block of C or C++ code with org-babel according to
  133. it's header arguments."
  134. (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  135. (main-p (not (string= (cdr (assoc :main params)) "no")))
  136. (includes (or (cdr (assoc :includes params))
  137. (org-babel-read (org-entry-get nil "includes" t))))
  138. (defines (org-babel-read
  139. (or (cdr (assoc :defines params))
  140. (org-babel-read (org-entry-get nil "defines" t))))))
  141. (mapconcat 'identity
  142. (list
  143. ;; includes
  144. (mapconcat
  145. (lambda (inc) (format "#include %s" inc))
  146. (if (listp includes) includes (list includes)) "\n")
  147. ;; defines
  148. (mapconcat
  149. (lambda (inc) (format "#define %s" inc))
  150. (if (listp defines) defines (list defines)) "\n")
  151. ;; variables
  152. (mapconcat 'org-babel-C-var-to-C vars "\n")
  153. ;; body
  154. (if main-p
  155. (org-babel-C-ensure-main-wrap body)
  156. body) "\n") "\n")))
  157. (defun org-babel-C-expand-D (body params)
  158. "Expand a block of D code with org-babel according to
  159. it's header arguments."
  160. (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  161. (main-p (not (string= (cdr (assoc :main params)) "no")))
  162. (imports (or (cdr (assoc :imports params))
  163. (org-babel-read (org-entry-get nil "imports" t)))))
  164. (mapconcat 'identity
  165. (list
  166. "module mmm;"
  167. ;; imports
  168. (mapconcat
  169. (lambda (inc) (format "import %s;" inc))
  170. (if (listp imports) imports (list imports)) "\n")
  171. ;; variables
  172. (mapconcat 'org-babel-C-var-to-C vars "\n")
  173. ;; body
  174. (if main-p
  175. (org-babel-C-ensure-main-wrap body)
  176. body) "\n") "\n")))
  177. (defun org-babel-C-ensure-main-wrap (body)
  178. "Wrap BODY in a \"main\" function call if none exists."
  179. (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
  180. body
  181. (format "int main() {\n%s\nreturn 0;\n}\n" body)))
  182. (defun org-babel-prep-session:C (session params)
  183. "This function does nothing as C is a compiled language with no
  184. support for sessions"
  185. (error "C is a compiled languages -- no support for sessions"))
  186. (defun org-babel-load-session:C (session body params)
  187. "This function does nothing as C is a compiled language with no
  188. support for sessions"
  189. (error "C is a compiled languages -- no support for sessions"))
  190. ;; helper functions
  191. (defun org-babel-C-format-val (type val)
  192. "Handle the FORMAT part of TYPE with the data from VAL."
  193. (let ((format-data (cadr type)))
  194. (if (stringp format-data)
  195. (cons "" (format format-data val))
  196. (funcall format-data val))))
  197. (defun org-babel-C-val-to-C-type (val)
  198. "Determine the type of VAL.
  199. Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type.
  200. FORMAT can be either a format string or a function which is called with VAL."
  201. (cond
  202. ((integerp val) '("int" "%d"))
  203. ((floatp val) '("double" "%f"))
  204. ((or (listp val) (vectorp val))
  205. (lexical-let ((type (org-babel-C-val-to-C-list-type val)))
  206. (list (car type)
  207. (lambda (val)
  208. (cons
  209. (format "[%d]%s"
  210. (length val)
  211. (car (org-babel-C-format-val type (elt val 0))))
  212. (concat (if (equal org-babel-c-variant 'd) "[ " "{ ")
  213. (mapconcat (lambda (v)
  214. (cdr (org-babel-C-format-val type v)))
  215. val
  216. ", ")
  217. (if (equal org-babel-c-variant 'd) " ]" " }")))))))
  218. (t ;; treat unknown types as string
  219. (list
  220. (if (equal org-babel-c-variant 'd) "string" "const char*")
  221. "\"%s\""))))
  222. (defun org-babel-C-val-to-C-list-type (val)
  223. "Determine the C array type of a VAL."
  224. (let (type)
  225. (mapc
  226. #'(lambda (i)
  227. (let* ((tmp-type (org-babel-C-val-to-C-type i))
  228. (type-name (car type))
  229. (tmp-type-name (car tmp-type)))
  230. (when (and type (not (string= type-name tmp-type-name)))
  231. (if (and (member type-name '("int" "double" "int32_t"))
  232. (member tmp-type-name '("int" "double" "int32_t")))
  233. (setq tmp-type '("double" "%f"))
  234. (error "Only homogeneous lists are supported by C. You can not mix %s and %s"
  235. type-name
  236. tmp-type-name)))
  237. (setq type tmp-type)))
  238. val)
  239. type))
  240. (defun org-babel-C-var-to-C (pair)
  241. "Convert an elisp val into a string of C code specifying a var
  242. of the same value."
  243. ;; TODO list support
  244. (let ((var (car pair))
  245. (val (cdr pair)))
  246. (when (symbolp val)
  247. (setq val (symbol-name val))
  248. (when (= (length val) 1)
  249. (setq val (string-to-char val))))
  250. (let* ((type-data (org-babel-C-val-to-C-type val))
  251. (type (car type-data))
  252. (formated (org-babel-C-format-val type-data val))
  253. (suffix (car formated))
  254. (data (cdr formated)))
  255. (format "%s %s%s = %s;"
  256. type
  257. var
  258. suffix
  259. data))))
  260. (provide 'ob-C)
  261. ;;; ob-C.el ends here