From 5153df63c1675f8c6190f76854ff5cee24b4efd4 Mon Sep 17 00:00:00 2001 From: ada Date: Thu, 22 Oct 2020 21:18:22 +0200 Subject: [PATCH] exploring read function --- lisp/lisp.rs | 118 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 72 insertions(+), 46 deletions(-) diff --git a/lisp/lisp.rs b/lisp/lisp.rs index b8df722a1..7786e8276 100644 --- a/lisp/lisp.rs +++ b/lisp/lisp.rs @@ -63,36 +63,42 @@ struct RispEnv<'a> { struct InPort { line: String, - lines: Vec, - cur_line: usize + lines: Vec, + cur_line: usize, } impl InPort { - fn init(filename: String) -> InPort { let file = File::open(filename).unwrap(); - let reader = io::BufReader::new(file).lines().map(|s| s.ok().unwrap().to_string()).collect(); - InPort { - line : String::new(), + let reader = io::BufReader::new(file) + .lines() + .map(|s| s.ok().unwrap().to_string()) + .collect(); + InPort { + line: String::new(), lines: reader, - cur_line: 0 + cur_line: 0, } } pub fn next_token(&mut self) -> String { loop { - if self.line.is_empty() { - self.line = self.lines.get(self.cur_line).unwrap_or(&"#".to_string()).to_string(); - self.cur_line = self.cur_line + 1; - } - if self.line.is_empty() { - return "#".to_string(); - } - let (token, rest) = InPort::tokenize(self.line.clone()); - self.line = rest; - if !token.is_empty() { - return token; - } + if self.line.is_empty() { + self.line = self + .lines + .get(self.cur_line) + .unwrap_or(&"#".to_string()) + .to_string(); + self.cur_line = self.cur_line + 1; + } + if self.line.is_empty() { + return "#".to_string(); + } + let (token, rest) = InPort::tokenize(self.line.clone()); + self.line = rest; + if !token.is_empty() { + return token; + } } } @@ -103,7 +109,7 @@ impl InPort { let captures = result .expect("Error running regex") .expect("No match found"); -// println!("{}", captures.get(0).unwrap().as_str().to_string()); + // println!("{}", captures.get(0).unwrap().as_str().to_string()); ( captures.get(1).unwrap().as_str().to_string(), captures.get(2).unwrap().as_str().to_string(), @@ -111,27 +117,48 @@ impl InPort { } } -// TODO change return type to Symbol -fn read(inport: InPort) -> String { - fn read_ahead(token: String) { - match token { - "(" => { - let L = Vec::new(); - loop { - let token = inport.next_token(); - if token == ")" { - return L; +fn read_ahead(inport: &InPort, token: String) -> String { + match token.as_str() { + "(" => { + let mut L = Vec::new(); + loop { + let t = &inport.next_token(); + if t.to_string() == ")" { + return L.into_iter().collect(); } else { - L.append(read_ahead(token)); + L.push(read_ahead(&inport, t.to_string())); } } - }, - _ => { println!("{}", token); } + } + _ => { + println!("{}", token); + return token; } } - let token1 = inport.next_token(); - if token1 == "#".to_string() { +} + +fn read_seq<'a>(tokens: &'a [String]) -> Result<(RispExp, &'a [String]), RispErr> { + let mut res: Vec = vec![]; + let mut xs = tokens; + loop { + let (next_token, rest) = xs + .split_first() + .ok_or(RispErr::Reason("could not find closing `)`".to_string()))?; + if next_token == ")" { + return Ok((RispExp::List(res), rest)); // skip `)`, head to the token after + } + let (exp, new_xs) = parse(&xs)?; + res.push(exp); + xs = new_xs; + } +} +// TODO change return type to Symbol +fn read(inport: &InPort) -> String { + let token1 = &inport.next_token().as_str(); + if token1.to_string() == "#".to_string() { return "#".to_string(); + } else { + return read_ahead(&inport, token1.to_string()); } } @@ -402,22 +429,21 @@ fn eval(exp: &RispExp, env: &mut RispEnv) -> Result { } } - fn main() { let env = &mut default_env(); let mut reader = InPort::init("new.lisp".to_string()); let mut token = String::new(); - read(reader); + read(&reader); // read from file -// let file = File::open("new.lisp").unwrap(); -// let lines = io::BufReader::new(file).lines(); -// let mut cur_line = String::new(); -// let mut token = String::new(); -// for line in lines { -// let (token, cur_line) = tokenize(line.ok().unwrap().as_str().to_string()); -// println!("token {:?}", token); -// println!("line {:?}", cur_line); - // } + // let file = File::open("new.lisp").unwrap(); + // let lines = io::BufReader::new(file).lines(); + // let mut cur_line = String::new(); + // let mut token = String::new(); + // for line in lines { + // let (token, cur_line) = tokenize(line.ok().unwrap().as_str().to_string()); + // println!("token {:?}", token); + // println!("line {:?}", cur_line); + // } /* loop {