From 5a64c6623c92e1db44d87ecd399d10e230c0e2d0 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Sat, 7 Dec 2024 22:29:10 -0500 Subject: [PATCH] fighting with lifetimes --- src/main.rs | 10 +++++----- src/parser.rs | 2 +- src/vm.rs | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index abfd34a..95ba2ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,7 +58,7 @@ use crate::base::*; pub fn main() { let src = " -let \"{foo}\" = bar +let \"{foo}bar{baz}\" = \"foo var baz\" "; let (tokens, lex_errs) = lexer().parse(src).into_output_errors(); if !lex_errs.is_empty() { @@ -73,13 +73,13 @@ let \"{foo}\" = bar let (ast, _) = parser() .parse(Stream::from_iter(to_parse).map((0..src.len()).into(), |(t, s)| (t, s))) .unwrap(); - println!("{}", ast); + // println!("{}", ast); - // let mut ctx = base(); + let mut ctx = base(); - // let result = eval(&ast, &mut ctx).unwrap(); + let result = eval(&ast, &mut ctx).unwrap(); - // println!("{}", result); + println!("{}", result); // struct_scalpel::print_dissection_info::() // struct_scalpel::print_dissection_info::(); diff --git a/src/parser.rs b/src/parser.rs index 2bd09bf..7e8d0ee 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -247,7 +247,7 @@ impl<'src> fmt::Display for PairPattern<'src> { } } -pub struct StringMatcher<'src>(Box Option> + 'src>); +pub struct StringMatcher<'src>(pub Box Option> + 'src>); impl PartialEq for StringMatcher<'_> { fn eq(&self, _other: &StringMatcher) -> bool { diff --git a/src/vm.rs b/src/vm.rs index e53fc13..d668938 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -64,6 +64,22 @@ pub fn match_pattern<'src, 'a>( (Pattern::Boolean(x), Value::Boolean(y)) => match_eq(x, y, ctx), (Pattern::Keyword(x), Value::Keyword(y)) => match_eq(x, y, ctx), (Pattern::String(x), Value::InternedString(y)) => match_eq(x, y, ctx), + (Pattern::String(x), Value::AllocatedString(y)) => match_eq(&x.to_string(), &**y, ctx), + (Pattern::Interpolated(_, StringMatcher(matcher)), Value::InternedString(y)) => { + match matcher(y.to_string()) { + Some(matches) => { + let mut matches = matches + .iter() + .map(|(word, string)| { + (word, Value::AllocatedString(Rc::new(string.clone()))) + }) + .collect::>(); + ctx.append(&mut matches); + Some(ctx) + } + None => None, + } + } (Pattern::Word(w), val) => { ctx.push((w, val.clone())); Some(ctx)