ob-ocaml.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ;;; ob-ocaml.el --- org-babel functions for ocaml evaluation
  2. ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs 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 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating ocaml source code. This one will
  19. ;; be sort of tricky because ocaml programs must be compiled before
  20. ;; they can be run, but ocaml code can also be run through an
  21. ;; interactive interpreter.
  22. ;;
  23. ;; For now lets only allow evaluation using the ocaml interpreter.
  24. ;;; Requirements:
  25. ;; - tuareg-mode :: http://www-rocq.inria.fr/~acohen/tuareg/
  26. ;;; Code:
  27. (require 'ob)
  28. (require 'comint)
  29. (eval-when-compile (require 'cl))
  30. (declare-function tuareg-run-caml "ext:tuareg" ())
  31. (declare-function tuareg-interactive-send-input "ext:tuareg" ())
  32. (defvar org-babel-tangle-lang-exts)
  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-execute:ocaml (body params)
  38. "Execute a block of Ocaml code with Babel."
  39. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  40. (full-body (org-babel-expand-body:generic
  41. body params
  42. (org-babel-variable-assignments:ocaml params)))
  43. (session (org-babel-prep-session:ocaml
  44. (cdr (assoc :session params)) params))
  45. (raw (org-babel-comint-with-output
  46. (session org-babel-ocaml-eoe-output t full-body)
  47. (insert
  48. (concat
  49. (org-babel-chomp full-body)"\n"org-babel-ocaml-eoe-indicator))
  50. (tuareg-interactive-send-input)))
  51. (clean
  52. (car (let ((re (regexp-quote org-babel-ocaml-eoe-output)) out)
  53. (delq nil (mapcar (lambda (line)
  54. (if out
  55. (progn (setq out nil) line)
  56. (when (string-match re line)
  57. (progn (setq out t) nil))))
  58. (mapcar #'org-babel-trim (reverse raw))))))))
  59. (org-babel-reassemble-table
  60. (org-babel-ocaml-parse-output (org-babel-trim clean))
  61. (org-babel-pick-name
  62. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  63. (org-babel-pick-name
  64. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  65. (defvar tuareg-interactive-buffer-name)
  66. (defun org-babel-prep-session:ocaml (session params)
  67. "Prepare SESSION according to the header arguments in PARAMS."
  68. (require 'tuareg)
  69. (let ((tuareg-interactive-buffer-name (if (and (not (string= session "none"))
  70. (not (string= session "default"))
  71. (stringp session))
  72. session
  73. tuareg-interactive-buffer-name)))
  74. (save-window-excursion (tuareg-run-caml)
  75. (get-buffer tuareg-interactive-buffer-name))))
  76. (defun org-babel-variable-assignments:ocaml (params)
  77. "Return list of ocaml statements assigning the block's variables."
  78. (mapcar
  79. (lambda (pair) (format "let %s = %s;;" (car pair)
  80. (org-babel-ocaml-elisp-to-ocaml (cdr pair))))
  81. (mapcar #'cdr (org-babel-get-header params :var))))
  82. (defun org-babel-ocaml-elisp-to-ocaml (val)
  83. "Return a string of ocaml code which evaluates to VAL."
  84. (if (listp val)
  85. (concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml val "; ") "|]")
  86. (format "%S" val)))
  87. (defun org-babel-ocaml-parse-output (output)
  88. "Parse OUTPUT.
  89. OUTPUT is string output from an ocaml process."
  90. (let ((regexp "%s = \\(.+\\)$"))
  91. (cond
  92. ((string-match (format regexp "string") output)
  93. (org-babel-read (match-string 1 output)))
  94. ((or (string-match (format regexp "int") output)
  95. (string-match (format regexp "float") output))
  96. (string-to-number (match-string 1 output)))
  97. ((string-match (format regexp "list") output)
  98. (org-babel-ocaml-read-list (match-string 1 output)))
  99. ((string-match (format regexp "array") output)
  100. (org-babel-ocaml-read-array (match-string 1 output)))
  101. (t (message "don't recognize type of %s" output) output))))
  102. (defun org-babel-ocaml-read-list (results)
  103. "Convert RESULTS into an elisp table or string.
  104. If the results look like a table, then convert them into an
  105. Emacs-lisp table, otherwise return the results as a string."
  106. (org-babel-script-escape (replace-regexp-in-string ";" "," results)))
  107. (defun org-babel-ocaml-read-array (results)
  108. "Convert RESULTS into an elisp table or string.
  109. If the results look like a table, then convert them into an
  110. Emacs-lisp table, otherwise return the results as a string."
  111. (org-babel-script-escape
  112. (replace-regexp-in-string
  113. "\\[|" "[" (replace-regexp-in-string
  114. "|\\]" "]" (replace-regexp-in-string
  115. "; " "," results)))))
  116. (provide 'ob-ocaml)
  117. ;;; ob-ocaml.el ends here