diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-12-18 14:18:35 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-12-18 14:18:35 +0100 |
commit | 5a2c0a0e9a908eda2ce4dfe03287f2a0e1a4bc11 (patch) | |
tree | 71190230828be6f96193bca1aa28f0e55165eba5 /2019 | |
parent | 57035d465f81d2e1a57292d92276f863ff88af9a (diff) |
Accept initial arguments
Diffstat (limited to '2019')
-rw-r--r-- | 2019/interpreter.lisp | 16 |
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)) |