mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 19:56:44 +01:00
This commit is contained in:
parent
11f686650f
commit
0b8ca326ef
4 changed files with 184 additions and 63 deletions
|
|
@ -5,6 +5,8 @@ pub(crate) use ::tengri_core::*;
|
||||||
pub(crate) use std::fmt::Debug;
|
pub(crate) use std::fmt::Debug;
|
||||||
pub(crate) use std::sync::Arc;
|
pub(crate) use std::sync::Arc;
|
||||||
pub(crate) use std::collections::VecDeque;
|
pub(crate) use std::collections::VecDeque;
|
||||||
|
pub(crate) use std::path::Path;
|
||||||
|
pub(crate) use std::fs::exists;
|
||||||
pub(crate) use konst::iter::{ConstIntoIter, IsIteratorKind};
|
pub(crate) use konst::iter::{ConstIntoIter, IsIteratorKind};
|
||||||
pub(crate) use konst::string::{split_at, str_range, char_indices};
|
pub(crate) use konst::string::{split_at, str_range, char_indices};
|
||||||
pub(crate) use thiserror::Error;
|
pub(crate) use thiserror::Error;
|
||||||
|
|
|
||||||
|
|
@ -1,43 +1,70 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
/// A collection of input bind.
|
||||||
/// A collection of input bindings.
|
|
||||||
///
|
///
|
||||||
/// Each contained layer defines a mapping from input event to command invocation
|
/// Each contained layer defines a mapping from input event to command invocation
|
||||||
/// over a given state. Furthermore, each layer may have an associated condition,
|
/// over a given state. Furthermore, each layer may have an associated cond,
|
||||||
/// so that only certain layers are active at a given time depending on state.
|
/// so that only certain layers are active at a given time depending on state.
|
||||||
#[derive(Debug)] pub struct InputLayers<T: Dsl>(Vec<InputLayer<T>>);
|
#[derive(Debug, Default)] pub struct InputMap<I, T: Dsl>(std::collections::BTreeMap<I, T>);
|
||||||
/// A single input binding layer.
|
impl<I, T: Dsl> InputMap<I, T> {
|
||||||
#[derive(Default, Debug)] struct InputLayer<T: Dsl> {
|
/// Create input layer collection from path to text file.
|
||||||
condition: Option<DslVal<T::Str, T::Exp>>,
|
pub fn from_path <P: Debug + AsRef<Path>> (path: P) -> Usually<Self> {
|
||||||
bindings: DslVal<T::Str, T::Exp>,
|
if !exists(path.as_ref())? {
|
||||||
|
return Err(format!("(e5) not found: {path:?}").into())
|
||||||
|
}
|
||||||
|
Self::from_source(read_and_leak(path)?)
|
||||||
|
}
|
||||||
|
/// Create input layer collection from string.
|
||||||
|
pub fn from_source <S: AsRef<str>> (source: S) -> Usually<Self> {
|
||||||
|
Self::from_dsl(CstIter::from(source.as_ref()))
|
||||||
|
}
|
||||||
|
/// Create input layer collection from DSL.
|
||||||
|
pub fn from_dsl (dsl: impl Dsl) -> Usually<Self> {
|
||||||
|
use DslVal::*;
|
||||||
|
let mut input_map: Self = Self(Default::default());
|
||||||
|
while let Exp(_, mut iter) = dsl.val() {
|
||||||
|
let mut iter = iter.clone();
|
||||||
|
match iter.next().map(|x|x.value) {
|
||||||
|
Some(Str(path)) => {
|
||||||
|
todo!("import");
|
||||||
|
for (key, val) in InputMap::<I, T>::from_path(path)?.0.into_iter() {
|
||||||
}
|
}
|
||||||
/// Input layers start out empty regardless if `T` implements [Default].
|
|
||||||
impl<T: Dsl> Default for InputLayers<T> { fn default () -> Self { Self(vec![]) } }
|
|
||||||
/// Bring up an input layer map from a string representation.
|
|
||||||
impl<'s> From<&'s str> for InputLayers<Ast> {
|
|
||||||
fn from (source: &'s str) -> Self {
|
|
||||||
let mut layers = vec![];
|
|
||||||
let mut source = CstIter::from(source);
|
|
||||||
while let Some(cst) = source.next() {
|
|
||||||
panic!("{:?}", cst.value);
|
|
||||||
layers.push(match cst.value.exp_head().map(|x|x.as_key()).flatten() {
|
|
||||||
Some("layer") => InputLayer {
|
|
||||||
condition: None,
|
|
||||||
bindings: dsl_val(source.nth(1).unwrap()),
|
|
||||||
},
|
},
|
||||||
Some("layer-if") => InputLayer {
|
Some(Exp(_, expr)) if let Some(Sym(sym)) = expr.nth(0) => {
|
||||||
condition: Some(dsl_val(source.nth(1).unwrap())),
|
//input_map.unconditional.push(expr);
|
||||||
bindings: dsl_val(source.nth(2).unwrap()),
|
todo!("binding");
|
||||||
},
|
},
|
||||||
_ => panic!("this shoulda been a TryFrom"),
|
Some(Exp(_, expr)) if expr.nth(0) == Some(Key("if")) => {
|
||||||
});
|
todo!("conditional binding");
|
||||||
}
|
},
|
||||||
Self(layers)
|
_ => return Result::Err("invalid token in keymap".into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(input_map)
|
||||||
impl<T: Dsl> InputLayers<T> {
|
}
|
||||||
/// Create an input map with a single non-conditional layer.
|
/// Evaluate the active layers for a given state,
|
||||||
|
/// returning the command to be executed, if any.
|
||||||
|
pub fn handle <S, O> (&self, state: &mut S, input: I) -> Perhaps<O> where
|
||||||
|
S: DslInto<bool> + DslInto<O>,
|
||||||
|
O: Command<S>
|
||||||
|
{
|
||||||
|
todo!();
|
||||||
|
//let layers = self.0.as_slice();
|
||||||
|
//for InputLayer { cond, bind } in layers.iter() {
|
||||||
|
//let mut matches = true;
|
||||||
|
//if let Some(cond) = cond {
|
||||||
|
//matches = state.dsl_into(cond, ||format!("input: no cond").into())?;
|
||||||
|
//}
|
||||||
|
//if matches
|
||||||
|
//&& let Some(exp) = bind.val().exp_head()
|
||||||
|
//&& input.dsl_into(exp, ||format!("InputMap: input.eval(binding) failed").into())?
|
||||||
|
//&& let Some(command) = state.try_dsl_into(exp)? {
|
||||||
|
//return Ok(Some(command))
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
/// Create an input map with a single non-condal layer.
|
||||||
/// (Use [Default::default] to get an empty map.)
|
/// (Use [Default::default] to get an empty map.)
|
||||||
pub fn new (layer: DslVal<T::Str, T::Exp>) -> Self {
|
pub fn new (layer: DslVal<T::Str, T::Exp>) -> Self {
|
||||||
Self::default().layer(layer)
|
Self::default().layer(layer)
|
||||||
|
|
@ -46,39 +73,128 @@ impl<T: Dsl> InputLayers<T> {
|
||||||
pub fn layer (mut self, layer: DslVal<T::Str, T::Exp>) -> Self {
|
pub fn layer (mut self, layer: DslVal<T::Str, T::Exp>) -> Self {
|
||||||
self.add_layer(layer); self
|
self.add_layer(layer); self
|
||||||
}
|
}
|
||||||
/// Add conditional layer, return `Self`.
|
/// Add condal layer, return `Self`.
|
||||||
pub fn layer_if (mut self, condition: DslVal<T::Str, T::Exp>, layer: DslVal<T::Str, T::Exp>) -> Self {
|
pub fn layer_if (mut self, cond: DslVal<T::Str, T::Exp>, layer: DslVal<T::Str, T::Exp>) -> Self {
|
||||||
self.add_layer_if(Some(condition), layer); self
|
self.add_layer_if(Some(cond), layer); self
|
||||||
}
|
}
|
||||||
/// Add layer, return `&mut Self`.
|
/// Add layer, return `&mut Self`.
|
||||||
pub fn add_layer (&mut self, layer: DslVal<T::Str, T::Exp>) -> &mut Self {
|
pub fn add_layer (&mut self, layer: DslVal<T::Str, T::Exp>) -> &mut Self {
|
||||||
self.add_layer_if(None, layer.into()); self
|
self.add_layer_if(None, layer.into()); self
|
||||||
}
|
}
|
||||||
/// Add conditional layer, return `&mut Self`.
|
/// Add condal layer, return `&mut Self`.
|
||||||
pub fn add_layer_if (&mut self, condition: Option<DslVal<T::Str, T::Exp>>, bindings: DslVal<T::Str, T::Exp>) -> &mut Self {
|
pub fn add_layer_if (&mut self, cond: Option<DslVal<T::Str, T::Exp>>, bind: DslVal<T::Str, T::Exp>) -> &mut Self {
|
||||||
self.0.push(InputLayer { condition, bindings });
|
self.0.push(InputLayer { cond, bind });
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
/// Evaluate the active layers for a given state,
|
*/
|
||||||
/// returning the command to be executed, if any.
|
|
||||||
pub fn handle <S, I, O> (&self, state: &mut S, input: I) -> Perhaps<O> where
|
|
||||||
S: DslInto<bool> + DslInto<O>,
|
|
||||||
I: DslInto<bool>,
|
|
||||||
O: Command<S>
|
|
||||||
{
|
|
||||||
let layers = self.0.as_slice();
|
|
||||||
for InputLayer { condition, bindings } in layers.iter() {
|
|
||||||
let mut matches = true;
|
|
||||||
if let Some(condition) = condition {
|
|
||||||
matches = state.dsl_into(condition, ||format!("input: no condition").into())?;
|
|
||||||
}
|
}
|
||||||
if matches
|
|
||||||
&& let Some(exp) = bindings.val().exp_head()
|
|
||||||
&& input.dsl_into(exp, ||format!("InputLayers: input.eval(binding) failed").into())?
|
|
||||||
&& let Some(command) = state.try_dsl_into(exp)? {
|
//let mut keys = iter.unwrap();
|
||||||
return Ok(Some(command))
|
//let mut map = InputMap::default();
|
||||||
|
//while let Some(token) = keys.next() {
|
||||||
|
//if let Value::Exp(_, mut exp) = token.value {
|
||||||
|
//let next = exp.next();
|
||||||
|
//if let Some(Token { value: Value::Key(sym), .. }) = next {
|
||||||
|
//match sym {
|
||||||
|
//"layer" => {
|
||||||
|
//if let Some(Token { value: Value::Str(path), .. }) = exp.peek() {
|
||||||
|
//let path = base.as_ref().parent().unwrap().join(unquote(path));
|
||||||
|
//if !std::fs::exists(&path)? {
|
||||||
|
//return Err(format!("(e5) not found: {path:?}").into())
|
||||||
|
//}
|
||||||
|
//map.add_layer(read_and_leak(path)?.into());
|
||||||
|
//print!("layer:\n path: {:?}...", exp.0.0.trim());
|
||||||
|
//println!("ok");
|
||||||
|
//} else {
|
||||||
|
//return Err(format!("(e4) unexpected non-string {next:?}").into())
|
||||||
|
//}
|
||||||
|
//},
|
||||||
|
|
||||||
|
//"layer-if" => {
|
||||||
|
//let mut cond = None;
|
||||||
|
|
||||||
|
//if let Some(Token { value: Value::Sym(sym), .. }) = exp.next() {
|
||||||
|
//cond = Some(leak(sym));
|
||||||
|
//} else {
|
||||||
|
//return Err(format!("(e4) unexpected non-symbol {next:?}").into())
|
||||||
|
//};
|
||||||
|
|
||||||
|
//if let Some(Token { value: Value::Str(path), .. }) = exp.peek() {
|
||||||
|
//let path = base.as_ref().parent().unwrap().join(unquote(path));
|
||||||
|
//if !std::fs::exists(&path)? {
|
||||||
|
//return Err(format!("(e5) not found: {path:?}").into())
|
||||||
|
//}
|
||||||
|
//print!("layer-if:\n cond: {}\n path: {path:?}...",
|
||||||
|
//cond.unwrap_or_default());
|
||||||
|
//let keys = read_and_leak(path)?.into();
|
||||||
|
//let cond = cond.unwrap();
|
||||||
|
//println!("ok");
|
||||||
|
//map.add_layer_if(
|
||||||
|
//Box::new(move |state: &App|Take::take_or_fail(
|
||||||
|
//state, exp, ||"missing input layer conditional"
|
||||||
|
//)), keys
|
||||||
|
//);
|
||||||
|
//} else {
|
||||||
|
//return Err(format!("(e4) unexpected non-symbol {next:?}").into())
|
||||||
|
//}
|
||||||
|
//},
|
||||||
|
|
||||||
|
//_ => return Err(format!("(e3) unexpected symbol {sym:?}").into())
|
||||||
|
//}
|
||||||
|
//} else {
|
||||||
|
//return Err(format!("(e2) unexpected exp {:?}", next.map(|x|x.value)).into())
|
||||||
|
//}
|
||||||
|
//} else {
|
||||||
|
//return Err(format!("(e1) unexpected token {token:?}").into())
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
//Ok(map)
|
||||||
|
//}
|
||||||
|
//{
|
||||||
|
//}
|
||||||
|
//fn from (source: &'s str) -> Self {
|
||||||
|
//// this should be for single layer:
|
||||||
|
//use DslVal::*;
|
||||||
|
//let mut layers = vec![];
|
||||||
|
//let mut source = CstIter::from(source);
|
||||||
|
//while let Some(Exp(_, mut iter)) = source.next().map(|x|x.value) {
|
||||||
|
//let mut iter = iter.clone();
|
||||||
|
//layers.push(match iter.next().map(|x|x.value) {
|
||||||
|
//Some(Sym(sym)) if sym.starts_with("@") => InputLayer {
|
||||||
|
//cond: None,
|
||||||
|
//bind: vec![[
|
||||||
|
//dsl_val(source.nth(1).unwrap()),
|
||||||
|
//dsl_val(source.nth(2).unwrap()),
|
||||||
|
//]],
|
||||||
|
//},
|
||||||
|
//Some(Str(layer)) => InputLayer {
|
||||||
|
//cond: None,
|
||||||
|
//bind: dsl_val(source.nth(1).unwrap()),
|
||||||
|
//},
|
||||||
|
//Some(Key("if")) => InputLayer {
|
||||||
|
//cond: Some(dsl_val(source.nth(1).unwrap())),
|
||||||
|
//bind: dsl_val(source.nth(2).unwrap()),
|
||||||
|
//},
|
||||||
|
//_ => panic!("invalid token in keymap"),
|
||||||
|
//})
|
||||||
|
//}
|
||||||
|
//Self(layers)
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
|
||||||
|
fn unquote (x: &str) -> &str {
|
||||||
|
let mut chars = x.chars();
|
||||||
|
chars.next();
|
||||||
|
//chars.next_back();
|
||||||
|
chars.as_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_and_leak (path: impl AsRef<Path>) -> Usually<&'static str> {
|
||||||
|
Ok(leak(String::from_utf8(std::fs::read(path.as_ref())?)?))
|
||||||
}
|
}
|
||||||
Ok(None)
|
|
||||||
}
|
fn leak (x: impl AsRef<str>) -> &'static str {
|
||||||
|
Box::leak(x.as_ref().into())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
#![feature(associated_type_defaults)]
|
#![feature(associated_type_defaults)]
|
||||||
#![feature(if_let_guard)]
|
#![feature(if_let_guard)]
|
||||||
|
|
||||||
|
pub(crate) use std::fmt::Debug;
|
||||||
|
pub(crate) use std::path::Path;
|
||||||
|
pub(crate) use std::fs::exists;
|
||||||
pub(crate) use tengri_core::*;
|
pub(crate) use tengri_core::*;
|
||||||
|
|
||||||
mod input_macros;
|
mod input_macros;
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ use crossterm::event::{Event, KeyEvent, KeyCode, KeyModifiers, KeyEventKind, Key
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut test = Test {
|
let mut test = Test {
|
||||||
keys: InputLayers::from("
|
keys: InputMap::from_source("
|
||||||
(@a do-thing)
|
(@a do-thing)
|
||||||
(@b do-thing-arg 0)
|
(@b do-thing-arg 0)
|
||||||
(@c do-sub do-other-thing)
|
(@c do-sub do-other-thing)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue