rpl-edb.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. ;;; -*- mode: emacs-lisp; lexical-binding: t -*-
  2. ;;; rpl-edb.el -- utilities to parse the entries database
  3. ;; Copyright (C) 2014 Paul Onions
  4. ;; Author: Paul Onions <paul.onions@acm.org>
  5. ;; Keywords: RPL, UserRPL, SysRPL, HP48, HP49, HP50
  6. ;; This file is free software, see the LICENCE file in this directory
  7. ;; for copying terms.
  8. ;;; Commentary:
  9. ;; Functions to parse the entries.db file and create accessible
  10. ;; databases of SysRPL information.
  11. ;;; Code:
  12. (require 'cl-lib)
  13. (require 'rpl-base)
  14. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  15. ;;; Functions for parsing the EDB file
  16. (defun rpl-edb-get-line ()
  17. "Get line that point is on from the current buffer.
  18. Return a string containing the line, or nil if at end of buffer.
  19. As a side-effect set point to the start of the next line."
  20. (cond ((eobp)
  21. nil)
  22. (t
  23. (beginning-of-line)
  24. (let ((start (point)))
  25. (end-of-line)
  26. (let ((line (buffer-substring-no-properties start (point))))
  27. (forward-char)
  28. line)))))
  29. ;;; Parsing identifier lines
  30. ;;;
  31. (defun rpl-trim-stack-effect-description (lines)
  32. "Trim leading and trailing fluff from strings in LINES list."
  33. (let ((left-edge 1000))
  34. (dolist (s lines)
  35. (string-match "[[:blank:]]*" s)
  36. (when (< (match-end 0) left-edge)
  37. (setq left-edge (match-end 0))))
  38. (mapcar (lambda (s)
  39. (if (string-match "\\([[:blank:]]*\\(\\\\\\)*[[:blank:]]*$\\)" s)
  40. (substring s left-edge (max left-edge (match-beginning 1)))
  41. (substring s left-edge)))
  42. lines)))
  43. (defun rpl-edb-consume-ident-line ()
  44. "Consume an EDB identifier line.
  45. Return a list of two strings: the identifier and its stack effect
  46. description. Move point to the start of the next line."
  47. (let ((line (rpl-edb-get-line)))
  48. (cond ((string-match "^[[:graph:]]+" line)
  49. (let* ((name (match-string 0 line))
  50. (desc (list (concat (make-string (match-end 0) 32)
  51. (substring line (match-end 0))))))
  52. ;; Automatically consume continuation lines
  53. ;; (after line ends with a backslash)
  54. (while (and (> (length (car desc)) 0)
  55. (string-match ".*\\\\[[:blank:]]*$" (car desc)))
  56. (setq desc (cons (rpl-edb-get-line) desc)))
  57. (list name (rpl-trim-stack-effect-description (reverse desc)))))
  58. (t
  59. (list "" "")))))
  60. ;;; Parsing keyword lines
  61. ;;;
  62. (defun rpl-edb-parse-keyword-line (line)
  63. "Parse the given EDB keyword line.
  64. Return a list consisting of the EDB keyword as a keyword symbol
  65. and a parameter string (to be further parsed later)."
  66. (cond ((string-match "\\.[[:blank:]]+\\([[:alnum:]]+\\):" line)
  67. (let ((keyword (intern (concat ":" (match-string 1 line))))
  68. (param-str (substring line (match-end 0))))
  69. (list keyword param-str)))
  70. (t
  71. (list nil ""))))
  72. (defun rpl-edb-parse-calc-param-str (str)
  73. (cond ((string-match "[[:blank:]]*\\([[:alnum:]]+\\)[[:blank:]]*\\(\\\\\\([[:graph:]]+?\\)\\\\\\)?" str)
  74. (let ((addr (match-string 1 str))
  75. (fmt (match-string 3 str))
  76. (flags nil))
  77. (setq str (substring str (match-end 0)))
  78. (while (string-match "[[:blank:]]*\\[\\([[:graph:]]+\\)\\]" str)
  79. (setq flags (cons (intern (concat ":" (match-string 1 str))) flags))
  80. (setq str (substring str (match-end 1))))
  81. (list addr fmt (reverse flags))))
  82. (t
  83. (list "" "" nil))))
  84. (defun rpl-edb-parse-aka-param-str (str)
  85. (let ((names nil))
  86. (while (string-match "[[:blank:]]*\\([[:graph:]]+\\)" str)
  87. (setq names (cons (match-string 1 str) names))
  88. (setq str (substring str (match-end 1))))
  89. (reverse names)))
  90. (defun rpl-edb-parse-userrpl-param-str (str)
  91. (let ((names nil))
  92. (while (string-match "[[:blank:]]*\\([[:graph:]]+\\)" str)
  93. (setq names (cons (match-string 1 str) names))
  94. (setq str (substring str (match-end 1))))
  95. (reverse names)))
  96. (defun rpl-edb-consume-keyword-line ()
  97. (let ((line (rpl-edb-get-line)))
  98. (cl-destructuring-bind (keyword param-str)
  99. (rpl-edb-parse-keyword-line line)
  100. (cond ((member keyword '(:38G :39G :48G :49G))
  101. (cl-destructuring-bind (addr fmt flags)
  102. (rpl-edb-parse-calc-param-str param-str)
  103. (append (list keyword addr fmt) flags)))
  104. ((eql keyword :AKA)
  105. (let ((names (rpl-edb-parse-aka-param-str param-str)))
  106. (cons keyword names)))
  107. ((eql keyword :UserRPL)
  108. (let ((names (rpl-edb-parse-userrpl-param-str param-str)))
  109. (cons keyword names)))
  110. (t
  111. (error "Illegal EDB keyword, %s" keyword))))))
  112. ;;; Parsing extended description lines
  113. ;;;
  114. (defun rpl-edb-consume-description-line ()
  115. "Consume an EDB extended description line.
  116. Return a string. Move point to the start of the next line."
  117. (let ((line (rpl-edb-get-line)))
  118. (substring line 80)))
  119. ;;; Parsing the entries.db buffer
  120. ;;;
  121. (defvar rpl-edb-entries nil
  122. "A place on which to push the entries parsed from the EDB file.")
  123. (defun rpl-edb-parse-buffer ()
  124. "Parse the current buffer, assumed to be the entries.db file.
  125. Set `rpl-edb-entries' to the parsed results, a list of EDB
  126. entries, where each entry has the format:
  127. (NAMES STACK-EFFECT DESCRIPTION CALC-INFOS)
  128. where NAMES is a list of strings representing the different names
  129. under which the entry is known, STACK-EFFECT and DESCRIPTION are
  130. lists of strings -- one for each line of text in their respective
  131. desciptions -- and CALC-INFOS is a list of entries of the form:
  132. (CALC-KEY ADDRESS NAME-FORMAT &rest FLAG-KEYS)
  133. where CALC-KEY is a keyword specifying a calculator
  134. model (:38G, :39G, :48G or :49G), ADDRESS is a string containing
  135. a hexadecimal address (5 digits for a ROM address, 6 digits for a
  136. library/flash pointer), NAME-FORMAT is a FORMAT string allowing
  137. the name of the entry to be modified for this particular
  138. calculator, and FLAG-KEYS are keyword symbols specifying certain
  139. flags for this calculator."
  140. (interactive)
  141. (let ((entry-names nil)
  142. (entry-stack-effect nil)
  143. (entry-description nil)
  144. (entry-calc-infos nil)
  145. (entries nil))
  146. (beginning-of-buffer)
  147. (while (not (eobp))
  148. (cond ((eql (char-after) ?*)
  149. ;; A comment line -- ignore it
  150. (forward-line))
  151. ((eql (char-after) ?@)
  152. ;; A directive -- ignore it
  153. (forward-line))
  154. ((eql (char-after) ?\;)
  155. ;; An extended description line
  156. (setq entry-description (cons (rpl-edb-consume-description-line) entry-description)))
  157. ((eql (char-after) ?.)
  158. ;; A keyword line
  159. (cl-destructuring-bind (keyword &rest params)
  160. (rpl-edb-consume-keyword-line)
  161. (cond ((eql keyword :AKA)
  162. (dolist (name params)
  163. (push name entry-names)))
  164. ((eql keyword :UserRPL)
  165. (dolist (name params)
  166. (push name entry-names)))
  167. (t
  168. (push (cons keyword params) entry-calc-infos)))))
  169. (t
  170. ;; An identifier/stack-effect line
  171. (when entry-names
  172. (push (list entry-names entry-stack-effect (reverse entry-description) entry-calc-infos) entries))
  173. (cl-destructuring-bind (name stack-effect)
  174. (rpl-edb-consume-ident-line)
  175. (setq entry-names (list name))
  176. (setq entry-stack-effect stack-effect)
  177. (setq entry-calc-infos nil)
  178. (setq entry-description nil)))))
  179. (when entry-names
  180. (push (list entry-names entry-stack-effect (reverse entry-description) entry-calc-infos) entries))
  181. (setq rpl-edb-entries (reverse entries))))
  182. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  183. ;;; Functions to create calculator data files
  184. (defun rpl-edb-generate-calculator-data (calculator)
  185. "Generate data for CALCULATOR (a keyword identifying the model).
  186. Return a hash-table whose entries are keyed by entry name and
  187. whose values are lists of the form:
  188. (STACK-EFFECT DESCRIPTION ADDRESS &rest FLAGS).
  189. Assumes `rpl-edb-entries' has been set by calling
  190. `rpl-edb-parse-buffer'."
  191. (cl-assert (keywordp calculator))
  192. (let ((table (make-hash-table :test 'equal)))
  193. (dolist (entry rpl-edb-entries)
  194. (cl-destructuring-bind (names stack-effect description calc-infos) entry
  195. (let ((calc-info (car (cl-member calculator calc-infos
  196. :test (lambda (key info) (equal key (car info)))))))
  197. (when calc-info
  198. (let* ((addr-str (cadr calc-info))
  199. (fmt-str (if (caddr calc-info) (caddr calc-info) "%s"))
  200. (flags (cdddr calc-info))
  201. (stack-str (apply 'concat (mapcar (lambda (s) (concat s "\n")) stack-effect)))
  202. (descrip-str (apply 'concat (mapcar (lambda (s) (concat s "\n")) description)))
  203. (data (cons stack-str (cons descrip-str (cons addr-str flags)))))
  204. (dolist (name names)
  205. (puthash (format fmt-str name) data table)))))))
  206. table))
  207. (defun rpl-edb-make-data-filename (calculator)
  208. "Make the SysRPL data filename used for CALCULATOR.
  209. Where CALCULATOR should be a keyword symbol identifying the
  210. calculator model, e.g. :48G, :49G etc."
  211. (cl-assert (keywordp calculator))
  212. (concat "sysrpl-data." (substring (symbol-name calculator) 1) ".el"))
  213. (defun rpl-edb-make-calculator-data-file (calculator)
  214. ""
  215. (cl-assert (keywordp calculator))
  216. (rpl-write-data-file (rpl-edb-generate-calculator-data calculator)
  217. (rpl-edb-make-data-filename calculator)))
  218. (defun rpl-edb-make-all-data-files ()
  219. ""
  220. (interactive)
  221. (unless rpl-edb-entries
  222. (rpl-edb-parse-buffer))
  223. (dolist (calculator '(:38G :39G :48G :49G))
  224. (rpl-edb-make-calculator-data-file rpl-edb-entries calculator)))
  225. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  226. ;;; Functions to read and query calculator data files
  227. (defvar rpl-edb-data-38g nil
  228. "SysRPL data for the 38G calculator.")
  229. (defvar rpl-edb-data-39g nil
  230. "SysRPL data for the 39G calculator.")
  231. (defvar rpl-sysrpl-data-48g nil
  232. "SysRPL data for the 48G calculator.")
  233. (defvar rpl-sysrpl-data-49g nil
  234. "SysRPL data for the 49G calculator.")
  235. (defun rpl-edb-data (calculator)
  236. "Get SysRPL data for the specified CALCULATOR.
  237. Returns a hash table, keyed by SysRPL word name, whose values each
  238. have the form (STACK-EFFECT DESCRIPTION ADDRESS &rest FLAGS)."
  239. (cl-assert (keywordp calculator))
  240. (cond ((eql calculator :38G)
  241. (unless rpl-sysrpl-data-38g
  242. (setq rpl-sysrpl-data-38g
  243. (rpl-read-data-file (rpl-edb-make-data-filename :38G))))
  244. rpl-sysrpl-data-38g)
  245. ((eql calculator :39G)
  246. (unless rpl-sysrpl-data-39g
  247. (setq rpl-sysrpl-data-39g
  248. (rpl-read-data-file (rpl-edb-make-data-filename :39G))))
  249. rpl-sysrpl-data-39g)
  250. ((eql calculator :48G)
  251. (unless rpl-sysrpl-data-48g
  252. (setq rpl-sysrpl-data-48g
  253. (rpl-read-data-file (rpl-edb-make-data-filename :48G))))
  254. rpl-sysrpl-data-48g)
  255. ((eql calculator :49G)
  256. (unless rpl-sysrpl-data-49g
  257. (setq rpl-sysrpl-data-49g
  258. (rpl-read-data-file (rpl-edb-make-data-filename :49G))))
  259. rpl-sysrpl-data-49g)))
  260. (defun rpl-edb-all-names (calculator)
  261. (cl-assert (keywordp calculator))
  262. (let ((names nil))
  263. (maphash (lambda (key val)
  264. (setq names (cons key names)))
  265. (rpl-edb-data calculator))
  266. names))
  267. (defun rpl-edb-get-stack-effect (calculator name)
  268. (car (gethash name (rpl-edb-data calculator))))
  269. (defun rpl-edb-get-description (calculator name)
  270. (cadr (gethash name (rpl-edb-data calculator))))
  271. (defun rpl-edb-get-address (calculator name)
  272. (caddr (gethash name (rpl-edb-data calculator))))
  273. (defun rpl-edb-get-flags (calculator name)
  274. (cadddr (gethash name (rpl-edb-data calculator))))
  275. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  276. ;; End of file
  277. ;;
  278. (provide 'rpl-edb)