1 ;;; ob-C.el --- org-babel functions for C and similar languages
3 ;; Copyright (C) 2010-2013 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
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 code.
28 ;; very limited implementation:
29 ;; - currently only support :results output
30 ;; - not much in the way of error feedback
36 (declare-function org-entry-get
"org"
37 (pom property
&optional inherit literal-nil
))
40 (defvar org-babel-tangle-lang-exts
)
41 (add-to-list 'org-babel-tangle-lang-exts
'("C++" .
"cpp"))
43 (defvar org-babel-default-header-args
:C
'())
45 (defvar org-babel-C-compiler
"gcc"
46 "Command used to compile a C source code file into an
49 (defvar org-babel-C
++-compiler
"g++"
50 "Command used to compile a C++ source code file into an
53 (defvar org-babel-c-variant nil
54 "Internal variable used to hold which type of C (e.g. C or C++)
55 is currently being evaluated.")
57 (defun org-babel-execute:cpp
(body params
)
58 "Execute BODY according to PARAMS.
59 This function calls `org-babel-execute:C++'."
60 (org-babel-execute:C
++ body params
))
62 (defun org-babel-execute:C
++ (body params
)
63 "Execute a block of C++ code with org-babel.
64 This function is called by `org-babel-execute-src-block'."
65 (let ((org-babel-c-variant 'cpp
)) (org-babel-C-execute body params
)))
67 (defun org-babel-expand-body:C
++ (body params
)
68 "Expand a block of C++ code with org-babel according to it's
69 header arguments (calls `org-babel-C-expand')."
70 (let ((org-babel-c-variant 'cpp
)) (org-babel-C-expand body params
)))
72 (defun org-babel-execute:C
(body params
)
73 "Execute a block of C code with org-babel.
74 This function is called by `org-babel-execute-src-block'."
75 (let ((org-babel-c-variant 'c
)) (org-babel-C-execute body params
)))
77 (defun org-babel-expand-body:c
(body params
)
78 "Expand a block of C code with org-babel according to it's
79 header arguments (calls `org-babel-C-expand')."
80 (let ((org-babel-c-variant 'c
)) (org-babel-C-expand body params
)))
82 (defun org-babel-C-execute (body params
)
83 "This function should only be called by `org-babel-execute:C'
84 or `org-babel-execute:C++'."
85 (let* ((tmp-src-file (org-babel-temp-file
88 ((equal org-babel-c-variant
'c
) ".c")
89 ((equal org-babel-c-variant
'cpp
) ".cpp"))))
90 (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext
))
91 (cmdline (cdr (assoc :cmdline params
)))
92 (flags (cdr (assoc :flags params
)))
93 (full-body (org-babel-C-expand body params
))
96 (with-temp-file tmp-src-file
(insert full-body
))
98 (format "%s -o %s %s %s"
100 ((equal org-babel-c-variant
'c
) org-babel-C-compiler
)
101 ((equal org-babel-c-variant
'cpp
) org-babel-C
++-compiler
))
102 (org-babel-process-file-name tmp-bin-file
)
104 (if (listp flags
) flags
(list flags
)) " ")
105 (org-babel-process-file-name tmp-src-file
)) ""))))
107 (org-babel-reassemble-table
108 (org-babel-result-cond (cdr (assoc :result-params params
))
109 (org-babel-read results
)
110 (let ((tmp-file (org-babel-temp-file "c-")))
111 (with-temp-file tmp-file
(insert results
))
112 (org-babel-import-elisp-from-file tmp-file
)))
114 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
116 (cdr (assoc :rowname-names params
)) (cdr (assoc :rownames params
)))))
119 (concat tmp-bin-file
(if cmdline
(concat " " cmdline
) "")) "")))))
121 (defun org-babel-C-expand (body params
)
122 "Expand a block of C or C++ code with org-babel according to
123 it's header arguments."
124 (let ((vars (mapcar #'cdr
(org-babel-get-header params
:var
)))
125 (main-p (not (string= (cdr (assoc :main params
)) "no")))
126 (includes (or (cdr (assoc :includes params
))
127 (org-babel-read (org-entry-get nil
"includes" t
))))
128 (defines (org-babel-read
129 (or (cdr (assoc :defines params
))
130 (org-babel-read (org-entry-get nil
"defines" t
))))))
135 (lambda (inc) (format "#include %s" inc
))
136 (if (listp includes
) includes
(list includes
)) "\n")
139 (lambda (inc) (format "#define %s" inc
))
140 (if (listp defines
) defines
(list defines
)) "\n")
142 (mapconcat 'org-babel-C-var-to-C vars
"\n")
145 (org-babel-C-ensure-main-wrap body
)
148 (defun org-babel-C-ensure-main-wrap (body)
149 "Wrap BODY in a \"main\" function call if none exists."
150 (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body
)
152 (format "int main() {\n%s\nreturn 0;\n}\n" body
)))
154 (defun org-babel-prep-session:C
(session params
)
155 "This function does nothing as C is a compiled language with no
156 support for sessions"
157 (error "C is a compiled languages -- no support for sessions"))
159 (defun org-babel-load-session:C
(session body params
)
160 "This function does nothing as C is a compiled language with no
161 support for sessions"
162 (error "C is a compiled languages -- no support for sessions"))
166 (defun org-babel-C-format-val (type val
)
167 "Handle the FORMAT part of TYPE with the data from VAL."
168 (let ((format-data (cadr type
)))
169 (if (stringp format-data
)
170 (cons "" (format format-data val
))
171 (funcall format-data val
))))
173 (defun org-babel-C-val-to-C-type (val)
174 "Determine the type of VAL.
175 Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type.
176 FORMAT can be either a format string or a function which is called with VAL."
178 ((integerp val
) '("int" "%d"))
179 ((floatp val
) '("double" "%f"))
180 ((or (listp val
) (vectorp val
))
181 (lexical-let ((type (org-babel-C-val-to-C-list-type val
)))
187 (car (org-babel-C-format-val type
(elt val
0))))
189 (mapconcat (lambda (v)
190 (cdr (org-babel-C-format-val type v
)))
194 (t ;; treat unknown types as string
195 '("char" (lambda (val)
196 (let ((s (format "%s" val
))) ;; convert to string for unknown types
197 (cons (format "[%d]" (1+ (length s
)))
198 (concat "\"" s
"\""))))))))
200 (defun org-babel-C-val-to-C-list-type (val)
201 "Determine the C array type of a VAL."
205 (let* ((tmp-type (org-babel-C-val-to-C-type i
))
206 (type-name (car type
))
207 (tmp-type-name (car tmp-type
)))
208 (when (and type
(not (string= type-name tmp-type-name
)))
209 (if (and (member type-name
'("int" "double" "int32_t"))
210 (member tmp-type-name
'("int" "double" "int32_t")))
211 (setq tmp-type
'("double" "" "%f"))
212 (error "Only homogeneous lists are supported by C. You can not mix %s and %s"
215 (setq type tmp-type
)))
219 (defun org-babel-C-var-to-C (pair)
220 "Convert an elisp val into a string of C code specifying a var
223 (let ((var (car pair
))
226 (setq val
(symbol-name val
))
227 (when (= (length val
) 1)
228 (setq val
(string-to-char val
))))
229 (let* ((type-data (org-babel-C-val-to-C-type val
))
230 (type (car type-data
))
231 (formated (org-babel-C-format-val type-data val
))
232 (suffix (car formated
))
233 (data (cdr formated
)))
234 (format "%s %s%s = %s;"
242 ;;; ob-C.el ends here