From 7069a70d70d7fdcc496eae6dea39e674e3761b87 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Sun, 6 Feb 2022 22:25:52 -0600 Subject: [PATCH] Kinda repl --- rust/src/main.rs | 24 +++++++++++++++++++++--- rust/src/parser.rs | 7 +++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/rust/src/main.rs b/rust/src/main.rs index 47891a9..c43f07d 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,14 +1,32 @@ mod parser; +use std::io; use std::io::prelude::*; use std::env; use std::fs::File; -fn main() { - let args: Vec = env::args().collect(); - let mut file = File::open(&args[1]).expect("Could not read file"); +fn repl() { + loop { + let mut line = String::new(); + io::stdin().read_line(&mut line).unwrap(); + let tree = parser::parse_stmt(&line); + println!("{:?}", tree); + } +} + +fn script(filename: &str) { + let mut file = File::open(filename).expect("Could not read file"); let mut prgm = String::new(); file.read_to_string(&mut prgm).unwrap(); let tree = parser::parse(&prgm); println!("{:?}", tree); } + +fn main() { + let args: Vec = env::args().collect(); + if args.len() == 1 { + repl(); + } else { + script(&args[1]); + } +} diff --git a/rust/src/parser.rs b/rust/src/parser.rs index b547a05..79c5857 100644 --- a/rust/src/parser.rs +++ b/rust/src/parser.rs @@ -111,6 +111,13 @@ pub fn parse<'a>(prgm: &'a str) -> Vec> { deelang_parser::program(prgm).unwrap() } +pub fn parse_stmt<'a>(stmt: &'a str) -> Stmt<'a> { + let stmt = preprocess(stmt); + let stmt = Box::new(stmt); + let stmt = Box::leak(stmt); + deelang_parser::stmt(stmt).unwrap() +} + #[cfg(test)] mod test {