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
193
194
195
196
197
198
use std::str::FromStr;
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub enum Port {
UP,
DOWN,
LEFT,
RIGHT,
}
use self::Port::*;
#[derive(Debug, PartialEq)]
pub struct ParsePortError;
impl FromStr for Port {
type Err = ParsePortError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"UP" => Ok(UP),
"DOWN" => Ok(DOWN),
"LEFT" => Ok(LEFT),
"RIGHT" => Ok(RIGHT),
_ => Err(ParsePortError)
}
}
}
pub fn opposite_port(port: Port) -> Port {
match port {
UP => DOWN,
DOWN => UP,
LEFT => RIGHT,
RIGHT => LEFT,
}
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum IoRegister {
DIR(Port),
ANY,
LAST,
}
use self::IoRegister::*;
#[derive(Debug, PartialEq)]
pub struct ParseIoRegisterError;
impl FromStr for IoRegister {
type Err = ParseIoRegisterError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"ANY" => Ok(ANY),
"LAST" => Ok(LAST),
_ => if let Ok(port) = str::parse::<Port>(s) {
Ok(DIR(port))
} else {
Err(ParseIoRegisterError)
}
}
}
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Register {
ACC,
NIL,
IO(IoRegister),
}
use self::Register::*;
#[derive(Debug, PartialEq)]
pub struct ParseRegisterError;
impl FromStr for Register {
type Err = ParseRegisterError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"ACC" => Ok(ACC),
"NIL" => Ok(NIL),
_ => {
if let Ok(reg) = str::parse::<IoRegister>(s) {
Ok(IO(reg))
} else {
Err(ParseRegisterError)
}
}
}
}
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Source {
VAL(isize),
REG(Register),
}
use self::Source::*;
#[derive(Debug, PartialEq)]
pub struct ParseSourceError;
impl FromStr for Source {
type Err = ParseSourceError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(val) = str::parse::<isize>(s) {
Ok(VAL(val))
} else if let Ok(register) = str::parse::<Register>(s) {
Ok(REG(register))
} else {
Err(ParseSourceError)
}
}
}
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Instruction {
Nop,
Mov(Source, Register),
Swp,
Sav,
Add(Source),
Sub(Source),
Neg,
Jmp(isize),
Jez(isize),
Jnz(isize),
Jgz(isize),
Jlz(isize),
Jro(Source),
}
pub type Program = Vec<Instruction>;
#[test]
fn test_parse_port() {
assert_eq!(str::parse::<Port>("UP"), Ok(UP));
assert_eq!(str::parse::<Port>("DOWN"), Ok(DOWN));
assert_eq!(str::parse::<Port>("LEFT"), Ok(LEFT));
assert_eq!(str::parse::<Port>("RIGHT"), Ok(RIGHT));
assert_eq!(str::parse::<Port>("up"), Err(ParsePortError));
assert_eq!(str::parse::<Port>("bad"), Err(ParsePortError));
}
#[test]
fn test_parse_io_register() {
assert_eq!(str::parse::<IoRegister>("UP"), Ok(DIR(UP)));
assert_eq!(str::parse::<IoRegister>("ANY"), Ok(ANY));
assert_eq!(str::parse::<IoRegister>("LAST"), Ok(LAST));
assert_eq!(str::parse::<IoRegister>("any"), Err(ParseIoRegisterError));
assert_eq!(str::parse::<IoRegister>("bad"), Err(ParseIoRegisterError));
}
#[test]
fn test_parse_register() {
assert_eq!(str::parse::<Register>("ACC"), Ok(ACC));
assert_eq!(str::parse::<Register>("NIL"), Ok(NIL));
assert_eq!(str::parse::<Register>("UP"), Ok(IO(DIR(UP))));
assert_eq!(str::parse::<Register>("acc"), Err(ParseRegisterError));
assert_eq!(str::parse::<Register>("bad"), Err(ParseRegisterError));
}
#[test]
fn test_parse_source() {
assert_eq!(str::parse::<Source>("ACC"), Ok(REG(ACC)));
assert_eq!(str::parse::<Source>("1"), Ok(VAL(1)));
assert_eq!(str::parse::<Source>("bad"), Err(ParseSourceError));
}