aboutsummaryrefslogtreecommitdiff
path: root/2019/interpreter.lisp
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-12-18 14:18:35 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-12-18 14:18:35 +0100
commit5a2c0a0e9a908eda2ce4dfe03287f2a0e1a4bc11 (patch)
tree71190230828be6f96193bca1aa28f0e55165eba5 /2019/interpreter.lisp
parent57035d465f81d2e1a57292d92276f863ff88af9a (diff)
Accept initial arguments
Diffstat (limited to '2019/interpreter.lisp')
-rw-r--r--2019/interpreter.lisp16
1 files changed, 11 insertions, 5 deletions
diff --git a/2019/interpreter.lisp b/2019/interpreter.lisp
index a1b4838..719ff9e 100644
--- a/2019/interpreter.lisp
+++ b/2019/interpreter.lisp
@@ -8,12 +8,13 @@
(defstruct (machine (:conc-name mach-)
(:constructor make-machine
- (program stdin stdout))
+ (program stdin stdout initial-arguments))
(:type (vector t)))
(ip 0 :type integer)
(program nil :type array)
(stdin nil :type (or null stream))
- (stdout nil :type (or null stream)))
+ (stdout nil :type (or null stream))
+ (initial-arguments nil :type list))
(defstruct (instruction (:conc-name instr-)
(:constructor make-instruction
@@ -39,8 +40,8 @@
(define-condition machine-sysjump (error) ())
(define-condition machine-sysexit (error) ())
-(defun run (program &optional stdin stdout)
- (loop with mach = (make-machine program stdin stdout) do
+(defun run (program &optional stdin stdout &key initial-arguments)
+ (loop with mach = (make-machine program stdin stdout initial-arguments) do
(let* ((instr (decode-next-instr mach))
(opcode (instr-opcode instr))
(handler (aref *handlers* opcode))
@@ -106,7 +107,12 @@
(definstruction set (mach dst)
(setf (aref (mach-program mach) dst)
- (parse-integer (read-line (mach-stdin mach)))))
+ (if (mach-initial-arguments mach)
+ (destructuring-bind (head &rest tail)
+ (mach-initial-arguments mach)
+ (setf (mach-initial-arguments mach) tail)
+ head)
+ (parse-integer (read-line (mach-stdin mach))))))
(definstruction out (mach x)
(format (mach-stdout mach) "~d~%" x))