ob-fortran.el 6.9 KB

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