ox-latex: Small refactor
[org-mode.git] / lisp / ob-C.el
blob64e9d209b4b5c285d38094cb7cbb49a1f8db9218
1 ;;; ob-C.el --- org-babel functions for C and similar languages
3 ;; Copyright (C) 2010-2015 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 (require 'ob)
35 (require 'cc-mode)
37 (declare-function org-entry-get "org"
38 (pom property &optional inherit literal-nil))
39 (declare-function org-remove-indentation "org" (code &optional n))
41 (defvar org-babel-tangle-lang-exts)
42 (add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp"))
43 (add-to-list 'org-babel-tangle-lang-exts '("D" . "d"))
45 (defvar org-babel-default-header-args:C '())
47 (defcustom org-babel-C-compiler "gcc"
48 "Command used to compile a C source code file into an executable.
49 May be either a command in the path, like gcc
50 or an absolute path name, like /usr/local/bin/gcc
51 parameter may be used, like gcc -v"
52 :group 'org-babel
53 :version "24.3"
54 :type 'string)
56 (defcustom org-babel-C++-compiler "g++"
57 "Command used to compile a C++ source code file into an executable.
58 May be either a command in the path, like g++
59 or an absolute path name, like /usr/local/bin/g++
60 parameter may be used, like g++ -v"
61 :group 'org-babel
62 :version "24.3"
63 :type 'string)
65 (defcustom org-babel-D-compiler "rdmd"
66 "Command used to compile and execute a D source code file.
67 May be either a command in the path, like rdmd
68 or an absolute path name, like /usr/local/bin/rdmd
69 parameter may be used, like rdmd --chatty"
70 :group 'org-babel
71 :version "24.3"
72 :type 'string)
74 (defvar org-babel-c-variant nil
75 "Internal variable used to hold which type of C (e.g. C or C++ or D)
76 is currently being evaluated.")
78 (defun org-babel-execute:cpp (body params)
79 "Execute BODY according to PARAMS.
80 This function calls `org-babel-execute:C++'."
81 (org-babel-execute:C++ body params))
83 (defun org-babel-expand-body:cpp (body params)
84 "Expand a block of C++ code with org-babel according to it's
85 header arguments."
86 (org-babel-expand-body:C++ 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 'cpp)) (org-babel-C-execute body params)))
93 (defun org-babel-expand-body:C++ (body params)
94 "Expand a block of C++ code with org-babel according to it's
95 header arguments."
96 (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand-C++ body params)))
98 (defun org-babel-execute:D (body params)
99 "Execute a block of D code with org-babel.
100 This function is called by `org-babel-execute-src-block'."
101 (let ((org-babel-c-variant 'd)) (org-babel-C-execute body params)))
103 (defun org-babel-expand-body:D (body params)
104 "Expand a block of D code with org-babel according to it's
105 header arguments."
106 (let ((org-babel-c-variant 'd)) (org-babel-C-expand-D body params)))
108 (defun org-babel-execute:C (body params)
109 "Execute a block of C code with org-babel.
110 This function is called by `org-babel-execute-src-block'."
111 (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params)))
113 (defun org-babel-expand-body:C (body params)
114 "Expand a block of C code with org-babel according to it's
115 header arguments."
116 (let ((org-babel-c-variant 'c)) (org-babel-C-expand-C body params)))
118 (defun org-babel-C-execute (body params)
119 "This function should only be called by `org-babel-execute:C'
120 or `org-babel-execute:C++' or `org-babel-execute:D'."
121 (let* ((tmp-src-file (org-babel-temp-file
122 "C-src-"
123 (case org-babel-c-variant
124 (c ".c" )
125 (cpp ".cpp")
126 (d ".d" ))))
127 (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext)) ;; not used for D
128 (cmdline (cdr (assoc :cmdline params)))
129 (cmdline (if cmdline (concat " " cmdline) ""))
130 (flags (cdr (assoc :flags params)))
131 (flags (mapconcat 'identity
132 (if (listp flags) flags (list flags)) " "))
133 (full-body
134 (case org-babel-c-variant
135 (c (org-babel-C-expand-C body params))
136 (cpp (org-babel-C-expand-C++ body params))
137 (d (org-babel-C-expand-D body params)))))
138 (with-temp-file tmp-src-file (insert full-body))
139 (case org-babel-c-variant
140 ((c cpp)
141 (org-babel-eval
142 (format "%s -o %s %s %s"
143 (case org-babel-c-variant
144 (c org-babel-C-compiler)
145 (cpp org-babel-C++-compiler))
146 (org-babel-process-file-name tmp-bin-file)
147 flags
148 (org-babel-process-file-name tmp-src-file)) ""))
149 (d nil)) ;; no separate compilation for D
150 (let ((results
151 (org-babel-eval
152 (case org-babel-c-variant
153 ((c cpp)
154 (concat tmp-bin-file cmdline))
156 (format "%s %s %s %s"
157 org-babel-D-compiler
158 flags
159 (org-babel-process-file-name tmp-src-file)
160 cmdline)))
161 "")))
162 (when results
163 (setq results (org-babel-trim (org-remove-indentation results)))
164 (org-babel-reassemble-table
165 (org-babel-result-cond (cdr (assoc :result-params params))
166 (org-babel-read results t)
167 (let ((tmp-file (org-babel-temp-file "c-")))
168 (with-temp-file tmp-file (insert results))
169 (org-babel-import-elisp-from-file tmp-file)))
170 (org-babel-pick-name
171 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
172 (org-babel-pick-name
173 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
176 (defun org-babel-C-expand-C++ (body params)
177 "Expand a block of C or C++ code with org-babel according to
178 it's header arguments."
179 (org-babel-C-expand-C body params))
181 (defun org-babel-C-expand-C (body params)
182 "Expand a block of C or C++ code with org-babel according to
183 it's header arguments."
184 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
185 (colnames (cdar (org-babel-get-header params :colname-names)))
186 (main-p (not (string= (cdr (assoc :main params)) "no")))
187 (includes (org-babel-read
188 (or (cdr (assoc :includes params))
189 (org-entry-get nil "includes" t))
190 nil))
191 (defines (org-babel-read
192 (or (cdr (assoc :defines params))
193 (org-entry-get nil "defines" t))
194 nil)))
195 (when (stringp includes)
196 (setq includes (split-string includes)))
197 (when (stringp defines)
198 (let ((y nil)
199 (result (list t)))
200 (dolist (x (split-string defines))
201 (if (null y)
202 (setq y x)
203 (nconc result (list (concat y " " x)))
204 (setq y nil)))
205 (setq defines (cdr result))))
206 (mapconcat 'identity
207 (list
208 ;; includes
209 (mapconcat
210 (lambda (inc) (format "#include %s" inc))
211 includes "\n")
212 ;; defines
213 (mapconcat
214 (lambda (inc) (format "#define %s" inc))
215 (if (listp defines) defines (list defines)) "\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-expand-D (body params)
231 "Expand a block of D code with org-babel according to
232 it's header arguments."
233 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
234 (colnames (cdar (org-babel-get-header params :colname-names)))
235 (main-p (not (string= (cdr (assoc :main params)) "no")))
236 (imports (or (cdr (assoc :imports params))
237 (org-babel-read (org-entry-get nil "imports" t)))))
238 (when (stringp imports)
239 (setq imports (split-string imports)))
240 (setq imports (append imports '("std.stdio" "std.conv")))
241 (mapconcat 'identity
242 (list
243 "module mmm;"
244 ;; imports
245 (mapconcat
246 (lambda (inc) (format "import %s;" inc))
247 imports "\n")
248 ;; variables
249 (mapconcat 'org-babel-C-var-to-C vars "\n")
250 ;; table sizes
251 (mapconcat 'org-babel-C-table-sizes-to-C vars "\n")
252 ;; tables headers utility
253 (when colnames
254 (org-babel-C-utility-header-to-C))
255 ;; tables headers
256 (mapconcat 'org-babel-C-header-to-C colnames "\n")
257 ;; body
258 (if main-p
259 (org-babel-C-ensure-main-wrap body)
260 body) "\n") "\n")))
262 (defun org-babel-C-ensure-main-wrap (body)
263 "Wrap BODY in a \"main\" function call if none exists."
264 (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
265 body
266 (format "int main() {\n%s\nreturn 0;\n}\n" body)))
268 (defun org-babel-prep-session:C (session params)
269 "This function does nothing as C is a compiled language with no
270 support for sessions"
271 (error "C is a compiled languages -- no support for sessions"))
273 (defun org-babel-load-session:C (session body params)
274 "This function does nothing as C is a compiled language with no
275 support for sessions"
276 (error "C is a compiled languages -- no support for sessions"))
278 ;; helper functions
280 (defun org-babel-C-format-val (type val)
281 "Handle the FORMAT part of TYPE with the data from VAL."
282 (let ((format-data (cadr type)))
283 (if (stringp format-data)
284 (cons "" (format format-data val))
285 (funcall format-data val))))
287 (defun org-babel-C-val-to-C-type (val)
288 "Determine the type of VAL.
289 Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type.
290 FORMAT can be either a format string or a function which is called with VAL."
291 (let* ((basetype (org-babel-C-val-to-base-type val))
292 (type
293 (case basetype
294 (integerp '("int" "%d"))
295 (floatp '("double" "%f"))
296 (stringp
297 (list
298 (if (equal org-babel-c-variant 'd) "string" "const char*")
299 "\"%s\""))
300 (t (error "unknown type %S" basetype)))))
301 (cond
302 ((integerp val) type) ;; an integer declared in the #+begin_src line
303 ((floatp val) type) ;; a numeric declared in the #+begin_src line
304 ((and (listp val) (listp (car val))) ;; a table
305 `(,(car type)
306 (lambda (val)
307 (cons
308 (format "[%d][%d]" (length val) (length (car val)))
309 (concat
310 (if (equal org-babel-c-variant 'd) "[\n" "{\n")
311 (mapconcat
312 (lambda (v)
313 (concat
314 (if (equal org-babel-c-variant 'd) " [" " {")
315 (mapconcat (lambda (w) (format ,(cadr type) w)) v ",")
316 (if (equal org-babel-c-variant 'd) "]" "}")))
318 ",\n")
319 (if (equal org-babel-c-variant 'd) "\n]" "\n}"))))))
320 ((or (listp val) (vectorp val)) ;; a list declared in the #+begin_src line
321 `(,(car type)
322 (lambda (val)
323 (cons
324 (format "[%d]" (length val))
325 (concat
326 (if (equal org-babel-c-variant 'd) "[" "{")
327 (mapconcat (lambda (v) (format ,(cadr type) v)) val ",")
328 (if (equal org-babel-c-variant 'd) "]" "}"))))))
329 (t ;; treat unknown types as string
330 type))))
332 (defun org-babel-C-val-to-base-type (val)
333 "Determine the base type of VAL which may be
334 `integerp' if all base values are integers
335 `floatp' if all base values are either floating points or integers
336 `stringp' otherwise."
337 (cond
338 ((integerp val) 'integerp)
339 ((floatp val) 'floatp)
340 ((or (listp val) (vectorp val))
341 (let ((type nil))
342 (mapc (lambda (v)
343 (case (org-babel-C-val-to-base-type v)
344 (stringp (setq type 'stringp))
345 (floatp
346 (if (or (not type) (eq type 'integerp))
347 (setq type 'floatp)))
348 (integerp
349 (unless type (setq type 'integerp)))))
350 val)
351 type))
352 (t 'stringp)))
354 (defun org-babel-C-var-to-C (pair)
355 "Convert an elisp val into a string of C code specifying a var
356 of the same value."
357 ;; TODO list support
358 (let ((var (car pair))
359 (val (cdr pair)))
360 (when (symbolp val)
361 (setq val (symbol-name val))
362 (when (= (length val) 1)
363 (setq val (string-to-char val))))
364 (let* ((type-data (org-babel-C-val-to-C-type val))
365 (type (car type-data))
366 (formated (org-babel-C-format-val type-data val))
367 (suffix (car formated))
368 (data (cdr formated)))
369 (format "%s %s%s = %s;"
370 type
372 suffix
373 data))))
375 (defun org-babel-C-table-sizes-to-C (pair)
376 "Create constants of table dimensions, if PAIR is a table."
377 (when (listp (cdr pair))
378 (cond
379 ((listp (cadr pair)) ;; a table
380 (concat
381 (format "const int %s_rows = %d;" (car pair) (length (cdr pair)))
382 "\n"
383 (format "const int %s_cols = %d;" (car pair) (length (cadr pair)))))
384 (t ;; a list declared in the #+begin_src line
385 (format "const int %s_cols = %d;" (car pair) (length (cdr pair)))))))
387 (defun org-babel-C-utility-header-to-C ()
388 "Generate a utility function to convert a column name
389 into a column number."
390 (case org-babel-c-variant
391 ((c cpp)
392 "int get_column_num (int nbcols, const char** header, const char* column)
394 int c;
395 for (c=0; c<nbcols; c++)
396 if (strcmp(header[c],column)==0)
397 return c;
398 return -1;
403 "int get_column_num (string[] header, string column)
405 foreach (c, h; header)
406 if (h==column)
407 return to!int(c);
408 return -1;
413 (defun org-babel-C-header-to-C (head)
414 "Convert an elisp list of header table into a C or D vector
415 specifying a variable with the name of the table."
416 (let ((table (car head))
417 (headers (cdr head)))
418 (concat
419 (format
420 (case org-babel-c-variant
421 ((c cpp) "const char* %s_header[%d] = {%s};")
422 (d "string %s_header[%d] = [%s];"))
423 table
424 (length headers)
425 (mapconcat (lambda (h) (format "%S" h)) headers ","))
426 "\n"
427 (case org-babel-c-variant
428 ((c cpp)
429 (format
430 "const char* %s_h (int row, const char* col) { return %s[row][get_column_num(%d,%s_header,col)]; }"
431 table table (length headers) table))
433 (format
434 "string %s_h (ulong row, string col) { return %s[row][get_column_num(%s_header,col)]; }"
435 table table table))))))
437 (provide 'ob-C)
439 ;;; ob-C.el ends here