ob-C.el 13 KB

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