diff --git a/dev/playground/src/enumhash.rs b/dev/playground/src/enumhash.rs index e6e46811..c64f240d 100644 --- a/dev/playground/src/enumhash.rs +++ b/dev/playground/src/enumhash.rs @@ -63,15 +63,9 @@ fn get_test_hash(x: &MyEnum) -> u64 { } fn main() { -<<<<<<< Updated upstream let a1: MyEnum = MyEnum::A(Alpha { val: "Hello World".to_owned() }); let a2: MyEnum = MyEnum::A(Alpha { val: "Bye World".to_owned() }); let a3: MyEnum = MyEnum::A(Alpha { val: "Bye World".to_owned() }); -======= - let a1: MyEnum = MyEnum::A(Alpha { val: "Hello World".to_string() }); - let a2: MyEnum = MyEnum::A(Alpha { val: "Bye World".to_string() }); - let a3: MyEnum = MyEnum::A(Alpha { val: "Bye World".to_string() }); ->>>>>>> Stashed changes let b: MyEnum = MyEnum::B(Beta { nr: 8, f: 2 }); let mut m = HashMap::new(); println!("{:?} {:?}", a1.to_text(), b.to_text()); diff --git a/src/mango/io/fortest/fromstr.rs b/src/mango/io/fortest/fromstr.rs deleted file mode 100644 index 8ed0c9db..00000000 --- a/src/mango/io/fortest/fromstr.rs +++ /dev/null @@ -1,37 +0,0 @@ -use mango::io::typ::Reader; -use mango::io::typ::ReaderResult; -use mango::io::util::REXCACHE; - -/// Implementation of [Reader] that reads from a pre-provided string. -/// Mostly for testing purposes. -pub struct StringReader { - code: String, - index: usize, -} - -impl StringReader { - pub fn new(code: String) -> Self { - StringReader { code, index: 0 } - } -} - -impl Reader for StringReader { - // fn equals(&mut self, texts: Vec<&str>) -> ReaderResult { - // for text in texts { - // if &self.code[self.index..self.index + text.len()] == text { - // self.index += text.len(); - // return ReaderResult::Match(self.code[self.index..self.index + text.len()]) - // } - // } - // ReaderResult::NoMatch() - // } - - fn matches(&mut self, subpattern: &str) -> ReaderResult { - REXCACHE.with(|rl| { - let mut rexlib = rl.borrow_mut(); - let regex = rexlib.make_or_get(subpattern); - println!("{:?}", regex); - }); - ReaderResult::EOF() // TODO - } -} diff --git a/src/mango/io/fortest/mod.rs b/src/mango/io/fortest/mod.rs index 9aa88ab0..100916ac 100644 --- a/src/mango/io/fortest/mod.rs +++ b/src/mango/io/fortest/mod.rs @@ -1,2 +1,2 @@ -pub mod fromstr; -pub use self::fromstr::*; +pub mod stringreader; +pub use self::stringreader::*; diff --git a/src/mango/io/fortest/stringreader.rs b/src/mango/io/fortest/stringreader.rs new file mode 100644 index 00000000..69984d72 --- /dev/null +++ b/src/mango/io/fortest/stringreader.rs @@ -0,0 +1,49 @@ +use mango::io::typ::Reader; +use mango::io::typ::ReaderResult; +use mango::io::util::REXCACHE; + +/// Implementation of [Reader] that reads from a pre-provided string. +/// Mostly for testing purposes. +pub struct StringReader { + code: String, + index: usize, +} + +impl StringReader { + pub fn new(code: String) -> Self { + StringReader { code, index: 0 } + } +} + +impl Reader for StringReader { + fn matches(&mut self, subpattern: &str) -> ReaderResult { + // Check for subpattern + REXCACHE.with(|rl| { + let mut rexlib = rl.borrow_mut(); + { + // Check for end of file + // TODO: is there a better/faster way for this? maybe try this after a match and set a flag? + let regex = rexlib.make_or_get(r"\s*"); + match regex.find(&self.code[self.index..]) { + Some(mtch) => { + self.index += mtch.as_str().len(); + return ReaderResult::EOF(); + } + None => (), + } + } + { + // Check for subpattern + let regex = rexlib.make_or_get(subpattern); + return match regex.find(&self.code[self.index..]) { + Some(mtch) => { + self.index += mtch.as_str().len(); + println!(">>> {}", mtch.as_str()); + ReaderResult::Match(mtch.as_str().to_owned()) + } + None => ReaderResult::NoMatch(), + }; + } + }) + } +} diff --git a/src/mango/io/util.rs b/src/mango/io/util.rs index 022ec795..9d8710ef 100644 --- a/src/mango/io/util.rs +++ b/src/mango/io/util.rs @@ -1,4 +1,3 @@ -use regex::Error; use regex::Regex; use std::cell::RefCell; use std::collections::HashMap; @@ -18,12 +17,14 @@ impl RegexCache { pub fn make_or_get(&mut self, subpattern: &str) -> &Regex { if !self.cache.contains_key(subpattern) { match Regex::new(&format!("^ *{}", subpattern)) { - Err(err) => panic!(format!("Invalid regular expression while adding to library; this is a bug:\n{:?}", err)), + Err(err) => panic!(format!( + "Invalid regular expression while adding to library; this is a bug:\n{:?}", + err + )), Ok(regex) => { self.cache.insert(subpattern.to_owned(), regex); } } - } self.cache.get(subpattern).unwrap() } diff --git a/src/mango/lexing/code_lexer.rs b/src/mango/lexing/code_lexer.rs index 44293df7..112ff22e 100644 --- a/src/mango/lexing/code_lexer.rs +++ b/src/mango/lexing/code_lexer.rs @@ -84,7 +84,7 @@ impl Lexer for CodeLexer { match delegated_token { End => { // Swap back from delegation to direct mode. - let reader = delegate.get_reader().clone(); + // let reader = delegate.get_reader().clone(); self.reader_or_delegate = ReaderOrDelegate::Reader(); self.lex() } @@ -207,19 +207,17 @@ impl Lexer for CodeLexer { mod tests { use super::CodeLexer; use mango::io::fortest::StringReader; - use mango::io::typ::Reader; use mango::lexing::util::lex_all::{lex_all, LexList}; use std::cell::RefCell; use std::rc::Rc; #[test] fn test_lexing() { - assert_eq!( - LexList::from_tokens(vec![]), - lex_all(Rc::new(RefCell::new(StringReader::new( - "let x = 0\nfor x < 128\n\tx += 1\n".to_owned(), - )))) - ) + let lexed = lex_all(&mut CodeLexer::new(Rc::new(RefCell::new( + StringReader::new("let x = 0\nfor x < 128\n\tx += 1\n".to_owned()), + )))); + println!("LEXED: {:?}", lexed); + assert_eq!(LexList::from_tokens(vec![]), lexed) // assert_eq!(1, cnt, "No item in ProblemCollector"); } diff --git a/src/mango/lexing/util/lex_all.rs b/src/mango/lexing/util/lex_all.rs index 99481231..82ee0c1d 100644 --- a/src/mango/lexing/util/lex_all.rs +++ b/src/mango/lexing/util/lex_all.rs @@ -1,11 +1,6 @@ -use mango::io::typ::Reader; -use mango::lexing::code_lexer::CodeLexer; use mango::lexing::typ::Lexer; use mango::lexing::typ::MaybeToken; -use mango::token::Token; use mango::token::Tokens; -use std::cell::RefCell; -use std::rc::Rc; /// Represents all the lex tokens in a source. #[derive(PartialEq, Eq, Debug)] @@ -18,14 +13,13 @@ impl LexList { LexList { tokens } } - pub fn from_reader(reader: Rc>) -> Self { - lex_all(reader) + pub fn from_reader(lexer: &mut Lexer) -> Self { + lex_all(lexer) } } -pub fn lex_all(reader: Rc>) -> LexList { +pub fn lex_all(lexer: &mut Lexer) -> LexList { let mut list = Vec::with_capacity(512); - let mut lexer = CodeLexer::new(reader); while let MaybeToken::Token(token) = lexer.lex() { list.push(token) } diff --git a/src/mango/towasm/tests.rs b/src/mango/towasm/tests.rs index 18f08dc2..0bf72f76 100644 --- a/src/mango/towasm/tests.rs +++ b/src/mango/towasm/tests.rs @@ -1,6 +1,5 @@ use mango::towasm::arithmetic::Add; use mango::towasm::collect::datatype::Value; -use mango::towasm::collect::typ::Wasm; use mango::towasm::collect::Type; use mango::towasm::control::BranchIf; use mango::towasm::control::Label; @@ -18,6 +17,7 @@ use mango::towasm::values::Const; use mango::towasm::values::DeclareLocal; #[test] +#[allow(unused_variables)] fn test_example_1() { let param_n = Parameter::new(Name::new("n".to_owned()).unwrap(), Type::Int32); let var_n = param_n.local(); @@ -62,5 +62,5 @@ fn test_example_1() { }, )]); - println!("WAT:\n{}\n", module.as_wat()); + // println!("WAT:\n{}\n", module.as_wat()); } diff --git a/src/mango/util/strslice/slice.rs b/src/mango/util/strslice/slice.rs index 58022f26..acf32ef1 100644 --- a/src/mango/util/strslice/slice.rs +++ b/src/mango/util/strslice/slice.rs @@ -35,7 +35,6 @@ pub fn charslice>(text: S, start: isize, end: isize) -> String { ); length = end as usize - from; } - println!("from: {}, length: {}", from, length); stext.chars().skip(from).take(length).collect() }