wip: dsl: more rework

This commit is contained in:
🪞👃🪞 2025-05-26 01:30:13 +03:00
parent f1b24d436a
commit 7097333993
4 changed files with 145 additions and 159 deletions

View file

@ -2,40 +2,40 @@ use crate::*;
use std::sync::Arc;
use std::fmt::{Debug, Display, Formatter};
/// Emits tokens.
pub trait Ast: Debug {
fn peek (&self) -> Option<AstValue>;
fn next (&mut self) -> Option<AstValue>;
fn rest (self) -> Option<Box<dyn Ast>>;
}
/// A [Cst] can be used as an [Ast].
impl<'source: 'static> Ast for Cst<'source> {
fn peek (&self) -> Option<AstValue> {
Cst::peek(self).map(|token|token.value.into())
}
fn next (&mut self) -> Option<AstValue> {
Iterator::next(self).map(|token|token.value.into())
}
fn rest (self) -> Option<Box<dyn Ast>> {
self.peek().is_some().then(||Box::new(self) as Box<dyn Ast>)
}
}
#[derive(Clone, Default, Debug)]
pub enum AstValue {
pub enum Ast {
#[default] Nil,
Err(DslError),
Num(usize),
Sym(Arc<str>),
Key(Arc<str>),
Str(Arc<str>),
Exp(Arc<Box<dyn Ast>>),
Exp(Arc<Box<dyn AstIter>>),
}
impl std::fmt::Display for AstValue {
/// Emits tokens.
pub trait AstIter: Debug {
fn peek (&self) -> Option<Ast>;
fn next (&mut self) -> Option<Ast>;
fn rest (self) -> Option<Box<dyn AstIter>>;
}
/// A [Cst] can be used as an [Ast].
impl<'source: 'static> AstIter for Cst<'source> {
fn peek (&self) -> Option<Ast> {
Cst::peek(self).map(|token|token.value.into())
}
fn next (&mut self) -> Option<Ast> {
Iterator::next(self).map(|token|token.value.into())
}
fn rest (self) -> Option<Box<dyn AstIter>> {
self.peek().is_some().then(||Box::new(self) as Box<dyn AstIter>)
}
}
impl std::fmt::Display for Ast {
fn fmt (&self, out: &mut Formatter) -> Result<(), std::fmt::Error> {
use AstValue::*;
use Ast::*;
write!(out, "{}", match self {
Nil => String::new(),
Err(e) => format!("[error: {e}]"),
@ -48,7 +48,7 @@ impl std::fmt::Display for AstValue {
}
}
impl<'source: 'static> From<CstValue<'source>> for AstValue {
impl<'source: 'static> From<CstValue<'source>> for Ast {
fn from (other: CstValue<'source>) -> Self {
use CstValue::*;
match other {
@ -63,8 +63,8 @@ impl<'source: 'static> From<CstValue<'source>> for AstValue {
}
}
impl<'source: 'static> Into<Box<dyn Ast>> for Cst<'source> {
fn into (self) -> Box<dyn Ast> {
impl<'source: 'static> Into<Box<dyn AstIter>> for Cst<'source> {
fn into (self) -> Box<dyn AstIter> {
Box::new(self)
}
}