Z-string encoding.
[ez.git] / ez.el
diff --git a/ez.el b/ez.el
index f96005a..2dec2d7 100644 (file)
--- a/ez.el
+++ b/ez.el
@@ -1,6 +1,6 @@
 ;;; ez.el --- Emacs Z-machine
 
-;; Copyright (C) 2021 Tim Vaughan
+;; Copyright (C) 2021,2022,2023 Tim Vaughan
 
 ;; Author: Tim Vaughan <timv@ughan.xyz>
 ;; Created: 13 Oct 2021
 (defvar ez-abbrevtab-addr nil)
 (defvar ez-globalvartab-addr nil)
 
+(defvar ez-dict-entries nil
+  "Parsed dictionary")
+
+(defvar ez-dict-separators nil
+  "Parsed dictionary")
+
 (defun ez-mem-ref-byte (addr)
   (aref ez-memory addr))
 
       (error "Invalid global variable %d" gvar))
   (let ((val
          (ez-mem-ref-word (+ (* 2 gvar) ez-globalvartab-addr))))
-    (message "\tRetrieved %x from global variable %x" val gvar)
+    (ez-debug-message "\tRetrieved %x from global variable %x" val gvar)
     val))
 
 (defun ez-set-global-var (gvar val)
   (ez-mem-set-word (+ (* 2 gvar) ez-globalvartab-addr) val)
-  (message "\tSet global variabl %x to %x" gvar val))
+  (ez-debug-message "\tSet global variabl %x to %x" gvar val))
 
 ;; Object tree
 
          (chars (cdr word-count-and-chars))
          (cur 0)
          (lock 0)
-         (abbrev-char nil)
          (s ""))
-    (dolist (char chars)
-      (cond
-       (abbrev-char
-        (let ((abbrev-addr
-               (* 2 (ez-mem-ref-word (+ ez-abbrevtab-addr
-                                        (* 2 (+ (* (- abbrev-char 1) 32) char)))))))
-          (setq s (concat s (cdr (ez-get-zstring-and-length abbrev-addr)))))
-        (setq abbrev-char nil))
-       ((memq char '(1 2 3)) ;Abbreviation
-        (setq abbrev-char char))
-       ((= char 4)
-        (setq cur (mod (+ cur 1) 3)))
-       ((= char 5)
-        (setq cur (mod (+ 3 (- cur 1)) 3)))
-       (t 
-        (setq s (concat s (substring (elt ez-zstring-alphabets cur)
-                                     char (+ char 1))))
-        (setq cur lock))))
+    (while (> (length chars) 0)
+      (let ((char (pop chars)))
+        (cond
+         ((memq char '(1 2 3)) ;Abbreviation
+          (let* ((abbrev-char char)
+                 (abbrev-addr
+                  (* 2 (ez-mem-ref-word (+ ez-abbrevtab-addr
+                                           (* 2 (+ (* (- abbrev-char 1) 32) (pop chars))))))))
+            (setq s (concat s (cdr (ez-get-zstring-and-length abbrev-addr))))))
+         ((= char 4)
+          (setq cur (mod (+ cur 1) 3)))
+         ((= char 5)
+          (setq cur (mod (+ 3 (- cur 1)) 3)))
+         ((and (= cur 2) (= char 6))
+          (setq s (concat s (char-to-string (+ (lsh (pop chars) 5) (pop chars)))))
+          (setq cur lock))
+         (t 
+          (setq s (concat s (substring (elt ez-zstring-alphabets cur)
+                                       char (+ char 1))))
+          (setq cur lock)))))
     (cons word-count s)))
 
 (defun ez-get-zstring (base-addr)
   (cdr (ez-get-zstring-and-length base-addr)))
 
+(defun ez-encode (string)
+  "Encodes a string as a 2-word 5-padded Z-string.
+Used for matching input with dictionary entries when tokenizing."
+  (let ((chars
+         (mapcan
+          (lambda (c)
+            (cond
+             ((seq-contains-p (elt ez-zstring-alphabets 0) c)
+              (list (seq-position (elt ez-zstring-alphabets 0) c)))
+             ((seq-contains-p (elt ez-zstring-alphabets 1) c)
+              (list 4 (seq-position (elt ez-zstring-alphabets 1) c)))
+             ((seq-contains-p (elt ez-zstring-alphabets 2) c)
+              (list 5 (seq-position (elt ez-zstring-alphabets 2) c)))
+             (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))))
+
+;; Dictionary
+
+(defun ez-parse-dictionary ()
+  (let* ((nseps (ez-mem-ref-byte ez-dict-base))
+         (separators
+          (mapcar (lambda (i) (ez-mem-ref-byte (+ ez-dict-base i)))
+                  (number-sequence 1 nseps)))
+         (bytes-per-entry (ez-mem-ref-byte (+ ez-dict-base 1 nseps)))
+         (nentries (ez-mem-ref-word (+ ez-dict-base 2 nseps)))
+         (entries-base (+ ez-dict-base nseps 4))
+         (entries nil))
+    (dotimes (i nentries)
+      (let ((this-base (+ entries-base (* 7 i))))
+        (setq entries (cons (cons (ez-get-zstring this-base)
+                                  this-base)
+                            entries))))
+    (setq ez-dict-entries (reverse entries))
+    (setq ez-dict-separators separators)))
+
 ;; Call stack
 
 (defvar ez-call-stack nil)
 
 (defun ez-routine-stack-push (val)
   (let ((frame (car ez-call-stack)))
-    (message "\tPushed %x to stack" val)
+    (ez-debug-message "\tPushed %x to stack" val)
     (setf (elt frame 1) (cons val (elt frame 1)))))
 
 (defun ez-routine-stack-pop ()
   (let* ((frame (car ez-call-stack))
          (rs-head (car (elt frame 1))))
     (setf (elt frame 1) (cdr (elt frame 1)))
-    (message "\tPopped %x from stack" rs-head)
+    (ez-debug-message "\tPopped %x from stack" rs-head)
     rs-head))
 
 (defun ez-get-local-var (lvar)
   (let* ((frame (car ez-call-stack))
          (val (aref (elt frame 2) (- lvar 1))))
-    (message "\tRetrieved value %x from local variable %x" val lvar)
+    (ez-debug-message "\tRetrieved value %x from local variable %x" val lvar)
     val))
 
 (defun ez-set-local-var (lvar val)
   (let ((frame (car ez-call-stack)))
-    (message "\tSet local variable %x to %x" lvar val)
+    (ez-debug-message "\tSet local variable %x to %x" lvar val)
     (aset (elt frame 2) (- lvar 1) val)))
 
 (defun ez-get-pc ()
         (error "Unsupported op PC:%x Optype:%s Opcode:%x Operands:%s Operand-types:%s"
                instr-pc optype opcode (ez-list-to-string-hex operands) operand-types))
       (let ((mnemonic (elt table-row 1)))
-        (message "PC:%x Optype:%s Opcode:%x Mnemonic:%s Operands:%s Operand-types:%s"
+        (ez-debug-message "PC:%x Optype:%s Opcode:%x Mnemonic:%s Operands:%s Operand-types:%s"
                  instr-pc optype opcode mnemonic
                  (ez-list-to-string-hex operands) operand-types))
       (funcall (elt table-row 2) operands operand-types))))
 
 (defun ez-op-ret (operands &optional operand-types)
   (let ((retval (car operands)))
-    (message "\tReturning value %x" retval)
+    (ez-debug-message "\tReturning value %x" retval)
     (ez-pop-call-stack-frame)
     (ez-set-var (ez-read-pc-byte-and-inc) retval))
   'run)
 
 (defun ez-op-ret-pulled (operands operand-types)
   (let ((retval (ez-routine-stack-pop)))
-    (message "\tReturning value %x" retval)
+    (ez-debug-message "\tReturning value %x" retval)
     (ez-op-ret (list retval)))
   'run)
 
   'wait-for-input)
 
 (defun ez-op-read2 (input-string)
-  (ez-tokenize-string input-string
-                      (car ez--next-read-args)
-                      (cadr ez--next-read-args))
-  'run)
+  (let* ((baddr1 (car ez--next-read-args))
+         (baddr2 (cadr ez--next-read-args))
+         (dict (ez-get-dictionary))
+         (separators (car dict))
+         (wordlist (cdr dict))
+         (token-start 0))
+
+    (dotimes (i (length input-string))
+      (let ((char (elt input-string i)))
+        (ez-mem-set-byte (+ baddr1 1 i) char)
+    (ez-mem-set-byte (+ baddr1 1 (length input-string)) 0)
+
+    (ez--tokenize baddr1 baddr2)))))
+
+(defun ez--tokenize (taddr baddr)
+
+  (let ((unfinished t)
+        (token-start 0)
+        (token-end 0)
+        (token-string ""))
+    (while unfinished
+      (let ((char (ez-mem-ref-byte (+ taddr 1 token-end))))
+        (cond
+         ((eq char ?\s))
+         ((memq char ez-dict-separators))
+         )
+      ))))
 
 ;; Execution loop
 
 (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))
 
 (defun ez-setup-buffer ()
   (with-current-buffer (get-buffer-create "*ez*")
+    (ez-mode)
     (let ((inhibit-read-only t))
-      (delete-region (point-min) (point-max)))
+      (erase-buffer))
     (setq-local scroll-conservatively 1)
     (if (markerp ez-input-marker)
         (set-marker ez-input-marker (point-max))
   (interactive)
   (if (not (eq ez-machine-state 'wait-for-input))
       (error "Z-machine not ready for input."))
-  (let ((input-string (with-current-buffer "*ez*"
-                        (buffer-substring ez-input-marker (point-max)))))
+  (let ((input-string
+         (downcase
+          (with-current-buffer "*ez*"
+            (buffer-substring ez-input-marker (point-max))))))
+    (delete-region ez-input-marker (point-max))
+    (ez-print (concat input-string "\n"))
+    (ez-debug-message "\tReceived string \"%s\"" input-string)
     (ez-op-read2 input-string)
     (ez-run)))
 
+;; Debugging info
+
+(defun ez-setup-debug-buffer ()
+  (with-current-buffer (get-buffer-create "*ez-debug-trace*")
+    (setq-local buffer-read-only t)
+    (let ((inhibit-read-only t))
+      (erase-buffer)
+      (insert "--- Trace Start ---\n\n"))))
+
+(defun ez-debug-message (&rest strings)
+  (with-current-buffer (get-buffer-create "*ez-debug-trace*")
+    (save-excursion
+      (goto-char (point-max))
+      (let ((inhibit-read-only t))
+        (insert (apply #'format-message strings) "\n")))))
+
+(defun ez-debug-memory ()
+  (interactive)
+  (with-current-buffer (get-buffer-create "*ez-debug-memory*")
+    (setq-local buffer-read-only t)
+    (let ((inhibit-read-only t)
+          (old-point (point)))
+      (if (eq major-mode 'hexl-mode)
+          (hexl-mode-exit))
+      (erase-buffer)
+      (insert ez-memory)
+      (setq-local buffer-undo-list nil)
+      (hexl-mode)
+      (goto-char old-point))))
+
 ;; Mode
 
 (defvar ez-mode-map
   (if (get-buffer "*ez*")
       (switch-to-buffer "*ez*")
     (switch-to-buffer "*ez*")
-    (ez-mode)
     (ez-setup-buffer)
     (ez-load-and-run zfile))
   "Started EZ.")
 
 (defun ez-debug ()
   (interactive)
-  (switch-to-buffer "*ez*")
-  (ez-mode)
   (ez-setup-buffer)
+  (ez-setup-debug-buffer)
   (ez-load-and-run "zork1.z3"))
 
 ;;; ez.el ends here