ob-forth.el 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ;;; ob-forth.el --- Babel Functions for Forth -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2014-2019 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, forth
  5. ;; Homepage: 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 'ob)
  25. (require 'org-macs)
  26. (declare-function forth-proc "ext:gforth" ())
  27. (defvar org-babel-default-header-args:forth '((:session . "yes"))
  28. "Default header arguments for forth code blocks.")
  29. (defun org-babel-execute:forth (body params)
  30. "Execute a block of Forth code with org-babel.
  31. This function is called by `org-babel-execute-src-block'"
  32. (if (string= "none" (cdr (assq :session params)))
  33. (error "Non-session evaluation not supported for Forth code blocks")
  34. (let ((all-results (org-babel-forth-session-execute body params)))
  35. (if (member "output" (cdr (assq :result-params params)))
  36. (mapconcat #'identity all-results "\n")
  37. (car (last all-results))))))
  38. (defun org-babel-forth-session-execute (body params)
  39. (require 'forth-mode)
  40. (let ((proc (forth-proc))
  41. (rx " \\(\n:\\|compiled\n\\\|ok\n\\)")
  42. (result-start))
  43. (with-current-buffer (process-buffer (forth-proc))
  44. (mapcar (lambda (line)
  45. (setq result-start (progn (goto-char (process-mark proc))
  46. (point)))
  47. (comint-send-string proc (concat line "\n"))
  48. ;; wait for forth to say "ok"
  49. (while (not (progn (goto-char result-start)
  50. (re-search-forward rx nil t)))
  51. (accept-process-output proc 0.01))
  52. (let ((case (match-string 1)))
  53. (cond
  54. ((string= "ok\n" case)
  55. ;; Collect intermediate output.
  56. (buffer-substring (+ result-start 1 (length line))
  57. (match-beginning 0)))
  58. ((string= "compiled\n" case))
  59. ;; Ignore partial compilation.
  60. ((string= "\n:" case)
  61. ;; Report errors.
  62. (org-babel-eval-error-notify 1
  63. (buffer-substring
  64. (+ (match-beginning 0) 1) (point-max))) nil))))
  65. (split-string (org-trim
  66. (org-babel-expand-body:generic body params))
  67. "\n"
  68. 'omit-nulls)))))
  69. (provide 'ob-forth)
  70. ;;; ob-forth.el ends here