diff --git a/rust/src/parser.rs b/rust/src/parser.rs index d5334df..94c89a0 100644 --- a/rust/src/parser.rs +++ b/rust/src/parser.rs @@ -72,6 +72,36 @@ peg::parser! { } } +fn preprocess(input: &str) -> String { + let mut stack = vec![0]; + let mut output = String::new(); + for line in input.lines() { + let mut count = 0; + for c in line.chars() { + if c == ' ' || c == '\t' { + count += 1; + } else if c == '#' { + break; + } else { + let curr = stack.last().unwrap(); + if curr < &count { + stack.push(count); + output.push_str(">>>"); + } else if curr > &count { + while stack.last().unwrap() > &count { + output.push_str("<<<"); + stack.pop(); + } + } + break; + } + } + output.push_str(line.trim()); + output.push('\n'); + } + output +} + #[cfg(test)] mod test { @@ -206,7 +236,26 @@ foo <- x -> y -> x * y"; ) ) ]; - assert_eq!(deelang_parser::program(prgm).unwrap(), expected); } + + #[test] + fn test_preprocess() { + let prgm = r" + . + . + . + . +. + ## Hello World"; + let expected = r" +>>>. +>>>. +<<<. +>>>. +<<<<<<. +## Hello World +"; + assert_eq!(preprocess(prgm), expected); + } }