ob-C.el 13 KB

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