Skip to content

Commit 2565b23

Browse files
committed
feat: command = command alias
1 parent b93782f commit 2565b23

6 files changed

Lines changed: 42 additions & 37 deletions

File tree

stack-control-doc/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use stack_control::{bytecode::commands::core::bind_default_commands, compiletime
44

55
#[derive(serde::Serialize)]
66
struct CommandMeta<'m> {
7-
key: char,
7+
key: &'m str,
88
aliases: &'m Vec<String>,
99
description: &'m str,
1010
}
@@ -14,7 +14,7 @@ fn main() -> Result<(), i32> {
1414
bind_default_commands(&mut command_map);
1515
let metadata: Vec<CommandMeta> = command_map.collection.values()
1616
.map(|c| CommandMeta {
17-
key: c.meta.key,
17+
key: &c.meta.key,
1818
aliases: &c.meta.aliases,
1919
description: &c.meta.description.trim()
2020
}).collect();

stack-control/src/bytecode/commands.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ pub struct DescribedCommand {
2323
}
2424

2525
pub struct CommandMeta {
26-
pub key: char,
26+
pub key: String,
2727
pub aliases: Vec<String>,
2828
pub description: String
2929
}
3030

3131
impl Default for CommandMeta {
3232
fn default() -> Self {
3333
CommandMeta {
34-
key: 'd',
34+
key: String::from('d'),
3535
aliases: vec![],
3636
description: String::from("No description provided")
3737
}

stack-control/src/bytecode/commands/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ macro_rules! define_commands {
2626
$command_map.set($crate::bytecode::commands::DescribedCommand {
2727
execution: Box::new($name {}),
2828
meta: std::sync::Arc::new($crate::bytecode::commands::CommandMeta {
29-
key: $key,
29+
key: String::from($key),
3030
aliases: [$($alias),*].iter().map(|s: &&str| s.to_string()).collect::<Vec<String>>(),
3131
$($vkey : $value),+,
3232
..core::default::Default::default()

stack-control/src/compiletime/command_map.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pub struct CommandMap {
77
// BUT i waste like 2-3 days trying to do everything on lifetimes and pure references
88
// Yeah, i m stupid, maybe later i will fix that
99
// but for now i fcking tired, so forgive me once
10-
pub collection: HashMap<char, Arc<DescribedCommand>>,
11-
pub aliases_collection: HashMap<String, char>
10+
pub collection: HashMap<String, Arc<DescribedCommand>>,
11+
pub aliases_collection: HashMap<String, String>
1212
}
1313

1414
impl CommandMap {
@@ -21,15 +21,19 @@ impl CommandMap {
2121

2222
pub fn set(&mut self, command: DescribedCommand) {
2323
command.meta.aliases.iter()
24-
.for_each(|e| { self.aliases_collection.insert(e.clone(), command.meta.key);});
25-
self.collection.insert(command.meta.key, Arc::new(command));
24+
.for_each(|e| { self.aliases_collection.insert(e.clone(), command.meta.key.clone());});
25+
self.collection.insert(command.meta.key.clone(), Arc::new(command));
2626
}
2727

28-
pub fn get<'a>(&'a self, name: char) -> Option<Arc<DescribedCommand>> {
29-
Some(self.collection.get(&name)?.clone())
28+
pub fn get_by_name(&self, name: &str) -> Option<Arc<DescribedCommand>> {
29+
Some(self.collection.get(name)?.clone())
3030
}
3131

32-
pub fn get_alias(&self, alias: &str) -> Option<char> {
33-
self.aliases_collection.get(alias).copied()
32+
pub fn get_by_alias(&self, alias: &str) -> Option<&str> {
33+
self.aliases_collection.get(alias).map(|s| s.as_str())
34+
}
35+
36+
pub fn get(&self, ident: &str) -> Option<Arc<DescribedCommand>> {
37+
self.get_by_name(ident).or_else(|| self.get_by_name(self.get_by_alias(ident)?))
3438
}
3539
}

stack-control/src/compiletime/compiler.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ pub enum CompilationException {
2020
AliasNotFound(String),
2121
}
2222

23+
// TODO: Try macro?
24+
2325
impl ToString for CompilationException {
2426
fn to_string(&self) -> String {
2527
match self {
2628
CompilationException::AliasNotFound(alias) =>
2729
format!("Alias {alias} not found"),
2830
CompilationException::CommandNotFound(cmd) =>
2931
format!("Command {cmd} not found"),
30-
3132
CompilationException::FunctionTokenRequired =>
3233
format!("Function token required"),
33-
3434
CompilationException::UnexcpectedEndToken(t) =>
3535
format!("Unexcpected END token: {t}")
3636
}
@@ -47,7 +47,7 @@ impl CompileTime {
4747
value_to_push: Value::OpenListIdentifier
4848
}),
4949
meta: Arc::new(CommandMeta {
50-
key: '[',
50+
key: String::from('['),
5151
aliases: vec![
5252
String::from("listopen")
5353
],
@@ -60,7 +60,7 @@ impl CompileTime {
6060
list_generator: Arc::new(DescribedCommand {
6161
execution: Box::new(ListGeneratorCommand {}),
6262
meta: Arc::new(CommandMeta {
63-
key: ']',
63+
key: String::from(']'),
6464
aliases: vec![
6565
String::from("listgen")
6666
],
@@ -71,7 +71,7 @@ impl CompileTime {
7171
}),
7272

7373
stack_pusher_meta: Arc::new(CommandMeta {
74-
key: '↓',
74+
key: String::from('↓'),
7575
aliases: vec![
7676
String::from("pushstack")
7777
],
@@ -115,14 +115,10 @@ impl CompileTime {
115115
})
116116
} else {return Err(CompilationException::FunctionTokenRequired);} // TODO: Error
117117
},
118-
119-
CommandToken::Command(name) =>
120-
self.command_map.get(name).ok_or(CompilationException::CommandNotFound(name))?,
121118

122119
CommandToken::CommandOrAlias(alias) =>
123-
self.command_map.get(
124-
self.command_map.get_alias(&alias).ok_or(CompilationException::AliasNotFound(alias))?
125-
).ok_or(CompilationException::CommandNotFound('0'))?,
120+
self.command_map.get(&alias)
121+
.ok_or(CompilationException::CommandNotFound('0'))?,
126122

127123
CommandToken::ListOpenBracket =>
128124
self.list_opener.clone(),

stack-control/src/compiletime/lexer.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ impl ToString for Token {
2525
}
2626

2727
pub enum CommandToken {
28-
Command(char),
2928
CommandOrAlias(String),
3029
ListOpenBracket,
3130
ListCloseBracket,
@@ -36,7 +35,6 @@ pub enum CommandToken {
3635
impl ToString for CommandToken {
3736
fn to_string(&self) -> String {
3837
match self {
39-
Self::Command(char) => String::from(*char),
4038
Self::CommandOrAlias(alias) => alias.clone(),
4139
Self::ListOpenBracket => String::from("["),
4240
Self::ListCloseBracket => String::from("]"),
@@ -63,20 +61,26 @@ pub fn split_string_to_tokens(string: &str) -> Vec<Token> {
6361
pub fn split_to_tokens<'a>(mut symbols: Peekable<impl Iterator<Item = char> + Clone>) -> Vec<Token> {
6462
let mut result: Vec<Token> = Vec::new();
6563

64+
let p = parse_special_symbol;
65+
6666
while let Some(_) = symbols.peek() {
6767
result.push(
68-
parse_number(&mut symbols)
69-
.or_else(|| parse_special_symbol(&mut symbols, '[', Token::CommandToken(CommandToken::ListOpenBracket)))
70-
.or_else(|| parse_special_symbol(&mut symbols, ']', Token::CommandToken(CommandToken::ListCloseBracket)))
71-
.or_else(|| parse_special_symbol(&mut symbols, '(', Token::FunctionOpenBracket))
72-
.or_else(|| parse_special_symbol(&mut symbols, ')', Token::FunctionCloseBracket))
73-
.or_else(|| parse_special_symbol(&mut symbols, '#', Token::CommandToken(CommandToken::Function)))
68+
// if one of special symbols
69+
p(&mut symbols, '[', Token::CommandToken(CommandToken::ListOpenBracket))
70+
.or_else(|| p(&mut symbols, ']', Token::CommandToken(CommandToken::ListCloseBracket)))
71+
.or_else(|| p(&mut symbols, '(', Token::FunctionOpenBracket))
72+
.or_else(|| p(&mut symbols, ')', Token::FunctionCloseBracket))
73+
// TODO: Change # to CommandContainer symbol
74+
.or_else(|| p(&mut symbols, '#', Token::CommandToken(CommandToken::Function)))
75+
76+
// Or whitespace
7477
.or_else(|| {
7578
if symbols.peek()?.is_whitespace() {return Some(Token::WhiteSpace(symbols.next()?))}
7679
None
7780
})
78-
.or_else(|| parse_alias(&mut symbols))
79-
.unwrap_or_else(|| Token::CommandToken(CommandToken::Command(symbols.next().expect("Unexcpected exception: peek is lier!"))))
81+
// Or one of "complex" tokens
82+
.or_else(|| parse_number(&mut symbols))
83+
.unwrap_or_else(|| parse_command_or_alias(&mut symbols).expect("Internal exception!"))
8084
);
8185
};
8286

@@ -88,11 +92,12 @@ fn parse_special_symbol(iter: &mut Peekable<impl Iterator<Item = char> + Clone>,
8892
return None
8993
}
9094

91-
fn parse_alias<'a>(iter: &mut Peekable<impl Iterator<Item = char> + Clone>) -> Option<Token> {
92-
if !iter.peek()?.is_alphabetic() {return None}
95+
fn parse_command_or_alias<'a>(iter: &mut Peekable<impl Iterator<Item = char> + Clone>) -> Option<Token> {
96+
let p = *iter.peek()?;
97+
if p.is_numeric() || p.is_whitespace() || p == '.' {return None}
9398
let mut result = String::new();
9499
while let Some(symbol) = iter.peek() {
95-
if !symbol.is_alphanumeric() {break;}
100+
if symbol.is_whitespace() {break;}
96101
result.push(*symbol);
97102
iter.next();
98103
};

0 commit comments

Comments
 (0)