1 ;;; ob-ocaml.el --- org-babel functions for ocaml evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
27 ;; Org-Babel support for evaluating ocaml source code. This one will
28 ;; be sort of tricky because ocaml programs must be compiled before
29 ;; they can be run, but ocaml code can also be run through an
30 ;; interactive interpreter.
32 ;; For now lets only allow evaluation using the ocaml interpreter.
36 ;; - tuareg-mode :: http://www-rocq.inria.fr/~acohen/tuareg/
42 (eval-when-compile (require 'cl
))
44 (declare-function tuareg-run-caml
"ext:taureg" ())
46 (add-to-list 'org-babel-tangle-lang-exts
'("ocaml" .
"ml"))
48 (defvar org-babel-default-header-args
:ocaml
'())
50 (defvar org-babel-ocaml-eoe-indicator
"\"org-babel-ocaml-eoe\";;")
51 (defvar org-babel-ocaml-eoe-output
"org-babel-ocaml-eoe")
53 (defun org-babel-expand-body:ocaml
(body params
&optional processed-params
)
54 "Expand BODY according to PARAMS, return the expanded body."
55 (let ((vars (nth 1 (or processed-params
(org-babel-process-params params
)))))
58 (lambda (pair) (format "let %s = %s;" (car pair
) (cdr pair
)))
59 vars
"\n") "\n" body
"\n")))
61 (defun org-babel-execute:ocaml
(body params
)
62 "Execute a block of Ocaml code with org-babel."
63 (message "executing ocaml source code block")
64 (let* ((processed-params (org-babel-process-params params
))
65 (vars (nth 1 processed-params
))
66 (full-body (org-babel-expand-body:ocaml body params processed-params
))
67 (session (org-babel-prep-session:ocaml
68 (cdr (assoc :session params
)) params
))
69 (raw (org-babel-comint-with-output
70 (session org-babel-ocaml-eoe-output t full-body
)
71 (insert (concat (org-babel-chomp full-body
) " ;;"))
72 (comint-send-input nil t
)
73 (insert org-babel-ocaml-eoe-indicator
)
74 (comint-send-input nil t
))))
75 (org-babel-reassemble-table
76 (org-babel-ocaml-parse-output (org-babel-trim (car raw
)))
77 (org-babel-pick-name (nth 4 processed-params
) (cdr (assoc :colnames params
)))
78 (org-babel-pick-name (nth 5 processed-params
) (cdr (assoc :rownames params
))))))
80 (defvar tuareg-interactive-buffer-name
)
81 (defun org-babel-prep-session:ocaml
(session params
)
82 "Prepare SESSION according to the header arguments specified in PARAMS."
84 (let ((tuareg-interactive-buffer-name (if (and (not (string= session
"none"))
85 (not (string= session
"default"))
88 tuareg-interactive-buffer-name
)))
89 (save-window-excursion (tuareg-run-caml)
90 (get-buffer tuareg-interactive-buffer-name
))))
92 (defun org-babel-ocaml-parse-output (output)
93 "Parse OUTPUT where OUTPUT is string output from an ocaml
95 (let ((regexp "%s = \\(.+\\)$"))
97 ((string-match (format regexp
"string") output
)
98 (org-babel-read (match-string 1 output
)))
99 ((or (string-match (format regexp
"int") output
)
100 (string-match (format regexp
"float") output
))
101 (string-to-number (match-string 1 output
)))
102 ((string-match (format regexp
"list") output
)
103 (org-babel-ocaml-read-list (match-string 1 output
)))
104 ((string-match (format regexp
"array") output
)
105 (org-babel-ocaml-read-array (match-string 1 output
)))
106 (t (message "don't recognize type of %s" output
) output
))))
108 (defun org-babel-ocaml-read-list (results)
109 "If the results look like a table, then convert them into an
110 Emacs-lisp table, otherwise return the results as a string."
112 (if (and (stringp results
) (string-match "^\\[.+\\]$" results
))
114 (replace-regexp-in-string
115 "\\[" "(" (replace-regexp-in-string
116 "\\]" ")" (replace-regexp-in-string
117 "; " " " (replace-regexp-in-string
118 "'" "\"" results
)))))
121 (defun org-babel-ocaml-read-array (results)
122 "If the results look like a table, then convert them into an
123 Emacs-lisp table, otherwise return the results as a string."
125 (if (and (stringp results
) (string-match "^\\[.+\\]$" results
))
127 (replace-regexp-in-string
128 "\\[|" "(" (replace-regexp-in-string
129 "|\\]" ")" (replace-regexp-in-string
130 "; " " " (replace-regexp-in-string
131 "'" "\"" results
)))))
136 ;; arch-tag: 2e815f4d-365e-4d69-b1df-dd17fdd7b7b7
138 ;;; ob-ocaml.el ends here