ob-fortran.el 6.2 KB

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