ob-fortran.el 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ;;; ob-fortran.el --- org-babel functions for fortran
  2. ;; Copyright (C) 2011-2012 Sergey Litvinov, Eric Schulte
  3. ;; Authors: Sergey Litvinov (based on ob-C.el by Eric Schulte), Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, fortran
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.8.02
  7. ;; This program is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 3, or (at your option)
  10. ;; any later version.
  11. ;;
  12. ;; This program 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. ;;
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  19. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20. ;; Boston, MA 02110-1301, USA.
  21. ;;; Commentary:
  22. ;; Org-Babel support for evaluating fortran code.
  23. ;;; Code:
  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. "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-"))
  39. (cmdline (cdr (assoc :cmdline params)))
  40. (flags (cdr (assoc :flags params)))
  41. (full-body (org-babel-expand-body:fortran body params))
  42. (compile
  43. (progn
  44. (with-temp-file tmp-src-file (insert full-body))
  45. (org-babel-eval
  46. (format "%s -o %s %s %s"
  47. org-babel-fortran-compiler
  48. (org-babel-process-file-name tmp-bin-file)
  49. (mapconcat 'identity
  50. (if (listp flags) flags (list flags)) " ")
  51. (org-babel-process-file-name tmp-src-file)) ""))))
  52. ((lambda (results)
  53. (org-babel-reassemble-table
  54. (if (member "vector" (cdr (assoc :result-params params)))
  55. (let ((tmp-file (org-babel-temp-file "f-")))
  56. (with-temp-file tmp-file (insert results))
  57. (org-babel-import-elisp-from-file tmp-file))
  58. (org-babel-read results))
  59. (org-babel-pick-name
  60. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  61. (org-babel-pick-name
  62. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
  63. (org-babel-trim
  64. (org-babel-eval
  65. (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))
  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. ((listp val)
  131. (format "real, parameter :: %S(%d) = %s\n"
  132. var (length val) (ob-fortran-transform-list val)))
  133. (t
  134. (error (format "the type of parameter %s is not supported by ob-fortran"
  135. var))))))
  136. (defun ob-fortran-transform-list (val)
  137. "Return a fortran representation of enclose syntactic lists."
  138. (if (listp val)
  139. (concat "(/" (mapconcat #'ob-fortran-transform-list val ", ") "/)")
  140. (format "%S" val)))
  141. (provide 'ob-fortran)
  142. ;;; ob-fortran.el ends here