ob-C.el: Tiny formatting fix
[org-mode/org-kjn.git] / lisp / ob-C.el
blob505b2909639bc1496a80c6262d32674209111fe3
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
6 ;; Thierry Banel
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating C, C++, D code.
29 ;; very limited implementation:
30 ;; - currently only support :results output
31 ;; - not much in the way of error feedback
33 ;;; Code:
34 (eval-when-compile
35 (require 'cl))
36 (require 'ob)
37 (require 'cc-mode)
39 (declare-function org-entry-get "org"
40 (pom property &optional inherit literal-nil))
41 (declare-function org-remove-indentation "org" (code &optional n))
43 (defvar org-babel-tangle-lang-exts)
44 (add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp"))
45 (add-to-list 'org-babel-tangle-lang-exts '("D" . "d"))
47 (defvar org-babel-default-header-args:C '())
49 (defvar org-babel-C-compiler "gcc"
50 "Command used to compile a C source code file into an
51 executable.")
53 (defvar org-babel-C++-compiler "g++"
54 "Command used to compile a C++ source code file into an
55 executable.")
57 (defvar org-babel-D-compiler "rdmd"
58 "Command used to compile and execute a D source code file.")
60 (defvar org-babel-c-variant nil
61 "Internal variable used to hold which type of C (e.g. C or C++ or D)
62 is currently being evaluated.")
64 (defun org-babel-execute:cpp (body params)
65 "Execute BODY according to PARAMS.
66 This function calls `org-babel-execute:C++'."
67 (org-babel-execute:C++ body params))
69 (defun org-babel-execute:C++ (body params)
70 "Execute a block of C++ code with org-babel.
71 This function is called by `org-babel-execute-src-block'."
72 (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params)))
74 (defun org-babel-expand-body:C++ (body params)
75 "Expand a block of C++ code with org-babel according to it's
76 header arguments."
77 (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand-C++ body params)))
79 (defun org-babel-execute:D (body params)
80 "Execute a block of D code with org-babel.
81 This function is called by `org-babel-execute-src-block'."
82 (let ((org-babel-c-variant 'd)) (org-babel-C-execute body params)))
84 (defun org-babel-expand-body:D (body params)
85 "Expand a block of D code with org-babel according to it's
86 header arguments."
87 (let ((org-babel-c-variant 'd)) (org-babel-C-expand-D body params)))
89 (defun org-babel-execute:C (body params)
90 "Execute a block of C code with org-babel.
91 This function is called by `org-babel-execute-src-block'."
92 (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
94 (defun org-babel-expand-body:C (body params)
95 "Expand a block of C code with org-babel according to it's
96 header arguments."
97 (let ((org-babel-c-variant 'c)) (org-babel-C-expand-C body params)))
99 (defun org-babel-C-execute (body params)
100 "This function should only be called by `org-babel-execute:C'
101 or `org-babel-execute:C++' or `org-babel-execute:D'."
102 (let* ((tmp-src-file (org-babel-temp-file
103 "C-src-"
104 (case org-babel-c-variant
105 (c ".c" )
106 (cpp ".cpp")
107 (d ".d" ))))
108 (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext)) ;; not used for D
109 (cmdline (cdr (assoc :cmdline params)))
110 (cmdline (if cmdline (concat " " cmdline) ""))
111 (flags (cdr (assoc :flags params)))
112 (flags (mapconcat 'identity
113 (if (listp flags) flags (list flags)) " "))
114 (full-body
115 (case org-babel-c-variant
116 (c (org-babel-C-expand-C body params))
117 (cpp (org-babel-C-expand-C++ body params))
118 (d (org-babel-C-expand-D body params)))))
119 (with-temp-file tmp-src-file (insert full-body))
120 (case org-babel-c-variant
121 ((c cpp)
122 (org-babel-eval
123 (format "%s -o %s %s %s"
124 (case org-babel-c-variant
125 (c org-babel-C-compiler)
126 (cpp org-babel-C++-compiler))
127 (org-babel-process-file-name tmp-bin-file)
128 flags
129 (org-babel-process-file-name tmp-src-file)) ""))
130 (d nil)) ;; no separate compilation for D
131 (let ((results
132 (org-babel-eval
133 (case org-babel-c-variant
134 ((c cpp)
135 (concat tmp-bin-file cmdline))
137 (format "%s %s %s %s"
138 org-babel-D-compiler
139 flags
140 (org-babel-process-file-name tmp-src-file)
141 cmdline)))
142 "")))
143 (when results
144 (setq results (org-babel-trim (org-remove-indentation results)))
145 (org-babel-reassemble-table
146 (org-babel-result-cond (cdr (assoc :result-params params))
147 (org-babel-read results t)
148 (let ((tmp-file (org-babel-temp-file "c-")))
149 (with-temp-file tmp-file (insert results))
150 (org-babel-import-elisp-from-file tmp-file)))
151 (org-babel-pick-name
152 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
153 (org-babel-pick-name
154 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames 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 (org-babel-C-expand-C body params))
162 (defun org-babel-C-expand-C (body params)
163 "Expand a block of C or C++ code with org-babel according to
164 it's header arguments."
165 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
166 (colnames (cdar (org-babel-get-header params :colname-names)))
167 (main-p (not (string= (cdr (assoc :main params)) "no")))
168 (includes (or (cdr (assoc :includes params))
169 (org-babel-read (org-entry-get nil "includes" t))))
170 (defines (org-babel-read
171 (or (cdr (assoc :defines params))
172 (org-babel-read (org-entry-get nil "defines" t))))))
173 (unless (listp includes) (setq includes (list includes)))
174 (setq includes (append includes '("<string.h>" "<stdio.h>" "<stdlib.h>")))
175 (mapconcat 'identity
176 (list
177 ;; includes
178 (mapconcat
179 (lambda (inc) (format "#include %s" inc))
180 includes "\n")
181 ;; defines
182 (mapconcat
183 (lambda (inc) (format "#define %s" inc))
184 (if (listp defines) defines (list defines)) "\n")
185 ;; variables
186 (mapconcat 'org-babel-C-var-to-C vars "\n")
187 ;; table sizes
188 (mapconcat 'org-babel-C-table-sizes-to-C vars "\n")
189 ;; tables headers utility
190 (when colnames
191 (org-babel-C-utility-header-to-C))
192 ;; tables headers
193 (mapconcat 'org-babel-C-header-to-C colnames "\n")
194 ;; body
195 (if main-p
196 (org-babel-C-ensure-main-wrap body)
197 body) "\n") "\n")))
199 (defun org-babel-C-expand-D (body params)
200 "Expand a block of D code with org-babel according to
201 it's header arguments."
202 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
203 (colnames (cdar (org-babel-get-header params :colname-names)))
204 (main-p (not (string= (cdr (assoc :main params)) "no")))
205 (imports (or (cdr (assoc :imports params))
206 (org-babel-read (org-entry-get nil "imports" t)))))
207 (unless (listp imports) (setq imports (list imports)))
208 (setq imports (append imports '("std.stdio" "std.conv")))
209 (mapconcat 'identity
210 (list
211 "module mmm;"
212 ;; imports
213 (mapconcat
214 (lambda (inc) (format "import %s;" inc))
215 imports "\n")
216 ;; variables
217 (mapconcat 'org-babel-C-var-to-C vars "\n")
218 ;; table sizes
219 (mapconcat 'org-babel-C-table-sizes-to-C vars "\n")
220 ;; tables headers utility
221 (when colnames
222 (org-babel-C-utility-header-to-C))
223 ;; tables headers
224 (mapconcat 'org-babel-C-header-to-C colnames "\n")
225 ;; body
226 (if main-p
227 (org-babel-C-ensure-main-wrap body)
228 body) "\n") "\n")))
230 (defun org-babel-C-ensure-main-wrap (body)
231 "Wrap BODY in a \"main\" function call if none exists."
232 (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
233 body
234 (format "int main() {\n%s\nreturn 0;\n}\n" body)))
236 (defun org-babel-prep-session:C (session params)
237 "This function does nothing as C is a compiled language with no
238 support for sessions"
239 (error "C is a compiled languages -- no support for sessions"))
241 (defun org-babel-load-session:C (session body params)
242 "This function does nothing as C is a compiled language with no
243 support for sessions"
244 (error "C is a compiled languages -- no support for sessions"))
246 ;; helper functions
248 (defun org-babel-C-format-val (type val)
249 "Handle the FORMAT part of TYPE with the data from VAL."
250 (let ((format-data (cadr type)))
251 (if (stringp format-data)
252 (cons "" (format format-data val))
253 (funcall format-data val))))
255 (defun org-babel-C-val-to-C-type (val)
256 "Determine the type of VAL.
257 Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type.
258 FORMAT can be either a format string or a function which is called with VAL."
259 (let* ((basetype (org-babel-C-val-to-base-type val))
260 (type
261 (case basetype
262 (integerp '("int" "%d"))
263 (floatp '("double" "%f"))
264 (stringp
265 (list
266 (if (equal org-babel-c-variant 'd) "string" "const char*")
267 "\"%s\""))
268 (t (error "unknown type %S" basetype)))))
269 (cond
270 ((integerp val) type) ;; an integer declared in the #+begin_src line
271 ((floatp val) type) ;; a numeric declared in the #+begin_src line
272 ((and (listp val) (listp (car val))) ;; a table
273 `(,(car type)
274 (lambda (val)
275 (cons
276 (format "[%d][%d]" (length val) (length (car val)))
277 (concat
278 (if (equal org-babel-c-variant 'd) "[\n" "{\n")
279 (mapconcat
280 (lambda (v)
281 (concat
282 (if (equal org-babel-c-variant 'd) " [" " {")
283 (mapconcat (lambda (w) (format ,(cadr type) w)) v ",")
284 (if (equal org-babel-c-variant 'd) "]" "}")))
286 ",\n")
287 (if (equal org-babel-c-variant 'd) "\n]" "\n}"))))))
288 ((or (listp val) (vectorp val)) ;; a list declared in the #+begin_src line
289 `(,(car type)
290 (lambda (val)
291 (cons
292 (format "[%d]" (length val))
293 (concat
294 (if (equal org-babel-c-variant 'd) "[" "{")
295 (mapconcat (lambda (v) (format ,(cadr type) v)) val ",")
296 (if (equal org-babel-c-variant 'd) "]" "}"))))))
297 (t ;; treat unknown types as string
298 type))))
300 (defun org-babel-C-val-to-base-type (val)
301 "Determine the base type of VAL which may be
302 'integerp if all base values are integers
303 'floatp if all base values are either floating points or integers
304 'stringp otherwise."
305 (cond
306 ((integerp val) 'integerp)
307 ((floatp val) 'floatp)
308 ((or (listp val) (vectorp val))
309 (let ((type nil))
310 (mapc (lambda (v)
311 (case (org-babel-C-val-to-base-type v)
312 (stringp (setq type 'stringp))
313 (floatp
314 (if (or (not type) (eq type 'integerp))
315 (setq type 'floatp)))
316 (integerp
317 (unless type (setq type 'integerp)))))
318 val)
319 type))
320 (t 'stringp)))
322 (defun org-babel-C-var-to-C (pair)
323 "Convert an elisp val into a string of C code specifying a var
324 of the same value."
325 ;; TODO list support
326 (let ((var (car pair))
327 (val (cdr pair)))
328 (when (symbolp val)
329 (setq val (symbol-name val))
330 (when (= (length val) 1)
331 (setq val (string-to-char val))))
332 (let* ((type-data (org-babel-C-val-to-C-type val))
333 (type (car type-data))
334 (formated (org-babel-C-format-val type-data val))
335 (suffix (car formated))
336 (data (cdr formated)))
337 (format "%s %s%s = %s;"
338 type
340 suffix
341 data))))
343 (defun org-babel-C-table-sizes-to-C (pair)
344 "Create constants of table dimensions, if PAIR is a table."
345 (when (listp (cdr pair))
346 (cond
347 ((listp (cadr pair)) ;; a table
348 (concat
349 (format "const int %s_rows = %d;" (car pair) (length (cdr pair)))
350 "\n"
351 (format "const int %s_cols = %d;" (car pair) (length (cadr pair)))))
352 (t ;; a list declared in the #+begin_src line
353 (format "const int %s_cols = %d;" (car pair) (length (cdr pair)))))))
355 (defun org-babel-C-utility-header-to-C ()
356 "Generate a utility function to convert a column name
357 into a column number."
358 (case org-babel-c-variant
359 ((c cpp)
360 "int get_column_num (int nbcols, const char** header, const char* column)
362 int c;
363 for (c=0; c<nbcols; c++)
364 if (strcmp(header[c],column)==0)
365 return c;
366 return -1;
371 "int get_column_num (string[] header, string column)
373 foreach (c, h; header)
374 if (h==column)
375 return to!int(c);
376 return -1;
381 (defun org-babel-C-header-to-C (head)
382 "Convert an elisp list of header table into a C or D vector
383 specifying a variable with the name of the table."
384 (let ((table (car head))
385 (headers (cdr head)))
386 (concat
387 (format
388 (case org-babel-c-variant
389 ((c cpp) "const char* %s_header[%d] = {%s};")
390 (d "string %s_header[%d] = [%s];"))
391 table
392 (length headers)
393 (mapconcat (lambda (h) (format "%S" h)) headers ","))
394 "\n"
395 (case org-babel-c-variant
396 ((c cpp)
397 (format
398 "const char* %s_h (int row, const char* col) { return %s[row][get_column_num(%d,%s_header,col)]; }"
399 table table (length headers) table))
401 (format
402 "string %s_h (ulong row, string col) { return %s[row][get_column_num(%s_header,col)]; }"
403 table table table))))))
405 (provide 'ob-C)
407 ;;; ob-C.el ends here