diff --git a/lisp/ob-ocaml.el b/lisp/ob-ocaml.el index 8cdffc9..43be08c 100644 --- a/lisp/ob-ocaml.el +++ b/lisp/ob-ocaml.el @@ -50,12 +50,44 @@ (defvar org-babel-ocaml-eoe-indicator "\"org-babel-ocaml-eoe\";;") (defvar org-babel-ocaml-eoe-output "org-babel-ocaml-eoe") +(defun org-babel-ocaml-quote-item (itm) + (cond ((stringp itm) + (format "\"%s\"" itm)) + (t + (format "%s" itm)))) + +(defun org-babel-ocaml-list-from-elisp (lst) + "Construct OCaml list from elisp list LST. + +As LST is expected to be an org-mode table, the result will actually +be a list of n-tuples, such as [(1, 2, 3); (2, 3, 4)]." + (concat "[" + (mapconcat + #'(lambda (itm) + (cond ((listp itm) + (concat "(" + (mapconcat #'org-babel-ocaml-quote-item itm ", ") + ")")) + ((stringp itm) + (format "\"%s\"" itm)) + (t + (format "%s" itm)))) + lst "; ") + "]")) + +(defun org-babel-ocaml-assign-elisp (name value colnames-p rownames-p) + "Construct OCaml code assigning the elisp VALUE to a variable named NAME." + (if (listp value) + (format "let %s = %s in\n" + name (org-babel-ocaml-list-from-elisp value)) + (format "let %s = %s in\n" name value))) + (defun org-babel-expand-body:ocaml (body params &optional processed-params) "Expand BODY according to PARAMS, return the expanded body." (let ((vars (nth 1 (or processed-params (org-babel-process-params params))))) (concat (mapconcat - (lambda (pair) (format "let %s = %s;" (car pair) (cdr pair))) + (lambda (pair) (org-babel-ocaml-assign-elisp (car pair) (cdr pair) nil nil)) vars "\n") "\n" body "\n"))) (defun org-babel-execute:ocaml (body params)