mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
wip: fix(dsl): kinda patch it up
This commit is contained in:
parent
e72225f83c
commit
d99b20c99d
3 changed files with 253 additions and 239 deletions
|
|
@ -4,24 +4,24 @@ use crate::*;
|
|||
/// Each contained layer defines a mapping from input event to command invocation
|
||||
/// 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.
|
||||
#[derive(Debug)] pub struct InputMap<I, T: Dsl>(
|
||||
#[derive(Debug)] pub struct InputMap<I, D: Dsl>(
|
||||
/// Map of input event (key combination) to
|
||||
/// all command expressions bound to it by
|
||||
/// all loaded input layers.
|
||||
pub BTreeMap<I, Vec<InputBinding<T>>>
|
||||
pub BTreeMap<I, Vec<InputBinding<D>>>
|
||||
);
|
||||
impl<I, T: Dsl> Default for InputMap<I, T> {
|
||||
impl<I, D: Dsl> Default for InputMap<I, D> {
|
||||
fn default () -> Self {
|
||||
Self(Default::default())
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Default)] pub struct InputBinding<T: Dsl> {
|
||||
condition: Option<T>,
|
||||
command: T,
|
||||
#[derive(Debug, Default)] pub struct InputBinding<D: Dsl> {
|
||||
condition: Option<D>,
|
||||
command: D,
|
||||
description: Option<Arc<str>>,
|
||||
source: Option<Arc<PathBuf>>,
|
||||
}
|
||||
impl<I: Debug + Ord, T: Dsl> InputMap<I, T> {
|
||||
impl<'s, I: Debug + Ord, D: Dsl + From<Cst<'s>>> InputMap<I, D> {
|
||||
/// Create input layer collection from path to text file.
|
||||
pub fn from_path <P: Debug + AsRef<Path>> (path: P) -> Usually<Self> {
|
||||
if !exists(path.as_ref())? {
|
||||
|
|
@ -30,22 +30,19 @@ impl<I: Debug + Ord, T: Dsl> InputMap<I, T> {
|
|||
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()))
|
||||
pub fn from_source (source: impl AsRef<str>) -> Usually<Self> {
|
||||
Self::from_dsl(D::from(Cst::from(source.as_ref())))
|
||||
}
|
||||
/// Create input layer collection from DSL.
|
||||
pub fn from_dsl <D: Dsl> (dsl: D) -> Usually<Self> {
|
||||
use DslVal::*;
|
||||
let mut input_map: BTreeMap<I, Vec<InputBinding<T>>> = Default::default();
|
||||
let mut index = 0;
|
||||
while let Some(Exp(_, mut exp)) = dsl.nth(index) {
|
||||
let val = exp.nth(0).map(|x|x.val());
|
||||
match val {
|
||||
pub fn from_dsl (dsl: D) -> Usually<Self> {
|
||||
use Val::*;
|
||||
let mut input_map: BTreeMap<I, Vec<InputBinding<D>>> = Default::default();
|
||||
match dsl.exp() {
|
||||
Some(exp) => match exp.head() {
|
||||
Some(Str(path)) => {
|
||||
let path = PathBuf::from(path.as_ref());
|
||||
let module = InputMap::<I, T>::from_path(&path)?;
|
||||
for (key, val) in module.0.into_iter() {
|
||||
todo!("import {exp:?} {key:?} {val:?} {path:?}");
|
||||
let path = PathBuf::from(path.as_ref());
|
||||
for (key, val) in InputMap::<I, D>::from_path(&path)?.0.into_iter() {
|
||||
todo!("import {path:?} {key:?} {val:?}");
|
||||
if !input_map.contains_key(&key) {
|
||||
input_map.insert(key, vec![]);
|
||||
}
|
||||
|
|
@ -56,14 +53,14 @@ impl<I: Debug + Ord, T: Dsl> InputMap<I, T> {
|
|||
//if !input_map.contains_key(&key) {
|
||||
//input_map.insert(key, vec![]);
|
||||
//}
|
||||
todo!("binding {exp:?} {sym:?}");
|
||||
todo!("binding {sym:?} {:?}", exp.tail());
|
||||
},
|
||||
Some(Key(key)) if key.as_ref() == "if" => {
|
||||
todo!("conditional binding {exp:?} {key:?}");
|
||||
Some(Key("if")) => {
|
||||
todo!("conditional binding {:?}", exp.tail());
|
||||
},
|
||||
_ => return Result::Err(format!("invalid token in keymap: {val:?}").into()),
|
||||
}
|
||||
index += 1;
|
||||
_ => return Err(format!("invalid form in keymap: {exp:?}").into())
|
||||
},
|
||||
_ => return Err(format!("not an expression: {dsl:?}").into())
|
||||
}
|
||||
Ok(Self(input_map))
|
||||
}
|
||||
|
|
@ -92,23 +89,23 @@ impl<I: Debug + Ord, T: Dsl> InputMap<I, T> {
|
|||
/*
|
||||
/// Create an input map with a single non-condal layer.
|
||||
/// (Use [Default::default] to get an empty map.)
|
||||
pub fn new (layer: DslVal<T::Str, T::Exp>) -> Self {
|
||||
pub fn new (layer: Val<T::Str, T::Exp>) -> Self {
|
||||
Self::default().layer(layer)
|
||||
}
|
||||
/// Add layer, return `Self`.
|
||||
pub fn layer (mut self, layer: DslVal<T::Str, T::Exp>) -> Self {
|
||||
pub fn layer (mut self, layer: Val<T::Str, T::Exp>) -> Self {
|
||||
self.add_layer(layer); self
|
||||
}
|
||||
/// Add condal layer, return `Self`.
|
||||
pub fn layer_if (mut self, cond: DslVal<T::Str, T::Exp>, layer: DslVal<T::Str, T::Exp>) -> Self {
|
||||
pub fn layer_if (mut self, cond: Val<T::Str, T::Exp>, layer: Val<T::Str, T::Exp>) -> Self {
|
||||
self.add_layer_if(Some(cond), layer); 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: Val<T::Str, T::Exp>) -> &mut Self {
|
||||
self.add_layer_if(None, layer.into()); self
|
||||
}
|
||||
/// Add condal layer, return `&mut Self`.
|
||||
pub fn add_layer_if (&mut self, cond: Option<DslVal<T::Str, T::Exp>>, bind: DslVal<T::Str, T::Exp>) -> &mut Self {
|
||||
pub fn add_layer_if (&mut self, cond: Option<Val<T::Str, T::Exp>>, bind: Val<T::Str, T::Exp>) -> &mut Self {
|
||||
self.0.push(InputLayer { cond, bind });
|
||||
self
|
||||
}
|
||||
|
|
@ -182,7 +179,7 @@ impl<I: Debug + Ord, T: Dsl> InputMap<I, T> {
|
|||
//}
|
||||
//fn from (source: &'s str) -> Self {
|
||||
//// this should be for single layer:
|
||||
//use DslVal::*;
|
||||
//use Val::*;
|
||||
//let mut layers = vec![];
|
||||
//let mut source = CstIter::from(source);
|
||||
//while let Some(Exp(_, mut iter)) = source.next().map(|x|x.value) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue