ob-C.el 14 KB

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