ob-fortran.el (6904B)
1 ;;; ob-fortran.el --- Babel Functions for Fortran -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2011-2024 Free Software Foundation, Inc. 4 5 ;; Authors: Sergey Litvinov 6 ;; Eric Schulte 7 ;; Keywords: literate programming, reproducible research, fortran 8 ;; URL: https://orgmode.org 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 <https://www.gnu.org/licenses/>. 24 25 ;;; Commentary: 26 27 ;; Org-Babel support for evaluating fortran code. 28 29 ;;; Code: 30 31 (require 'org-macs) 32 (org-assert-version) 33 34 (require 'ob) 35 (require 'org-macs) 36 (require 'cc-mode) 37 (require 'cl-lib) 38 39 (declare-function org-entry-get "org" 40 (pom property &optional inherit literal-nil)) 41 42 (defvar org-babel-tangle-lang-exts) 43 (add-to-list 'org-babel-tangle-lang-exts '("fortran" . "F90")) 44 45 (defvar org-babel-default-header-args:fortran '()) 46 47 (defcustom org-babel-fortran-compiler "gfortran" 48 "Fortran command used to compile Fortran source code file." 49 :group 'org-babel 50 :package-version '(Org . "9.5") 51 :type 'string) 52 53 (defun org-babel-execute:fortran (body params) 54 "Execute Fortran BODY according to PARAMS. 55 This function is called by `org-babel-execute-src-block'." 56 (let* ((tmp-src-file (org-babel-temp-file "fortran-src-" ".F90")) 57 (tmp-bin-file (org-babel-temp-file "fortran-bin-" org-babel-exeext)) 58 (cmdline (cdr (assq :cmdline params))) 59 (flags (cdr (assq :flags params))) 60 (full-body (org-babel-expand-body:fortran body params))) 61 (with-temp-file tmp-src-file (insert full-body)) 62 (org-babel-eval 63 (format "%s -o %s %s %s" 64 org-babel-fortran-compiler 65 (org-babel-process-file-name tmp-bin-file) 66 (mapconcat 'identity 67 (if (listp flags) flags (list flags)) " ") 68 (org-babel-process-file-name tmp-src-file)) "") 69 (let ((results 70 (org-trim 71 (org-remove-indentation 72 (org-babel-eval 73 (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) ""))))) 74 (org-babel-reassemble-table 75 (org-babel-result-cond (cdr (assq :result-params params)) 76 (org-babel-read results) 77 (let ((tmp-file (org-babel-temp-file "f-"))) 78 (with-temp-file tmp-file (insert results)) 79 (org-babel-import-elisp-from-file tmp-file))) 80 (org-babel-pick-name 81 (cdr (assq :colname-names params)) (cdr (assq :colnames params))) 82 (org-babel-pick-name 83 (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))) 84 85 (defun org-babel-expand-body:fortran (body params) 86 "Expand a fortran BODY according to its header arguments defined in PARAMS." 87 (let ((vars (org-babel--get-vars params)) 88 (prologue (cdr (assq :prologue params))) 89 (epilogue (cdr (assq :epilogue params))) 90 (main-p (not (string= (cdr (assq :main params)) "no"))) 91 (includes (or (cdr (assq :includes params)) 92 (org-babel-read (org-entry-get nil "includes" t)))) 93 (defines (org-babel-read 94 (or (cdr (assq :defines params)) 95 (org-babel-read (org-entry-get nil "defines" t)))))) 96 (mapconcat 'identity 97 (list 98 ;; includes 99 (mapconcat 100 (lambda (inc) (format "#include %s" inc)) 101 (if (listp includes) includes (list includes)) "\n") 102 ;; defines 103 (mapconcat 104 (lambda (inc) (format "#define %s" inc)) 105 (if (listp defines) defines (list defines)) "\n") 106 ;; body 107 (if main-p 108 (org-babel-fortran-ensure-main-wrap 109 (concat 110 ;; variables 111 (mapconcat 'org-babel-fortran-var-to-fortran vars "\n") 112 (and prologue (concat prologue "\n")) 113 body 114 (and prologue (concat prologue "\n"))) 115 params) 116 (concat 117 (and prologue (concat prologue "\n")) 118 body 119 (and epilogue (concat "\n" epilogue "\n")))) 120 "\n") 121 "\n"))) 122 123 (defun org-babel-fortran-ensure-main-wrap (body params) 124 "Wrap BODY in a \"program ... end program\" block if none exists. 125 Variable assignments are derived from PARAMS." 126 (if (string-match "^[ \t]*program\\>" (capitalize body)) 127 (let ((vars (org-babel--get-vars params))) 128 (when vars (error "Cannot use :vars if `program' statement is present")) 129 body) 130 (format "program main\n%s\nend program main\n" body))) 131 132 (defun org-babel-prep-session:fortran (_session _params) 133 "Do nothing. 134 This function does nothing as fortran is a compiled language with no 135 support for sessions." 136 (error "Fortran is a compiled languages -- no support for sessions")) 137 138 (defun org-babel-load-session:fortran (_session _body _params) 139 "Do nothing. 140 This function does nothing as fortran is a compiled language with no 141 support for sessions." 142 (error "Fortran is a compiled languages -- no support for sessions")) 143 144 ;; helper functions 145 146 (defun org-babel-fortran-var-to-fortran (pair) 147 "Convert PAIR of (VAR . VAL) into a string of fortran code. 148 The fortran code will assign VAL to VAR variable." 149 ;; TODO list support 150 (let ((var (car pair)) 151 (val (cdr pair))) 152 (when (symbolp val) 153 (setq val (symbol-name val)) 154 (when (= (length val) 1) 155 (setq val (string-to-char val)))) 156 (cond 157 ((integerp val) 158 (format "integer, parameter :: %S = %S\n" var val)) 159 ((floatp val) 160 (format "real, parameter :: %S = %S\n" var val)) 161 ((or (integerp val)) 162 (format "character, parameter :: %S = '%S'\n" var val)) 163 ((stringp val) 164 (format "character(len=%d), parameter :: %S = '%s'\n" 165 (length val) var val)) 166 ;; val is a matrix 167 ((and (listp val) (cl-every #'listp val)) 168 (format "real, parameter :: %S(%d,%d) = transpose( reshape( %s , (/ %d, %d /) ) )\n" 169 var (length val) (length (car val)) 170 (org-babel-fortran-transform-list val) 171 (length (car val)) (length val))) 172 ((listp val) 173 (format "real, parameter :: %S(%d) = %s\n" 174 var (length val) (org-babel-fortran-transform-list val))) 175 (t 176 (error "The type of parameter %s is not supported by ob-fortran" var))))) 177 178 (defun org-babel-fortran-transform-list (val) 179 "Return a fortran representation of enclose syntactic list VAL." 180 (if (listp val) 181 (concat "(/" (mapconcat #'org-babel-fortran-transform-list val ", ") "/)") 182 (format "%S" val))) 183 184 (provide 'ob-fortran) 185 186 ;;; ob-fortran.el ends here