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
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/>.
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
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
53 (defvar org-babel-C
++-compiler
"g++"
54 "Command used to compile a C++ source code file into an
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
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
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
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
104 (case org-babel-c-variant
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
)) " "))
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
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
)
129 (org-babel-process-file-name tmp-src-file
)) ""))
130 (d nil
)) ;; no separate compilation for D
133 (case org-babel-c-variant
135 (concat tmp-bin-file cmdline
))
137 (format "%s %s %s %s"
140 (org-babel-process-file-name tmp-src-file
)
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
)))
152 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
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>")))
179 (lambda (inc) (format "#include %s" inc
))
183 (lambda (inc) (format "#define %s" inc
))
184 (if (listp defines
) defines
(list defines
)) "\n")
186 (mapconcat 'org-babel-C-var-to-C vars
"\n")
188 (mapconcat 'org-babel-C-table-sizes-to-C vars
"\n")
189 ;; tables headers utility
191 (org-babel-C-utility-header-to-C))
193 (mapconcat 'org-babel-C-header-to-C colnames
"\n")
196 (org-babel-C-ensure-main-wrap body
)
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")))
214 (lambda (inc) (format "import %s;" inc
))
217 (mapconcat 'org-babel-C-var-to-C vars
"\n")
219 (mapconcat 'org-babel-C-table-sizes-to-C vars
"\n")
220 ;; tables headers utility
222 (org-babel-C-utility-header-to-C))
224 (mapconcat 'org-babel-C-header-to-C colnames
"\n")
227 (org-babel-C-ensure-main-wrap body
)
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
)
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"))
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
))
262 (integerp '("int" "%d"))
263 (floatp '("double" "%f"))
266 (if (equal org-babel-c-variant
'd
) "string" "const char*")
268 (t (error "unknown type %S" basetype
)))))
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
276 (format "[%d][%d]" (length val
) (length (car val
)))
278 (if (equal org-babel-c-variant
'd
) "[\n" "{\n")
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
) "]" "}")))
287 (if (equal org-babel-c-variant
'd
) "\n]" "\n}"))))))
288 ((or (listp val
) (vectorp val
)) ;; a list declared in the #+begin_src line
292 (format "[%d]" (length val
))
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
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
306 ((integerp val
) 'integerp
)
307 ((floatp val
) 'floatp
)
308 ((or (listp val
) (vectorp val
))
311 (case (org-babel-C-val-to-base-type v
)
312 (stringp (setq type
'stringp
))
314 (if (or (not type
) (eq type
'integerp
))
315 (setq type
'floatp
)))
317 (unless type
(setq type
'integerp
)))))
322 (defun org-babel-C-var-to-C (pair)
323 "Convert an elisp val into a string of C code specifying a var
326 (let ((var (car pair
))
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;"
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
))
347 ((listp (cadr pair
)) ;; a table
349 (format "const int %s_rows = %d;" (car pair
) (length (cdr pair
)))
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
360 "int get_column_num (int nbcols, const char** header, const char* column)
363 for (c=0; c<nbcols; c++)
364 if (strcmp(header[c],column)==0)
371 "int get_column_num (string[] header, string column)
373 foreach (c, h; header)
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
)))
388 (case org-babel-c-variant
389 ((c cpp
) "const char* %s_header[%d] = {%s};")
390 (d "string %s_header[%d] = [%s];"))
393 (mapconcat (lambda (h) (format "%S" h
)) headers
","))
395 (case org-babel-c-variant
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
))
402 "string %s_h (ulong row, string col) { return %s[row][get_column_num(%s_header,col)]; }"
403 table table table
))))))
407 ;;; ob-C.el ends here