1 ;;; ob-forth.el --- Babel Functions for Forth -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2014-2018 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research, forth
7 ;; Homepage: https://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 <https://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" ())
38 (declare-function org-trim
"org" (s &optional keep-lead
))
40 (defvar org-babel-default-header-args
:forth
'((:session .
"yes"))
41 "Default header arguments for forth code blocks.")
43 (defun org-babel-execute:forth
(body params
)
44 "Execute a block of Forth code with org-babel.
45 This function is called by `org-babel-execute-src-block'"
46 (if (string= "none" (cdr (assq :session params
)))
47 (error "Non-session evaluation not supported for Forth code blocks")
48 (let ((all-results (org-babel-forth-session-execute body params
)))
49 (if (member "output" (cdr (assq :result-params params
)))
50 (mapconcat #'identity all-results
"\n")
51 (car (last all-results
))))))
53 (defun org-babel-forth-session-execute (body params
)
55 (let ((proc (forth-proc))
56 (rx " \\(\n:\\|compiled\n\\\|ok\n\\)")
58 (with-current-buffer (process-buffer (forth-proc))
59 (mapcar (lambda (line)
60 (setq result-start
(progn (goto-char (process-mark proc
))
62 (comint-send-string proc
(concat line
"\n"))
63 ;; wait for forth to say "ok"
64 (while (not (progn (goto-char result-start
)
65 (re-search-forward rx nil t
)))
66 (accept-process-output proc
0.01))
67 (let ((case (match-string 1)))
69 ((string= "ok\n" case
)
70 ;; Collect intermediate output.
71 (buffer-substring (+ result-start
1 (length line
))
73 ((string= "compiled\n" case
))
74 ;; Ignore partial compilation.
77 (org-babel-eval-error-notify 1
79 (+ (match-beginning 0) 1) (point-max))) nil
))))
80 (split-string (org-trim
81 (org-babel-expand-body:generic body params
))
87 ;;; ob-forth.el ends here