ob-fortran.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ;;; ob-fortran.el --- Babel Functions for Fortran -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2011-2017 Free Software Foundation, Inc.
  3. ;; Authors: Sergey Litvinov
  4. ;; Eric Schulte
  5. ;; Keywords: literate programming, reproducible research, fortran
  6. ;; Homepage: http://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 <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; Org-Babel support for evaluating fortran code.
  21. ;;; Code:
  22. (require 'ob)
  23. (require 'cc-mode)
  24. (require 'cl-lib)
  25. (declare-function org-entry-get "org"
  26. (pom property &optional inherit literal-nil))
  27. (declare-function org-remove-indentation "org" (code &optional n))
  28. (declare-function org-trim "org" (s &optional keep-lead))
  29. (defvar org-babel-tangle-lang-exts)
  30. (add-to-list 'org-babel-tangle-lang-exts '("fortran" . "F90"))
  31. (defvar org-babel-default-header-args:fortran '())
  32. (defvar org-babel-fortran-compiler "gfortran"
  33. "fortran command used to compile a fortran source code file into an
  34. executable.")
  35. (defun org-babel-execute:fortran (body params)
  36. "This function should only be called by `org-babel-execute:fortran'"
  37. (let* ((tmp-src-file (org-babel-temp-file "fortran-src-" ".F90"))
  38. (tmp-bin-file (org-babel-temp-file "fortran-bin-" org-babel-exeext))
  39. (cmdline (cdr (assq :cmdline params)))
  40. (flags (cdr (assq :flags params)))
  41. (full-body (org-babel-expand-body:fortran body params)))
  42. (with-temp-file tmp-src-file (insert full-body))
  43. (org-babel-eval
  44. (format "%s -o %s %s %s"
  45. org-babel-fortran-compiler
  46. (org-babel-process-file-name tmp-bin-file)
  47. (mapconcat 'identity
  48. (if (listp flags) flags (list flags)) " ")
  49. (org-babel-process-file-name tmp-src-file)) "")
  50. (let ((results
  51. (org-trim
  52. (org-remove-indentation
  53. (org-babel-eval
  54. (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
  55. (org-babel-reassemble-table
  56. (org-babel-result-cond (cdr (assq :result-params params))
  57. (org-babel-read results)
  58. (let ((tmp-file (org-babel-temp-file "f-")))
  59. (with-temp-file tmp-file (insert results))
  60. (org-babel-import-elisp-from-file tmp-file)))
  61. (org-babel-pick-name
  62. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  63. (org-babel-pick-name
  64. (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))))
  65. (defun org-babel-expand-body:fortran (body params)
  66. "Expand a block of fortran or fortran code with org-babel according to
  67. its header arguments."
  68. (let ((vars (org-babel--get-vars params))
  69. (main-p (not (string= (cdr (assq :main params)) "no")))
  70. (includes (or (cdr (assq :includes params))
  71. (org-babel-read (org-entry-get nil "includes" t))))
  72. (defines (org-babel-read
  73. (or (cdr (assq :defines params))
  74. (org-babel-read (org-entry-get nil "defines" t))))))
  75. (mapconcat 'identity
  76. (list
  77. ;; includes
  78. (mapconcat
  79. (lambda (inc) (format "#include %s" inc))
  80. (if (listp includes) includes (list includes)) "\n")
  81. ;; defines
  82. (mapconcat
  83. (lambda (inc) (format "#define %s" inc))
  84. (if (listp defines) defines (list defines)) "\n")
  85. ;; body
  86. (if main-p
  87. (org-babel-fortran-ensure-main-wrap
  88. (concat
  89. ;; variables
  90. (mapconcat 'org-babel-fortran-var-to-fortran vars "\n")
  91. body) params)
  92. body) "\n") "\n")))
  93. (defun org-babel-fortran-ensure-main-wrap (body params)
  94. "Wrap body in a \"program ... end program\" block if none exists."
  95. (if (string-match "^[ \t]*program[ \t]*.*" (capitalize body))
  96. (let ((vars (org-babel--get-vars params)))
  97. (if vars (error "Cannot use :vars if `program' statement is present"))
  98. body)
  99. (format "program main\n%s\nend program main\n" body)))
  100. (defun org-babel-prep-session:fortran (_session _params)
  101. "This function does nothing as fortran is a compiled language with no
  102. support for sessions"
  103. (error "Fortran is a compiled languages -- no support for sessions"))
  104. (defun org-babel-load-session:fortran (_session _body _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. ;; helper functions
  109. (defun org-babel-fortran-var-to-fortran (pair)
  110. "Convert an elisp val into a string of fortran code specifying a var
  111. of the same value."
  112. ;; TODO list support
  113. (let ((var (car pair))
  114. (val (cdr pair)))
  115. (when (symbolp val)
  116. (setq val (symbol-name val))
  117. (when (= (length val) 1)
  118. (setq val (string-to-char val))))
  119. (cond
  120. ((integerp val)
  121. (format "integer, parameter :: %S = %S\n" var val))
  122. ((floatp val)
  123. (format "real, parameter :: %S = %S\n" var val))
  124. ((or (integerp val))
  125. (format "character, parameter :: %S = '%S'\n" var val))
  126. ((stringp val)
  127. (format "character(len=%d), parameter :: %S = '%s'\n"
  128. (length val) var val))
  129. ;; val is a matrix
  130. ((and (listp val) (cl-every #'listp val))
  131. (format "real, parameter :: %S(%d,%d) = transpose( reshape( %s , (/ %d, %d /) ) )\n"
  132. var (length val) (length (car val))
  133. (org-babel-fortran-transform-list val)
  134. (length (car val)) (length val)))
  135. ((listp val)
  136. (format "real, parameter :: %S(%d) = %s\n"
  137. var (length val) (org-babel-fortran-transform-list val)))
  138. (t
  139. (error "the type of parameter %s is not supported by ob-fortran" var)))))
  140. (defun org-babel-fortran-transform-list (val)
  141. "Return a fortran representation of enclose syntactic lists."
  142. (if (listp val)
  143. (concat "(/" (mapconcat #'org-babel-fortran-transform-list val ", ") "/)")
  144. (format "%S" val)))
  145. (provide 'ob-fortran)
  146. ;;; ob-fortran.el ends here