mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
wip: dsl: continuing to offload to Ast
This commit is contained in:
parent
e9d4c7e0bc
commit
08a8dff93d
4 changed files with 19 additions and 20 deletions
|
|
@ -1,16 +1,8 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
#[derive(Debug, Clone, Default, PartialEq)]
|
||||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
pub struct Ast(pub Value<Arc<str>, AstIter>);
|
||||||
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>;
|
|
||||||
impl<'source> From<CstToken<'source>> for Ast {
|
impl<'source> From<CstToken<'source>> for Ast {
|
||||||
fn from (token: CstToken<'source>) -> Self {
|
fn from (token: CstToken<'source>) -> Self {
|
||||||
token.value().into()
|
token.value().into()
|
||||||
|
|
@ -19,7 +11,7 @@ impl<'source> From<CstToken<'source>> for Ast {
|
||||||
impl<'source> From<CstValue<'source>> for Ast {
|
impl<'source> From<CstValue<'source>> for Ast {
|
||||||
fn from (value: CstValue<'source>) -> Self {
|
fn from (value: CstValue<'source>) -> Self {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
match value {
|
Self(match value {
|
||||||
Nil => Nil,
|
Nil => Nil,
|
||||||
Err(e) => Err(e),
|
Err(e) => Err(e),
|
||||||
Num(u) => Num(u),
|
Num(u) => Num(u),
|
||||||
|
|
@ -27,6 +19,14 @@ impl<'source> From<CstValue<'source>> for Ast {
|
||||||
Key(s) => Key(s.into()),
|
Key(s) => Key(s.into()),
|
||||||
Str(s) => Str(s.into()),
|
Str(s) => Str(s.into()),
|
||||||
Exp(d, x) => Exp(d, x.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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,12 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
/// CST stores strings as source references and expressions as new [SourceIter] instances.
|
||||||
/// Keeps the reference to the source slice.
|
pub type CstValue<'source> = Value<&'source str, SourceIter<'source>>;
|
||||||
|
/// Token sharing memory with source reference.
|
||||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||||
pub struct CstToken<'source>(pub CstValue<'source>, pub CstMeta<'source>);
|
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> {
|
#[derive(Debug, Copy, Clone, Default, PartialEq)] pub struct CstMeta<'source> {
|
||||||
pub source: &'source str,
|
pub source: &'source str,
|
||||||
pub start: usize,
|
pub start: usize,
|
||||||
pub length: usize,
|
pub length: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type CstValue<'source> = Value<&'source str, SourceIter<'source>>;
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use std::fmt::{Debug, Display, Formatter, Error as FormatError};
|
||||||
impl Display for Ast {
|
impl Display for Ast {
|
||||||
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
write!(out, "{}", match self {
|
write!(out, "{}", match &self.0 {
|
||||||
Nil => String::new(),
|
Nil => String::new(),
|
||||||
Err(e) => format!("[error: {e}]"),
|
Err(e) => format!("[error: {e}]"),
|
||||||
Num(n) => format!("{n}"),
|
Num(n) => format!("{n}"),
|
||||||
|
|
|
||||||
|
|
@ -23,14 +23,14 @@ impl InputLayers {
|
||||||
self.0.push(InputLayer(condition, binding));
|
self.0.push(InputLayer(condition, binding));
|
||||||
self
|
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)
|
InputHandle(state, self.0.as_slice()).try_eval(input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct InputHandle<'a, S>(&'a mut S, &'a [InputLayer]);
|
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> {
|
fn try_eval (&self, input: I) -> Perhaps<O> {
|
||||||
let Self(state, layers) = self;
|
let Self(state, layers) = self;
|
||||||
for InputLayer(condition, binding) in layers.iter() {
|
for InputLayer(condition, binding) in layers.iter() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue