rpl-edb.el 9.2 KB

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