ob-forth.el 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ;;; ob-forth.el --- Babel Functions for Forth -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, forth
  5. ;; URL: https://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Requires the gforth forth compiler and `forth-mode' (see below).
  19. ;; https://www.gnu.org/software/gforth/
  20. ;;; Requirements:
  21. ;; Session evaluation requires the gforth forth compiler as well as
  22. ;; `forth-mode' which is distributed with gforth (in gforth.el).
  23. ;;; Code:
  24. (require 'org-macs)
  25. (org-assert-version)
  26. (require 'ob)
  27. (require 'org-macs)
  28. (declare-function forth-proc "ext:gforth" ())
  29. (defvar org-babel-default-header-args:forth '((:session . "yes"))
  30. "Default header arguments for forth code blocks.")
  31. (defun org-babel-execute:forth (body params)
  32. "Execute a block of Forth code with org-babel.
  33. This function is called by `org-babel-execute-src-block'."
  34. (if (string= "none" (cdr (assq :session params)))
  35. (error "Non-session evaluation not supported for Forth code blocks")
  36. (let ((all-results (org-babel-forth-session-execute body params)))
  37. (if (member "output" (cdr (assq :result-params params)))
  38. (mapconcat #'identity all-results "\n")
  39. (car (last all-results))))))
  40. (defun org-babel-forth-session-execute (body params)
  41. (require 'forth-mode)
  42. (let ((proc (forth-proc))
  43. (rx " \\(\n:\\|compiled\n\\|ok\n\\)")
  44. (result-start))
  45. (with-current-buffer (process-buffer (forth-proc))
  46. (mapcar (lambda (line)
  47. (setq result-start (progn (goto-char (process-mark proc))
  48. (point)))
  49. (comint-send-string proc (concat line "\n"))
  50. ;; wait for forth to say "ok"
  51. (while (not (progn (goto-char result-start)
  52. (re-search-forward rx nil t)))
  53. (accept-process-output proc 0.01))
  54. (let ((case (match-string 1)))
  55. (cond
  56. ((string= "ok\n" case)
  57. ;; Collect intermediate output.
  58. (buffer-substring (+ result-start 1 (length line))
  59. (match-beginning 0)))
  60. ((string= "compiled\n" case))
  61. ;; Ignore partial compilation.
  62. ((string= "\n:" case)
  63. ;; Report errors.
  64. (org-babel-eval-error-notify 1
  65. (buffer-substring
  66. (+ (match-beginning 0) 1) (point-max)))
  67. nil))))
  68. (split-string (org-trim
  69. (org-babel-expand-body:generic body params))
  70. "\n"
  71. 'omit-nulls)))))
  72. (provide 'ob-forth)
  73. ;;; ob-forth.el ends here