File size: 1,862 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use loda_rust_core::parser::{InstructionId, ParseInstructionId};
use std::fmt;

#[derive(Hash, Eq, PartialEq, Debug, Clone, Copy)]
pub enum Word {
    ProgramStart,
    ProgramStop,
    Instruction(InstructionId)
}

impl Word {
    pub fn parse(raw: &str) -> Option<Word> {
        match raw {
            "START" => {
                return Some(Word::ProgramStart);
            },
            "STOP" => {
                return Some(Word::ProgramStop);
            },
            _ => {}
        }
        match InstructionId::parse(raw, 1) {
            Ok(instruction_id) => {
                return Some(Word::Instruction(instruction_id));
            },
            Err(_) => {
                return None;
            }
        }
    }
}

impl fmt::Display for Word {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::ProgramStart => write!(f, "START"),
            Self::ProgramStop => write!(f, "STOP"),
            Self::Instruction(instruction) => write!(f, "{}", instruction)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse(input: &str) -> String {
        match Word::parse(input) {
            Some(word) => word.to_string(),
            None => "NONE".to_string()
        }
    }

    #[test]
    fn test_10000_parse_success() {
        assert_eq!(parse("START"), "START");
        assert_eq!(parse("STOP"), "STOP");
        assert_eq!(parse("mov"), "mov");
        assert_eq!(parse("add"), "add");
    }

    #[test]
    fn test_10001_parse_unrecognized() {
        assert_eq!(parse(""), "NONE");
        assert_eq!(parse("unrecognized"), "NONE");
        assert_eq!(parse("junk"), "NONE");
        assert_eq!(parse("start"), "NONE");
        assert_eq!(parse("stop"), "NONE");
        assert_eq!(parse("MOV"), "NONE");
        assert_eq!(parse("ADD"), "NONE");
    }
}