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