ob-C.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. (eval-when-compile
  29. (require 'cl))
  30. (declare-function org-entry-get "org"
  31. (pom property &optional inherit literal-nil))
  32. (declare-function org-remove-indentation "org" (code &optional n))
  33. (defvar org-babel-tangle-lang-exts)
  34. (add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp"))
  35. (add-to-list 'org-babel-tangle-lang-exts '("D" . "d"))
  36. (defvar org-babel-default-header-args:C '())
  37. (defvar org-babel-C-compiler "gcc"
  38. "Command used to compile a C source code file into an
  39. executable.")
  40. (defvar org-babel-C++-compiler "g++"
  41. "Command used to compile a C++ source code file into an
  42. executable.")
  43. (defvar org-babel-D-compiler "rdmd"
  44. "Command used to compile and execute a D source code file.")
  45. (defvar org-babel-c-variant nil
  46. "Internal variable used to hold which type of C (e.g. C or C++ or D)
  47. is currently being evaluated.")
  48. (defun org-babel-execute:cpp (body params)
  49. "Execute BODY according to PARAMS.
  50. This function calls `org-babel-execute:C++'."
  51. (org-babel-execute:C++ body params))
  52. (defun org-babel-execute:C++ (body params)
  53. "Execute a block of C++ code with org-babel.
  54. This function is called by `org-babel-execute-src-block'."
  55. (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params)))
  56. ;;(defun org-babel-expand-body:C++ (body params) ;; unused
  57. ;; "Expand a block of C++ code with org-babel according to it's
  58. ;;header arguments (calls `org-babel-C-expand')."
  59. ;; (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand body params)))
  60. (defun org-babel-execute:D (body params)
  61. "Execute a block of D code with org-babel.
  62. This function is called by `org-babel-execute-src-block'."
  63. (let ((org-babel-c-variant 'd)) (org-babel-C-execute body params)))
  64. ;; (defun org-babel-expand-body:D (body params) ;; unused
  65. ;; "Expand a block of D code with org-babel according to it's
  66. ;;header arguments (calls `org-babel-C-expand')."
  67. ;; (let ((org-babel-c-variant 'd)) (org-babel-C-expand body params)))
  68. (defun org-babel-execute:C (body params)
  69. "Execute a block of C code with org-babel.
  70. This function is called by `org-babel-execute-src-block'."
  71. (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
  72. ;; (defun org-babel-expand-body:c (body params) ;; unused
  73. ;; "Expand a block of C code with org-babel according to it's
  74. ;;header arguments (calls `org-babel-C-expand')."
  75. ;; (let ((org-babel-c-variant 'c)) (org-babel-C-expand body params)))
  76. (defun org-babel-C-execute (body params)
  77. "This function should only be called by `org-babel-execute:C'
  78. or `org-babel-execute:C++' or `org-babel-execute:D'."
  79. (let* ((tmp-src-file (org-babel-temp-file
  80. "C-src-"
  81. (cond
  82. ((equal org-babel-c-variant 'c ) ".c" )
  83. ((equal org-babel-c-variant 'cpp) ".cpp")
  84. ((equal org-babel-c-variant 'd ) ".d" ))))
  85. (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext)) ;; not used for D
  86. (cmdline (cdr (assoc :cmdline params)))
  87. (cmdline (if cmdline (concat " " cmdline) ""))
  88. (flags (cdr (assoc :flags params)))
  89. (flags (mapconcat 'identity
  90. (if (listp flags) flags (list flags)) " "))
  91. (full-body
  92. (cond ((equal org-babel-c-variant 'c ) (org-babel-C-expand-C body params))
  93. ((equal org-babel-c-variant 'cpp) (org-babel-C-expand-C++ body params))
  94. ((equal org-babel-c-variant 'd ) (org-babel-C-expand-D body params)))))
  95. (with-temp-file tmp-src-file (insert full-body))
  96. (if (memq org-babel-c-variant '(c cpp)) ;; no separate compilation for D
  97. (org-babel-eval
  98. (format "%s -o %s %s %s"
  99. (cond
  100. ((equal org-babel-c-variant 'c ) org-babel-C-compiler)
  101. ((equal org-babel-c-variant '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. (let ((results
  106. (org-babel-trim
  107. (org-remove-indentation
  108. (org-babel-eval
  109. (cond ((memq org-babel-c-variant '(c cpp))
  110. (concat tmp-bin-file cmdline))
  111. ((equal org-babel-c-variant '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. (org-babel-reassemble-table
  119. (org-babel-result-cond (cdr (assoc :result-params params))
  120. (org-babel-read results t)
  121. (let ((tmp-file (org-babel-temp-file "c-")))
  122. (with-temp-file tmp-file (insert results))
  123. (org-babel-import-elisp-from-file tmp-file)))
  124. (org-babel-pick-name
  125. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  126. (org-babel-pick-name
  127. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
  128. ))
  129. (defun org-babel-C-expand-C++ (body params)
  130. "Expand a block of C or C++ code with org-babel according to
  131. it's header arguments."
  132. (org-babel-C-expand-C body params))
  133. (defun org-babel-C-expand-C (body params)
  134. "Expand a block of C or C++ code with org-babel according to
  135. it's header arguments."
  136. (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  137. (main-p (not (string= (cdr (assoc :main params)) "no")))
  138. (includes (or (cdr (assoc :includes params))
  139. (org-babel-read (org-entry-get nil "includes" t))))
  140. (defines (org-babel-read
  141. (or (cdr (assoc :defines params))
  142. (org-babel-read (org-entry-get nil "defines" t))))))
  143. (mapconcat 'identity
  144. (list
  145. ;; includes
  146. (mapconcat
  147. (lambda (inc) (format "#include %s" inc))
  148. (if (listp includes) includes (list includes)) "\n")
  149. ;; defines
  150. (mapconcat
  151. (lambda (inc) (format "#define %s" inc))
  152. (if (listp defines) defines (list defines)) "\n")
  153. ;; variables
  154. (mapconcat 'org-babel-C-var-to-C vars "\n")
  155. ;; body
  156. (if main-p
  157. (org-babel-C-ensure-main-wrap body)
  158. body) "\n") "\n")))
  159. (defun org-babel-C-expand-D (body params)
  160. "Expand a block of D code with org-babel according to
  161. it's header arguments."
  162. (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  163. (main-p (not (string= (cdr (assoc :main params)) "no")))
  164. (imports (or (cdr (assoc :imports params))
  165. (org-babel-read (org-entry-get nil "imports" t)))))
  166. (mapconcat 'identity
  167. (list
  168. "module mmm;"
  169. ;; imports
  170. (mapconcat
  171. (lambda (inc) (format "import %s;" inc))
  172. (if (listp imports) imports (list imports)) "\n")
  173. ;; variables
  174. (mapconcat 'org-babel-C-var-to-C vars "\n")
  175. ;; body
  176. (if main-p
  177. (org-babel-C-ensure-main-wrap body)
  178. body) "\n") "\n")))
  179. (defun org-babel-C-ensure-main-wrap (body)
  180. "Wrap BODY in a \"main\" function call if none exists."
  181. (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
  182. body
  183. (format "int main() {\n%s\nreturn 0;\n}\n" body)))
  184. (defun org-babel-prep-session:C (session params)
  185. "This function does nothing as C is a compiled language with no
  186. support for sessions"
  187. (error "C is a compiled languages -- no support for sessions"))
  188. (defun org-babel-load-session:C (session body params)
  189. "This function does nothing as C is a compiled language with no
  190. support for sessions"
  191. (error "C is a compiled languages -- no support for sessions"))
  192. ;; helper functions
  193. (defun org-babel-C-format-val (type val)
  194. "Handle the FORMAT part of TYPE with the data from VAL."
  195. (let ((format-data (cadr type)))
  196. (if (stringp format-data)
  197. (cons "" (format format-data val))
  198. (funcall format-data val))))
  199. (defun org-babel-C-val-to-C-type (val)
  200. "Determine the type of VAL.
  201. Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type.
  202. FORMAT can be either a format string or a function which is called with VAL."
  203. (cond
  204. ((integerp val) '("int" "%d"))
  205. ((floatp val) '("double" "%f"))
  206. ((or (listp val) (vectorp val))
  207. (lexical-let ((type (org-babel-C-val-to-C-list-type val)))
  208. (list (car type)
  209. (lambda (val)
  210. (cons
  211. (format "[%d]%s"
  212. (length val)
  213. (car (org-babel-C-format-val type (elt val 0))))
  214. (concat (if (equal org-babel-c-variant 'd) "[ " "{ ")
  215. (mapconcat (lambda (v)
  216. (cdr (org-babel-C-format-val type v)))
  217. val
  218. ", ")
  219. (if (equal org-babel-c-variant 'd) " ]" " }")))))))
  220. (t ;; treat unknown types as string
  221. (list
  222. (if (equal org-babel-c-variant 'd) "string" "const char*")
  223. "\"%s\""))))
  224. (defun org-babel-C-val-to-C-list-type (val)
  225. "Determine the C array type of a VAL."
  226. (let (type)
  227. (mapc
  228. #'(lambda (i)
  229. (let* ((tmp-type (org-babel-C-val-to-C-type i))
  230. (type-name (car type))
  231. (tmp-type-name (car tmp-type)))
  232. (when (and type (not (string= type-name tmp-type-name)))
  233. (if (and (member type-name '("int" "double" "int32_t"))
  234. (member tmp-type-name '("int" "double" "int32_t")))
  235. (setq tmp-type '("double" "%f"))
  236. (error "Only homogeneous lists are supported by C. You can not mix %s and %s"
  237. type-name
  238. tmp-type-name)))
  239. (setq type tmp-type)))
  240. val)
  241. type))
  242. (defun org-babel-C-var-to-C (pair)
  243. "Convert an elisp val into a string of C code specifying a var
  244. of the same value."
  245. ;; TODO list support
  246. (let ((var (car pair))
  247. (val (cdr pair)))
  248. (when (symbolp val)
  249. (setq val (symbol-name val))
  250. (when (= (length val) 1)
  251. (setq val (string-to-char val))))
  252. (let* ((type-data (org-babel-C-val-to-C-type val))
  253. (type (car type-data))
  254. (formated (org-babel-C-format-val type-data val))
  255. (suffix (car formated))
  256. (data (cdr formated)))
  257. (format "%s %s%s = %s;"
  258. type
  259. var
  260. suffix
  261. data))))
  262. (provide 'ob-C)
  263. ;;; ob-C.el ends here