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/>.
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
40 (declare-function org-entry-get
"org"
41 (pom property
&optional inherit literal-nil
))
42 (declare-function org-remove-indentation
"org" (code &optional n
))
44 (defvar org-babel-tangle-lang-exts
)
45 (add-to-list 'org-babel-tangle-lang-exts
'("C++" .
"cpp"))
46 (add-to-list 'org-babel-tangle-lang-exts
'("D" .
"d"))
48 (defvar org-babel-default-header-args
:C
'())
50 (defvar org-babel-C-compiler
"gcc"
51 "Command used to compile a C source code file into an
54 (defvar org-babel-C
++-compiler
"g++"
55 "Command used to compile a C++ source code file into an
58 (defvar org-babel-D-compiler
"rdmd"
59 "Command used to compile and execute a D source code file.")
61 (defvar org-babel-c-variant nil
62 "Internal variable used to hold which type of C (e.g. C or C++ or D)
63 is currently being evaluated.")
65 (defun org-babel-execute:cpp
(body params
)
66 "Execute BODY according to PARAMS.
67 This function calls `org-babel-execute:C++'."
68 (org-babel-execute:C
++ body params
))
70 (defun org-babel-execute:C
++ (body params
)
71 "Execute a block of C++ code with org-babel.
72 This function is called by `org-babel-execute-src-block'."
73 (let ((org-babel-c-variant 'cpp
)) (org-babel-C-execute body params
)))
75 ;;(defun org-babel-expand-body:C++ (body params) ;; unused
76 ;; "Expand a block of C++ code with org-babel according to it's
77 ;;header arguments (calls `org-babel-C-expand')."
78 ;; (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand body params)))
80 (defun org-babel-execute:D
(body params
)
81 "Execute a block of D code with org-babel.
82 This function is called by `org-babel-execute-src-block'."
83 (let ((org-babel-c-variant 'd
)) (org-babel-C-execute body params
)))
85 ;; (defun org-babel-expand-body:D (body params) ;; unused
86 ;; "Expand a block of D code with org-babel according to it's
87 ;;header arguments (calls `org-babel-C-expand')."
88 ;; (let ((org-babel-c-variant 'd)) (org-babel-C-expand body params)))
90 (defun org-babel-execute:C
(body params
)
91 "Execute a block of C code with org-babel.
92 This function is called by `org-babel-execute-src-block'."
93 (let ((org-babel-c-variant 'c
)) (org-babel-C-execute body params
)))
95 ;; (defun org-babel-expand-body:c (body params) ;; unused
96 ;; "Expand a block of C code with org-babel according to it's
97 ;;header arguments (calls `org-babel-C-expand')."
98 ;; (let ((org-babel-c-variant 'c)) (org-babel-C-expand body params)))
100 (defun org-babel-C-execute (body params
)
101 "This function should only be called by `org-babel-execute:C'
102 or `org-babel-execute:C++' or `org-babel-execute:D'."
103 (let* ((tmp-src-file (org-babel-temp-file
106 ((equal org-babel-c-variant
'c
) ".c" )
107 ((equal org-babel-c-variant
'cpp
) ".cpp")
108 ((equal org-babel-c-variant
'd
) ".d" ))))
109 (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext
)) ;; not used for D
110 (cmdline (cdr (assoc :cmdline params
)))
111 (cmdline (if cmdline
(concat " " cmdline
) ""))
112 (flags (cdr (assoc :flags params
)))
113 (flags (mapconcat 'identity
114 (if (listp flags
) flags
(list flags
)) " "))
116 (cond ((equal org-babel-c-variant
'c
) (org-babel-C-expand-C body params
))
117 ((equal org-babel-c-variant
'cpp
) (org-babel-C-expand-C++ body params
))
118 ((equal org-babel-c-variant
'd
) (org-babel-C-expand-D body params
)))))
119 (with-temp-file tmp-src-file
(insert full-body
))
120 (if (memq org-babel-c-variant
'(c cpp
)) ;; no separate compilation for D
122 (format "%s -o %s %s %s"
124 ((equal org-babel-c-variant
'c
) org-babel-C-compiler
)
125 ((equal org-babel-c-variant
'cpp
) org-babel-C
++-compiler
))
126 (org-babel-process-file-name tmp-bin-file
)
128 (org-babel-process-file-name tmp-src-file
)) ""))
131 (org-remove-indentation
133 (cond ((memq org-babel-c-variant
'(c cpp
))
134 (concat tmp-bin-file cmdline
))
135 ((equal org-babel-c-variant
'd
)
136 (format "%s %s %s %s"
139 (org-babel-process-file-name tmp-src-file
)
142 (org-babel-reassemble-table
143 (org-babel-result-cond (cdr (assoc :result-params params
))
144 (org-babel-read results t
)
145 (let ((tmp-file (org-babel-temp-file "c-")))
146 (with-temp-file tmp-file
(insert results
))
147 (org-babel-import-elisp-from-file tmp-file
)))
149 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
151 (cdr (assoc :rowname-names params
)) (cdr (assoc :rownames params
)))))
154 (defun org-babel-C-expand-C++ (body params
)
155 "Expand a block of C or C++ code with org-babel according to
156 it's header arguments."
157 (org-babel-C-expand-C body params
))
159 (defun org-babel-C-expand-C (body params
)
160 "Expand a block of C or C++ code with org-babel according to
161 it's header arguments."
162 (let ((vars (mapcar #'cdr
(org-babel-get-header params
:var
)))
163 (main-p (not (string= (cdr (assoc :main params
)) "no")))
164 (includes (or (cdr (assoc :includes params
))
165 (org-babel-read (org-entry-get nil
"includes" t
))))
166 (defines (org-babel-read
167 (or (cdr (assoc :defines params
))
168 (org-babel-read (org-entry-get nil
"defines" t
))))))
173 (lambda (inc) (format "#include %s" inc
))
174 (if (listp includes
) includes
(list includes
)) "\n")
177 (lambda (inc) (format "#define %s" inc
))
178 (if (listp defines
) defines
(list defines
)) "\n")
180 (mapconcat 'org-babel-C-var-to-C vars
"\n")
183 (org-babel-C-ensure-main-wrap body
)
186 (defun org-babel-C-expand-D (body params
)
187 "Expand a block of D code with org-babel according to
188 it's header arguments."
189 (let ((vars (mapcar #'cdr
(org-babel-get-header params
:var
)))
190 (main-p (not (string= (cdr (assoc :main params
)) "no")))
191 (imports (or (cdr (assoc :imports params
))
192 (org-babel-read (org-entry-get nil
"imports" t
)))))
198 (lambda (inc) (format "import %s;" inc
))
199 (if (listp imports
) imports
(list imports
)) "\n")
201 (mapconcat 'org-babel-C-var-to-C vars
"\n")
204 (org-babel-C-ensure-main-wrap body
)
207 (defun org-babel-C-ensure-main-wrap (body)
208 "Wrap BODY in a \"main\" function call if none exists."
209 (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body
)
211 (format "int main() {\n%s\nreturn 0;\n}\n" body
)))
213 (defun org-babel-prep-session:C
(session params
)
214 "This function does nothing as C is a compiled language with no
215 support for sessions"
216 (error "C is a compiled languages -- no support for sessions"))
218 (defun org-babel-load-session:C
(session body params
)
219 "This function does nothing as C is a compiled language with no
220 support for sessions"
221 (error "C is a compiled languages -- no support for sessions"))
225 (defun org-babel-C-format-val (type val
)
226 "Handle the FORMAT part of TYPE with the data from VAL."
227 (let ((format-data (cadr type
)))
228 (if (stringp format-data
)
229 (cons "" (format format-data val
))
230 (funcall format-data val
))))
232 (defun org-babel-C-val-to-C-type (val)
233 "Determine the type of VAL.
234 Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type.
235 FORMAT can be either a format string or a function which is called with VAL."
237 ((integerp val
) '("int" "%d"))
238 ((floatp val
) '("double" "%f"))
239 ((or (listp val
) (vectorp val
))
240 (lexical-let ((type (org-babel-C-val-to-C-list-type val
)))
246 (car (org-babel-C-format-val type
(elt val
0))))
247 (concat (if (equal org-babel-c-variant
'd
) "[ " "{ ")
248 (mapconcat (lambda (v)
249 (cdr (org-babel-C-format-val type v
)))
252 (if (equal org-babel-c-variant
'd
) " ]" " }")))))))
253 (t ;; treat unknown types as string
255 (if (equal org-babel-c-variant
'd
) "string" "const char*")
258 (defun org-babel-C-val-to-C-list-type (val)
259 "Determine the C array type of a VAL."
263 (let* ((tmp-type (org-babel-C-val-to-C-type i
))
264 (type-name (car type
))
265 (tmp-type-name (car tmp-type
)))
266 (when (and type
(not (string= type-name tmp-type-name
)))
267 (if (and (member type-name
'("int" "double" "int32_t"))
268 (member tmp-type-name
'("int" "double" "int32_t")))
269 (setq tmp-type
'("double" "%f"))
270 (error "Only homogeneous lists are supported by C. You can not mix %s and %s"
273 (setq type tmp-type
)))
277 (defun org-babel-C-var-to-C (pair)
278 "Convert an elisp val into a string of C code specifying a var
281 (let ((var (car pair
))
284 (setq val
(symbol-name val
))
285 (when (= (length val
) 1)
286 (setq val
(string-to-char val
))))
287 (let* ((type-data (org-babel-C-val-to-C-type val
))
288 (type (car type-data
))
289 (formated (org-babel-C-format-val type-data val
))
290 (suffix (car formated
))
291 (data (cdr formated
)))
292 (format "%s %s%s = %s;"
300 ;;; ob-C.el ends here