1 ;;; ob-forth.el --- org-babel functions for Forth
3 ;; Copyright (C) 2014 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 (defvar org-babel-default-header-args
:forth
'((:session .
"yes"))
38 "Default header arguments for forth code blocks.")
40 (defun org-babel-execute:forth
(body params
)
41 "Execute a block of Forth code with org-babel.
42 This function is called by `org-babel-execute-src-block'"
43 (if (string= "none" (cdr (assoc :session params
)))
44 (error "Non-session evaluation not supported for Forth code blocks")
45 (let ((all-results (org-babel-forth-session-execute body params
)))
46 (if (member "output" (cdr (assoc :result-params params
)))
47 (mapconcat #'identity all-results
"\n")
48 (car (last all-results
))))))
50 (defun org-babel-forth-session-execute (body params
)
52 (let ((proc (forth-proc))
53 (rx " \\(\n:\\|compiled\n\\\|ok\n\\)")
55 (with-current-buffer (process-buffer (forth-proc))
56 (mapcar (lambda (line)
57 (setq result-start
(progn (goto-char (process-mark proc
))
59 (comint-send-string proc
(concat line
"\n"))
60 ;; wait for forth to say "ok"
61 (while (not (progn (goto-char result-start
)
62 (re-search-forward rx nil t
)))
63 (accept-process-output proc
0.01))
64 (let ((case (match-string 1)))
66 ((string= "ok\n" case
)
67 ;; Collect intermediate output.
68 (buffer-substring (+ result-start
1 (length line
))
70 ((string= "compiled\n" case
))
71 ;; Ignore partial compilation.
74 (org-babel-eval-error-notify 1
76 (+ (match-beginning 0) 1) (point-max))) nil
))))
77 (split-string (org-babel-trim
78 (org-babel-expand-body:generic
84 ;;; ob-forth.el ends here