org-babel-ocaml.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ;;; org-babel-ocaml.el --- org-babel functions for ocaml evaluation
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating ocaml source code. This one will
  24. ;; be sort of tricky because ocaml programs must be compiled before
  25. ;; they can be run, but ocaml code can also be run through an
  26. ;; interactive interpreter.
  27. ;;
  28. ;; For now lets only allow evaluation using the ocaml interpreter.
  29. ;;; Requirements:
  30. ;; - tuareg-mode :: http://www-rocq.inria.fr/~acohen/tuareg/
  31. ;;; Code:
  32. (require 'org-babel)
  33. (require 'tuareg)
  34. (org-babel-add-interpreter "ocaml")
  35. (add-to-list 'org-babel-tangle-langs '("ocaml" "ml"))
  36. (defvar org-babel-ocaml-eoe-indicator "\"org-babel-ocaml-eoe\";;")
  37. (defvar org-babel-ocaml-eoe-output "org-babel-ocaml-eoe")
  38. (defun org-babel-execute:ocaml (body params)
  39. "Execute a block of Ocaml code with org-babel. This function
  40. is called by `org-babel-execute-src-block' with the following
  41. variables pre-set using `multiple-value-bind'.
  42. (session vars result-params result-type)"
  43. (message "executing ocaml source code block")
  44. (let* ((full-body (concat
  45. (mapconcat
  46. (lambda (pair) (format "let %s = %s;" (car pair) (cdr pair)))
  47. vars "\n") "\n" body "\n"))
  48. (session (org-babel-prep-session:ocaml session params))
  49. (raw (org-babel-comint-with-output session org-babel-ocaml-eoe-output t
  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-ocaml-parse-output (org-babel-trim (car raw)))))
  55. (defun org-babel-prep-session:ocaml (session params)
  56. "Prepare SESSION according to the header arguments specified in PARAMS."
  57. (let ((tuareg-interactive-buffer-name (if (and (not (string= session "none"))
  58. (not (string= session "default"))
  59. (stringp session))
  60. session
  61. tuareg-interactive-buffer-name)))
  62. (save-window-excursion (tuareg-run-caml)
  63. (get-buffer tuareg-interactive-buffer-name))))
  64. (defun org-babel-ocaml-parse-output (output)
  65. (let ((regexp "%s = \\(.+\\)$"))
  66. (cond
  67. ((string-match (format regexp "string") output)
  68. (org-babel-read (match-string 1 output)))
  69. ((or (string-match (format regexp "int") output)
  70. (string-match (format regexp "float") output))
  71. (string-to-number (match-string 1 output)))
  72. ((string-match (format regexp "list") output)
  73. (org-babel-ocaml-read-list (match-string 1 output)))
  74. ((string-match (format regexp "array") output)
  75. (org-babel-ocaml-read-array (match-string 1 output)))
  76. (t (message "don't recognize type of %s" output) output))))
  77. (defun org-babel-ocaml-read-list (results)
  78. "If the results look like a table, then convert them into an
  79. Emacs-lisp table, otherwise return the results as a string."
  80. (org-babel-read
  81. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  82. (org-babel-read
  83. (replace-regexp-in-string
  84. "\\[" "(" (replace-regexp-in-string
  85. "\\]" ")" (replace-regexp-in-string
  86. "; " " " (replace-regexp-in-string
  87. "'" "\"" results)))))
  88. results)))
  89. (defun org-babel-ocaml-read-array (results)
  90. "If the results look like a table, then convert them into an
  91. Emacs-lisp table, otherwise return the results as a string."
  92. (org-babel-read
  93. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  94. (org-babel-read
  95. (replace-regexp-in-string
  96. "\\[|" "(" (replace-regexp-in-string
  97. "|\\]" ")" (replace-regexp-in-string
  98. "; " " " (replace-regexp-in-string
  99. "'" "\"" results)))))
  100. results)))
  101. (provide 'org-babel-ocaml)
  102. ;;; org-babel-ocaml.el ends here