ob-fortran.el 6.3 KB

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