Commit | Line | Data |
---|---|---|
e66ba1df BG |
1 | ;;; ob-fortran.el --- org-babel functions for fortran |
2 | ||
3460d159 | 3 | ;; Copyright (C) 2011-2012 Free Software Foundation, Inc. |
e66ba1df | 4 | |
153ae947 | 5 | ;; Authors: Sergey Litvinov (based on ob-C.el by Eric Schulte), Eric Schulte |
e66ba1df BG |
6 | ;; Keywords: literate programming, reproducible research, fortran |
7 | ;; Homepage: http://orgmode.org | |
e66ba1df | 8 | |
153ae947 | 9 | ;; This program is free software; you can redistribute it and/or modify |
e66ba1df | 10 | ;; it under the terms of the GNU General Public License as published by |
153ae947 BG |
11 | ;; the Free Software Foundation; either version 3, or (at your option) |
12 | ;; any later version. | |
13 | ;; | |
14 | ;; This program is distributed in the hope that it will be useful, | |
e66ba1df BG |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
153ae947 | 18 | ;; |
e66ba1df | 19 | ;; You should have received a copy of the GNU General Public License |
153ae947 BG |
20 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
21 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
22 | ;; Boston, MA 02110-1301, USA. | |
e66ba1df BG |
23 | |
24 | ;;; Commentary: | |
25 | ||
26 | ;; Org-Babel support for evaluating fortran code. | |
27 | ||
28 | ;;; Code: | |
29 | (require 'ob) | |
30 | (require 'ob-eval) | |
31 | (require 'cc-mode) | |
32 | ||
33 | (declare-function org-entry-get "org" | |
34 | (pom property &optional inherit literal-nil)) | |
35 | ||
36 | (defvar org-babel-tangle-lang-exts) | |
37 | (add-to-list 'org-babel-tangle-lang-exts '("fortran" . "F90")) | |
38 | ||
39 | (defvar org-babel-default-header-args:fortran '()) | |
40 | ||
41 | (defvar org-babel-fortran-compiler "gfortran" | |
42 | "fortran command used to compile a fortran source code file into an | |
43 | executable.") | |
44 | ||
45 | (defun org-babel-execute:fortran (body params) | |
46 | "This function should only be called by `org-babel-execute:fortran'" | |
47 | (let* ((tmp-src-file (org-babel-temp-file "fortran-src-" ".F90")) | |
48 | (tmp-bin-file (org-babel-temp-file "fortran-bin-")) | |
49 | (cmdline (cdr (assoc :cmdline params))) | |
50 | (flags (cdr (assoc :flags params))) | |
51 | (full-body (org-babel-expand-body:fortran body params)) | |
52 | (compile | |
53 | (progn | |
54 | (with-temp-file tmp-src-file (insert full-body)) | |
55 | (org-babel-eval | |
56 | (format "%s -o %s %s %s" | |
57 | org-babel-fortran-compiler | |
58 | (org-babel-process-file-name tmp-bin-file) | |
59 | (mapconcat 'identity | |
60 | (if (listp flags) flags (list flags)) " ") | |
61 | (org-babel-process-file-name tmp-src-file)) "")))) | |
62 | ((lambda (results) | |
63 | (org-babel-reassemble-table | |
64 | (if (member "vector" (cdr (assoc :result-params params))) | |
65 | (let ((tmp-file (org-babel-temp-file "f-"))) | |
66 | (with-temp-file tmp-file (insert results)) | |
67 | (org-babel-import-elisp-from-file tmp-file)) | |
68 | (org-babel-read results)) | |
69 | (org-babel-pick-name | |
70 | (cdr (assoc :colname-names params)) (cdr (assoc :colnames params))) | |
71 | (org-babel-pick-name | |
72 | (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))) | |
73 | (org-babel-trim | |
74 | (org-babel-eval | |
75 | (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) ""))))) | |
76 | ||
77 | (defun org-babel-expand-body:fortran (body params) | |
78 | "Expand a block of fortran or fortran code with org-babel according to | |
79 | it's header arguments." | |
80 | (let ((vars (mapcar #'cdr (org-babel-get-header params :var))) | |
81 | (main-p (not (string= (cdr (assoc :main params)) "no"))) | |
82 | (includes (or (cdr (assoc :includes params)) | |
83 | (org-babel-read (org-entry-get nil "includes" t)))) | |
84 | (defines (org-babel-read | |
85 | (or (cdr (assoc :defines params)) | |
86 | (org-babel-read (org-entry-get nil "defines" t)))))) | |
87 | (mapconcat 'identity | |
88 | (list | |
89 | ;; includes | |
90 | (mapconcat | |
91 | (lambda (inc) (format "#include %s" inc)) | |
92 | (if (listp includes) includes (list includes)) "\n") | |
93 | ;; defines | |
94 | (mapconcat | |
95 | (lambda (inc) (format "#define %s" inc)) | |
96 | (if (listp defines) defines (list defines)) "\n") | |
97 | ;; body | |
98 | (if main-p | |
99 | (org-babel-fortran-ensure-main-wrap | |
100 | (concat | |
101 | ;; variables | |
102 | (mapconcat 'org-babel-fortran-var-to-fortran vars "\n") | |
103 | body) params) | |
104 | body) "\n") "\n"))) | |
105 | ||
106 | (defun org-babel-fortran-ensure-main-wrap (body params) | |
107 | "Wrap body in a \"program ... end program\" block if none exists." | |
108 | (if (string-match "^[ \t]*program[ \t]*.*" (capitalize body)) | |
109 | (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))) | |
27e428e7 | 110 | (if vars (error "cannot use :vars if 'program' statement is present")) |
e66ba1df BG |
111 | body) |
112 | (format "program main\n%s\nend program main\n" body))) | |
113 | ||
114 | (defun org-babel-prep-session:fortran (session params) | |
115 | "This function does nothing as fortran is a compiled language with no | |
116 | support for sessions" | |
117 | (error "fortran is a compiled languages -- no support for sessions")) | |
118 | ||
119 | (defun org-babel-load-session:fortran (session body params) | |
120 | "This function does nothing as fortran is a compiled language with no | |
121 | support for sessions" | |
122 | (error "fortran is a compiled languages -- no support for sessions")) | |
123 | ||
124 | ;; helper functions | |
125 | ||
126 | (defun org-babel-fortran-var-to-fortran (pair) | |
27e428e7 | 127 | "Convert an elisp val into a string of fortran code specifying a var |
e66ba1df BG |
128 | of the same value." |
129 | ;; TODO list support | |
130 | (let ((var (car pair)) | |
131 | (val (cdr pair))) | |
132 | (when (symbolp val) | |
133 | (setq val (symbol-name val)) | |
134 | (when (= (length val) 1) | |
135 | (setq val (string-to-char val)))) | |
136 | (cond | |
137 | ((integerp val) | |
138 | (format "integer, parameter :: %S = %S\n" var val)) | |
139 | ((floatp val) | |
140 | (format "real, parameter :: %S = %S\n" var val)) | |
141 | ((or (integerp val)) | |
142 | (format "character, parameter :: %S = '%S'\n" var val)) | |
143 | ((stringp val) | |
144 | (format "character(len=%d), parameter :: %S = '%s'\n" | |
145 | (length val) var val)) | |
146 | ((listp val) | |
147 | (format "real, parameter :: %S(%d) = %s\n" | |
148 | var (length val) (ob-fortran-transform-list val))) | |
149 | (t | |
150 | (error (format "the type of parameter %s is not supported by ob-fortran" | |
151 | var)))))) | |
152 | ||
153 | (defun ob-fortran-transform-list (val) | |
154 | "Return a fortran representation of enclose syntactic lists." | |
155 | (if (listp val) | |
156 | (concat "(/" (mapconcat #'ob-fortran-transform-list val ", ") "/)") | |
157 | (format "%S" val))) | |
158 | ||
159 | (provide 'ob-fortran) | |
160 | ||
161 | ;;; ob-fortran.el ends here |