wip: dsl: continuing to offload to Ast

This commit is contained in:
🪞👃🪞 2025-05-26 23:39:02 +03:00
parent e9d4c7e0bc
commit 08a8dff93d
4 changed files with 19 additions and 20 deletions

View file

@ -1,16 +1,8 @@
use crate::*;
use std::sync::Arc;
use std::borrow::Cow;
#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub struct AstIter;
impl<'source> From<SourceIter<'source>> for AstIter {
fn from (source: SourceIter<'source>) -> Self {
Self // TODO
}
}
/// Owns its values, and has no metadata.
pub type Ast = Value<Arc<str>, AstIter>;
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Ast(pub Value<Arc<str>, AstIter>);
impl<'source> From<CstToken<'source>> for Ast {
fn from (token: CstToken<'source>) -> Self {
token.value().into()
@ -19,7 +11,7 @@ impl<'source> From<CstToken<'source>> for Ast {
impl<'source> From<CstValue<'source>> for Ast {
fn from (value: CstValue<'source>) -> Self {
use Value::*;
match value {
Self(match value {
Nil => Nil,
Err(e) => Err(e),
Num(u) => Num(u),
@ -27,6 +19,14 @@ impl<'source> From<CstValue<'source>> for Ast {
Key(s) => Key(s.into()),
Str(s) => Str(s.into()),
Exp(d, x) => Exp(d, x.into()),
}
})
}
}
#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub struct AstIter; // TODO
impl<'source> From<SourceIter<'source>> for AstIter {
fn from (source: SourceIter<'source>) -> Self {
Self // TODO
}
}

View file

@ -1,13 +1,12 @@
use crate::*;
/// Keeps the reference to the source slice.
/// CST stores strings as source references and expressions as new [SourceIter] instances.
pub type CstValue<'source> = Value<&'source str, SourceIter<'source>>;
/// Token sharing memory with source reference.
#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub struct CstToken<'source>(pub CstValue<'source>, pub CstMeta<'source>);
/// Reference to the source slice.
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstMeta<'source> {
pub source: &'source str,
pub start: usize,
pub length: usize,
}
pub type CstValue<'source> = Value<&'source str, SourceIter<'source>>;

View file

@ -3,7 +3,7 @@ use std::fmt::{Debug, Display, Formatter, Error as FormatError};
impl Display for Ast {
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
use Value::*;
write!(out, "{}", match self {
write!(out, "{}", match &self.0 {
Nil => String::new(),
Err(e) => format!("[error: {e}]"),
Num(n) => format!("{n}"),

View file

@ -23,14 +23,14 @@ impl InputLayers {
self.0.push(InputLayer(condition, binding));
self
}
pub fn handle <S, I, O: Command<S>> (&self, state: &mut S, input: I) -> Perhaps<O> {
pub fn handle <S: Eval<Ast, bool>, I, O: Command<S>> (&self, state: &mut S, input: I) -> Perhaps<O> {
InputHandle(state, self.0.as_slice()).try_eval(input)
}
}
pub struct InputHandle<'a, S>(&'a mut S, &'a [InputLayer]);
impl<'a, S, I, O: Command<S>> Eval<I, O> for InputHandle<'a, S> {
impl<'a, S: Eval<Ast, bool>, I, O: Command<S>> Eval<I, O> for InputHandle<'a, S> {
fn try_eval (&self, input: I) -> Perhaps<O> {
let Self(state, layers) = self;
for InputLayer(condition, binding) in layers.iter() {