ob-fortran.el 5.8 KB

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