Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-C.el --- org-babel functions for C and similar languages |
2 | ||
3ab2c837 | 3 | ;; Copyright (C) 2010 Free Software Foundation, Inc. |
86fbb8ca CD |
4 | |
5 | ;; Author: Eric Schulte | |
6 | ;; Keywords: literate programming, reproducible research | |
7 | ;; Homepage: http://orgmode.org | |
3ab2c837 | 8 | ;; Version: 7.7 |
86fbb8ca CD |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
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. | |
16 | ||
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. | |
21 | ||
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/>. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Org-Babel support for evaluating C code. | |
28 | ;; | |
29 | ;; very limited implementation: | |
30 | ;; - currently only support :results output | |
31 | ;; - not much in the way of error feedback | |
32 | ||
33 | ;;; Code: | |
34 | (require 'ob) | |
35 | (require 'ob-eval) | |
86fbb8ca CD |
36 | (require 'cc-mode) |
37 | ||
38 | (declare-function org-entry-get "org" | |
39 | (pom property &optional inherit literal-nil)) | |
40 | ||
3ab2c837 BG |
41 | |
42 | (defvar org-babel-tangle-lang-exts) | |
43 | (add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp")) | |
86fbb8ca CD |
44 | |
45 | (defvar org-babel-default-header-args:C '()) | |
46 | ||
47 | (defvar org-babel-C-compiler "gcc" | |
48 | "Command used to compile a C source code file into an | |
49 | executable.") | |
50 | ||
3ab2c837 BG |
51 | (defvar org-babel-C++-compiler "g++" |
52 | "Command used to compile a C++ source code file into an | |
86fbb8ca CD |
53 | executable.") |
54 | ||
55 | (defvar org-babel-c-variant nil | |
56 | "Internal variable used to hold which type of C (e.g. C or C++) | |
57 | is currently being evaluated.") | |
58 | ||
59 | (defun org-babel-execute:cpp (body params) | |
60 | "Execute BODY according to PARAMS. This function calls | |
3ab2c837 BG |
61 | `org-babel-execute:C++'." |
62 | (org-babel-execute:C++ body params)) | |
86fbb8ca | 63 | |
3ab2c837 | 64 | (defun org-babel-execute:C++ (body params) |
86fbb8ca CD |
65 | "Execute a block of C++ code with org-babel. This function is |
66 | called by `org-babel-execute-src-block'." | |
67 | (let ((org-babel-c-variant 'cpp)) (org-babel-C-execute body params))) | |
68 | ||
3ab2c837 | 69 | (defun org-babel-expand-body:C++ (body params) |
86fbb8ca CD |
70 | "Expand a block of C++ code with org-babel according to it's |
71 | header arguments (calls `org-babel-C-expand')." | |
afe98dfa | 72 | (let ((org-babel-c-variant 'cpp)) (org-babel-C-expand body params))) |
86fbb8ca CD |
73 | |
74 | (defun org-babel-execute:C (body params) | |
75 | "Execute a block of C code with org-babel. This function is | |
76 | called by `org-babel-execute-src-block'." | |
77 | (let ((org-babel-c-variant 'c)) (org-babel-C-execute body params))) | |
78 | ||
afe98dfa | 79 | (defun org-babel-expand-body:c (body params) |
86fbb8ca CD |
80 | "Expand a block of C code with org-babel according to it's |
81 | header arguments (calls `org-babel-C-expand')." | |
afe98dfa | 82 | (let ((org-babel-c-variant 'c)) (org-babel-C-expand body params))) |
86fbb8ca CD |
83 | |
84 | (defun org-babel-C-execute (body params) | |
85 | "This function should only be called by `org-babel-execute:C' | |
3ab2c837 | 86 | or `org-babel-execute:C++'." |
afe98dfa CD |
87 | (let* ((tmp-src-file (org-babel-temp-file |
88 | "C-src-" | |
89 | (cond | |
90 | ((equal org-babel-c-variant 'c) ".c") | |
91 | ((equal org-babel-c-variant 'cpp) ".cpp")))) | |
92 | (tmp-bin-file (org-babel-temp-file "C-bin-")) | |
86fbb8ca CD |
93 | (cmdline (cdr (assoc :cmdline params))) |
94 | (flags (cdr (assoc :flags params))) | |
95 | (full-body (org-babel-C-expand body params)) | |
96 | (compile | |
97 | (progn | |
98 | (with-temp-file tmp-src-file (insert full-body)) | |
99 | (org-babel-eval | |
100 | (format "%s -o %s %s %s" | |
101 | (cond | |
102 | ((equal org-babel-c-variant 'c) org-babel-C-compiler) | |
3ab2c837 | 103 | ((equal org-babel-c-variant 'cpp) org-babel-C++-compiler)) |
afe98dfa | 104 | (org-babel-process-file-name tmp-bin-file) |
86fbb8ca CD |
105 | (mapconcat 'identity |
106 | (if (listp flags) flags (list flags)) " ") | |
afe98dfa | 107 | (org-babel-process-file-name tmp-src-file)) "")))) |
86fbb8ca CD |
108 | ((lambda (results) |
109 | (org-babel-reassemble-table | |
afe98dfa CD |
110 | (if (member "vector" (cdr (assoc :result-params params))) |
111 | (let ((tmp-file (org-babel-temp-file "c-"))) | |
86fbb8ca CD |
112 | (with-temp-file tmp-file (insert results)) |
113 | (org-babel-import-elisp-from-file tmp-file)) | |
114 | (org-babel-read results)) | |
115 | (org-babel-pick-name | |
afe98dfa | 116 | (cdr (assoc :colname-names params)) (cdr (assoc :colnames params))) |
86fbb8ca | 117 | (org-babel-pick-name |
afe98dfa | 118 | (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))) |
86fbb8ca CD |
119 | (org-babel-trim |
120 | (org-babel-eval | |
121 | (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) ""))))) | |
122 | ||
afe98dfa | 123 | (defun org-babel-C-expand (body params) |
86fbb8ca CD |
124 | "Expand a block of C or C++ code with org-babel according to |
125 | it's header arguments." | |
afe98dfa | 126 | (let ((vars (mapcar #'cdr (org-babel-get-header params :var))) |
86fbb8ca CD |
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)))))) | |
86fbb8ca CD |
133 | (mapconcat 'identity |
134 | (list | |
135 | ;; includes | |
136 | (mapconcat | |
137 | (lambda (inc) (format "#include %s" inc)) | |
138 | (if (listp includes) includes (list includes)) "\n") | |
139 | ;; defines | |
140 | (mapconcat | |
141 | (lambda (inc) (format "#define %s" inc)) | |
142 | (if (listp defines) defines (list defines)) "\n") | |
143 | ;; variables | |
144 | (mapconcat 'org-babel-C-var-to-C vars "\n") | |
145 | ;; body | |
146 | (if main-p | |
147 | (org-babel-C-ensure-main-wrap body) | |
afe98dfa | 148 | body) "\n") "\n"))) |
86fbb8ca CD |
149 | |
150 | (defun org-babel-C-ensure-main-wrap (body) | |
151 | "Wrap body in a \"main\" function call if none exists." | |
afe98dfa | 152 | (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body) |
86fbb8ca CD |
153 | body |
154 | (format "int main() {\n%s\n}\n" body))) | |
155 | ||
156 | (defun org-babel-prep-session:C (session params) | |
157 | "This function does nothing as C is a compiled language with no | |
158 | support for sessions" | |
159 | (error "C is a compiled languages -- no support for sessions")) | |
160 | ||
161 | (defun org-babel-load-session:C (session body params) | |
162 | "This function does nothing as C is a compiled language with no | |
163 | support for sessions" | |
164 | (error "C is a compiled languages -- no support for sessions")) | |
165 | ||
166 | ;; helper functions | |
167 | ||
168 | (defun org-babel-C-var-to-C (pair) | |
169 | "Convert an elisp val into a string of C code specifying a var | |
170 | of the same value." | |
171 | ;; TODO list support | |
172 | (let ((var (car pair)) | |
173 | (val (cdr pair))) | |
174 | (when (symbolp val) | |
175 | (setq val (symbol-name val)) | |
176 | (when (= (length val) 1) | |
177 | (setq val (string-to-char val)))) | |
178 | (cond | |
179 | ((integerp val) | |
180 | (format "int %S = %S;" var val)) | |
181 | ((floatp val) | |
182 | (format "double %S = %S;" var val)) | |
183 | ((or (characterp val)) | |
184 | (format "char %S = '%S';" var val)) | |
185 | ((stringp val) | |
186 | (format "char %S[%d] = \"%s\";" | |
187 | var (+ 1 (length val)) val)) | |
188 | (t | |
189 | (format "u32 %S = %S;" var val))))) | |
190 | ||
191 | ||
192 | (provide 'ob-C) | |
193 | ||
5b409b39 | 194 | |
86fbb8ca CD |
195 | |
196 | ;;; ob-C.el ends here |