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
|
pub enum CommandType {
Label,
Mov,
Add,
}
pub enum CommandArgsType {
Nil,
Label,
NotLabel,
}
pub struct CommandArgs {
arg_type: CommandArgsType,
args_vec: Vec<String>,
}
pub struct Row {
pub command: CommandType,
pub args: CommandArgs,
}
impl Row {
pub fn new() -> Self {
let command = CommandArgs {
arg_type: CommandArgsType::Nil,
args_vec: Vec::<String>::new(),
};
Row {
command: CommandType::Label,
args: command,
}
}
}
#[test]
fn check_row() {
let r: Row = Row {
command: CommandType::Mov,
args: CommandArgs {
arg_type: CommandArgsType::NotLabel,
args_vec: vec!["r0".to_string(), "r1".to_string()],
},
};
assert_eq!(r.args.args_vec[0], "r0");
}
fn remove_spaces(line: String) -> String {
let mut it = line.chars().peekable();
let mut splitted_string = "".to_string();
while let Some(&c) = it.peek() {
match c {
' ' | '\t' => {
it.next();
}
ch => {
splitted_string.push(ch);
it.next();
if let Some(&t) = it.peek() {
match t {
' ' | '\t' => {
splitted_string.push(' ');
}
ch => {
splitted_string.push(ch);
}
}
}
it.next();
}
}
}
splitted_string
}
pub fn parse_line(line: String) -> Result<Row, String> {
let mut row = Row::new();
let mut splitted_string = remove_spaces(line);
splitted_string = splitted_string.to_lowercase();
let mut words : Vec<&str> = splitted_string.split(" ").collect();
if words.len() > 1 {
if words[1].ends_with(",") {
words[1] = words[1].trim_end_matches(',');
}
}
println!("{}, {:?}", words.len(), words);
Ok(row)
}
|