Add the pre-processor

This commit is contained in:
Dane Johnson 2022-02-01 11:41:23 -06:00
parent acb27abb34
commit d0e56a8603

View File

@ -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);
}
}