ob-C.el 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ;;; ob-C.el --- org-babel functions for C and similar languages
  2. ;; Copyright (C) 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  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 code.
  20. ;;
  21. ;; very limited implementation:
  22. ;; - currently only support :results output
  23. ;; - not much in the way of error feedback
  24. ;;; Code:
  25. (require 'ob)
  26. (require 'ob-eval)
  27. (require 'org)
  28. (require 'cc-mode)
  29. (declare-function org-entry-get "org"
  30. (pom property &optional inherit literal-nil))
  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. This function calls
  44. `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. This function is
  48. 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 &optional processed-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 processed-params)))
  54. (defun org-babel-execute:C (body params)
  55. "Execute a block of C code with org-babel. This function is
  56. 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 &optional processed-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 processed-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* ((processed-params (org-babel-process-params params))
  66. (tmp-src-file (make-temp-file "org-babel-C-src" nil
  67. (cond
  68. ((equal org-babel-c-variant 'c) ".c")
  69. ((equal org-babel-c-variant 'cpp) ".cpp"))))
  70. (tmp-bin-file (make-temp-file "org-babel-C-bin"))
  71. (tmp-out-file (make-temp-file "org-babel-C-out"))
  72. (cmdline (cdr (assoc :cmdline params)))
  73. (flags (cdr (assoc :flags params)))
  74. (full-body (org-babel-C-expand body params))
  75. (compile
  76. (progn
  77. (with-temp-file tmp-src-file (insert full-body))
  78. (org-babel-eval
  79. (format "%s -o %s %s %s"
  80. (cond
  81. ((equal org-babel-c-variant 'c) org-babel-C-compiler)
  82. ((equal org-babel-c-variant 'cpp) org-babel-c++-compiler))
  83. tmp-bin-file
  84. (mapconcat 'identity
  85. (if (listp flags) flags (list flags)) " ")
  86. tmp-src-file) ""))))
  87. (org-babel-reassemble-table
  88. (org-babel-read
  89. (org-babel-trim
  90. (org-babel-eval
  91. (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))
  92. (org-babel-pick-name
  93. (nth 4 processed-params) (cdr (assoc :colnames params)))
  94. (org-babel-pick-name
  95. (nth 5 processed-params) (cdr (assoc :rownames params))))))
  96. (defun org-babel-C-expand (body params &optional processed-params)
  97. "Expand a block of C or C++ code with org-babel according to
  98. it's header arguments."
  99. (let ((vars (nth 1 (or processed-params
  100. (org-babel-process-params params))))
  101. (main-p (not (string= (cdr (assoc :main params)) "no")))
  102. (includes (or (cdr (assoc :includes params))
  103. (org-babel-read (org-entry-get nil "includes" t))))
  104. (defines (org-babel-read
  105. (or (cdr (assoc :defines params))
  106. (org-babel-read (org-entry-get nil "defines" t))))))
  107. (mapconcat 'identity
  108. (list
  109. ;; includes
  110. (mapconcat
  111. (lambda (inc) (format "#include %s" inc))
  112. (if (listp includes) includes (list includes)) "\n")
  113. ;; defines
  114. (mapconcat
  115. (lambda (inc) (format "#define %s" inc))
  116. (if (listp defines) defines (list defines)) "\n")
  117. ;; variables
  118. (mapconcat 'org-babel-C-var-to-C vars "\n")
  119. ;; body
  120. (if main-p
  121. (org-babel-C-ensure-main-wrap body)
  122. body) "\n") "\n")))
  123. (defun org-babel-C-ensure-main-wrap (body)
  124. "Wrap body in a \"main\" function call if none exists."
  125. (if (string-match "^[ \t]*[intvod]+[ \t]*main[ \t]*(.*)" body)
  126. body
  127. (format "int main() {\n%s\n}\n" body)))
  128. (defun org-babel-prep-session:C (session params)
  129. "This function does nothing as C is a compiled language with no
  130. support for sessions"
  131. (error "C is a compiled languages -- no support for sessions"))
  132. (defun org-babel-load-session:C (session body 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. ;; helper functions
  137. (defun org-babel-C-var-to-C (pair)
  138. "Convert an elisp val into a string of C code specifying a var
  139. of the same value."
  140. ;; TODO list support
  141. (let ((var (car pair))
  142. (val (cdr pair)))
  143. (when (symbolp val)
  144. (setq val (symbol-name val))
  145. (when (= (length val) 1)
  146. (setq val (string-to-char val))))
  147. (cond
  148. ((integerp val)
  149. (format "int %S = %S;" var val))
  150. ((floatp val)
  151. (format "double %S = %S;" var val))
  152. ((or (characterp val))
  153. (format "char %S = '%S';" var val))
  154. ((stringp val)
  155. (format "char %S[%d] = \"%s\";"
  156. var (+ 1 (length val)) val))
  157. (t
  158. (format "u32 %S = %S;" var val)))))
  159. (provide 'ob-C)
  160. ;; arch-tag: 8f49e462-54e3-417b-9a8d-423864893b37
  161. ;;; ob-C.el ends here