p101_enc 0.11.0

Library to convert Olivetti P101 program to and from different encodings
Documentation
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use p101_is::*;
use crate::encoding::*;

#[derive(Default)]
/// AsciiEncoding is an alternative to StandardEncoding.
/// It does not use original P101 symbols to express
/// instructions in ASCII.
pub struct AsciiEncoding;

impl AsciiEncoding {
    const OP_ADD: &'static str = "add";
    const OP_COPY_M: &'static str = "copy_m";
    const OP_COPY_TO_A: &'static str = "copy_to_a";
    const OP_DIV: &'static str = "div";
    const OP_MUL: &'static str = "mul";
    const OP_PRINT: &'static str = "print";
    const OP_RESET: &'static str = "reset";
    const OP_SUB: &'static str = "sub";
    const OP_SWAP: &'static str = "swap";
    const OP_CAI: &'static str = "cai";

    fn add(&self, o: &Register) -> String {
        match o {
            Register::M => Self::OP_ADD.into(),
            _ => format!("{} {}", o, Self::OP_ADD)
        }        
    }
    
    fn copy_m(&self, o: &Register) -> String {
        match o {
            Register::M => Self::OP_COPY_M.into(),
            _ => format!("{} {}", o, Self::OP_COPY_M)
        }        
    }
    
    fn copy_to_a(&self, o: &Register) -> String {
        match o {
            Register::M => Self::OP_COPY_TO_A.into(),
            _ => format!("{} {}", o, Self::OP_COPY_TO_A)
        }        
    }

    fn div(&self, o: &Register) -> String {
        match o {
            Register::M => Self::OP_DIV.into(),
            _ => format!("{} {}", o, Self::OP_DIV)
        }
    }
    
    fn mul(&self, o: &Register) -> String {
        match o {
            Register::M => Self::OP_MUL.into(),
            _ => format!("{} {}", o, Self::OP_MUL)
        }        
    }
    
    fn print(&self, o: &Register) -> String {
        match o {
            Register::M => Self::OP_PRINT.into(),
            _ => format!("{} {}", o, Self::OP_PRINT)
        }        
    }

    fn reset(&self, o: &Register) -> String {
        match o {
            Register::M => Self::OP_RESET.into(),
            _ => format!("{} {}", o, Self::OP_RESET)
        }
    }
   
    fn sub(&self, o: &Register) -> String {
        match o {
            Register::M => Self::OP_SUB.into(),
            _ => format!("{} {}", o, Self::OP_SUB)
        }
    }

    fn swap(&self, o: &Register) -> String {
        match o {
            Register::M => Self::OP_SWAP.into(),
            _ => format!("{} {}", o, Self::OP_SWAP)
        }
    }

    fn cai(&self, sign: &CaiSign, order: &CaiOrder, digit: &CaiDigit, comma: &CaiComma) -> std::string::String {
        format!("{}{}{}{} {}", order, sign, digit, comma, Self::OP_CAI)
    }
}

impl Encoding for AsciiEncoding {
    fn encode(&self, i: &Instruction) -> String {
        match i {
            Instruction::Abs => "A abs".into(),
            Instruction::Add(o) => self.add(o),
            Instruction::CaiStart => "cai".into(),
            Instruction::Cai(sign, order, digit, comma) => format!("{}{}{}{} cai", order, sign, digit, comma),
            Instruction::ConditionalJump(o) => o.to_string(),
            Instruction::CopyDecimal => "decimals".into(),
            Instruction::CopyM(o) => self.copy_m(o),
            Instruction::CopyToA(o) => self.copy_to_a(o),
            Instruction::Div(o) => self.div(o),
            Instruction::DrExchange => "dr_exchange".into(),
            Instruction::Jump(o) => o.to_string(),
            Instruction::Label(d) => d.to_string(),
            Instruction::Mul(o) => self.mul(o),
            Instruction::NewLine => "newline".into(),
            Instruction::Print(o) => self.print(o),
            Instruction::Reset(o) => self.reset(o),
            Instruction::Sqr(o) => format!("{} sqr", o),
            Instruction::Stop => "stop".into(),
            Instruction::Sub(o) => self.sub(o),
            Instruction::SwapA(o) => self.swap(o),
        }
    }

    fn decode_instr(&self, _text: &str) -> DecodeResult {
        unimplemented!()

        // Instructions always 1 character long
        // S stop

        // Instructions always 2 character long
        // A↕ abs
        // /↕ copy decimals
        // /â‹„ Newline
        // RS DRexchange

        // Instructions that can range from 1 to 3 charaters long
        // [reg[/]](+|-|x|÷|√)
        // [reg[/]]↑ copy m
        // [reg[/]]↓ copy to a
        // [reg[/]]↕ swap
        // [reg[/]]* reset

        // Instructions that can range be 1 or 2 charaters long
        // (.*)V|W|Y|Z unconditional jump is 1 or 2 characters long ending with 
        // [.*]V|W|Y|Z conditional jump is always 2 characters long
        // [.*]V|W|Y|Z jump destinantion is always 2 characters long
        
        // CAI expressions needs a separate parsing function
        // because uses the same expressions as some instructions.
        // The only recognized instruction is CAI start A/↑
    }

    fn decode_cai(&self, _text: &str) -> DecodeResult {
        unimplemented!()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::encoder::*;
    
    fn build_encoder() -> Encoder<AsciiEncoding, NullAnnotator> {
        Encoder::<AsciiEncoding, NullAnnotator>::new()
    }

    #[test]
    pub fn ascii_encoding_of_stop_is_s() {
        let program = vec!(Instruction::Stop);
        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(1, lines.len());
        assert_eq!("stop", lines[0]);
    }

    #[test]
    pub fn ascii_encoding_of_reset_is_reg_and_asterisk() {
        let program = vec!(Instruction::Reset(Register::A),
            Instruction::Reset(Register::B),
            Instruction::Reset(Register::b),
            Instruction::Reset(Register::M));

        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(4, lines.len());
        assert_eq!(String::from("A reset"), lines[0]);
        assert_eq!(String::from("B reset"), lines[1]);
        assert_eq!(String::from("B/ reset"), lines[2]);
        assert_eq!(String::from("reset"), lines[3]);
    }

    #[test]
    pub fn ascii_encoding_of_print_is_reg_and_diamond() {
        let program = vec!(Instruction::Print(Register::A),
            Instruction::Print(Register::B),
            Instruction::Print(Register::b),
            Instruction::Print(Register::M));

        let encoder = build_encoder();

        let text = encoder.encode(&program);

        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(4, lines.len());

        assert_eq!(String::from("A print"), lines[0]);
        assert_eq!(String::from("B print"), lines[1]);
        assert_eq!(String::from("B/ print"), lines[2]);
        assert_eq!(String::from("print"), lines[3]);
    }

    #[test]
    pub fn ascii_encoding_of_newline_is_slash_diamond() {
        let program = vec!(Instruction::NewLine);
        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(1, lines.len());
        assert_eq!(String::from("newline"), lines[0]);
    }

    #[test]
    pub fn ascii_encoding_of_copy_to_a_is_reg_and_down_arrow() {
        let program = vec!(Instruction::CopyToA(Register::M),
            Instruction::CopyToA(Register::B),
            Instruction::CopyToA(Register::b));

        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(3, lines.len());
        assert_eq!(String::from("copy_to_a"), lines[0]);
        assert_eq!(String::from("B copy_to_a"), lines[1]);
        assert_eq!(String::from("B/ copy_to_a"), lines[2]);
    }

    #[test]
    pub fn ascii_encoding_of_copy_m_is_reg_and_up_arrow() {
        let program = vec!(Instruction::CopyM(Register::M),
            Instruction::CopyM(Register::B),
            Instruction::CopyM(Register::b));

        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(3, lines.len());
        assert_eq!(String::from("copy_m"), lines[0]);
        assert_eq!(String::from("B copy_m"), lines[1]);
        assert_eq!(String::from("B/ copy_m"), lines[2]);
    }

    #[test]
    pub fn ascii_encoding_of_exchange_is_reg_and_up_down_arrow() {
        let program = vec!(Instruction::SwapA(Register::M),
            Instruction::SwapA(Register::B),
            Instruction::SwapA(Register::b));

        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(3, lines.len());
        assert_eq!("swap", lines[0]);
        assert_eq!("B swap", lines[1]);
        assert_eq!("B/ swap", lines[2]);
    }

    #[test]
    pub fn ascii_encoding_of_d_r_exchange_is_rs() {
        let program = vec!(Instruction::DrExchange);
        let encoder = build_encoder();

        let text = encoder.encode(&program);

        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(1, lines.len());
        assert_eq!("dr_exchange", lines[0]);
    }

    #[test]
    pub fn ascii_encoding_of_decimal_part_to_is_slash_up_down_arrow() {
        let program = vec!(Instruction::CopyDecimal);

        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(1, lines.len());
        assert_eq!("decimals", lines[0]);
    }

    #[test]
    pub fn ascii_encoding_of_add_is_reg_plus() {
        let program = vec!(Instruction::Add(Register::M),
            Instruction::Add(Register::B),
            Instruction::Add(Register::b));

        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(3, lines.len());
        assert_eq!("add", lines[0]);
        assert_eq!("B add", lines[1]);
        assert_eq!("B/ add", lines[2]);
    }

    #[test]
    pub fn ascii_encoding_of_sub_is_reg_minus() {
        let program = vec!(Instruction::Sub(Register::M),
            Instruction::Sub(Register::B),
            Instruction::Sub(Register::b));

        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(3, lines.len());
        assert_eq!("sub", lines[0]);
        assert_eq!("B sub", lines[1]);
        assert_eq!("B/ sub", lines[2]);
    }

    #[test]
    pub fn ascii_encoding_of_mul_is_reg_x() {
        let program = vec!(Instruction::Mul(Register::M),
            Instruction::Mul(Register::B),
            Instruction::Mul(Register::b));

        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(3, lines.len());
        assert_eq!("mul", lines[0]);
        assert_eq!("B mul", lines[1]);
        assert_eq!("B/ mul", lines[2]);
    }

    #[test]
    pub fn ascii_encoding_of_div_is_reg_divided_by() {
        let program = vec!(Instruction::Div(Register::M),
            Instruction::Div(Register::B),
            Instruction::Div(Register::b));

        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(3, lines.len());
        assert_eq!("div", lines[0]);
        assert_eq!("B div", lines[1]);
        assert_eq!("B/ div", lines[2]);
    }

    #[test]
    pub fn ascii_encoding_of_sqr() {
        // TODO
    }

    #[test]
    pub fn ascii_encoding_of_abs_is_accumulator_up_down_arrows() {
        let program = vec!(Instruction::Abs);
        let encoder = build_encoder();

        let text = encoder.encode(&program);
        
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(String::from("A abs"), lines[0]);
    }

    #[test]
    pub fn ascii_encoding_of_jump_is_the_point_of_origin() {
        let program = vec!(Instruction::Jump(Origin::V),
            Instruction::Jump(Origin::RZ),
            Instruction::Jump(Origin::CW));

        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(3, lines.len());
        assert_eq!("V", lines[0]);
        assert_eq!(String::from("RZ"), lines[1]);
        assert_eq!(String::from("CW"), lines[2]);
    }

    #[test]
    pub fn ascii_encoding_of_conditional_jump_the_point_of_origin() {
        let program = vec!(Instruction::ConditionalJump(ConditionalOrigin::_V),
            Instruction::ConditionalJump(ConditionalOrigin::rV),
            Instruction::ConditionalJump(ConditionalOrigin::dY));

        let encoder = build_encoder();

        let text = encoder.encode(&program);
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(3, lines.len());
        assert_eq!("/V", lines[0]);
        assert_eq!(String::from("rV"), lines[1]);
        assert_eq!(String::from("dY"), lines[2]);
    }

    #[test]
    pub fn ascii_encoding_of_cai() {
        // TODO
        return;
    }
}