1 ;;; ob-ocaml.el --- org-babel functions for ocaml evaluation
3 ;; Copyright (C) 2009-2015 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-run-ocaml
"ext:tuareg" ())
44 (declare-function tuareg-interactive-send-input
"ext:tuareg" ())
46 (defvar org-babel-tangle-lang-exts
)
47 (add-to-list 'org-babel-tangle-lang-exts
'("ocaml" .
"ml"))
49 (defvar org-babel-default-header-args
:ocaml
'())
51 (defvar org-babel-ocaml-eoe-indicator
"\"org-babel-ocaml-eoe\";;")
52 (defvar org-babel-ocaml-eoe-output
"org-babel-ocaml-eoe")
54 (defcustom org-babel-ocaml-command
"ocaml"
55 "Name of the command for executing Ocaml code."
57 :package-version
'(Org .
"8.0")
61 (defun org-babel-execute:ocaml
(body params
)
62 "Execute a block of Ocaml code with Babel."
63 (let* ((vars (mapcar #'cdr
(org-babel-get-header params
:var
)))
64 (full-body (org-babel-expand-body:generic
66 (org-babel-variable-assignments:ocaml 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
)
73 (org-babel-chomp full-body
)";;\n"org-babel-ocaml-eoe-indicator
))
74 (tuareg-interactive-send-input)))
76 (car (let ((re (regexp-quote org-babel-ocaml-eoe-output
)) out
)
77 (delq nil
(mapcar (lambda (line)
79 (progn (setq out nil
) line
)
80 (when (string-match re line
)
81 (progn (setq out t
) nil
))))
82 (mapcar #'org-babel-trim
(reverse raw
))))))))
83 (org-babel-reassemble-table
84 (let ((raw (org-babel-trim clean
))
85 (result-params (cdr (assoc :result-params params
))))
86 (org-babel-result-cond result-params
87 ;; strip type information from output unless verbatim is specified
88 (if (and (not (member "verbatim" result-params
))
89 (string-match "= \\(.+\\)$" raw
))
90 (match-string 1 raw
) raw
)
91 (org-babel-ocaml-parse-output raw
)))
93 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
95 (cdr (assoc :rowname-names params
)) (cdr (assoc :rownames params
))))))
97 (defvar tuareg-interactive-buffer-name
)
98 (defun org-babel-prep-session:ocaml
(session params
)
99 "Prepare SESSION according to the header arguments in PARAMS."
101 (let ((tuareg-interactive-buffer-name (if (and (not (string= session
"none"))
102 (not (string= session
"default"))
105 tuareg-interactive-buffer-name
)))
106 (save-window-excursion (if (fboundp 'tuareg-run-process-if-needed
)
107 (tuareg-run-process-if-needed org-babel-ocaml-command
)
109 (get-buffer tuareg-interactive-buffer-name
)))
111 (defun org-babel-variable-assignments:ocaml
(params)
112 "Return list of ocaml statements assigning the block's variables."
114 (lambda (pair) (format "let %s = %s;;" (car pair
)
115 (org-babel-ocaml-elisp-to-ocaml (cdr pair
))))
116 (mapcar #'cdr
(org-babel-get-header params
:var
))))
118 (defun org-babel-ocaml-elisp-to-ocaml (val)
119 "Return a string of ocaml code which evaluates to VAL."
121 (concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml val
"; ") "|]")
124 (defun org-babel-ocaml-parse-output (output)
126 OUTPUT is string output from an ocaml process."
127 (let ((regexp "[^:]+ : %s = \\(.+\\)$"))
129 ((string-match (format regexp
"string") output
)
130 (org-babel-read (match-string 1 output
)))
131 ((or (string-match (format regexp
"int") output
)
132 (string-match (format regexp
"float") output
))
133 (string-to-number (match-string 1 output
)))
134 ((string-match (format regexp
"list") output
)
135 (org-babel-ocaml-read-list (match-string 1 output
)))
136 ((string-match (format regexp
"array") output
)
137 (org-babel-ocaml-read-array (match-string 1 output
)))
138 (t (message "don't recognize type of %s" output
) output
))))
140 (defun org-babel-ocaml-read-list (results)
141 "Convert RESULTS into an elisp table or string.
142 If the results look like a table, then convert them into an
143 Emacs-lisp table, otherwise return the results as a string."
144 (org-babel-script-escape (replace-regexp-in-string ";" "," results
)))
146 (defun org-babel-ocaml-read-array (results)
147 "Convert RESULTS into an elisp table or string.
148 If the results look like a table, then convert them into an
149 Emacs-lisp table, otherwise return the results as a string."
150 (org-babel-script-escape
151 (replace-regexp-in-string
152 "\\[|" "[" (replace-regexp-in-string
153 "|\\]" "]" (replace-regexp-in-string
154 "; " "," results
)))))
160 ;;; ob-ocaml.el ends here