Browse Source

started adding support for the ocaml language

Eric Schulte 16 years ago
parent
commit
cfe40c4ca1
4 changed files with 97 additions and 2 deletions
  1. 76 0
      lisp/langs/org-babel-ocaml.el
  2. 1 1
      lisp/org-babel-comint.el
  3. 1 0
      org-babel-worg.org
  4. 19 1
      org-babel.org

+ 76 - 0
lisp/langs/org-babel-ocaml.el

@@ -0,0 +1,76 @@
+;;; org-babel-ocaml.el --- org-babel functions for ocaml evaluation
+
+;; Copyright (C) 2009 Eric Schulte
+
+;; Author: Eric Schulte
+;; Keywords: literate programming, reproducible research
+;; Homepage: http://orgmode.org
+;; Version: 0.01
+
+;;; License:
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3, or (at your option)
+;; any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;;; Commentary:
+
+;; Org-Babel support for evaluating ocaml source code.  This one will
+;; be sort of tricky because ocaml programs must be compiled before
+;; they can be run, but ocaml code can also be run through an
+;; interactive interpreter.
+;;
+;; For now lets only allow evaluation using the ocaml interpreter.
+
+;;; Requirements:
+
+;; - tuareg-mode :: http://www-rocq.inria.fr/~acohen/tuareg/
+
+;;; Code:
+(require 'org-babel)
+(require 'tuareg)
+
+(org-babel-add-interpreter "ocaml")
+
+(add-to-list 'org-babel-tangle-langs '("ocaml" "ml"))
+
+(defvar org-babel-ocaml-eoe-indicator "\"org-babel-ocaml-eoe\";;")
+(defvar org-babel-ocaml-eoe-output "- : string = \"org-babel-ocaml-eoe\"")
+
+(defun org-babel-execute:ocaml (body params)
+  "Execute a block of Ocaml code with org-babel.  This function
+is called by `org-babel-execute-src-block' with the following
+variables pre-set using `multiple-value-bind'.
+
+  (session vars result-params result-type)"
+  (message "executing Ocaml source code block")
+  (let* ((full-body (concat
+                     (mapconcat
+                      (lambda (pair) (format "let %s = %s;" (car pair) (cdr pair)))
+                      vars "\n") "\n" body "\n"))
+         (session (get-buffer (org-babel-prep-session:ocaml session params)))
+         (raw (org-babel-comint-with-output session org-babel-ocaml-eoe-output t
+                (insert full-body)
+                (insert ";;")
+                (comint-send-input nil t)
+                (insert org-babel-ocaml-eoe-indicator)
+                (comint-send-input nil t))))
+    (org-babel-trim (car raw))))
+
+(defun org-babel-prep-session:ocaml (session params)
+  "Prepare SESSION according to the header arguments specified in PARAMS."
+  (tuareg-run-caml) tuareg-interactive-buffer-name)
+
+(provide 'org-babel-ocaml)
+;;; org-babel-ocaml.el ends here

+ 1 - 1
lisp/org-babel-comint.el

@@ -58,7 +58,7 @@ output, then return all process output.  This ensures that the
 filter is removed in case of an error or user `keyboard-quit'
 filter is removed in case of an error or user `keyboard-quit'
 during execution of body."
 during execution of body."
   (declare (indent 3))
   (declare (indent 3))
-  `(org-babel-comint-in-buffer buffer
+  `(org-babel-comint-in-buffer ,buffer
      (let ((string-buffer ""))
      (let ((string-buffer ""))
        (flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
        (flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
          ;; setup filter
          ;; setup filter

+ 1 - 0
org-babel-worg.org

@@ -68,6 +68,7 @@
        ;; (require 'org-babel-ditaa)     ;; ditaa
        ;; (require 'org-babel-ditaa)     ;; ditaa
        ;; (require 'org-babel-dot)       ;; dot
        ;; (require 'org-babel-dot)       ;; dot
        ;; (require 'org-babel-gnuplot)   ;; gnuplot, and gnuplot-mode
        ;; (require 'org-babel-gnuplot)   ;; gnuplot, and gnuplot-mode
+       ;; (require 'org-babel-ocaml)     ;; ocaml, and tuareg-mode
        ;; (require 'org-babel-python)    ;; python, and python-mode
        ;; (require 'org-babel-python)    ;; python, and python-mode
        ;; (require 'org-babel-ruby)      ;; ruby, irb, ruby-mode, and inf-ruby mode
        ;; (require 'org-babel-ruby)      ;; ruby, irb, ruby-mode, and inf-ruby mode
        ;; (require 'org-babel-sql)       ;; none
        ;; (require 'org-babel-sql)       ;; none

+ 19 - 1
org-babel.org

@@ -2618,7 +2618,7 @@ This is currently working only with emacs lisp as in the following
 example in the [[* emacs lisp source reference][emacs lisp source reference]].
 example in the [[* emacs lisp source reference][emacs lisp source reference]].
 
 
 
 
-** TODO Add languages [10/13]
+** TODO Add languages [10/14]
 I'm sure there are many more that aren't listed here.  Please add
 I'm sure there are many more that aren't listed here.  Please add
 them, and bubble any that you particularly care about up to the top.
 them, and bubble any that you particularly care about up to the top.
 
 
@@ -2626,6 +2626,24 @@ Any new language should be implemented in a org-babel-lang.el file.
 Follow the pattern set by [[file:lisp/org-babel-script.el][org-babel-script.el]], [[file:lisp/org-babel-shell.el][org-babel-shell.el]] and
 Follow the pattern set by [[file:lisp/org-babel-script.el][org-babel-script.el]], [[file:lisp/org-babel-shell.el][org-babel-shell.el]] and
 [[file:lisp/org-babel-R.el][org-babel-R.el]].
 [[file:lisp/org-babel-R.el][org-babel-R.el]].
 
 
+*** STARTED ocaml [1/3]
+
+- [X] Working for the simple case (no arguments, simple output)
+- [ ] correct handling of vector/list output
+- [ ] ability to import arguments
+
+#+begin_src ocaml
+let rec fib x =
+  match x with
+    | 0 -> 1
+    | 1 -> 1
+    | n -> fib(n - 1) + fib(n - 2) in
+  fib 12
+#+end_src
+
+#+resname:
+: - : int = 233
+
 *** TODO perl
 *** TODO perl
 This could probably be added to [[file:lisp/org-babel-script.el][org-babel-script.el]]
 This could probably be added to [[file:lisp/org-babel-script.el][org-babel-script.el]]
 *** TODO java
 *** TODO java