ob-ocaml.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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: 7.6
  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:tuareg" ())
  33. (declare-function tuareg-interactive-send-input "ext:tuareg" ())
  34. (defvar org-babel-tangle-lang-exts)
  35. (add-to-list 'org-babel-tangle-lang-exts '("ocaml" . "ml"))
  36. (defvar org-babel-default-header-args:ocaml '())
  37. (defvar org-babel-ocaml-eoe-indicator "\"org-babel-ocaml-eoe\";;")
  38. (defvar org-babel-ocaml-eoe-output "org-babel-ocaml-eoe")
  39. (defun org-babel-execute:ocaml (body params)
  40. "Execute a block of Ocaml code with Babel."
  41. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  42. (full-body (org-babel-expand-body:generic
  43. body params
  44. (org-babel-variable-assignments:ocaml params)))
  45. (session (org-babel-prep-session:ocaml
  46. (cdr (assoc :session params)) params))
  47. (raw (org-babel-comint-with-output
  48. (session org-babel-ocaml-eoe-output t full-body)
  49. (insert
  50. (concat
  51. (org-babel-chomp full-body)"\n"org-babel-ocaml-eoe-indicator))
  52. (tuareg-interactive-send-input)))
  53. (clean
  54. (car (let ((re (regexp-quote org-babel-ocaml-eoe-output)) out)
  55. (delq nil (mapcar (lambda (line)
  56. (if out
  57. (progn (setq out nil) line)
  58. (when (string-match re line)
  59. (progn (setq out t) nil))))
  60. (mapcar #'org-babel-trim (reverse raw))))))))
  61. (org-babel-reassemble-table
  62. (org-babel-ocaml-parse-output (org-babel-trim clean))
  63. (org-babel-pick-name
  64. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  65. (org-babel-pick-name
  66. (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
  67. (defvar tuareg-interactive-buffer-name)
  68. (defun org-babel-prep-session:ocaml (session params)
  69. "Prepare SESSION according to the header arguments in PARAMS."
  70. (require 'tuareg)
  71. (let ((tuareg-interactive-buffer-name (if (and (not (string= session "none"))
  72. (not (string= session "default"))
  73. (stringp session))
  74. session
  75. tuareg-interactive-buffer-name)))
  76. (save-window-excursion (tuareg-run-caml)
  77. (get-buffer tuareg-interactive-buffer-name))))
  78. (defun org-babel-variable-assignments:ocaml (params)
  79. "Return list of ocaml statements assigning the block's variables"
  80. (mapcar
  81. (lambda (pair) (format "let %s = %s;;" (car pair)
  82. (org-babel-ocaml-elisp-to-ocaml (cdr pair))))
  83. (mapcar #'cdr (org-babel-get-header params :var))))
  84. (defun org-babel-ocaml-elisp-to-ocaml (val)
  85. "Return a string of ocaml code which evaluates to VAL."
  86. (if (listp val)
  87. (concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml val "; ") "|]")
  88. (format "%S" val)))
  89. (defun org-babel-ocaml-parse-output (output)
  90. "Parse OUTPUT.
  91. OUTPUT is string output from an ocaml process."
  92. (let ((regexp "%s = \\(.+\\)$"))
  93. (cond
  94. ((string-match (format regexp "string") output)
  95. (org-babel-read (match-string 1 output)))
  96. ((or (string-match (format regexp "int") output)
  97. (string-match (format regexp "float") output))
  98. (string-to-number (match-string 1 output)))
  99. ((string-match (format regexp "list") output)
  100. (org-babel-ocaml-read-list (match-string 1 output)))
  101. ((string-match (format regexp "array") output)
  102. (org-babel-ocaml-read-array (match-string 1 output)))
  103. (t (message "don't recognize type of %s" output) output))))
  104. (defun org-babel-ocaml-read-list (results)
  105. "Convert RESULTS into an elisp table or string.
  106. If the results look like a table, then convert them into an
  107. Emacs-lisp table, otherwise return the results as a string."
  108. (org-babel-script-escape (replace-regexp-in-string ";" "," results)))
  109. (defun org-babel-ocaml-read-array (results)
  110. "Convert RESULTS into an elisp table or string.
  111. If the results look like a table, then convert them into an
  112. Emacs-lisp table, otherwise return the results as a string."
  113. (org-babel-script-escape
  114. (replace-regexp-in-string
  115. "\\[|" "[" (replace-regexp-in-string
  116. "|\\]" "]" (replace-regexp-in-string
  117. "; " "," results)))))
  118. (provide 'ob-ocaml)
  119. ;; arch-tag: 2e815f4d-365e-4d69-b1df-dd17fdd7b7b7
  120. ;;; ob-ocaml.el ends here