rpl-edb.el 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 (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-consume-keyword-line ()
  86. (let ((line (rpl-edb-get-line)))
  87. (cl-destructuring-bind (keyword param-str)
  88. (rpl-edb-parse-keyword-line line)
  89. (cond ((member keyword '(:38G :39G :48G :49G))
  90. (cl-destructuring-bind (addr fmt flags)
  91. (rpl-edb-parse-calc-param-str param-str)
  92. (append (list keyword addr fmt) flags)))
  93. ((eql keyword :AKA)
  94. (let ((names (rpl-edb-parse-aka-param-str param-str)))
  95. (cons keyword names)))
  96. (t
  97. (error "Illegal EDB keyword, %s" keyword))))))
  98. ;;; Parsing extended description lines
  99. ;;;
  100. (defun rpl-edb-consume-description-line ()
  101. "Consume an EDB extended description line.
  102. Return a string. Move point to the start of the next line."
  103. (let ((line (rpl-edb-get-line)))
  104. (substring line 80)))
  105. ;;; Parsing the entries.db buffer
  106. ;;;
  107. (defun rpl-edb-parse-buffer ()
  108. "Parse the current buffer, assumed to be the entries.db file.
  109. Return a list of EDB entries, where each entry has the format:
  110. (NAMES STACK-EFFECT DESCRIPTION CALC-INFOS)
  111. where NAMES is a list of strings representing the different names
  112. under which the entry is known, STACK-EFFECT and DESCRIPTION are
  113. lists of strings -- one for each line of text in their respective
  114. desciptions -- and CALC-INFOS is a list of entries of the form:
  115. (CALC-KEY ADDRESS NAME-FORMAT &rest FLAG-KEYS)
  116. where CALC-KEY is a keyword specifying a calculator
  117. model (:38G, :39G, :48G or :49G), ADDRESS is a string containing
  118. a hexadecimal address (5 digits for a ROM address, 6 digits for a
  119. library/flash pointer), NAME-FORMAT is a FORMAT string allowing
  120. the name of the entry to be modified for this particular
  121. calculator, and FLAG-KEYS are keyword symbols specifying certain
  122. flags for this calculator."
  123. (let ((entry-names nil)
  124. (entry-stack-effect nil)
  125. (entry-description nil)
  126. (entry-calc-infos nil)
  127. (entries nil))
  128. (beginning-of-buffer)
  129. (while (not (eobp))
  130. (cond ((eql (char-after) ?*)
  131. ;; A comment line -- ignore it
  132. (forward-line))
  133. ((eql (char-after) ?@)
  134. ;; A directive -- ignore it
  135. (forward-line))
  136. ((eql (char-after) ?\;)
  137. ;; An extended description line
  138. (setq entry-description (cons (rpl-edb-consume-description-line) entry-description)))
  139. ((eql (char-after) ?.)
  140. ;; A keyword line
  141. (cl-destructuring-bind (keyword &rest params)
  142. (rpl-edb-consume-keyword-line)
  143. (cond ((eql keyword :AKA)
  144. (dolist (name params)
  145. (push name entry-names)))
  146. (t
  147. (push (cons keyword params) entry-calc-infos)))))
  148. (t
  149. ;; An identifier/stack-effect line
  150. (when entry-names
  151. (push (list entry-names entry-stack-effect (reverse entry-description) entry-calc-infos) entries))
  152. (cl-destructuring-bind (name stack-effect)
  153. (rpl-edb-consume-ident-line)
  154. (setq entry-names (list name))
  155. (setq entry-stack-effect stack-effect)
  156. (setq entry-calc-infos nil)
  157. (setq entry-description nil)))))
  158. (when entry-names
  159. (push (list entry-names entry-stack-effect (reverse entry-description) entry-calc-infos) entries))
  160. (reverse entries)))
  161. ;;; Creating calculator data files
  162. ;;;
  163. (defun rpl-edb-generate-calculator-data (edb-entries calculator)
  164. "Generate data for CALCULATOR (a keyword identifying the model).
  165. Return a hash-table whose entries are keyed by entry name and
  166. whose values are lists of the form:
  167. (STACK-EFFECT DESCRIPTION ADDRESS &rest FLAGS)."
  168. (assert (keywordp calculator))
  169. (let ((table (make-hash-table)))
  170. (dolist (entry edb-entries)
  171. (cl-destructuring-bind (names stack-effect description calc-infos) entry
  172. (let ((calc-info (car (cl-member calculator calc-infos
  173. :test (lambda (key info) (equal key (car info)))))))
  174. (when calc-info
  175. (let* ((addr-str (cadr calc-info))
  176. (fmt-str (if (caddr calc-info) (caddr calc-info) "%s"))
  177. (flags (cdddr calc-info))
  178. (stack-str (apply 'concat (mapcar (lambda (s) (concat s "\n")) stack-effect)))
  179. (descrip-str (apply 'concat (mapcar (lambda (s) (concat s "\n")) description)))
  180. (data (cons stack-str (cons descrip-str (cons addr-str flags)))))
  181. (dolist (name names)
  182. (puthash (format fmt-str name) data table)))))))
  183. table))
  184. (defun rpl-edb-make-calculator-data-file (edb-entries calculator)
  185. ""
  186. (assert (keywordp calculator))
  187. (rpl-write-data-file (rpl-edb-generate-calculator-data edb-entries calculator)
  188. (rpl-make-sysrpl-data-filename calculator)))
  189. (defvar rpl-edb-entries nil
  190. "A place on which to push the parsed entries.")
  191. (defun rpl-edb-make-all-data-files ()
  192. ""
  193. (interactive)
  194. (setq rpl-edb-entries (rpl-edb-parse-buffer))
  195. (dolist (calculator '(:38G :39G :48G :49G)) ; TODO auto-find all calculator models
  196. (rpl-edb-make-calculator-data-file rpl-edb-entries calculator)))