wip: slowly remembering where i was
Some checks failed
/ build (push) Has been cancelled

This commit is contained in:
🪞👃🪞 2025-06-13 11:42:20 +03:00
parent 17506726cb
commit c8827b43c3
9 changed files with 114 additions and 135 deletions

View file

@ -1,30 +1,29 @@
use crate::*;
use std::fmt::{Display, Formatter, Error as FormatError};
impl Display for Ast {
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
use Value::*;
write!(out, "{}", match &self.0 {
Nil => String::new(),
Err(e) => format!("[error: {e}]"),
Num(n) => format!("{n}"),
Sym(s) => format!("{s}"),
Key(s) => format!("{s}"),
Str(s) => format!("{s}"),
Exp(_, e) => format!("{e:?}"),
})
match &self.0 {
Value::Nil => Ok(()),
Value::Err(e) => write!(out, "[error: {e}]"),
Value::Num(n) => write!(out, "{n}"),
Value::Sym(s) => write!(out, "{s}"),
Value::Key(s) => write!(out, "{s}"),
Value::Str(s) => write!(out, "{s}"),
Value::Exp(_, e) => write!(out, "{e:?}"),
}
}
}
impl<'source> Display for CstValue<'source> {
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
use Value::*;
write!(out, "{}", match self {
Nil => String::new(),
Err(e) => format!("[error: {e}]"),
Num(n) => format!("{n}"),
Sym(s) => format!("{s}"),
Key(s) => format!("{s}"),
Str(s) => format!("{s}"),
Exp(_, e) => format!("{e:?}"),
})
match self {
Value::Nil => Ok(()),
Value::Err(e) => write!(out, "[error: {e}]"),
Value::Num(n) => write!(out, "{n}"),
Value::Sym(s) => write!(out, "{s}"),
Value::Key(s) => write!(out, "{s}"),
Value::Str(s) => write!(out, "{s}"),
Value::Exp(_, e) => write!(out, "{e:?}"),
}
}
}

View file

@ -1,84 +0,0 @@
use crate::*;
pub trait Eval<Input, Output> {
fn try_eval (&self, input: Input) -> Perhaps<Output>;
fn eval <E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
&self, input: Input, error: F
) -> Usually<Output> {
if let Some(value) = self.try_eval(input)? {
Ok(value)
} else {
Result::Err(format!("Eval: {}", error().into()).into())
}
}
}
//impl<S: Eval<I, O>, I, O> Eval<I, O> for &S {
//fn try_eval (&self, input: I) -> Perhaps<O> {
//(*self).try_eval(input)
//}
//}
//impl<S: Eval<I, O>, I: Ast, O: Dsl<S>> Eval<I, O> for S {
//fn try_eval (&self, input: I) -> Perhaps<O> {
//Dsl::try_provide(self, input)
//}
//}
/// May construct [Self] from token stream.
pub trait Dsl<State>: Sized {
fn try_provide (state: &State, source: Ast) -> Perhaps<Self>;
fn provide <E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
state: &State, source: Ast, error: F
) -> Usually<Self> {
let next = format!("{source:?}");
if let Some(value) = Self::try_provide(state, source)? {
Ok(value)
} else {
Result::Err(format!("Dsl: {}: {next:?}", error().into()).into())
}
}
}
//pub trait Give<'state, 'source, Type> {
///// Implement this to be able to [Give] [Type] from the [Cst].
///// Advance the stream if returning `Ok<Some<Type>>`.
//fn give (&'state self, words: Cst<'source>) -> Perhaps<Type>;
///// Return custom error on [None].
//fn give_or_fail <E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
//&'state self, mut words: Cst<'source>, error: F
//) -> Usually<Type> {
//let next = words.peek().map(|x|x.value).clone();
//if let Some(value) = Give::<Type>::give(self, words)? {
//Ok(value)
//} else {
//Result::Err(format!("give: {}: {next:?}", error().into()).into())
//}
//}
//}
//#[macro_export] macro_rules! give {
//($Type:ty|$state:ident:$State:ident,$words:ident|$expr:expr) => {
//impl Give<$Type> for $State {
//fn give (&self, mut $words: Cst) -> Perhaps<$Type> {
//let $state = self;
//$expr
//}
//}
//};
//($Type:path$(,$Arg:ident)*|$state:ident,$words:ident|$expr:expr) => {
//impl<State: $(Give<$Arg>)++ $(, $Arg)*> Give<$Type> for State {
//fn give (&self, mut $words: Cst) -> Perhaps<$Type> {
//let $state = self;
//$expr
//}
//}
//}
//}
/////// Implement the [Give] trait, which boils down to
/////// specifying two types and providing an expression.
//#[macro_export] macro_rules! from_dsl {
//($Type:ty: |$state:ident:$State:ty, $words:ident|$expr:expr) => {
//give! { $Type|$state:$State,$words|$expr }
//};
//}

View file

@ -1,5 +1,20 @@
use crate::*;
use std::fmt::Display;
/// Thing that may construct itself from state and [DslValue].
pub trait Dsl<State>: Sized {
fn try_provide (state: &State, value: impl DslValue) -> Perhaps<Self>;
fn provide (
state: &State,
value: impl DslValue,
error: impl Fn()->Box<dyn std::error::Error>
) -> Usually<Self> {
match Self::try_provide(state, value)? {
Some(value) => Ok(value),
_ => Err(error())
}
}
}
pub trait DslValue: PartialEq + Clone + Default + Debug {
type Str: AsRef<str> + PartialEq + Clone + Default + Debug;
type Exp: PartialEq + Clone + Default + Debug;
@ -29,6 +44,7 @@ pub trait DslValue: PartialEq + Clone + Default + Debug {
fn exp_head (&self) -> Option<&Self> { None } // TODO
fn exp_tail (&self) -> Option<&[Self]> { None } // TODO
}
impl<
Str: AsRef<str> + PartialEq + Clone + Default + Debug,
Exp: PartialEq + Clone + Default + Debug,
@ -40,8 +56,20 @@ impl<
}
}
pub enum Value<S, X> { Nil, Err(DslError), Num(usize), Sym(S), Key(S), Str(S), Exp(usize, X), }
impl<S, X> Default for Value<S, X> { fn default () -> Self { Self:: Nil } }
pub enum Value<S, X> {
Nil,
Err(DslError),
Num(usize),
Sym(S),
Key(S),
Str(S),
Exp(usize, X),
}
impl<S, X> Default for Value<S, X> {
fn default () -> Self { Self:: Nil }
}
impl<S: PartialEq, X: PartialEq,> PartialEq for Value<S, X> {
fn eq (&self, other: &Self) -> bool {
use Value::*;
@ -57,6 +85,7 @@ impl<S: PartialEq, X: PartialEq,> PartialEq for Value<S, X> {
}
}
}
impl<S: Clone, X: Clone,> Clone for Value<S, X> {
fn clone (&self) -> Self {
use Value::*;
@ -71,12 +100,15 @@ impl<S: Clone, X: Clone,> Clone for Value<S, X> {
}
}
}
impl<S: Copy, X: Copy,> Copy for Value<S, X> {}
impl<S: Debug, X: Debug,> Debug for Value<S, X> {
fn fmt (&self, _f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
todo!()
}
}
impl<S: Display, X: Display,> Display for Value<S, X> {
fn fmt (&self, _f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
todo!()

View file

@ -36,6 +36,7 @@
#![feature(impl_trait_in_fn_trait_return)]
pub(crate) use ::tengri_core::*;
pub(crate) use std::fmt::Debug;
pub(crate) use std::fmt::{Display, Formatter, Error as FormatError};
pub(crate) use konst::iter::{ConstIntoIter, IsIteratorKind};
pub(crate) use konst::string::{split_at, str_range, char_indices};
pub(crate) use thiserror::Error;
@ -43,7 +44,6 @@ pub(crate) use self::DslError::*;
mod dsl_ast; pub use self::dsl_ast::*;
mod dsl_cst; pub use self::dsl_cst::*;
mod dsl_display; //pub use self::dsl_display::*;
mod dsl_domain; pub use self::dsl_domain::*;
mod dsl_error; pub use self::dsl_error::*;
mod dsl_iter; pub use self::dsl_iter::*;
mod dsl_token; pub use self::dsl_token::*;