Merge branch 'maint'
[org-mode/org-kjn.git] / lisp / ob-C.el
blobbbd05255f1c89c5d4e527a270b3e092ec051bdc0
1 ;;; ob-C.el --- org-babel functions for C and similar languages
3 ;; Copyright (C) 2010-2014 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte, Thierry Banel
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating C, C++, D code.
28 ;; very limited implementation:
29 ;; - currently only support :results output
30 ;; - not much in the way of error feedback
32 ;;; Code:
33 (require 'ob)
34 (require 'cc-mode)
35 (eval-when-compile
36 (require 'cl))
38 (declare-function org-entry-get "org"
39 (pom property &optional inherit literal-nil))
40 (declare-function org-remove-indentation "org" (code &optional n))
42 (defvar org-babel-tangle-lang-exts)
43 (add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp"))
44 (add-to-list 'org-babel-tangle-lang-exts '("D" . "d"))
46 (defvar org-babel-default-header-args:C '())
48 (defvar org-babel-C-compiler "gcc"
49 "Command used to compile a C source code file into an
50 executable.")
52 (defvar org-babel-C++-compiler "g++"
53 "Command used to compile a C++ source code file into an
54 executable.")
56 (defvar org-babel-D-compiler "rdmd"
57 "Command used to compile and execute a D source code file.")
59 (defvar org-babel-c-variant nil
60 "Internal variable used to hold which type of C (e.g. C or C++ or D)
61 is currently being evaluated.")
63 (defun org-babel-execute:cpp (body params)
64 "Execute BODY according to PARAMS.
65 This function calls `org-babel-execute:C++'."
66 (org-babel-execute:C++ 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 'cpp)) (org-babel-C-execute body params)))
73 ;;(defun org-babel-expand-body:C++ (body params) ;; unused
74 ;; "Expand a block of C++ code with org-babel according to it's
75 ;;header arguments (calls `org-babel-C-expand')."
76 ;; (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand body params)))
78 (defun org-babel-execute:D (body params)
79 "Execute a block of D code with org-babel.
80 This function is called by `org-babel-execute-src-block'."
81 (let ((org-babel-c-variant 'd)) (org-babel-C-execute body params)))
83 ;; (defun org-babel-expand-body:D (body params) ;; unused
84 ;; "Expand a block of D code with org-babel according to it's
85 ;;header arguments (calls `org-babel-C-expand')."
86 ;; (let ((org-babel-c-variant 'd)) (org-babel-C-expand body params)))
88 (defun org-babel-execute:C (body params)
89 "Execute a block of C code with org-babel.
90 This function is called by `org-babel-execute-src-block'."
91 (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
93 ;; (defun org-babel-expand-body:c (body params) ;; unused
94 ;; "Expand a block of C code with org-babel according to it's
95 ;;header arguments (calls `org-babel-C-expand')."
96 ;; (let ((org-babel-c-variant 'c)) (org-babel-C-expand body params)))
98 (defun org-babel-C-execute (body params)
99 "This function should only be called by `org-babel-execute:C'
100 or `org-babel-execute:C++' or `org-babel-execute:D'."
101 (let* ((tmp-src-file (org-babel-temp-file
102 "C-src-"
103 (cond
104 ((equal org-babel-c-variant 'c ) ".c" )
105 ((equal org-babel-c-variant 'cpp) ".cpp")
106 ((equal org-babel-c-variant 'd ) ".d" ))))
107 (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext)) ;; not used for D
108 (cmdline (cdr (assoc :cmdline params)))
109 (cmdline (if cmdline (concat " " cmdline) ""))
110 (flags (cdr (assoc :flags params)))
111 (flags (mapconcat 'identity
112 (if (listp flags) flags (list flags)) " "))
113 (full-body
114 (cond ((equal org-babel-c-variant 'c ) (org-babel-C-expand-C body params))
115 ((equal org-babel-c-variant 'cpp) (org-babel-C-expand-C++ body params))
116 ((equal org-babel-c-variant 'd ) (org-babel-C-expand-D body params)))))
117 (with-temp-file tmp-src-file (insert full-body))
118 (if (memq org-babel-c-variant '(c cpp)) ;; no separate compilation for D
119 (org-babel-eval
120 (format "%s -o %s %s %s"
121 (cond
122 ((equal org-babel-c-variant 'c ) org-babel-C-compiler)
123 ((equal org-babel-c-variant 'cpp) org-babel-C++-compiler))
124 (org-babel-process-file-name tmp-bin-file)
125 flags
126 (org-babel-process-file-name tmp-src-file)) ""))
127 (let ((results
128 (org-babel-trim
129 (org-remove-indentation
130 (org-babel-eval
131 (cond ((memq org-babel-c-variant '(c cpp))
132 (concat tmp-bin-file cmdline))
133 ((equal org-babel-c-variant 'd)
134 (format "%s %s %s %s"
135 org-babel-D-compiler
136 flags
137 (org-babel-process-file-name tmp-src-file)
138 cmdline)))
139 "")))))
140 (org-babel-reassemble-table
141 (org-babel-result-cond (cdr (assoc :result-params params))
142 (org-babel-read results t)
143 (let ((tmp-file (org-babel-temp-file "c-")))
144 (with-temp-file tmp-file (insert results))
145 (org-babel-import-elisp-from-file tmp-file)))
146 (org-babel-pick-name
147 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
148 (org-babel-pick-name
149 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
152 (defun org-babel-C-expand-C++ (body params)
153 "Expand a block of C or C++ code with org-babel according to
154 it's header arguments."
155 (org-babel-C-expand-C body params))
157 (defun org-babel-C-expand-C (body params)
158 "Expand a block of C or C++ code with org-babel according to
159 it's header arguments."
160 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
161 (main-p (not (string= (cdr (assoc :main params)) "no")))
162 (includes (or (cdr (assoc :includes params))
163 (org-babel-read (org-entry-get nil "includes" t))))
164 (defines (org-babel-read
165 (or (cdr (assoc :defines params))
166 (org-babel-read (org-entry-get nil "defines" t))))))
167 (mapconcat 'identity
168 (list
169 ;; includes
170 (mapconcat
171 (lambda (inc) (format "#include %s" inc))
172 (if (listp includes) includes (list includes)) "\n")
173 ;; defines
174 (mapconcat
175 (lambda (inc) (format "#define %s" inc))
176 (if (listp defines) defines (list defines)) "\n")
177 ;; variables
178 (mapconcat 'org-babel-C-var-to-C vars "\n")
179 ;; body
180 (if main-p
181 (org-babel-C-ensure-main-wrap body)
182 body) "\n") "\n")))
184 (defun org-babel-C-expand-D (body params)
185 "Expand a block of D code with org-babel according to
186 it's header arguments."
187 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
188 (main-p (not (string= (cdr (assoc :main params)) "no")))
189 (imports (or (cdr (assoc :imports params))
190 (org-babel-read (org-entry-get nil "imports" t)))))
191 (mapconcat 'identity
192 (list
193 "module mmm;"
194 ;; imports
195 (mapconcat
196 (lambda (inc) (format "import %s;" inc))
197 (if (listp imports) imports (list imports)) "\n")
198 ;; variables
199 (mapconcat 'org-babel-C-var-to-C vars "\n")
200 ;; body
201 (if main-p
202 (org-babel-C-ensure-main-wrap body)
203 body) "\n") "\n")))
205 (defun org-babel-C-ensure-main-wrap (body)
206 "Wrap BODY in a \"main\" function call if none exists."
207 (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
208 body
209 (format "int main() {\n%s\nreturn 0;\n}\n" body)))
211 (defun org-babel-prep-session:C (session params)
212 "This function does nothing as C is a compiled language with no
213 support for sessions"
214 (error "C is a compiled languages -- no support for sessions"))
216 (defun org-babel-load-session:C (session body params)
217 "This function does nothing as C is a compiled language with no
218 support for sessions"
219 (error "C is a compiled languages -- no support for sessions"))
221 ;; helper functions
223 (defun org-babel-C-format-val (type val)
224 "Handle the FORMAT part of TYPE with the data from VAL."
225 (let ((format-data (cadr type)))
226 (if (stringp format-data)
227 (cons "" (format format-data val))
228 (funcall format-data val))))
230 (defun org-babel-C-val-to-C-type (val)
231 "Determine the type of VAL.
232 Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type.
233 FORMAT can be either a format string or a function which is called with VAL."
234 (cond
235 ((integerp val) '("int" "%d"))
236 ((floatp val) '("double" "%f"))
237 ((or (listp val) (vectorp val))
238 (lexical-let ((type (org-babel-C-val-to-C-list-type val)))
239 (list (car type)
240 (lambda (val)
241 (cons
242 (format "[%d]%s"
243 (length val)
244 (car (org-babel-C-format-val type (elt val 0))))
245 (concat (if (equal org-babel-c-variant 'd) "[ " "{ ")
246 (mapconcat (lambda (v)
247 (cdr (org-babel-C-format-val type v)))
249 ", ")
250 (if (equal org-babel-c-variant 'd) " ]" " }")))))))
251 (t ;; treat unknown types as string
252 (list
253 (if (equal org-babel-c-variant 'd) "string" "const char*")
254 "\"%s\""))))
256 (defun org-babel-C-val-to-C-list-type (val)
257 "Determine the C array type of a VAL."
258 (let (type)
259 (mapc
260 #'(lambda (i)
261 (let* ((tmp-type (org-babel-C-val-to-C-type i))
262 (type-name (car type))
263 (tmp-type-name (car tmp-type)))
264 (when (and type (not (string= type-name tmp-type-name)))
265 (if (and (member type-name '("int" "double" "int32_t"))
266 (member tmp-type-name '("int" "double" "int32_t")))
267 (setq tmp-type '("double" "%f"))
268 (error "Only homogeneous lists are supported by C. You can not mix %s and %s"
269 type-name
270 tmp-type-name)))
271 (setq type tmp-type)))
272 val)
273 type))
275 (defun org-babel-C-var-to-C (pair)
276 "Convert an elisp val into a string of C code specifying a var
277 of the same value."
278 ;; TODO list support
279 (let ((var (car pair))
280 (val (cdr pair)))
281 (when (symbolp val)
282 (setq val (symbol-name val))
283 (when (= (length val) 1)
284 (setq val (string-to-char val))))
285 (let* ((type-data (org-babel-C-val-to-C-type val))
286 (type (car type-data))
287 (formated (org-babel-C-format-val type-data val))
288 (suffix (car formated))
289 (data (cdr formated)))
290 (format "%s %s%s = %s;"
291 type
293 suffix
294 data))))
296 (provide 'ob-C)
298 ;;; ob-C.el ends here