ob-ocaml.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ;;; ob-ocaml.el --- org-babel functions for ocaml evaluation
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  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 ocaml source code. This one will
  20. ;; be sort of tricky because ocaml programs must be compiled before
  21. ;; they can be run, but ocaml code can also be run through an
  22. ;; interactive interpreter.
  23. ;;
  24. ;; For now lets only allow evaluation using the ocaml interpreter.
  25. ;;; Requirements:
  26. ;; - tuareg-mode :: http://www-rocq.inria.fr/~acohen/tuareg/
  27. ;;; Code:
  28. (require 'ob)
  29. (require 'ob-comint)
  30. (require 'comint)
  31. (eval-when-compile (require 'cl))
  32. (declare-function tuareg-run-caml "ext:taureg" ())
  33. (add-to-list 'org-babel-tangle-lang-exts '("ocaml" . "ml"))
  34. (defvar org-babel-default-header-args:ocaml '())
  35. (defvar org-babel-ocaml-eoe-indicator "\"org-babel-ocaml-eoe\";;")
  36. (defvar org-babel-ocaml-eoe-output "org-babel-ocaml-eoe")
  37. (defun org-babel-expand-body:ocaml (body params &optional processed-params)
  38. "Expand BODY according to PARAMS, return the expanded body."
  39. (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
  40. (concat
  41. (mapconcat
  42. (lambda (pair) (format "let %s = %s;" (car pair) (cdr pair)))
  43. vars "\n") "\n" body "\n")))
  44. (defun org-babel-execute:ocaml (body params)
  45. "Execute a block of Ocaml code with org-babel."
  46. (message "executing ocaml source code block")
  47. (let* ((processed-params (org-babel-process-params params))
  48. (vars (nth 1 processed-params))
  49. (full-body (org-babel-expand-body:ocaml body params processed-params))
  50. (session (org-babel-prep-session:ocaml
  51. (cdr (assoc :session params)) params))
  52. (raw (org-babel-comint-with-output
  53. (session org-babel-ocaml-eoe-output t full-body)
  54. (insert (concat (org-babel-chomp full-body) " ;;"))
  55. (comint-send-input nil t)
  56. (insert org-babel-ocaml-eoe-indicator)
  57. (comint-send-input nil t))))
  58. (org-babel-reassemble-table
  59. (org-babel-ocaml-parse-output (org-babel-trim (car raw)))
  60. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  61. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params))))))
  62. (defvar tuareg-interactive-buffer-name)
  63. (defun org-babel-prep-session:ocaml (session params)
  64. "Prepare SESSION according to the header arguments specified in PARAMS."
  65. (require 'tuareg)
  66. (let ((tuareg-interactive-buffer-name (if (and (not (string= session "none"))
  67. (not (string= session "default"))
  68. (stringp session))
  69. session
  70. tuareg-interactive-buffer-name)))
  71. (save-window-excursion (tuareg-run-caml)
  72. (get-buffer tuareg-interactive-buffer-name))))
  73. (defun org-babel-ocaml-parse-output (output)
  74. "Parse OUTPUT where OUTPUT is string output from an ocaml
  75. process."
  76. (let ((regexp "%s = \\(.+\\)$"))
  77. (cond
  78. ((string-match (format regexp "string") output)
  79. (org-babel-read (match-string 1 output)))
  80. ((or (string-match (format regexp "int") output)
  81. (string-match (format regexp "float") output))
  82. (string-to-number (match-string 1 output)))
  83. ((string-match (format regexp "list") output)
  84. (org-babel-ocaml-read-list (match-string 1 output)))
  85. ((string-match (format regexp "array") output)
  86. (org-babel-ocaml-read-array (match-string 1 output)))
  87. (t (message "don't recognize type of %s" output) output))))
  88. (defun org-babel-ocaml-read-list (results)
  89. "If the results look like a table, then convert them into an
  90. Emacs-lisp table, otherwise return the results as a string."
  91. (org-babel-read
  92. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  93. (org-babel-read
  94. (replace-regexp-in-string
  95. "\\[" "(" (replace-regexp-in-string
  96. "\\]" ")" (replace-regexp-in-string
  97. "; " " " (replace-regexp-in-string
  98. "'" "\"" results)))))
  99. results)))
  100. (defun org-babel-ocaml-read-array (results)
  101. "If the results look like a table, then convert them into an
  102. Emacs-lisp table, otherwise return the results as a string."
  103. (org-babel-read
  104. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  105. (org-babel-read
  106. (replace-regexp-in-string
  107. "\\[|" "(" (replace-regexp-in-string
  108. "|\\]" ")" (replace-regexp-in-string
  109. "; " " " (replace-regexp-in-string
  110. "'" "\"" results)))))
  111. results)))
  112. (provide 'ob-ocaml)
  113. ;; arch-tag: 2e815f4d-365e-4d69-b1df-dd17fdd7b7b7
  114. ;;; ob-ocaml.el ends here