Supported 2op var instructions.
[ez.git] / ez.el
diff --git a/ez.el b/ez.el
index 549b8f0..181a37a 100644 (file)
--- a/ez.el
+++ b/ez.el
 (defun ez-decode-signed-word (w)
   (ez-decode-signed-bits w 16))
 
+(defun ez-decode-signed-operand (operand operand-type)
+  (if (eq operand-type 'b)
+      (ez-decode-signed-byte operand)
+    (ez-decode-signed-word operand)))
+
 (defun binformat (n &optional s)
   (unless s
     (setq s ""))
 ;; Global variables
 
 (defun ez-get-global-var (gvar)
+  (if (> gvar 239)
+      (error "Invalid global variable %d" gvar))
   (ez-mem-ref-word (+ (* 2 gvar) ez-globalvartab-addr)))
 
 (defun ez-set-global-var (gvar val)
 (defun ez-get-zstring-chars (base-addr)
   (let ((addr base-addr)
         (chars nil)
-        (not-done t))
+        (not-done t)
+        (word-count 0))
     (while not-done
       (let ((components (ez-parse-zstring-word (ez-mem-ref-word addr))))
         (setq chars (append chars (cdr components)))
         (setq addr (+ addr 2))
+        (setq word-count (+ word-count 1))
         (when (= (car components) 1)
           (setq not-done nil))))
-    chars))
+    (cons word-count chars)))
 
 (defun ez-get-zstring (base-addr)
-  (let ((chars (ez-get-zstring-chars base-addr))
-        (cur 0)
-        (lock 0)
-        (abbrev-char nil)
-        (s ""))
+  (let* ((word-count-and-chars (ez-get-zstring-chars base-addr))
+         (word-count (car word-count-and-chars))
+         (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 (ez-get-zstring abbrev-addr))))
+          (setq s (concat s (cdr (ez-get-zstring abbrev-addr)))))
         (setq abbrev-char nil))
        ((memq char '(1 2 3)) ;Abbreviation
         (setq abbrev-char char))
         (setq s (concat s (substring (elt ez-zstring-alphabets cur)
                                      char (+ char 1))))
         (setq cur lock))))
-    s))
+    (cons word-count s)))
 
 
 ;; Call stack
 (defun ez-execute-instr ()
   (let ((instr-pc (ez-get-pc))
         (opbyte (ez-read-pc-byte-and-inc))
-        (optype)
-        (opcode nil)
-        (operands))
+        (optype) (opcode) (operands))
     (cond
      ((<= #x0 opbyte #x1f)
       (setq optype '2op
             operands '()
             operand-types '()))
      ((<= #xC0 opbyte #xDF)
-      (error "Unsupported op %x" opbyte))
+      (setq optype '2op
+            opcode (- opbyte #xc0))
+      (let ((operands-and-types (ez-read-var-operands-and-inc)))
+        (setq operands (car operands-and-types)
+              operand-types (cdr operands-and-types))))
      ((<= #xE0 opbyte #xFF)
       (setq optype 'var
             opcode (- opbyte #xe0))
   '(((0op #x00) rtrue ez-op-rtrue)
     ((0op #x01) rfalse ez-op-rfalse)
     ((1op #x00) jz ez-op-jz)
+    ((1op #x05) inc ez-op-inc)
+    ((1op #x06) dec ez-op-dec)
     ((1op #x0B) ret ez-op-ret)
     ((1op #x0C) jump ez-op-jump)
+    ((2op #x05) inc_jg ez-op-inc-jg)
+    ((2op #x04) dec_jg ez-op-dec-jg)
     ((2op #x0D) store ez-op-store)
     ((1op #x0E) load ez-op-load)
     ((var #x01) storew ez-op-storew)
     ((1op #x02) get_child ez-op-get-child)
     ((1op #x03) get_parent ez-op-get-parent)
     ((2op #x0A) test_attr ez-op-test-attr)
-    ((var #x03) put_prop ez-op-put-prop)))
+    ((var #x03) put_prop ez-op-put-prop)
+    ((0op #x02) print ez-op-print)
+    ((0op #x0B) new_line ez-op-new-line)
+    ((var #x06) print_num ez-op-print-num)))
 
 (defun ez-op-ret (operands &optional operand-types)
   (let ((retval (car operands)))
   (ez-do-branch (memq (car operands) (cdr operands)))
   t)
 
+(defun ez-op-jg (operands operand-types)
+  (let ((s1 (ez-decode-signed-operand (car operands) (car operand-types)))
+        (s2 (ez-decode-signed-operand (cadr operands) (cadr operand-types))))
+    (ez-do-branch (> s1 s2)))
+  t)
+
+(defun ez-op-jl (operands operand-types)
+  (let ((s1 (ez-decode-signed-operand (car operands) (car operand-types)))
+        (s2 (ez-decode-signed-operand (cadr operands) (cadr operand-types))))
+    (ez-do-branch (< s1 s2)))
+  t)
+
+(defun ez-op-inc-jg (operands operand-types)
+  (let ((var (car operands)))
+    (ez-op-inc (list var))
+    (ez-op-jg (cons (ez-get-var var) (cdr operands)) (cons 'w (cdr operand-types))))
+  t)
+
+(defun ez-op-dec-jl (operands operand-types)
+  (let ((var (car operands)))
+    (ez-op-dec (list var))
+    (ez-op-jl (cons (ez-get-var var) (cdr operands)) (cons 'w (cdr operand-types))))
+  t)
+
+
 (defun ez-op-jump (operands operand-types)
   (let ((offset (if (eq (car operand-types) 'b)
                     (ez-decode-signed-byte (car operands))
     (ez-set-pc (+ (ez-get-pc) offset -2)))
   t)
 
+(defun ez-op-inc (operands &optional operand-types)
+  (let ((var (car operands)))
+    (ez-set-var var (mod (+ 1 (ez-get-var var)) #x10000)))
+  t)
+
+(defun ez-op-dec (operands &optional operand-types)
+  (let ((var (car operands)))
+    (ez-set-var var (mod (+ (ez-get-var var) 1) #x10000)))
+  t)
+
 (defun ez-op-store (operands operand-types)
   (let ((var (car operands))
         (a (cadr operands)))
     (ez-set-obj-prop obj prop a)
     t))
 
-;; Main
+(defun ez-op-print (operands operand-types)
+  (let* ((word-count-and-string (ez-get-zstring (ez-get-pc)))
+         (word-count (car word-count-and-string))
+         (string (cdr word-count-and-string)))
+    (ez-print string)
+    (ez-increment-pc (* 2 word-count)))
+  t)
+
+(defun ez-op-new-line (operands operand-types)
+  (ez-print "\n")
+  t)
+
+(defun ez-op-print-num (operands operand-types)
+  (let ((s (ez-decode-signed-operand (car operands) (car operand-types))))
+    (ez-print (number-to-string s)))
+  t)
+
+
+;; Execution loop
 
 (defun ez-run (filename)
   (ez-load-file filename)
 
   (while (ez-execute-instr)))
 
-;; Scratch
-
-;; (ez-load-file "zork1.z3")
-;; (ez-parse-header)
-;; (setq ez-call-stack (list (ez-make-call-stack-frame ez-start-pc)))
-;; (while (ez-execute-instr))
 
-(ez-run "zork1.z3")
+;;; Buffer and I/O
+;;
+
+(defun ez-render-prompt ()
+  (with-current-buffer "*ez*"
+    (let ((update-point (= ez-input-marker (point)))
+          (update-window-points (mapcar (lambda (w)
+                                          (list (= (window-point w) ez-input-marker)
+                                                w))
+                                        (get-buffer-window-list nil nil t))))
+      (save-excursion
+        (set-marker-insertion-type ez-prompt-marker nil)
+        (set-marker-insertion-type ez-input-marker t)
+        (let ((inhibit-read-only t))
+          (delete-region ez-prompt-marker ez-input-marker)
+          (goto-char ez-prompt-marker)
+          (insert
+           ">"
+           (propertize " " ; Need this to be separate to mark it as rear-nonsticky
+                       'read-only t
+                       'rear-nonsticky t)))
+        (set-marker-insertion-type ez-input-marker nil))
+      (goto-char ez-input-marker))))
+
+(defvar ez-prompt-marker nil
+  "Marker for prompt position in buffer.")
+
+(defvar ez-input-marker nil
+  "Marker for prompt position in buffer.")
+
+(defun ez-setup-buffer ()
+  (with-current-buffer (get-buffer-create "*ez*")
+    (let ((inhibit-read-only t))
+      (delete-region (point-min) (point-max)))
+    (setq-local scroll-conservatively 1)
+    (if (markerp ez-prompt-marker)
+        (set-marker ez-prompt-marker (point-max))
+      (setq ez-prompt-marker (point-max-marker)))
+    (if (markerp ez-input-marker)
+        (set-marker ez-input-marker (point-max))
+      (setq ez-input-marker (point-max-marker)))
+    (goto-char (point-max))
+    (ez-render-prompt)))
+
+
+(defun ez-print (string)
+  (with-current-buffer "*ez*"
+    (save-excursion
+      (goto-char ez-prompt-marker)
+      (insert-before-markers string))))
+
+;; Mode
+
+(defvar ez-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map (kbd "RET") 'ez-enter)
+    map))
+
+(define-derived-mode ez-mode text-mode "ez"
+  "Major mode for EZ.")
+
+(when (fboundp 'evil-set-initial-state)
+  (evil-set-initial-state 'ez-mode 'insert))
+
+(defun ez (zfile)
+  (interactive "fEnter name of z3 story file: ")
+  (if (get-buffer "*ez*")
+      (switch-to-buffer "*ez*")
+    (switch-to-buffer "*ez*")
+    (ez-mode)
+    (ez-setup-buffer)
+    (ez-run zfile))
+  "Started EZ.")
+
+(defun ez-debug ()
+  (interactive)
+  (switch-to-buffer "*ez*")
+  (ez-mode)
+  (ez-setup-buffer)
+  (ez-run "zork1.z3"))
 
 ;;; ez.el ends here