1 ;;; ob-C.el --- org-babel functions for C and similar languages
3 ;; Copyright (C) 2010-2012 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
37 (declare-function org-entry-get
"org"
38 (pom property
&optional inherit literal-nil
))
41 (defvar org-babel-tangle-lang-exts
)
42 (add-to-list 'org-babel-tangle-lang-exts
'("C++" .
"cpp"))
44 (defvar org-babel-default-header-args
:C
'())
46 (defvar org-babel-C-compiler
"gcc"
47 "Command used to compile a C source code file into an
50 (defvar org-babel-C
++-compiler
"g++"
51 "Command used to compile a C++ source code file into an
54 (defvar org-babel-c-variant nil
55 "Internal variable used to hold which type of C (e.g. C or C++)
56 is currently being evaluated.")
58 (defun org-babel-execute:cpp
(body params
)
59 "Execute BODY according to PARAMS. This function calls
60 `org-babel-execute:C++'."
61 (org-babel-execute:C
++ body params
))
63 (defun org-babel-execute:C
++ (body params
)
64 "Execute a block of C++ code with org-babel. This function is
65 called by `org-babel-execute-src-block'."
66 (let ((org-babel-c-variant 'cpp
)) (org-babel-C-execute body params
)))
68 (defun org-babel-expand-body:C
++ (body params
)
69 "Expand a block of C++ code with org-babel according to it's
70 header arguments (calls `org-babel-C-expand')."
71 (let ((org-babel-c-variant 'cpp
)) (org-babel-C-expand body params
)))
73 (defun org-babel-execute:C
(body params
)
74 "Execute a block of C code with org-babel. This function is
75 called by `org-babel-execute-src-block'."
76 (let ((org-babel-c-variant 'c
)) (org-babel-C-execute body params
)))
78 (defun org-babel-expand-body:c
(body params
)
79 "Expand a block of C code with org-babel according to it's
80 header arguments (calls `org-babel-C-expand')."
81 (let ((org-babel-c-variant 'c
)) (org-babel-C-expand body params
)))
83 (defun org-babel-C-execute (body params
)
84 "This function should only be called by `org-babel-execute:C'
85 or `org-babel-execute:C++'."
86 (let* ((tmp-src-file (org-babel-temp-file
89 ((equal org-babel-c-variant
'c
) ".c")
90 ((equal org-babel-c-variant
'cpp
) ".cpp"))))
91 (tmp-bin-file (org-babel-temp-file
93 (if (equal system-type
'windows-nt
) ".exe" "")))
94 (cmdline (cdr (assoc :cmdline params
)))
95 (flags (cdr (assoc :flags params
)))
96 (full-body (org-babel-C-expand body params
))
99 (with-temp-file tmp-src-file
(insert full-body
))
101 (format "%s -o %s %s %s"
103 ((equal org-babel-c-variant
'c
) org-babel-C-compiler
)
104 ((equal org-babel-c-variant
'cpp
) org-babel-C
++-compiler
))
105 (org-babel-process-file-name tmp-bin-file
)
107 (if (listp flags
) flags
(list flags
)) " ")
108 (org-babel-process-file-name tmp-src-file
)) ""))))
110 (org-babel-reassemble-table
111 (if (member "vector" (cdr (assoc :result-params params
)))
112 (let ((tmp-file (org-babel-temp-file "c-")))
113 (with-temp-file tmp-file
(insert results
))
114 (org-babel-import-elisp-from-file tmp-file
))
115 (org-babel-read results
))
117 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
119 (cdr (assoc :rowname-names params
)) (cdr (assoc :rownames params
)))))
122 (concat tmp-bin-file
(if cmdline
(concat " " cmdline
) "")) "")))))
124 (defun org-babel-C-expand (body params
)
125 "Expand a block of C or C++ code with org-babel according to
126 it's header arguments."
127 (let ((vars (mapcar #'cdr
(org-babel-get-header params
:var
)))
128 (main-p (not (string= (cdr (assoc :main params
)) "no")))
129 (includes (or (cdr (assoc :includes params
))
130 (org-babel-read (org-entry-get nil
"includes" t
))))
131 (defines (org-babel-read
132 (or (cdr (assoc :defines params
))
133 (org-babel-read (org-entry-get nil
"defines" t
))))))
138 (lambda (inc) (format "#include %s" inc
))
139 (if (listp includes
) includes
(list includes
)) "\n")
142 (lambda (inc) (format "#define %s" inc
))
143 (if (listp defines
) defines
(list defines
)) "\n")
145 (mapconcat 'org-babel-C-var-to-C vars
"\n")
148 (org-babel-C-ensure-main-wrap body
)
151 (defun org-babel-C-ensure-main-wrap (body)
152 "Wrap body in a \"main\" function call if none exists."
153 (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body
)
155 (format "int main() {\n%s\nreturn(0);\n}\n" body
)))
157 (defun org-babel-prep-session:C
(session params
)
158 "This function does nothing as C is a compiled language with no
159 support for sessions"
160 (error "C is a compiled languages -- no support for sessions"))
162 (defun org-babel-load-session:C
(session body params
)
163 "This function does nothing as C is a compiled language with no
164 support for sessions"
165 (error "C is a compiled languages -- no support for sessions"))
169 (defun org-babel-C-var-to-C (pair)
170 "Convert an elisp val into a string of C code specifying a var
173 (let ((var (car pair
))
176 (setq val
(symbol-name val
))
177 (when (= (length val
) 1)
178 (setq val
(string-to-char val
))))
181 (format "int %S = %S;" var val
))
183 (format "double %S = %S;" var val
))
185 (format "char %S = '%S';" var val
))
187 (format "char %S[%d] = \"%s\";"
188 var
(+ 1 (length val
)) val
))
190 (format "u32 %S = %S;" var val
)))))
197 ;;; ob-C.el ends here