ob-C.el 8.3 KB

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