From 93b1cf1a5cebb22909c914a40e9685af125d7d42 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Mon, 26 May 2025 01:45:08 +0300 Subject: [PATCH] wip: dsl: getting interesting --- output/src/ops_dsl.rs | 180 ++++++++++++++++++++---------------------- 1 file changed, 86 insertions(+), 94 deletions(-) diff --git a/output/src/ops_dsl.rs b/output/src/ops_dsl.rs index d718b10..e86a634 100644 --- a/output/src/ops_dsl.rs +++ b/output/src/ops_dsl.rs @@ -2,137 +2,129 @@ use crate::*; impl Dsl for When where S: Eval + Eval { fn try_provide (state: S, source: Ast) -> Perhaps { - Ok(match source.peek() { - Some(Value::Key("when")) => Some(Self( - state.eval(source, ||"when: expected condition")?, - state.eval(source, ||"when: expected content")?, - )), - _ => None - }) + if let Ast::Exp(source) = source { + if let Some(Ast::Key(id)) = source.next() && *id == *"when" { + return Ok(Some(Self( + state.eval(source.next().unwrap(), ||"when: expected condition")?, + state.eval(source.next().unwrap(), ||"when: expected content")?, + ))) + } + } + Ok(None) } } impl Dsl for Either where S: Eval + Eval + Eval { - fn try_provide (state: S, source: Ast) -> Perhaps { - Ok(match source.peek() { - Some(Value::Key("either")) => Some(Self( - state.eval(source, ||"either: expected condition")?, - state.eval(source, ||"either: expected content 1")?, - state.eval(source, ||"either: expected content 2")? - )), - _ => None - }) + fn try_provide (state: S, mut source: Ast) -> Perhaps { + if let Ast::Exp(mut source) = source { + if let Some(Ast::Key(id)) = source.next() && *id == *"either" { + return Ok(Some(Self( + state.eval(source.next().unwrap(), ||"either: expected condition")?, + state.eval(source.next().unwrap(), ||"either: expected content 1")?, + state.eval(source.next().unwrap(), ||"either: expected content 2")?, + ))) + } + } + Ok(None) } } -impl Dsl for Bsp where S: Eval + Eval { +impl Dsl for Bsp +where S: Eval, A> + Eval, B> { fn try_provide (state: S, source: Ast) -> Perhaps { - Ok(Some(match source.peek() { - Some(Value::Key("bsp/n")) => { - let _ = source.next(); - let a: A = state.eval(source, ||"bsp/n: expected content 1")?; - let b: B = state.eval(source, ||"bsp/n: expected content 2")?; - Self::n(a, b) - }, - Some(Value::Key("bsp/s")) => { - let _ = source.next(); - let a: A = state.eval(source, ||"bsp/s: expected content 1")?; - let b: B = state.eval(source, ||"bsp/s: expected content 2")?; - Self::s(a, b) - }, - Some(Value::Key("bsp/e")) => { - let _ = source.next(); - let a: A = state.eval(source, ||"bsp/e: expected content 1")?; - let b: B = state.eval(source, ||"bsp/e: expected content 2")?; - Self::e(a, b) - }, - Some(Value::Key("bsp/w")) => { - let _ = source.next(); - let a: A = state.eval(source, ||"bsp/w: expected content 1")?; - let b: B = state.eval(source, ||"bsp/w: expected content 2")?; - Self::w(a, b) - }, - Some(Value::Key("bsp/a")) => { - let _ = source.next(); - let a: A = state.eval(source, ||"bsp/a: expected content 1")?; - let b: B = state.eval(source, ||"bsp/a: expected content 2")?; - Self::a(a, b) - }, - Some(Value::Key("bsp/b")) => { - let _ = source.next(); - let a: A = state.eval(source, ||"bsp/b: expected content 1")?; - let b: B = state.eval(source, ||"bsp/b: expected content 2")?; - Self::b(a, b) - }, - _ => return Ok(None), + Ok(Some(if let Ast::Exp(source) = source { + match source.next() { + Some(Value::Key("bsp/n")) => { + let a: A = state.eval(source.next(), ||"bsp/n: expected content 1")?; + let b: B = state.eval(source.next(), ||"bsp/n: expected content 2")?; + Self::n(a, b) + }, + Some(Value::Key("bsp/s")) => { + let a: A = state.eval(source.next(), ||"bsp/s: expected content 1")?; + let b: B = state.eval(source.next(), ||"bsp/s: expected content 2")?; + Self::s(a, b) + }, + Some(Value::Key("bsp/e")) => { + let a: A = state.eval(source.next(), ||"bsp/e: expected content 1")?; + let b: B = state.eval(source.next(), ||"bsp/e: expected content 2")?; + Self::e(a, b) + }, + Some(Value::Key("bsp/w")) => { + let a: A = state.eval(source.next(), ||"bsp/w: expected content 1")?; + let b: B = state.eval(source.next(), ||"bsp/w: expected content 2")?; + Self::w(a, b) + }, + Some(Value::Key("bsp/a")) => { + let a: A = state.eval(source.next(), ||"bsp/a: expected content 1")?; + let b: B = state.eval(source.next(), ||"bsp/a: expected content 2")?; + Self::a(a, b) + }, + Some(Value::Key("bsp/b")) => { + let a: A = state.eval(source.next(), ||"bsp/b: expected content 1")?; + let b: B = state.eval(source.next(), ||"bsp/b: expected content 2")?; + Self::b(a, b) + }, + _ => return Ok(None), + } + } else { + return Ok(None) })) } } -impl Dsl for Align where S: Eval { +impl Dsl for Align where S: Eval, A> { fn try_provide (state: S, source: Ast) -> Perhaps { - Ok(if let Some(Value::Key(key)) = source.peek() { - Some(match key { - "align/c" => { - let _ = source.next(); - let content: A = state.eval(source, ||"align/c: expected content")?; + Ok(Some(if let Ast::Exp(source) = source { + match source.next() { + Some(Value::Key("align/c")) => { + let content: A = state.eval(source.next(), ||"align/c: expected content")?; Self::c(content) }, - "align/x" => { - let _ = source.next(); - let content: A = state.eval(source, ||"align/x: expected content")?; + Some(Value::Key("align/x")) => { + let content: A = state.eval(source.next(), ||"align/x: expected content")?; Self::x(content) }, - "align/y" => { - let _ = source.next(); - let content: A = state.eval(source, ||"align/y: expected content")?; + Some(Value::Key("align/y")) => { + let content: A = state.eval(source.next(), ||"align/y: expected content")?; Self::y(content) }, - "align/n" => { - let _ = source.next(); - let content: A = state.eval(source, ||"align/n: expected content")?; + Some(Value::Key("align/n")) => { + let content: A = state.eval(source.next(), ||"align/n: expected content")?; Self::n(content) }, - "align/s" => { - let _ = source.next(); - let content: A = state.eval(source, ||"align/s: expected content")?; + Some(Value::Key("align/s")) => { + let content: A = state.eval(source.next(), ||"align/s: expected content")?; Self::s(content) }, - "align/e" => { - let _ = source.next(); - let content: A = state.eval(source, ||"align/e: expected content")?; + Some(Value::Key("align/e")) => { + let content: A = state.eval(source.next(), ||"align/e: expected content")?; Self::e(content) }, - "align/w" => { - let _ = source.next(); - let content: A = state.eval(source, ||"align/w: expected content")?; + Some(Value::Key("align/w")) => { + let content: A = state.eval(source.next(), ||"align/w: expected content")?; Self::w(content) }, - "align/nw" => { - let _ = source.next(); - let content: A = state.eval(source, ||"align/nw: expected content")?; + Some(Value::Key("align/nw")) => { + let content: A = state.eval(source.next(), ||"align/nw: expected content")?; Self::nw(content) }, - "align/ne" => { - let _ = source.next(); - let content: A = state.eval(source, ||"align/ne: expected content")?; + Some(Value::Key("align/ne")) => { + let content: A = state.eval(source.next(), ||"align/ne: expected content")?; Self::ne(content) }, - "align/sw" => { - let _ = source.next(); - let content: A = state.eval(source, ||"align/sw: expected content")?; + Some(Value::Key("align/sw")) => { + let content: A = state.eval(source.next(), ||"align/sw: expected content")?; Self::sw(content) }, - "align/se" => { - let _ = source.next(); - let content: A = state.eval(source, ||"align/se: expected content")?; + Some(Value::Key("align/se")) => { + let content: A = state.eval(source.next(), ||"align/se: expected content")?; Self::se(content) }, _ => return Ok(None), - }) + } } else { - None - }) + return Ok(None) + })) } }