1 ;;; ob-forth.el --- org-babel functions for Forth
3 ;; Copyright (C) 2014-2016 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research, forth
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 ;; Requires the gforth forth compiler and `forth-mode' (see below).
27 ;; https://www.gnu.org/software/gforth/
31 ;; Session evaluation requires the gforth forth compiler as well as
32 ;; `forth-mode' which is distributed with gforth (in gforth.el).
37 (declare-function forth-proc
"ext:gforth" ())
39 (defvar org-babel-default-header-args
:forth
'((:session .
"yes"))
40 "Default header arguments for forth code blocks.")
42 (defun org-babel-execute:forth
(body params
)
43 "Execute a block of Forth code with org-babel.
44 This function is called by `org-babel-execute-src-block'"
45 (if (string= "none" (cdr (assoc :session params
)))
46 (error "Non-session evaluation not supported for Forth code blocks")
47 (let ((all-results (org-babel-forth-session-execute body params
)))
48 (if (member "output" (cdr (assoc :result-params params
)))
49 (mapconcat #'identity all-results
"\n")
50 (car (last all-results
))))))
52 (defun org-babel-forth-session-execute (body params
)
54 (let ((proc (forth-proc))
55 (rx " \\(\n:\\|compiled\n\\\|ok\n\\)")
57 (with-current-buffer (process-buffer (forth-proc))
58 (mapcar (lambda (line)
59 (setq result-start
(progn (goto-char (process-mark proc
))
61 (comint-send-string proc
(concat line
"\n"))
62 ;; wait for forth to say "ok"
63 (while (not (progn (goto-char result-start
)
64 (re-search-forward rx nil t
)))
65 (accept-process-output proc
0.01))
66 (let ((case (match-string 1)))
68 ((string= "ok\n" case
)
69 ;; Collect intermediate output.
70 (buffer-substring (+ result-start
1 (length line
))
72 ((string= "compiled\n" case
))
73 ;; Ignore partial compilation.
76 (org-babel-eval-error-notify 1
78 (+ (match-beginning 0) 1) (point-max))) nil
))))
79 (split-string (org-babel-trim
80 (org-babel-expand-body:generic
86 ;;; ob-forth.el ends here