Pre-processor puts dedents each on own line (for simplicity)

This commit is contained in:
Dane Johnson 2022-02-01 12:16:18 -06:00
parent d0e56a8603
commit 2f74e5785c
3 changed files with 26 additions and 5 deletions

View File

@ -28,4 +28,4 @@ obj <- {
field <- "hello"
method <- x -> x * 2
}
## Object creation
## Object creation

View File

@ -1,5 +1,14 @@
mod parser;
use std::io::prelude::*;
use std::env;
use std::fs::File;
fn main() {
println!("Hello, world!");
let args: Vec<String> = env::args().collect();
let mut file = File::open(&args[1]).expect("Could not read file");
let mut prgm = String::new();
file.read_to_string(&mut prgm).unwrap();
let tree = parser::parse(&prgm);
println!("{:?}", tree);
}

View File

@ -90,6 +90,7 @@ fn preprocess(input: &str) -> String {
} else if curr > &count {
while stack.last().unwrap() > &count {
output.push_str("<<<");
output.push('\n');
stack.pop();
}
}
@ -99,9 +100,17 @@ fn preprocess(input: &str) -> String {
output.push_str(line.trim());
output.push('\n');
}
println!("{}", output);
output
}
pub fn parse<'a>(prgm: &'a str) -> Vec<Stmt<'a>> {
let prgm = preprocess(prgm);
let prgm = Box::new(prgm);
let prgm = Box::leak(prgm);
deelang_parser::program(prgm).unwrap()
}
#[cfg(test)]
mod test {
@ -109,7 +118,7 @@ mod test {
#[test]
fn test_comments() {
let prgm = "## This is a comment
let prgm = r"## This is a comment
apple <- 1 ## This is too
## This comment ends the file";
let expected = vec![Stmt::Assignment("apple", Expr::Num(1.0))];
@ -251,9 +260,12 @@ foo <- x -> y -> x * y";
let expected = r"
>>>.
>>>.
<<<.
<<<
.
>>>.
<<<<<<.
<<<
<<<
.
## Hello World
";
assert_eq!(preprocess(prgm), expected);