ob-C.el 14 KB

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