ob-ocaml.el 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ;;; ob-ocaml.el --- Babel Functions for Ocaml -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; URL: https://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 <https://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 :: https://elpa.nongnu.org/nongnu/tuareg.html
  26. ;;; Code:
  27. (require 'org-macs)
  28. (org-assert-version)
  29. (require 'ob)
  30. (require 'comint)
  31. (require 'org-macs)
  32. (declare-function tuareg-run-caml "ext:tuareg" ())
  33. (declare-function tuareg-run-ocaml "ext:tuareg" ())
  34. (declare-function tuareg-interactive-send-input "ext:tuareg" ())
  35. (defvar org-babel-tangle-lang-exts)
  36. (add-to-list 'org-babel-tangle-lang-exts '("ocaml" . "ml"))
  37. (defvar org-babel-default-header-args:ocaml '())
  38. (defvar org-babel-ocaml-eoe-indicator "\"org-babel-ocaml-eoe\";;")
  39. (defvar org-babel-ocaml-eoe-output "org-babel-ocaml-eoe")
  40. (defcustom org-babel-ocaml-command "ocaml"
  41. "Name of the command for executing Ocaml code."
  42. :version "24.4"
  43. :package-version '(Org . "8.0")
  44. :group 'org-babel
  45. :type 'string)
  46. (defun org-babel-execute:ocaml (body params)
  47. "Execute a block of Ocaml code with Babel."
  48. (let* ((full-body (org-babel-expand-body:generic
  49. body params
  50. (org-babel-variable-assignments:ocaml params)))
  51. (session (org-babel-prep-session:ocaml
  52. (cdr (assq :session params)) params))
  53. (raw (org-babel-comint-with-output
  54. (session org-babel-ocaml-eoe-output nil full-body)
  55. (insert
  56. (concat
  57. (org-babel-chomp full-body) ";;\n"
  58. org-babel-ocaml-eoe-indicator))
  59. (tuareg-interactive-send-input)))
  60. (clean
  61. (car (let ((re (regexp-quote org-babel-ocaml-eoe-output)) out)
  62. (delq nil (mapcar (lambda (line)
  63. (if out
  64. (progn (setq out nil) line)
  65. (when (string-match re line)
  66. (progn (setq out t) nil))))
  67. (mapcar #'org-trim (reverse raw)))))))
  68. (raw (org-trim clean))
  69. (result-params (cdr (assq :result-params params))))
  70. (string-match
  71. "\\(\\(.*\n\\)*\\)[^:\n]+ : \\([^=\n]+\\) =[[:space:]]+\\(\\(.\\|\n\\)+\\)$"
  72. raw)
  73. (let ((output (match-string 1 raw))
  74. (type (match-string 3 raw))
  75. (value (match-string 4 raw)))
  76. (org-babel-reassemble-table
  77. (org-babel-result-cond result-params
  78. (cond
  79. ((member "verbatim" result-params) raw)
  80. ((member "output" result-params) output)
  81. (t raw))
  82. (if (and value type)
  83. (org-babel-ocaml-parse-output value type)
  84. raw))
  85. (org-babel-pick-name
  86. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  87. (org-babel-pick-name
  88. (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))))
  89. (defvar tuareg-interactive-buffer-name)
  90. (defun org-babel-prep-session:ocaml (session _params)
  91. "Prepare SESSION according to the header arguments in PARAMS."
  92. (require 'tuareg)
  93. (let ((tuareg-interactive-buffer-name (if (and (not (string= session "none"))
  94. (not (string= session "default"))
  95. (stringp session))
  96. session
  97. tuareg-interactive-buffer-name)))
  98. (save-window-excursion (if (fboundp 'tuareg-run-process-if-needed)
  99. (tuareg-run-process-if-needed org-babel-ocaml-command)
  100. (tuareg-run-caml)))
  101. (get-buffer tuareg-interactive-buffer-name)))
  102. (defun org-babel-variable-assignments:ocaml (params)
  103. "Return list of ocaml statements assigning the block's variables."
  104. (mapcar
  105. (lambda (pair) (format "let %s = %s;;" (car pair)
  106. (org-babel-ocaml-elisp-to-ocaml (cdr pair))))
  107. (org-babel--get-vars params)))
  108. (defun org-babel-ocaml-elisp-to-ocaml (val)
  109. "Return a string of ocaml code which evaluates to VAL."
  110. (if (listp val)
  111. (concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml val "; ") "|]")
  112. (format "%S" val)))
  113. (defun org-babel-ocaml-parse-output (value type)
  114. "Parse VALUE of type TYPE.
  115. VALUE and TYPE are string output from an ocaml process."
  116. (cond
  117. ((string= "string" type)
  118. (org-babel-read value))
  119. ((or (string= "int" type)
  120. (string= "float" type))
  121. (string-to-number value))
  122. ((string-match "list" type)
  123. (org-babel-ocaml-read-list value))
  124. ((string-match "array" type)
  125. (org-babel-ocaml-read-array value))
  126. (t (message "don't recognize type %s" type) value)))
  127. (defun org-babel-ocaml-read-list (results)
  128. "Convert RESULTS into an elisp table or string.
  129. If the results look like a table, then convert them into an
  130. Emacs-lisp table, otherwise return the results as a string."
  131. ;; XXX: This probably does not behave as expected when a semicolon
  132. ;; is in a string in a list. The same comment applies to
  133. ;; `org-babel-ocaml-read-array' below (with even more failure
  134. ;; modes).
  135. (org-babel-script-escape (replace-regexp-in-string ";" "," results)))
  136. (defun org-babel-ocaml-read-array (results)
  137. "Convert RESULTS into an elisp table or string.
  138. If the results look like a table, then convert them into an
  139. Emacs-lisp table, otherwise return the results as a string."
  140. (org-babel-script-escape
  141. (replace-regexp-in-string
  142. "\\[|" "[" (replace-regexp-in-string
  143. "|\\]" "]" (replace-regexp-in-string
  144. "; " "," results)))))
  145. (provide 'ob-ocaml)
  146. ;;; ob-ocaml.el ends here