ob-C.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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
  4. ;; Thierry Banel
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: http://orgmode.org
  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, C++, D code.
  20. ;;
  21. ;; very limited implementation:
  22. ;; - currently only support :results output
  23. ;; - not much in the way of error feedback
  24. ;;; Code:
  25. (eval-when-compile
  26. (require 'cl))
  27. (require 'ob)
  28. (require 'cc-mode)
  29. (declare-function org-entry-get "org"
  30. (pom property &optional inherit literal-nil))
  31. (declare-function org-remove-indentation "org" (code &optional n))
  32. (defvar org-babel-tangle-lang-exts)
  33. (add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp"))
  34. (add-to-list 'org-babel-tangle-lang-exts '("D" . "d"))
  35. (defvar org-babel-default-header-args:C '())
  36. (defcustom org-babel-C-compiler "gcc"
  37. "Command used to compile a C source code file into an executable.
  38. May be either a command in the path, like gcc
  39. or an absolute path name, like /usr/local/bin/gcc
  40. parameter may be used, like gcc -v"
  41. :group 'org-babel
  42. :version "24.3"
  43. :type 'string)
  44. (defcustom org-babel-C++-compiler "g++"
  45. "Command used to compile a C++ source code file into an executable.
  46. May be either a command in the path, like g++
  47. or an absolute path name, like /usr/local/bin/g++
  48. parameter may be used, like g++ -v"
  49. :group 'org-babel
  50. :version "24.3"
  51. :type 'string)
  52. (defcustom org-babel-D-compiler "rdmd"
  53. "Command used to compile and execute a D source code file.
  54. May be either a command in the path, like rdmd
  55. or an absolute path name, like /usr/local/bin/rdmd
  56. parameter may be used, like rdmd --chatty"
  57. :group 'org-babel
  58. :version "24.3"
  59. :type 'string)
  60. (defvar org-babel-c-variant nil
  61. "Internal variable used to hold which type of C (e.g. C or C++ or D)
  62. is currently being evaluated.")
  63. (defun org-babel-execute:cpp (body params)
  64. "Execute BODY according to PARAMS.
  65. This function calls `org-babel-execute:C++'."
  66. (org-babel-execute:C++ body params))
  67. (defun org-babel-expand-body:cpp (body params)
  68. "Expand a block of C++ code with org-babel according to it's
  69. header arguments."
  70. (org-babel-expand-body:C++ body params))
  71. (defun org-babel-execute:C++ (body params)
  72. "Execute a block of C++ code with org-babel.
  73. This function is called by `org-babel-execute-src-block'."
  74. (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params)))
  75. (defun org-babel-expand-body:C++ (body params)
  76. "Expand a block of C++ code with org-babel according to it's
  77. header arguments."
  78. (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand-C++ body params)))
  79. (defun org-babel-execute:D (body params)
  80. "Execute a block of D code with org-babel.
  81. This function is called by `org-babel-execute-src-block'."
  82. (let ((org-babel-c-variant 'd)) (org-babel-C-execute body params)))
  83. (defun org-babel-expand-body:D (body params)
  84. "Expand a block of D code with org-babel according to it's
  85. header arguments."
  86. (let ((org-babel-c-variant 'd)) (org-babel-C-expand-D body params)))
  87. (defun org-babel-execute:C (body params)
  88. "Execute a block of C code with org-babel.
  89. This function is called by `org-babel-execute-src-block'."
  90. (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
  91. (defun org-babel-expand-body:C (body params)
  92. "Expand a block of C code with org-babel according to it's
  93. header arguments."
  94. (let ((org-babel-c-variant 'c)) (org-babel-C-expand-C body params)))
  95. (defun org-babel-C-execute (body params)
  96. "This function should only be called by `org-babel-execute:C'
  97. or `org-babel-execute:C++' or `org-babel-execute:D'."
  98. (let* ((tmp-src-file (org-babel-temp-file
  99. "C-src-"
  100. (case org-babel-c-variant
  101. (c ".c" )
  102. (cpp ".cpp")
  103. (d ".d" ))))
  104. (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext)) ;; not used for D
  105. (cmdline (cdr (assoc :cmdline params)))
  106. (cmdline (if cmdline (concat " " cmdline) ""))
  107. (flags (cdr (assoc :flags params)))
  108. (flags (mapconcat 'identity
  109. (if (listp flags) flags (list flags)) " "))
  110. (full-body
  111. (case org-babel-c-variant
  112. (c (org-babel-C-expand-C body params))
  113. (cpp (org-babel-C-expand-C++ body params))
  114. (d (org-babel-C-expand-D body params)))))
  115. (with-temp-file tmp-src-file (insert full-body))
  116. (case org-babel-c-variant
  117. ((c cpp)
  118. (org-babel-eval
  119. (format "%s -o %s %s %s"
  120. (case org-babel-c-variant
  121. (c org-babel-C-compiler)
  122. (cpp org-babel-C++-compiler))
  123. (org-babel-process-file-name tmp-bin-file)
  124. flags
  125. (org-babel-process-file-name tmp-src-file)) ""))
  126. (d nil)) ;; no separate compilation for D
  127. (let ((results
  128. (org-babel-eval
  129. (case org-babel-c-variant
  130. ((c cpp)
  131. (concat tmp-bin-file cmdline))
  132. (d
  133. (format "%s %s %s %s"
  134. org-babel-D-compiler
  135. flags
  136. (org-babel-process-file-name tmp-src-file)
  137. cmdline)))
  138. "")))
  139. (when results
  140. (setq results (org-babel-trim (org-remove-indentation results)))
  141. (org-babel-reassemble-table
  142. (org-babel-result-cond (cdr (assoc :result-params params))
  143. (org-babel-read results t)
  144. (let ((tmp-file (org-babel-temp-file "c-")))
  145. (with-temp-file tmp-file (insert results))
  146. (org-babel-import-elisp-from-file tmp-file)))
  147. (org-babel-pick-name
  148. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  149. (org-babel-pick-name
  150. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
  151. )))
  152. (defun org-babel-C-expand-C++ (body params)
  153. "Expand a block of C or C++ code with org-babel according to
  154. it's header arguments."
  155. (org-babel-C-expand-C body params))
  156. (defun org-babel-C-expand-C (body params)
  157. "Expand a block of C or C++ code with org-babel according to
  158. it's header arguments."
  159. (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  160. (colnames (cdar (org-babel-get-header params :colname-names)))
  161. (main-p (not (string= (cdr (assoc :main params)) "no")))
  162. (includes (or (cdr (assoc :includes params))
  163. (org-babel-read (org-entry-get nil "includes" t))))
  164. (defines (org-babel-read
  165. (or (cdr (assoc :defines params))
  166. (org-babel-read (org-entry-get nil "defines" t))))))
  167. (unless (listp includes) (setq includes (list includes)))
  168. (setq includes (append includes '("<string.h>" "<stdio.h>" "<stdlib.h>")))
  169. (mapconcat 'identity
  170. (list
  171. ;; includes
  172. (mapconcat
  173. (lambda (inc) (format "#include %s" inc))
  174. includes "\n")
  175. ;; defines
  176. (mapconcat
  177. (lambda (inc) (format "#define %s" inc))
  178. (if (listp defines) defines (list defines)) "\n")
  179. ;; variables
  180. (mapconcat 'org-babel-C-var-to-C vars "\n")
  181. ;; table sizes
  182. (mapconcat 'org-babel-C-table-sizes-to-C vars "\n")
  183. ;; tables headers utility
  184. (when colnames
  185. (org-babel-C-utility-header-to-C))
  186. ;; tables headers
  187. (mapconcat 'org-babel-C-header-to-C colnames "\n")
  188. ;; body
  189. (if main-p
  190. (org-babel-C-ensure-main-wrap body)
  191. body) "\n") "\n")))
  192. (defun org-babel-C-expand-D (body params)
  193. "Expand a block of D code with org-babel according to
  194. it's header arguments."
  195. (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  196. (colnames (cdar (org-babel-get-header params :colname-names)))
  197. (main-p (not (string= (cdr (assoc :main params)) "no")))
  198. (imports (or (cdr (assoc :imports params))
  199. (org-babel-read (org-entry-get nil "imports" t)))))
  200. (unless (listp imports) (setq imports (list imports)))
  201. (setq imports (append imports '("std.stdio" "std.conv")))
  202. (mapconcat 'identity
  203. (list
  204. "module mmm;"
  205. ;; imports
  206. (mapconcat
  207. (lambda (inc) (format "import %s;" inc))
  208. imports "\n")
  209. ;; variables
  210. (mapconcat 'org-babel-C-var-to-C vars "\n")
  211. ;; table sizes
  212. (mapconcat 'org-babel-C-table-sizes-to-C vars "\n")
  213. ;; tables headers utility
  214. (when colnames
  215. (org-babel-C-utility-header-to-C))
  216. ;; tables headers
  217. (mapconcat 'org-babel-C-header-to-C colnames "\n")
  218. ;; body
  219. (if main-p
  220. (org-babel-C-ensure-main-wrap body)
  221. body) "\n") "\n")))
  222. (defun org-babel-C-ensure-main-wrap (body)
  223. "Wrap BODY in a \"main\" function call if none exists."
  224. (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
  225. body
  226. (format "int main() {\n%s\nreturn 0;\n}\n" body)))
  227. (defun org-babel-prep-session:C (session params)
  228. "This function does nothing as C is a compiled language with no
  229. support for sessions"
  230. (error "C is a compiled languages -- no support for sessions"))
  231. (defun org-babel-load-session:C (session body params)
  232. "This function does nothing as C is a compiled language with no
  233. support for sessions"
  234. (error "C is a compiled languages -- no support for sessions"))
  235. ;; helper functions
  236. (defun org-babel-C-format-val (type val)
  237. "Handle the FORMAT part of TYPE with the data from VAL."
  238. (let ((format-data (cadr type)))
  239. (if (stringp format-data)
  240. (cons "" (format format-data val))
  241. (funcall format-data val))))
  242. (defun org-babel-C-val-to-C-type (val)
  243. "Determine the type of VAL.
  244. Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type.
  245. FORMAT can be either a format string or a function which is called with VAL."
  246. (let* ((basetype (org-babel-C-val-to-base-type val))
  247. (type
  248. (case basetype
  249. (integerp '("int" "%d"))
  250. (floatp '("double" "%f"))
  251. (stringp
  252. (list
  253. (if (equal org-babel-c-variant 'd) "string" "const char*")
  254. "\"%s\""))
  255. (t (error "unknown type %S" basetype)))))
  256. (cond
  257. ((integerp val) type) ;; an integer declared in the #+begin_src line
  258. ((floatp val) type) ;; a numeric declared in the #+begin_src line
  259. ((and (listp val) (listp (car val))) ;; a table
  260. `(,(car type)
  261. (lambda (val)
  262. (cons
  263. (format "[%d][%d]" (length val) (length (car val)))
  264. (concat
  265. (if (equal org-babel-c-variant 'd) "[\n" "{\n")
  266. (mapconcat
  267. (lambda (v)
  268. (concat
  269. (if (equal org-babel-c-variant 'd) " [" " {")
  270. (mapconcat (lambda (w) (format ,(cadr type) w)) v ",")
  271. (if (equal org-babel-c-variant 'd) "]" "}")))
  272. val
  273. ",\n")
  274. (if (equal org-babel-c-variant 'd) "\n]" "\n}"))))))
  275. ((or (listp val) (vectorp val)) ;; a list declared in the #+begin_src line
  276. `(,(car type)
  277. (lambda (val)
  278. (cons
  279. (format "[%d]" (length val))
  280. (concat
  281. (if (equal org-babel-c-variant 'd) "[" "{")
  282. (mapconcat (lambda (v) (format ,(cadr type) v)) val ",")
  283. (if (equal org-babel-c-variant 'd) "]" "}"))))))
  284. (t ;; treat unknown types as string
  285. type))))
  286. (defun org-babel-C-val-to-base-type (val)
  287. "Determine the base type of VAL which may be
  288. 'integerp if all base values are integers
  289. 'floatp if all base values are either floating points or integers
  290. 'stringp otherwise."
  291. (cond
  292. ((integerp val) 'integerp)
  293. ((floatp val) 'floatp)
  294. ((or (listp val) (vectorp val))
  295. (let ((type nil))
  296. (mapc (lambda (v)
  297. (case (org-babel-C-val-to-base-type v)
  298. (stringp (setq type 'stringp))
  299. (floatp
  300. (if (or (not type) (eq type 'integerp))
  301. (setq type 'floatp)))
  302. (integerp
  303. (unless type (setq type 'integerp)))))
  304. val)
  305. type))
  306. (t 'stringp)))
  307. (defun org-babel-C-var-to-C (pair)
  308. "Convert an elisp val into a string of C code specifying a var
  309. of the same value."
  310. ;; TODO list support
  311. (let ((var (car pair))
  312. (val (cdr pair)))
  313. (when (symbolp val)
  314. (setq val (symbol-name val))
  315. (when (= (length val) 1)
  316. (setq val (string-to-char val))))
  317. (let* ((type-data (org-babel-C-val-to-C-type val))
  318. (type (car type-data))
  319. (formated (org-babel-C-format-val type-data val))
  320. (suffix (car formated))
  321. (data (cdr formated)))
  322. (format "%s %s%s = %s;"
  323. type
  324. var
  325. suffix
  326. data))))
  327. (defun org-babel-C-table-sizes-to-C (pair)
  328. "Create constants of table dimensions, if PAIR is a table."
  329. (when (listp (cdr pair))
  330. (cond
  331. ((listp (cadr pair)) ;; a table
  332. (concat
  333. (format "const int %s_rows = %d;" (car pair) (length (cdr pair)))
  334. "\n"
  335. (format "const int %s_cols = %d;" (car pair) (length (cadr pair)))))
  336. (t ;; a list declared in the #+begin_src line
  337. (format "const int %s_cols = %d;" (car pair) (length (cdr pair)))))))
  338. (defun org-babel-C-utility-header-to-C ()
  339. "Generate a utility function to convert a column name
  340. into a column number."
  341. (case org-babel-c-variant
  342. ((c cpp)
  343. "int get_column_num (int nbcols, const char** header, const char* column)
  344. {
  345. int c;
  346. for (c=0; c<nbcols; c++)
  347. if (strcmp(header[c],column)==0)
  348. return c;
  349. return -1;
  350. }
  351. "
  352. )
  353. (d
  354. "int get_column_num (string[] header, string column)
  355. {
  356. foreach (c, h; header)
  357. if (h==column)
  358. return to!int(c);
  359. return -1;
  360. }
  361. "
  362. )))
  363. (defun org-babel-C-header-to-C (head)
  364. "Convert an elisp list of header table into a C or D vector
  365. specifying a variable with the name of the table."
  366. (let ((table (car head))
  367. (headers (cdr head)))
  368. (concat
  369. (format
  370. (case org-babel-c-variant
  371. ((c cpp) "const char* %s_header[%d] = {%s};")
  372. (d "string %s_header[%d] = [%s];"))
  373. table
  374. (length headers)
  375. (mapconcat (lambda (h) (format "%S" h)) headers ","))
  376. "\n"
  377. (case org-babel-c-variant
  378. ((c cpp)
  379. (format
  380. "const char* %s_h (int row, const char* col) { return %s[row][get_column_num(%d,%s_header,col)]; }"
  381. table table (length headers) table))
  382. (d
  383. (format
  384. "string %s_h (ulong row, string col) { return %s[row][get_column_num(%s_header,col)]; }"
  385. table table table))))))
  386. (provide 'ob-C)
  387. ;;; ob-C.el ends here