fighting with lifetimes

This commit is contained in:
Scott Richmond 2024-12-07 22:29:10 -05:00
parent 2a26316b50
commit 5a64c6623c
3 changed files with 22 additions and 6 deletions

View File

@ -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::<value::Value>()
// struct_scalpel::print_dissection_info::<parser::Ast>();

View File

@ -247,7 +247,7 @@ impl<'src> fmt::Display for PairPattern<'src> {
}
}
pub struct StringMatcher<'src>(Box<dyn Fn(String) -> Option<Vec<(String, String)>> + 'src>);
pub struct StringMatcher<'src>(pub Box<dyn Fn(String) -> Option<Vec<(String, String)>> + 'src>);
impl PartialEq for StringMatcher<'_> {
fn eq(&self, _other: &StringMatcher) -> bool {

View File

@ -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::<Vec<_>>();
ctx.append(&mut matches);
Some(ctx)
}
None => None,
}
}
(Pattern::Word(w), val) => {
ctx.push((w, val.clone()));
Some(ctx)