Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-C.el --- org-babel functions for C and similar languages |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2010-2014 Free Software Foundation, Inc. |
86fbb8ca CD |
4 | |
5 | ;; Author: Eric Schulte | |
6 | ;; Keywords: literate programming, reproducible research | |
7 | ;; Homepage: http://orgmode.org | |
86fbb8ca CD |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
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. | |
15 | ||
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. | |
20 | ||
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/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ||
26 | ;; Org-Babel support for evaluating C code. | |
27 | ;; | |
28 | ;; very limited implementation: | |
29 | ;; - currently only support :results output | |
30 | ;; - not much in the way of error feedback | |
31 | ||
32 | ;;; Code: | |
33 | (require 'ob) | |
86fbb8ca CD |
34 | (require 'cc-mode) |
35 | ||
36 | (declare-function org-entry-get "org" | |
37 | (pom property &optional inherit literal-nil)) | |
38 | ||
3ab2c837 BG |
39 | |
40 | (defvar org-babel-tangle-lang-exts) | |
41 | (add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp")) | |
86fbb8ca CD |
42 | |
43 | (defvar org-babel-default-header-args:C '()) | |
44 | ||
45 | (defvar org-babel-C-compiler "gcc" | |
46 | "Command used to compile a C source code file into an | |
271672fa | 47 | executable.") |
86fbb8ca | 48 | |
3ab2c837 BG |
49 | (defvar org-babel-C++-compiler "g++" |
50 | "Command used to compile a C++ source code file into an | |
271672fa | 51 | executable.") |
86fbb8ca CD |
52 | |
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.") | |
56 | ||
57 | (defun org-babel-execute:cpp (body params) | |
271672fa BG |
58 | "Execute BODY according to PARAMS. |
59 | This function calls `org-babel-execute:C++'." | |
3ab2c837 | 60 | (org-babel-execute:C++ body params)) |
86fbb8ca | 61 | |
3ab2c837 | 62 | (defun org-babel-execute:C++ (body params) |
271672fa BG |
63 | "Execute a block of C++ code with org-babel. |
64 | This function is called by `org-babel-execute-src-block'." | |
86fbb8ca CD |
65 | (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params))) |
66 | ||
3ab2c837 | 67 | (defun org-babel-expand-body:C++ (body params) |
86fbb8ca CD |
68 | "Expand a block of C++ code with org-babel according to it's |
69 | header arguments (calls `org-babel-C-expand')." | |
afe98dfa | 70 | (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand body params))) |
86fbb8ca CD |
71 | |
72 | (defun org-babel-execute:C (body params) | |
271672fa BG |
73 | "Execute a block of C code with org-babel. |
74 | This function is called by `org-babel-execute-src-block'." | |
86fbb8ca CD |
75 | (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params))) |
76 | ||
afe98dfa | 77 | (defun org-babel-expand-body:c (body params) |
86fbb8ca CD |
78 | "Expand a block of C code with org-babel according to it's |
79 | header arguments (calls `org-babel-C-expand')." | |
afe98dfa | 80 | (let ((org-babel-c-variant 'c)) (org-babel-C-expand body params))) |
86fbb8ca CD |
81 | |
82 | (defun org-babel-C-execute (body params) | |
83 | "This function should only be called by `org-babel-execute:C' | |
3ab2c837 | 84 | or `org-babel-execute:C++'." |
afe98dfa CD |
85 | (let* ((tmp-src-file (org-babel-temp-file |
86 | "C-src-" | |
87 | (cond | |
88 | ((equal org-babel-c-variant 'c) ".c") | |
89 | ((equal org-babel-c-variant 'cpp) ".cpp")))) | |
8223b1d2 | 90 | (tmp-bin-file (org-babel-temp-file "C-bin-" org-babel-exeext)) |
86fbb8ca CD |
91 | (cmdline (cdr (assoc :cmdline params))) |
92 | (flags (cdr (assoc :flags params))) | |
93 | (full-body (org-babel-C-expand body params)) | |
94 | (compile | |
95 | (progn | |
96 | (with-temp-file tmp-src-file (insert full-body)) | |
97 | (org-babel-eval | |
98 | (format "%s -o %s %s %s" | |
99 | (cond | |
100 | ((equal org-babel-c-variant 'c) org-babel-C-compiler) | |
3ab2c837 | 101 | ((equal org-babel-c-variant 'cpp) org-babel-C++-compiler)) |
afe98dfa | 102 | (org-babel-process-file-name tmp-bin-file) |
86fbb8ca CD |
103 | (mapconcat 'identity |
104 | (if (listp flags) flags (list flags)) " ") | |
afe98dfa | 105 | (org-babel-process-file-name tmp-src-file)) "")))) |
666ffc7e SM |
106 | (let ((results |
107 | (org-babel-trim | |
108 | (org-babel-eval | |
109 | (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))) | |
110 | (org-babel-reassemble-table | |
111 | (org-babel-result-cond (cdr (assoc :result-params params)) | |
112 | (org-babel-read results) | |
113 | (let ((tmp-file (org-babel-temp-file "c-"))) | |
114 | (with-temp-file tmp-file (insert results)) | |
115 | (org-babel-import-elisp-from-file tmp-file))) | |
116 | (org-babel-pick-name | |
117 | (cdr (assoc :colname-names params)) (cdr (assoc :colnames params))) | |
118 | (org-babel-pick-name | |
119 | (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))) | |
120 | )) | |
86fbb8ca | 121 | |
afe98dfa | 122 | (defun org-babel-C-expand (body params) |
86fbb8ca CD |
123 | "Expand a block of C or C++ code with org-babel according to |
124 | it's header arguments." | |
afe98dfa | 125 | (let ((vars (mapcar #'cdr (org-babel-get-header params :var))) |
86fbb8ca CD |
126 | (main-p (not (string= (cdr (assoc :main params)) "no"))) |
127 | (includes (or (cdr (assoc :includes params)) | |
128 | (org-babel-read (org-entry-get nil "includes" t)))) | |
129 | (defines (org-babel-read | |
130 | (or (cdr (assoc :defines params)) | |
131 | (org-babel-read (org-entry-get nil "defines" t)))))) | |
8223b1d2 BG |
132 | (mapconcat 'identity |
133 | (list | |
134 | ;; includes | |
135 | (mapconcat | |
136 | (lambda (inc) (format "#include %s" inc)) | |
137 | (if (listp includes) includes (list includes)) "\n") | |
138 | ;; defines | |
139 | (mapconcat | |
140 | (lambda (inc) (format "#define %s" inc)) | |
141 | (if (listp defines) defines (list defines)) "\n") | |
142 | ;; variables | |
143 | (mapconcat 'org-babel-C-var-to-C vars "\n") | |
144 | ;; body | |
145 | (if main-p | |
146 | (org-babel-C-ensure-main-wrap body) | |
147 | body) "\n") "\n"))) | |
86fbb8ca CD |
148 | |
149 | (defun org-babel-C-ensure-main-wrap (body) | |
271672fa | 150 | "Wrap BODY in a \"main\" function call if none exists." |
afe98dfa | 151 | (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body) |
86fbb8ca | 152 | body |
271672fa | 153 | (format "int main() {\n%s\nreturn 0;\n}\n" body))) |
86fbb8ca CD |
154 | |
155 | (defun org-babel-prep-session:C (session params) | |
156 | "This function does nothing as C is a compiled language with no | |
157 | support for sessions" | |
158 | (error "C is a compiled languages -- no support for sessions")) | |
159 | ||
160 | (defun org-babel-load-session:C (session body params) | |
161 | "This function does nothing as C is a compiled language with no | |
162 | support for sessions" | |
163 | (error "C is a compiled languages -- no support for sessions")) | |
164 | ||
165 | ;; helper functions | |
166 | ||
271672fa BG |
167 | (defun org-babel-C-format-val (type val) |
168 | "Handle the FORMAT part of TYPE with the data from VAL." | |
169 | (let ((format-data (cadr type))) | |
170 | (if (stringp format-data) | |
171 | (cons "" (format format-data val)) | |
172 | (funcall format-data val)))) | |
173 | ||
174 | (defun org-babel-C-val-to-C-type (val) | |
175 | "Determine the type of VAL. | |
176 | Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type. | |
177 | FORMAT can be either a format string or a function which is called with VAL." | |
178 | (cond | |
179 | ((integerp val) '("int" "%d")) | |
180 | ((floatp val) '("double" "%f")) | |
181 | ((or (listp val) (vectorp val)) | |
182 | (lexical-let ((type (org-babel-C-val-to-C-list-type val))) | |
183 | (list (car type) | |
184 | (lambda (val) | |
185 | (cons | |
186 | (format "[%d]%s" | |
187 | (length val) | |
188 | (car (org-babel-C-format-val type (elt val 0)))) | |
189 | (concat "{ " | |
190 | (mapconcat (lambda (v) | |
191 | (cdr (org-babel-C-format-val type v))) | |
192 | val | |
193 | ", ") | |
194 | " }")))))) | |
195 | (t ;; treat unknown types as string | |
196 | '("char" (lambda (val) | |
197 | (let ((s (format "%s" val))) ;; convert to string for unknown types | |
198 | (cons (format "[%d]" (1+ (length s))) | |
199 | (concat "\"" s "\"")))))))) | |
200 | ||
201 | (defun org-babel-C-val-to-C-list-type (val) | |
202 | "Determine the C array type of a VAL." | |
203 | (let (type) | |
204 | (mapc | |
205 | #'(lambda (i) | |
206 | (let* ((tmp-type (org-babel-C-val-to-C-type i)) | |
207 | (type-name (car type)) | |
208 | (tmp-type-name (car tmp-type))) | |
209 | (when (and type (not (string= type-name tmp-type-name))) | |
210 | (if (and (member type-name '("int" "double" "int32_t")) | |
211 | (member tmp-type-name '("int" "double" "int32_t"))) | |
212 | (setq tmp-type '("double" "" "%f")) | |
213 | (error "Only homogeneous lists are supported by C. You can not mix %s and %s" | |
214 | type-name | |
215 | tmp-type-name))) | |
216 | (setq type tmp-type))) | |
217 | val) | |
218 | type)) | |
219 | ||
86fbb8ca CD |
220 | (defun org-babel-C-var-to-C (pair) |
221 | "Convert an elisp val into a string of C code specifying a var | |
222 | of the same value." | |
223 | ;; TODO list support | |
224 | (let ((var (car pair)) | |
225 | (val (cdr pair))) | |
226 | (when (symbolp val) | |
227 | (setq val (symbol-name val)) | |
228 | (when (= (length val) 1) | |
229 | (setq val (string-to-char val)))) | |
271672fa BG |
230 | (let* ((type-data (org-babel-C-val-to-C-type val)) |
231 | (type (car type-data)) | |
232 | (formated (org-babel-C-format-val type-data val)) | |
233 | (suffix (car formated)) | |
234 | (data (cdr formated))) | |
235 | (format "%s %s%s = %s;" | |
236 | type | |
237 | var | |
238 | suffix | |
239 | data)))) | |
86fbb8ca CD |
240 | |
241 | (provide 'ob-C) | |
242 | ||
86fbb8ca | 243 | ;;; ob-C.el ends here |