1 ;;; ob-ocaml.el --- org-babel functions for ocaml evaluation
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Org-Babel support for evaluating ocaml source code. This one will
27 ;; be sort of tricky because ocaml programs must be compiled before
28 ;; they can be run, but ocaml code can also be run through an
29 ;; interactive interpreter.
31 ;; For now lets only allow evaluation using the ocaml interpreter.
35 ;; - tuareg-mode :: http://www-rocq.inria.fr/~acohen/tuareg/
40 (eval-when-compile (require 'cl
))
42 (declare-function tuareg-run-caml
"ext:tuareg" ())
43 (declare-function tuareg-interactive-send-input
"ext:tuareg" ())
45 (defvar org-babel-tangle-lang-exts
)
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-execute:ocaml
(body params
)
54 "Execute a block of Ocaml code with Babel."
55 (let* ((vars (mapcar #'cdr
(org-babel-get-header params
:var
)))
56 (full-body (org-babel-expand-body:generic
58 (org-babel-variable-assignments:ocaml params
)))
59 (session (org-babel-prep-session:ocaml
60 (cdr (assoc :session params
)) params
))
61 (raw (org-babel-comint-with-output
62 (session org-babel-ocaml-eoe-output t full-body
)
65 (org-babel-chomp full-body
)"\n"org-babel-ocaml-eoe-indicator
))
66 (tuareg-interactive-send-input)))
68 (car (let ((re (regexp-quote org-babel-ocaml-eoe-output
)) out
)
69 (delq nil
(mapcar (lambda (line)
71 (progn (setq out nil
) line
)
72 (when (string-match re line
)
73 (progn (setq out t
) nil
))))
74 (mapcar #'org-babel-trim
(reverse raw
))))))))
75 (org-babel-reassemble-table
76 (org-babel-ocaml-parse-output (org-babel-trim clean
))
78 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
80 (cdr (assoc :rowname-names params
)) (cdr (assoc :rownames params
))))))
82 (defvar tuareg-interactive-buffer-name
)
83 (defun org-babel-prep-session:ocaml
(session params
)
84 "Prepare SESSION according to the header arguments in PARAMS."
86 (let ((tuareg-interactive-buffer-name (if (and (not (string= session
"none"))
87 (not (string= session
"default"))
90 tuareg-interactive-buffer-name
)))
91 (save-window-excursion (tuareg-run-caml)
92 (get-buffer tuareg-interactive-buffer-name
))))
94 (defun org-babel-variable-assignments:ocaml
(params)
95 "Return list of ocaml statements assigning the block's variables."
97 (lambda (pair) (format "let %s = %s;;" (car pair
)
98 (org-babel-ocaml-elisp-to-ocaml (cdr pair
))))
99 (mapcar #'cdr
(org-babel-get-header params
:var
))))
101 (defun org-babel-ocaml-elisp-to-ocaml (val)
102 "Return a string of ocaml code which evaluates to VAL."
104 (concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml val
"; ") "|]")
107 (defun org-babel-ocaml-parse-output (output)
109 OUTPUT is string output from an ocaml process."
110 (let ((regexp "%s = \\(.+\\)$"))
112 ((string-match (format regexp
"string") output
)
113 (org-babel-read (match-string 1 output
)))
114 ((or (string-match (format regexp
"int") output
)
115 (string-match (format regexp
"float") output
))
116 (string-to-number (match-string 1 output
)))
117 ((string-match (format regexp
"list") output
)
118 (org-babel-ocaml-read-list (match-string 1 output
)))
119 ((string-match (format regexp
"array") output
)
120 (org-babel-ocaml-read-array (match-string 1 output
)))
121 (t (message "don't recognize type of %s" output
) output
))))
123 (defun org-babel-ocaml-read-list (results)
124 "Convert RESULTS into an elisp table or string.
125 If the results look like a table, then convert them into an
126 Emacs-lisp table, otherwise return the results as a string."
127 (org-babel-script-escape (replace-regexp-in-string ";" "," results
)))
129 (defun org-babel-ocaml-read-array (results)
130 "Convert RESULTS into an elisp table or string.
131 If the results look like a table, then convert them into an
132 Emacs-lisp table, otherwise return the results as a string."
133 (org-babel-script-escape
134 (replace-regexp-in-string
135 "\\[|" "[" (replace-regexp-in-string
136 "|\\]" "]" (replace-regexp-in-string
137 "; " "," results
)))))
143 ;;; ob-ocaml.el ends here