sysrpl-mode.el 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. ;;; -*- mode: emacs-lisp; lexical-binding: t -*-
  2. ;;; sysrpl-mode.el -- Major mode for the SysRPL programming language
  3. ;; Copyright (C) 2014 Paul Onions
  4. ;; Author: Paul Onions <paul.onions@acm.org>
  5. ;; Keywords: RPL, SysRPL, HP48, HP49, HP50, calculator
  6. ;; This file is free software, see the LICENCE file in this directory
  7. ;; for copying terms.
  8. ;;; Commentary:
  9. ;; A major mode for the SysRPL language, the system programming
  10. ;; language of HP48/49/50-series calculators.
  11. ;;; Code:
  12. (require 'cl-lib)
  13. (require 'rpl-base)
  14. (require 'rpl-edb)
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16. ;; Customizations
  17. ;;
  18. (defcustom sysrpl-default-calculator :HP50G
  19. "Default calculator type for SysRPL mode."
  20. :type '(radio :HP38G :HP39G :HP48G :HP49G :HP50G)
  21. :group 'rpl)
  22. (defcustom sysrpl-compiler-program "rplcomp"
  23. "External SysRPL compiler program name."
  24. :type 'string
  25. :group 'rpl)
  26. (defcustom sysrpl-compiler-output-bufname "*rplcomp*"
  27. "Buffer name in which to capture SysRPL compiler output."
  28. :type 'string
  29. :group 'rpl)
  30. (defface sysrpl-name '((t :foreground "darkblue"))
  31. "Face used for displaying SysRPL names (e.g DROP)."
  32. :group 'rpl)
  33. (defface sysrpl-keyword '((t :foreground "purple"))
  34. "Face used for displaying SysRPL keywords (e.g. :: ;)."
  35. :group 'rpl)
  36. (defface sysrpl-comment '((t :foreground "darkgreen"))
  37. "Face used for displaying SysRPL comments."
  38. :group 'rpl)
  39. (defcustom sysrpl-font-lock-name-face 'sysrpl-name
  40. "Name of face to use for displaying SysRPL names."
  41. :type 'symbol
  42. :group 'rpl)
  43. (defcustom sysrpl-font-lock-keyword-face 'sysrpl-keyword
  44. "Name of face to use for displaying SysRPL keywords."
  45. :type 'symbol
  46. :group 'rpl)
  47. (defcustom sysrpl-font-lock-comment-face 'sysrpl-comment
  48. "Name of face to use for displaying SysRPL comments."
  49. :type 'symbol
  50. :group 'rpl)
  51. (defun sysrpl-edb-calculator (calculator)
  52. "Map SysRPL calculator identifier to EDB identifier."
  53. (cond ((eql calculator :HP38G) :38G)
  54. ((eql calculator :HP39G) :39G)
  55. ((eql calculator :HP48G) :48G)
  56. ((eql calculator :HP49G) :49G)
  57. ((eql calculator :HP50G) :49G)))
  58. (defvar sysrpl-mode-syntax-table
  59. (let ((table (make-syntax-table prog-mode-syntax-table)))
  60. (modify-syntax-entry ?: "w" table)
  61. (modify-syntax-entry ?\; "w" table)
  62. (modify-syntax-entry ?! "w" table)
  63. (modify-syntax-entry ?@ "w" table)
  64. (modify-syntax-entry ?# "w" table)
  65. (modify-syntax-entry ?$ "w" table)
  66. (modify-syntax-entry ?% "w" table)
  67. (modify-syntax-entry ?^ "w" table)
  68. (modify-syntax-entry ?& "w" table)
  69. (modify-syntax-entry ?\? "w" table)
  70. (modify-syntax-entry ?- "w" table)
  71. (modify-syntax-entry ?_ "w" table)
  72. (modify-syntax-entry ?= "w" table)
  73. (modify-syntax-entry ?+ "w" table)
  74. (modify-syntax-entry ?* "w" table)
  75. (modify-syntax-entry ?/ "w" table)
  76. (modify-syntax-entry ?< "w" table)
  77. (modify-syntax-entry ?> "w" table)
  78. (modify-syntax-entry ?| "w" table)
  79. table)
  80. "The SysRPL syntax table.")
  81. (defvar sysrpl-rplcomp-keywords '("LAM" "ID" "TAG" "CHR" "CODE" "CODEM" "ENDCODE" "PTR"
  82. "ROMPTR" "FLASHPTR" "ZINT" "ARRY" "LNKARRY" "HXS" "GROB"
  83. "::" ";" "BEGIN" "AGAIN" "UNTIL" "WHILE" "REPEAT" "DO"
  84. "LOOP" "+LOOP" "IF" "ELSE" "THEN" "FCN" "ENDFCN" "{" "}"
  85. "ASSEMBLE" "RPL" "ASSEMBLEM" "!RPL" "ROMID" "xROMID"
  86. "NAME" "NULLNAME" "xNAME" "sNAME" "tNAME"
  87. "NAMELESS" "LABEL" "LOCALNAME" "LOCALLABEL"
  88. "EXTERNAL" "LOCAL" "FEXTERNAL" "DEFINE" "INCLUDE"
  89. "TITLE" "STITLE" "EJECT")
  90. "Keywords used by the RPLCOMP SysRPL compiler.")
  91. (defun sysrpl-font-lock-compile-keywords (names)
  92. "Construct a list of keyword matcher clauses suitable for `font-lock-keywords'."
  93. (append (list (list "^\\*.*$" (list 0 'sysrpl-font-lock-comment-face))
  94. (list "(.*)" (list 0 'sysrpl-font-lock-comment-face))
  95. (list (concat "\\<" (regexp-opt sysrpl-rplcomp-keywords) "\\>")
  96. (list 0 'sysrpl-font-lock-keyword-face)))
  97. (mapcar (lambda (str) (list (concat "\\<" (regexp-quote str) "\\>")
  98. (list 0 'sysrpl-font-lock-name-face)))
  99. names)))
  100. (defvar sysrpl-font-lock-keywords
  101. (sysrpl-font-lock-compile-keywords (rpl-edb-all-names (sysrpl-edb-calculator sysrpl-default-calculator))))
  102. (defvar sysrpl-selected-calculator sysrpl-default-calculator
  103. "Currently selected calculator model.")
  104. (defun sysrpl-select-hp38g ()
  105. "Set the currently selected calculator model to be the HP38G."
  106. (interactive)
  107. (setq sysrpl-selected-calculator :HP38G)
  108. (setq sysrpl-font-lock-keywords
  109. (sysrpl-font-lock-compile-keywords (rpl-edb-all-names (sysrpl-edb-calculator :HP38G))))
  110. (sysrpl-mode))
  111. (defun sysrpl-select-hp39g ()
  112. "Set the currently selected calculator model to be the HP39G."
  113. (interactive)
  114. (setq sysrpl-selected-calculator :HP39G)
  115. (setq sysrpl-font-lock-keywords
  116. (sysrpl-font-lock-compile-keywords (rpl-edb-all-names (sysrpl-edb-calculator :HP39G))))
  117. (sysrpl-mode))
  118. (defun sysrpl-select-hp48g ()
  119. "Set the currently selected calculator model to be the HP48G."
  120. (interactive)
  121. (setq sysrpl-selected-calculator :HP48G)
  122. (setq sysrpl-font-lock-keywords
  123. (sysrpl-font-lock-compile-keywords (rpl-edb-all-names (sysrpl-edb-calculator :HP48G))))
  124. (sysrpl-mode))
  125. (defun sysrpl-select-hp49g ()
  126. "Set the currently selected calculator model to be the HP49G."
  127. (interactive)
  128. (setq sysrpl-selected-calculator :HP49G)
  129. (setq sysrpl-font-lock-keywords
  130. (sysrpl-font-lock-compile-keywords (rpl-edb-all-names (sysrpl-edb-calculator :HP49G))))
  131. (sysrpl-mode))
  132. (defun sysrpl-select-hp50g ()
  133. "Set the currently selected calculator model to be the HP50G."
  134. (interactive)
  135. (setq sysrpl-selected-calculator :HP50G)
  136. (setq sysrpl-font-lock-keywords
  137. (sysrpl-font-lock-compile-keywords (rpl-edb-all-names (sysrpl-edb-calculator :HP50G))))
  138. (sysrpl-mode))
  139. (defun sysrpl-get-eldoc-message ()
  140. (interactive)
  141. (rpl-edb-get-stack-effect (sysrpl-edb-calculator sysrpl-selected-calculator)
  142. (thing-at-point 'word)))
  143. (defun sysrpl-apropos-thing-at-point (name)
  144. "Show information about NAME in a popup buffer.
  145. When called interactively NAME defaults to the word around
  146. point."
  147. (interactive (list (completing-read "Apropos: " (rpl-edb-all-names (sysrpl-edb-calculator sysrpl-selected-calculator))
  148. nil nil (thing-at-point 'word))))
  149. (let ((bufname (format "*SysRPL: %s*" name)))
  150. (with-current-buffer (get-buffer-create bufname)
  151. (setq buffer-read-only nil)
  152. (erase-buffer)
  153. (insert (rpl-edb-get-stack-effect (sysrpl-edb-calculator sysrpl-selected-calculator) name))
  154. (newline)
  155. (insert (rpl-edb-get-description (sysrpl-edb-calculator sysrpl-selected-calculator) name))
  156. (newline)
  157. (insert (format "Address: %s" (rpl-edb-get-address (sysrpl-edb-calculator sysrpl-selected-calculator) name)))
  158. (newline)
  159. (insert (format "Flags: %s" (rpl-edb-get-flags (sysrpl-edb-calculator sysrpl-selected-calculator) name)))
  160. (newline)
  161. (end-of-buffer)
  162. (help-mode)
  163. (set-buffer-modified-p nil)
  164. (setq buffer-read-only t))
  165. (fit-window-to-buffer (display-buffer bufname))))
  166. (defun sysrpl-compile-buffer ()
  167. "Compile the current buffer."
  168. (interactive)
  169. (let ((tmp-filename (make-temp-file "sysrpl" nil ".s"))
  170. (rtn-code 0))
  171. (write-region (point-min) (point-max) tmp-filename)
  172. (with-current-buffer (get-buffer-create sysrpl-compiler-output-bufname)
  173. (setq buffer-read-only nil)
  174. (erase-buffer)
  175. (setq rtn-code (call-process sysrpl-compiler-program tmp-filename t nil "-" "-")))
  176. (display-buffer sysrpl-compiler-output-bufname)
  177. (if (eql rtn-code 0)
  178. (message "Compilation complete")
  179. (message "*** Compiled with ERRORS ***"))))
  180. (defvar sysrpl-mode-map
  181. (let ((map (make-sparse-keymap))
  182. (menu-map (make-sparse-keymap)))
  183. (set-keymap-parent map rpl-common-keymap)
  184. ;; Menu items
  185. (define-key map [menu-bar rpl-menu] (cons "RPL" menu-map))
  186. (define-key menu-map [sysrpl-menu-separator-1]
  187. '(menu-item "--"))
  188. (define-key menu-map [sysrpl-menu-select-hp50g]
  189. '(menu-item "HP50G" sysrpl-select-hp50g
  190. :button (:radio . (eql :HP50G sysrpl-selected-calculator))))
  191. (define-key menu-map [sysrpl-menu-select-hp49g]
  192. '(menu-item "HP49G" sysrpl-select-hp49g
  193. :button (:radio . (eql :HP49G sysrpl-selected-calculator))))
  194. (define-key menu-map [sysrpl-menu-select-hp48g]
  195. '(menu-item "HP48G" sysrpl-select-hp48g
  196. :button (:radio . (eql :HP48G sysrpl-selected-calculator))))
  197. (define-key menu-map [sysrpl-menu-select-hp39g]
  198. '(menu-item "HP39G" sysrpl-select-hp39g
  199. :button (:radio . (eql :HP39G sysrpl-selected-calculator))))
  200. (define-key menu-map [sysrpl-menu-select-hp38g]
  201. '(menu-item "HP38G" sysrpl-select-hp38g
  202. :button (:radio . (eql :HP38G sysrpl-selected-calculator))))
  203. map)
  204. "The SysRPL mode local keymap.")
  205. (defvar sysrpl-mode-hook nil
  206. "Hook for customizing SysRPL mode.")
  207. (define-derived-mode sysrpl-mode prog-mode "SysRPL"
  208. "Major mode for the SysRPL language."
  209. :group 'rpl
  210. (make-local-variable 'eldoc-documentation-function)
  211. (setq eldoc-documentation-function 'sysrpl-get-eldoc-message)
  212. (setq font-lock-defaults (list 'sysrpl-font-lock-keywords))
  213. (setq rpl-menu-compile-buffer-enable t))
  214. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  215. ;; End of file
  216. ;;
  217. (provide 'sysrpl-mode)