ob-ocaml.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 'tuareg)
  30. (add-to-list 'org-babel-tangle-lang-exts '("ocaml" . "ml"))
  31. (defvar org-babel-default-header-args:ocaml '())
  32. (defvar org-babel-ocaml-eoe-indicator "\"org-babel-ocaml-eoe\";;")
  33. (defvar org-babel-ocaml-eoe-output "org-babel-ocaml-eoe")
  34. (defun org-babel-expand-body:ocaml (body params &optional processed-params)
  35. "Expand BODY according to PARAMS, return the expanded body."
  36. (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
  37. (concat
  38. (mapconcat
  39. (lambda (pair) (format "let %s = %s;" (car pair) (cdr pair)))
  40. vars "\n") "\n" body "\n")))
  41. (defun org-babel-execute:ocaml (body params)
  42. "Execute a block of Ocaml code with org-babel."
  43. (message "executing ocaml source code block")
  44. (let* ((processed-params (org-babel-process-params params))
  45. (vars (nth 1 processed-params))
  46. (full-body (org-babel-expand-body:ocaml body params processed-params))
  47. (session (org-babel-prep-session:ocaml session params))
  48. (raw (org-babel-comint-with-output
  49. (session org-babel-ocaml-eoe-output t full-body)
  50. (insert (concat (org-babel-chomp full-body) " ;;"))
  51. (comint-send-input nil t)
  52. (insert org-babel-ocaml-eoe-indicator)
  53. (comint-send-input nil t))))
  54. (org-babel-reassemble-table
  55. (org-babel-ocaml-parse-output (org-babel-trim (car raw)))
  56. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  57. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params))))))
  58. (defun org-babel-prep-session:ocaml (session params)
  59. "Prepare SESSION according to the header arguments specified in PARAMS."
  60. (let ((tuareg-interactive-buffer-name (if (and (not (string= session "none"))
  61. (not (string= session "default"))
  62. (stringp session))
  63. session
  64. tuareg-interactive-buffer-name)))
  65. (save-window-excursion (tuareg-run-caml)
  66. (get-buffer tuareg-interactive-buffer-name))))
  67. (defun org-babel-ocaml-parse-output (output)
  68. "Parse OUTPUT where OUTPUT is string output from an ocaml
  69. process."
  70. (let ((regexp "%s = \\(.+\\)$"))
  71. (cond
  72. ((string-match (format regexp "string") output)
  73. (org-babel-read (match-string 1 output)))
  74. ((or (string-match (format regexp "int") output)
  75. (string-match (format regexp "float") output))
  76. (string-to-number (match-string 1 output)))
  77. ((string-match (format regexp "list") output)
  78. (org-babel-ocaml-read-list (match-string 1 output)))
  79. ((string-match (format regexp "array") output)
  80. (org-babel-ocaml-read-array (match-string 1 output)))
  81. (t (message "don't recognize type of %s" output) output))))
  82. (defun org-babel-ocaml-read-list (results)
  83. "If the results look like a table, then convert them into an
  84. Emacs-lisp table, otherwise return the results as a string."
  85. (org-babel-read
  86. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  87. (org-babel-read
  88. (replace-regexp-in-string
  89. "\\[" "(" (replace-regexp-in-string
  90. "\\]" ")" (replace-regexp-in-string
  91. "; " " " (replace-regexp-in-string
  92. "'" "\"" results)))))
  93. results)))
  94. (defun org-babel-ocaml-read-array (results)
  95. "If the results look like a table, then convert them into an
  96. Emacs-lisp table, otherwise return the results as a string."
  97. (org-babel-read
  98. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  99. (org-babel-read
  100. (replace-regexp-in-string
  101. "\\[|" "(" (replace-regexp-in-string
  102. "|\\]" ")" (replace-regexp-in-string
  103. "; " " " (replace-regexp-in-string
  104. "'" "\"" results)))))
  105. results)))
  106. (provide 'ob-ocaml)
  107. ;; arch-tag: 2e815f4d-365e-4d69-b1df-dd17fdd7b7b7
  108. ;;; ob-ocaml.el ends here