ob-fortran.el 6.1 KB

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