From: plugd Date: Sat, 11 Nov 2023 13:15:57 +0000 (+0100) Subject: Dictionary lookup working. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=ez.git Dictionary lookup working. --- diff --git a/ez.el b/ez.el index a9d63e3..a6f1f9c 100644 --- a/ez.el +++ b/ez.el @@ -93,6 +93,10 @@ (defun ez-mem-ref-byte (addr) (aref ez-memory addr)) +(defun ez-mem-ref-bytes (addr count) + (mapcar (lambda (offset) (ez-mem-ref-byte (+ addr offset))) + (number-sequence 0 (- count 1)))) + (defun ez-mem-set-byte (addr val) (aset ez-memory addr val)) @@ -101,8 +105,12 @@ (ez-mem-set-byte (+ addr i) (elt vals i)))) (defun ez-mem-ref-word (addr) - (+ (* 256 (aref ez-memory addr)) - (aref ez-memory (+ addr 1)))) + (logior (lsh (aref ez-memory addr) 8) + (aref ez-memory (+ addr 1)))) + +(defun ez-mem-ref-words (addr count) + (mapcar (lambda (offset) (ez-mem-ref-word (+ addr offset))) + (number-sequence 0 (- (* 2 count) 1) 2))) (defun ez-mem-set-word (addr val) (let ((byte-high (/ val 256)) @@ -287,6 +295,12 @@ (logand (lsh word -5) #b11111) (logand word #b11111))) +(defun ez-make-zstring-word (zchar-triple) + (let ((c1 (elt zchar-triple 0)) + (c2 (elt zchar-triple 1)) + (c3 (elz zchar-triple 2))) + (logand ))) + (defun ez-get-zstring-chars-and-length (base-addr) (let ((addr base-addr) (chars nil) @@ -335,7 +349,8 @@ (defun ez-encode (string) "Encodes a string as a 2-word 5-padded Z-string. -Used for matching input with dictionary entries when tokenizing." +Used for matching input with dictionary entries when tokenizing. +Returns a length-2 list of words for comparison with a dictionary entry." (let ((chars (mapcan (lambda (c) @@ -349,9 +364,24 @@ Used for matching input with dictionary entries when tokenizing." (t (list 5 6 (lsh c -5) (logand c #b11111))))) string))) - (if (< (length chars) 6) - (append chars (make-list (- 6 (length chars)) 5)) - (take 6 chars)))) + (let* ((zstring-chars + (if (< (length chars) 6) + (append chars (make-list (- 6 (length chars)) 5)) + (take 6 chars))) + (c1 (elt zstring-chars 0)) + (c2 (elt zstring-chars 1)) + (c3 (elt zstring-chars 2)) + (c4 (elt zstring-chars 3)) + (c5 (elt zstring-chars 4)) + (c6 (elt zstring-chars 5))) + (list + (logior (lsh c1 10) + (lsh c2 5) + c3) + (logior (lsh 1 15) + (lsh c4 10) + (lsh c5 5) + c6))))) ;; Dictionary @@ -381,16 +411,16 @@ Used for matching input with dictionary entries when tokenizing." (> nseps 0))) (defun ez-lookup-dictionary (text) - (let ((encoded-text (ez-encode text)) - (nseps (ez-mem-ref-byte ez-dict-base)) - (bytes-per-entry (ez-mem-ref-byte (+ ez-dict-base 1 nseps))) - (nentries (ez-mem-ref-word (+ ez-dict-base 2 nseps))) - (entries-seen 0) - (this-entry (+ ez-dict-base nseps 4))) + (let* ((encoded-text (ez-encode text)) + (nseps (ez-mem-ref-byte ez-dict-base)) + (bytes-per-entry (ez-mem-ref-byte (+ ez-dict-base 1 nseps))) + (nentries (ez-mem-ref-word (+ ez-dict-base 2 nseps))) + (entries-seen 0) + (this-entry (+ ez-dict-base nseps 4))) (while (and (< entries-seen nentries) (not (equal - (ez-mem-ref-bytes this-entry 4) + (ez-mem-ref-words this-entry 2) encoded-text))) (setq entries-seen (+ entries-seen 1)) (setq this-entry (+ this-entry bytes-per-entry))) @@ -936,7 +966,8 @@ Used for matching input with dictionary entries when tokenizing." (ez-mem-set-byte (+ baddr1 1 i) char))) (ez-mem-set-byte (+ baddr1 1 (length input-string)) 0) - (ez--tokenize baddr1 baddr2))) + (ez--tokenize baddr1 baddr2)) + 'run) (defun ez--tokenize (tb-baddr pb-baddr) @@ -948,7 +979,7 @@ Used for matching input with dictionary entries when tokenizing." (while unfinished (let ((next-char (ez-mem-ref-byte (+ tb-baddr 1 token-end)))) (cond - ((eq char ?\s) + ((eq next-char ?\s) ;; Add token (setq token-end (- token-end 1)) (let* ((text (ez-mem-ref-string (+ tb-baddr 1 token-start) @@ -976,7 +1007,6 @@ Used for matching input with dictionary entries when tokenizing." (defun ez-load-and-run (filename) (ez-load-file filename) (ez-parse-header) - (ez-parse-dictionary) (setq ez-call-stack (list (ez-make-call-stack-frame ez-start-pc))) (ez-run))