diff options
| author | Santo Cariotti <dcariotti24@gmail.com> | 2020-06-16 20:32:53 +0200 | 
|---|---|---|
| committer | Santo Cariotti <dcariotti24@gmail.com> | 2020-06-16 20:32:53 +0200 | 
| commit | fd61dbea369510d4e352306aa25a4c96c4575fad (patch) | |
| tree | c4c0860ae80e5062f20bc5c499ceb3dc623d562b /src | |
| parent | bd389beb9efda3c6ded2c4dbe2e25ce70b6bf615 (diff) | |
feat: remove whitespaces from line
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 5 | ||||
| -rw-r--r-- | src/parser/mod.rs | 63 | 
2 files changed, 66 insertions, 2 deletions
| diff --git a/src/main.rs b/src/main.rs index a68851f..551710a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@  pub mod parser; -pub mod processor;  fn main() { -    println!("Hello, world!"); +    let _ = parser::parse_line("loop      ".to_string()); +    let _ = parser::parse_line("     mov     r0, r1".to_string()); +    let _ = parser::parse_line("     beq     suca".to_string());  } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 4cf7e00..69fa6ca 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -20,6 +20,20 @@ pub struct Row {      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 { @@ -31,3 +45,52 @@ fn check_row() {      };      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) +} | 
