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
39 (declare-function org-entry-get
"org"
40 (pom property
&optional inherit literal-nil
))
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
&optional processed-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 processed-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
&optional processed-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 processed-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* ((processed-params (org-babel-process-params params
))
87 (tmp-src-file (make-temp-file "org-babel-C-src" nil
89 ((equal org-babel-c-variant
'c
) ".c")
90 ((equal org-babel-c-variant
'cpp
) ".cpp"))))
91 (tmp-bin-file (make-temp-file "org-babel-C-bin"))
92 (tmp-out-file (make-temp-file "org-babel-C-out"))
93 (cmdline (cdr (assoc :cmdline params
)))
94 (flags (cdr (assoc :flags params
)))
95 (full-body (org-babel-C-expand body params
))
98 (with-temp-file tmp-src-file
(insert full-body
))
100 (format "%s -o %s %s %s"
102 ((equal org-babel-c-variant
'c
) org-babel-C-compiler
)
103 ((equal org-babel-c-variant
'cpp
) org-babel-c
++-compiler
))
106 (if (listp flags
) flags
(list flags
)) " ")
108 (org-babel-reassemble-table
112 (concat tmp-bin-file
(if cmdline
(concat " " cmdline
) "")) "")))
114 (nth 4 processed-params
) (cdr (assoc :colnames params
)))
116 (nth 5 processed-params
) (cdr (assoc :rownames params
))))))
119 (defun org-babel-C-expand (body params
&optional processed-params
)
120 "Expand a block of C or C++ code with org-babel according to
121 it's header arguments."
122 (let ((vars (nth 1 (or processed-params
123 (org-babel-process-params params
))))
124 (main-p (not (string= (cdr (assoc :main params
)) "no")))
125 (includes (or (cdr (assoc :includes params
))
126 (org-babel-read (org-entry-get nil
"includes" t
))))
127 (defines (org-babel-read
128 (or (cdr (assoc :defines params
))
129 (org-babel-read (org-entry-get nil
"defines" t
))))))
134 (lambda (inc) (format "#include %s" inc
))
135 (if (listp includes
) includes
(list includes
)) "\n")
138 (lambda (inc) (format "#define %s" inc
))
139 (if (listp defines
) defines
(list defines
)) "\n")
141 (mapconcat 'org-babel-C-var-to-C vars
"\n")
144 (org-babel-C-ensure-main-wrap body
)
147 (defun org-babel-C-ensure-main-wrap (body)
148 "Wrap body in a \"main\" function call if none exists."
149 (if (string-match "^[ \t]*[intvod]+[ \t]*main[ \t]*(.*)" body
)
151 (format "int main() {\n%s\n}\n" body
)))
153 (defun org-babel-prep-session:C
(session params
)
154 "This function does nothing as C is a compiled language with no
155 support for sessions"
156 (error "C is a compiled languages -- no support for sessions"))
158 (defun org-babel-load-session:C
(session body params
)
159 "This function does nothing as C is a compiled language with no
160 support for sessions"
161 (error "C is a compiled languages -- no support for sessions"))
165 (defun org-babel-C-var-to-C (pair)
166 "Convert an elisp val into a string of C code specifying a var
169 (let ((var (car pair
))
172 (setq val
(symbol-name val
))
173 (when (= (length val
) 1)
174 (setq val
(string-to-char val
))))
177 (format "int %S = %S;" var val
))
179 (format "double %S = %S;" var val
))
180 ((or (characterp val
))
181 (format "char %S = '%S';" var val
))
183 (format "char %S[%d] = \"%s\";"
184 var
(+ 1 (length val
)) val
))
186 (format "u32 %S = %S;" var val
)))))
191 ;; arch-tag: 8f49e462-54e3-417b-9a8d-423864893b37
193 ;;; ob-C.el ends here