ob-C.el 14 KB

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