1 ;;; ob-C.el --- org-babel functions for C and similar languages
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; 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 code.
29 ;; very limited implementation:
30 ;; - currently only support :results output
31 ;; - not much in the way of error feedback
38 (declare-function org-entry-get
"org"
39 (pom property
&optional inherit literal-nil
))
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. This function calls
59 `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. This function is
64 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
&optional processed-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 processed-params
)))
72 (defun org-babel-execute:C
(body params
)
73 "Execute a block of C code with org-babel. This function is
74 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
&optional processed-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 processed-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* ((processed-params (org-babel-process-params params
))
86 (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 "C-bin-"))
92 (cmdline (cdr (assoc :cmdline params
)))
93 (flags (cdr (assoc :flags params
)))
94 (full-body (org-babel-C-expand body params
))
97 (with-temp-file tmp-src-file
(insert full-body
))
99 (format "%s -o %s %s %s"
101 ((equal org-babel-c-variant
'c
) org-babel-C-compiler
)
102 ((equal org-babel-c-variant
'cpp
) org-babel-c
++-compiler
))
103 (org-babel-process-file-name tmp-bin-file
)
105 (if (listp flags
) flags
(list flags
)) " ")
106 (org-babel-process-file-name tmp-src-file
)) ""))))
108 (org-babel-reassemble-table
109 (if (member "vector" (nth 2 processed-params
))
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
))
113 (org-babel-read results
))
115 (nth 4 processed-params
) (cdr (assoc :colnames params
)))
117 (nth 5 processed-params
) (cdr (assoc :rownames params
)))))
120 (concat tmp-bin-file
(if cmdline
(concat " " cmdline
) "")) "")))))
122 (defun org-babel-C-expand (body params
&optional processed-params
)
123 "Expand a block of C or C++ code with org-babel according to
124 it's header arguments."
125 (let ((vars (nth 1 (or processed-params
126 (org-babel-process-params params
))))
127 (main-p (not (string= (cdr (assoc :main params
)) "no")))
128 (includes (or (cdr (assoc :includes params
))
129 (org-babel-read (org-entry-get nil
"includes" t
))))
130 (defines (org-babel-read
131 (or (cdr (assoc :defines params
))
132 (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\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
))
184 ((or (characterp 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
)))))
195 ;; arch-tag: 8f49e462-54e3-417b-9a8d-423864893b37
197 ;;; ob-C.el ends here