-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntcode.hs
192 lines (163 loc) · 5.87 KB
/
Intcode.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{-# Language Safe #-}
{-|
Module : Intcode
Description : Intcode interpreter
Copyright : (c) Eric Mertens, 2019
License : ISC
Maintainer : emertens@gmail.com
Intcode is a virtual machine environment defined to have some arithmetic,
conditional jumps, and simple input and output facilities.
The instruction set is designed with independently selectable address modes for
each of its input and output parameters. The architecture is designed to be
simple to implement while powerful enough to write interesting programs
efficiently. The addition of a /relative base pointer/ makes it easy to
implement function calls in the language.
This Intcode architecture is defined across multiple
<https://adventofcode.com/2019/about Advent of Code 2019> tasks:
<https://adventofcode.com/2019/day/2 2>,
<https://adventofcode.com/2019/day/5 5>,
<https://adventofcode.com/2019/day/7 7>, and
<https://adventofcode.com/2019/day/9 9>
Common use modes:
* Machine construction: 'new'
* List functions: 'intcodeToList', 'effectList'
* Individual machine step processing: 'Step', 'step'
* Input/output interpretation: 'Effect', 'run'
Submodules:
* "Intcode.Machine" exposes the implementation details of the interpreter state.
* "Intcode.Parse" provides a parser for intcode text files.
* "Intcode.Opcode" provides types and the decoder for opcodes.
-}
module Intcode
(
-- * Simple list interface
intcodeToList,
-- * Machine state
Machine, (!), new, set, memoryList,
-- * Big-step semantics
Effect(..), run,
-- * Effect operations
effectList,
-- * Small-step semantics
Step(..), step,
-- * Exceptions
IntcodeFault(..),
-- * ASCII I/O interface
runIO, hRunIO,
) where
import Control.Exception (Exception(..), throw, throwIO)
import Data.Char (chr, ord)
import System.IO (Handle, hGetChar, hPutChar, hPutStrLn, stdin, stdout)
import Intcode.Machine (Machine(..), (!), memoryList, new, set)
import Intcode.Step (Step(..), step)
------------------------------------------------------------------------
-- ASCII I/O
------------------------------------------------------------------------
-- | Run intcode program using stdio. Non-ASCII outputs are printed as
-- integers.
--
-- Note that input and output is affected by handle buffering modes.
--
-- >>> runIO (run (new [104,72,104,101,104,108,104,108,104,111,104,33,104,10,99]))
-- Hello!
--
-- >>> runIO (run (new [104,-50,104,1000,99]))
-- <<-50>>
-- <<1000>>
runIO :: Effect -> IO ()
runIO = hRunIO stdin stdout
-- | 'runIO' generalized to an arbitrary input and output handle.
hRunIO ::
Handle {- ^ input handle -} ->
Handle {- ^ output handle -} ->
Effect {- ^ effect -} ->
IO ()
hRunIO inH outH = go
where
go (Output o e)
| 0 <= o, o < 0x80 = hPutChar outH (chr (fromIntegral o)) >> go e
| otherwise = hPutStrLn outH ("<<" ++ show o ++ ">>") >> go e
go (Input f) = go . f . fromIntegral . ord =<< hGetChar inH
go Halt = return ()
go Fault = throwIO IntcodeFault
------------------------------------------------------------------------
-- High-level interface
------------------------------------------------------------------------
-- | Run a given memory image as a list transducer.
--
-- Use 'effectList' when you want to provide a specific 'Effect'.
--
-- Throws: 'IntcodeFault' when machine faults or too few inputs are provided.
--
--
-- >>> intcodeToList [3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9] <$> [[0],[10]]
-- [[0],[1]]
--
-- >>> intcodeToList [3,3,1105,-1,9,1101,0,0,12,4,12,99,1] <$> [[0],[10]]
-- [[0],[1]]
--
-- >>> :{
-- >>> intcodeToList
-- >>> [3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
-- >>> 1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
-- >>> 999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99]
-- >>> <$> [[7],[8],[9]]
-- >>> :}
-- [[999],[1000],[1001]]
intcodeToList ::
[Int] {- ^ initial memory -} ->
[Int] {- ^ inputs -} ->
[Int] {- ^ outputs -}
intcodeToList = effectList . run . new
-- | Evaluate a program's effect as a function from a list of
-- inputs to a list of outputs.
--
-- Throws: 'IntcodeFault' when machine faults or too few inputs are provided.
effectList ::
Effect {- ^ program effect -} ->
[Int] {- ^ inputs -} ->
[Int] {- ^ outputs -}
effectList effect inputs =
case effect of
Fault -> throw IntcodeFault
Halt -> []
Output o e -> o : effectList e inputs
Input f ->
case inputs of
x:xs -> effectList (f x) xs
[] -> throw IntcodeFault
------------------------------------------------------------------------
-- Big-step semantics
------------------------------------------------------------------------
-- | Possible effects from running a machine
data Effect
= Output !Int Effect -- ^ Output an integer
| Input (Int -> Effect) -- ^ Input an integer
| Halt -- ^ Halt execution
| Fault -- ^ Execution failure
deriving Show
-- | Big-step semantics of virtual machine. The implementation details
-- of 'Machine' are abstracted away and the program behavior can be
-- observed by interpreting the various 'Effect' constructors.
--
-- >>> run (new [1102,34915192,34915192,7,4,7,99,0])
-- Output 1219070632396864 Halt
--
-- >>> run (new [3,1,99])
-- Input <function>
run :: Machine -> Effect
run mach =
case step mach of
Step mach' -> run mach'
StepOut out mach' -> Output out (run mach')
StepIn f -> Input (run . f)
StepHalt -> Halt
StepFault -> Fault
------------------------------------------------------------------------
-- Exceptions
------------------------------------------------------------------------
-- | Error when a machine fails to decode an instruction.
data IntcodeFault = IntcodeFault
deriving (Eq, Ord, Show, Read)
instance Exception IntcodeFault where
displayException _ = "intcode machine fault"