ob-C.el 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. ;;; ob-C.el --- org-babel functions for C and similar languages
  2. ;; Copyright (C) 2010-2013 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. (require 'ob)
  25. (require 'cc-mode)
  26. (declare-function org-entry-get "org"
  27. (pom property &optional inherit literal-nil))
  28. (defvar org-babel-tangle-lang-exts)
  29. (add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp"))
  30. (defvar org-babel-default-header-args:C '())
  31. (defvar org-babel-C-compiler "gcc"
  32. "Command used to compile a C source code file into an
  33. executable.")
  34. (defvar org-babel-C++-compiler "g++"
  35. "Command used to compile a C++ source code file into an
  36. executable.")
  37. (defvar org-babel-c-variant nil
  38. "Internal variable used to hold which type of C (e.g. C or C++)
  39. is currently being evaluated.")
  40. (defun org-babel-execute:cpp (body params)
  41. "Execute BODY according to PARAMS. This function calls
  42. `org-babel-execute:C++'."
  43. (org-babel-execute:C++ body params))
  44. (defun org-babel-execute:C++ (body params)
  45. "Execute a block of C++ code with org-babel. This function is
  46. called by `org-babel-execute-src-block'."
  47. (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params)))
  48. (defun org-babel-expand-body:C++ (body params)
  49. "Expand a block of C++ code with org-babel according to it's
  50. header arguments (calls `org-babel-C-expand')."
  51. (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand body params)))
  52. (defun org-babel-execute:C (body params)
  53. "Execute a block of C code with org-babel. This function is
  54. called by `org-babel-execute-src-block'."
  55. (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
  56. (defun org-babel-expand-body:c (body params)
  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 'c)) (org-babel-C-expand body params)))
  60. (defun org-babel-C-execute (body params)
  61. "This function should only be called by `org-babel-execute:C'
  62. or `org-babel-execute:C++'."
  63. (let* ((tmp-src-file (org-babel-temp-file
  64. "C-src-"
  65. (cond
  66. ((equal org-babel-c-variant 'c) ".c")
  67. ((equal org-babel-c-variant 'cpp) ".cpp"))))
  68. (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext))
  69. (cmdline (cdr (assoc :cmdline params)))
  70. (flags (cdr (assoc :flags params)))
  71. (full-body (org-babel-C-expand body params))
  72. (compile
  73. (progn
  74. (with-temp-file tmp-src-file (insert full-body))
  75. (org-babel-eval
  76. (format "%s -o %s %s %s"
  77. (cond
  78. ((equal org-babel-c-variant 'c) org-babel-C-compiler)
  79. ((equal org-babel-c-variant 'cpp) org-babel-C++-compiler))
  80. (org-babel-process-file-name tmp-bin-file)
  81. (mapconcat 'identity
  82. (if (listp flags) flags (list flags)) " ")
  83. (org-babel-process-file-name tmp-src-file)) ""))))
  84. ((lambda (results)
  85. (org-babel-reassemble-table
  86. (org-babel-result-cond (cdr (assoc :result-params params))
  87. (org-babel-read results)
  88. (let ((tmp-file (org-babel-temp-file "c-")))
  89. (with-temp-file tmp-file (insert results))
  90. (org-babel-import-elisp-from-file tmp-file)))
  91. (org-babel-pick-name
  92. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  93. (org-babel-pick-name
  94. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
  95. (org-babel-trim
  96. (org-babel-eval
  97. (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
  98. (defun org-babel-C-expand (body params)
  99. "Expand a block of C or C++ code with org-babel according to
  100. it's header arguments."
  101. (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  102. (main-p (not (string= (cdr (assoc :main params)) "no")))
  103. (includes (or (cdr (assoc :includes params))
  104. (org-babel-read (org-entry-get nil "includes" t))))
  105. (defines (org-babel-read
  106. (or (cdr (assoc :defines params))
  107. (org-babel-read (org-entry-get nil "defines" t))))))
  108. (mapconcat 'identity
  109. (list
  110. ;; includes
  111. (mapconcat
  112. (lambda (inc) (format "#include %s" inc))
  113. (if (listp includes) includes (list includes)) "\n")
  114. ;; defines
  115. (mapconcat
  116. (lambda (inc) (format "#define %s" inc))
  117. (if (listp defines) defines (list defines)) "\n")
  118. ;; variables
  119. (mapconcat 'org-babel-C-var-to-C vars "\n")
  120. ;; body
  121. (if main-p
  122. (org-babel-C-ensure-main-wrap body)
  123. body) "\n") "\n")))
  124. (defun org-babel-C-ensure-main-wrap (body)
  125. "Wrap body in a \"main\" function call if none exists."
  126. (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
  127. body
  128. (format "int main() {\n%s\nreturn(0);\n}\n" body)))
  129. (defun org-babel-prep-session:C (session params)
  130. "This function does nothing as C is a compiled language with no
  131. support for sessions"
  132. (error "C is a compiled languages -- no support for sessions"))
  133. (defun org-babel-load-session:C (session body params)
  134. "This function does nothing as C is a compiled language with no
  135. support for sessions"
  136. (error "C is a compiled languages -- no support for sessions"))
  137. ;; helper functions
  138. (defun org-babel-C-var-to-C (pair)
  139. "Convert an elisp val into a string of C code specifying a var
  140. of the same value."
  141. ;; TODO list support
  142. (let ((var (car pair))
  143. (val (cdr pair)))
  144. (when (symbolp val)
  145. (setq val (symbol-name val))
  146. (when (= (length val) 1)
  147. (setq val (string-to-char val))))
  148. (cond
  149. ((integerp val)
  150. (format "int %S = %S;" var val))
  151. ((floatp val)
  152. (format "double %S = %S;" var val))
  153. ((or (integerp val))
  154. (format "char %S = '%S';" var val))
  155. ((stringp val)
  156. (format "char %S[%d] = \"%s\";"
  157. var (+ 1 (length val)) val))
  158. (t
  159. (format "u32 %S = %S;" var val)))))
  160. (provide 'ob-C)
  161. ;;; ob-C.el ends here