Skip to content

Commit a7d0318

Browse files
committed
feat: better execution utils + simplification
1 parent b74106e commit a7d0318

5 files changed

Lines changed: 48 additions & 20 deletions

File tree

stack-control-shell/src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
use itertools::Itertools;
22
use prompted::input;
3-
use stack_control::{runtime::stack::Stack, utils::execution::{execute, ExecutionException}};
3+
use stack_control::{bytecode::commands::core::bind_default_commands, compiletime::{compiler::Scope, lexer::split_string_to_tokens}, runtime::stack::Stack, utils::execution::{execute_code, join, simplify, ExecutionException}};
44

55
fn main() {
66
loop {
77
let input = input!("> ");
88
if input == "exit" {break;}
99

10+
let mut scope = Scope::new();
11+
bind_default_commands(&mut scope.command_map);
12+
1013
let mut stack = Stack::new();
1114

12-
match execute(&input, &mut stack) {
15+
let tokens = split_string_to_tokens(&input);
16+
println!("< {}", simplify(&tokens, &scope.command_map).map(|e| e.to_string()).join(""));
17+
18+
19+
match execute_code(&input, &mut stack) {
1320
Err(ExecutionException::Compilation(compile_error)) =>
1421
println!("Compilation error: {:?}", compile_error.to_string()),
1522
Err(ExecutionException::Runtime(runtime_error)) =>

stack-control/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ itertools = "0.14.0"
88
unicode-segmentation = "1.10.1"
99
indoc = "2"
1010
strum = "0.27"
11-
strum_macros = "0.27"
11+
strum_macros = "0.27"

stack-control/src/compiletime/compiler.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{bytecode::commands::{core::{ListGeneratorCommand, StackPusherCommand
66

77
use super::{command_map::{CommandMap}, lexer::{CommandToken, Token}};
88

9-
pub struct CompileTime {
9+
pub struct Scope {
1010
pub command_map: CommandMap,
1111
list_opener: Arc<DescribedCommand>, // StackPusherCommand{value_to_push: Value::OpenListIdentifier}
1212
list_generator: Arc<DescribedCommand>,
@@ -16,7 +16,7 @@ pub struct CompileTime {
1616
pub enum CompilationException {
1717
UnexcpectedEndToken(String),
1818
FunctionTokenRequired,
19-
CommandNotFound(char),
19+
CommandNotFound(String),
2020
AliasNotFound(String),
2121
}
2222

@@ -28,7 +28,7 @@ impl ToString for CompilationException {
2828
CompilationException::AliasNotFound(alias) =>
2929
format!("Alias {alias} not found"),
3030
CompilationException::CommandNotFound(cmd) =>
31-
format!("Command {cmd} not found"),
31+
format!("Command '{cmd}' not found"),
3232
CompilationException::FunctionTokenRequired =>
3333
format!("Function token required"),
3434
CompilationException::UnexcpectedEndToken(t) =>
@@ -37,9 +37,9 @@ impl ToString for CompilationException {
3737
}
3838
}
3939

40-
impl CompileTime {
40+
impl Scope {
4141
pub fn new() -> Self {
42-
CompileTime {
42+
Scope {
4343
command_map: CommandMap::new(),
4444

4545
list_opener: Arc::new(DescribedCommand {
@@ -118,7 +118,7 @@ impl CompileTime {
118118

119119
CommandToken::CommandOrAlias(alias) =>
120120
self.command_map.get(&alias)
121-
.ok_or(CompilationException::CommandNotFound('0'))?,
121+
.ok_or(CompilationException::CommandNotFound(alias.clone()))?,
122122

123123
CommandToken::ListOpenBracket =>
124124
self.list_opener.clone(),

stack-control/src/compiletime/lexer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Splits input string into separate tokens, no other logic involved
66

77
use std::iter::Peekable;
88

9+
#[derive(Clone)]
910
pub enum Token {
1011
FunctionOpenBracket,
1112
FunctionCloseBracket,
@@ -24,6 +25,7 @@ impl ToString for Token {
2425
}
2526
}
2627

28+
#[derive(Clone)]
2729
pub enum CommandToken {
2830
CommandOrAlias(String),
2931
ListOpenBracket,

stack-control/src/utils/execution.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
1-
use crate::{bytecode::commands::{core::bind_default_commands, RuntimeException}, compiletime::{compiler::{CompilationException, CompileTime}, lexer::split_string_to_tokens}, runtime::stack::Stack};
1+
use std::sync::Arc;
2+
3+
use itertools::Itertools;
4+
5+
use crate::{bytecode::commands::{core::bind_default_commands, DescribedCommand, RuntimeException}, compiletime::{command_map::{self, CommandMap}, compiler::{CompilationException, Scope}, lexer::{split_string_to_tokens, CommandToken, Token}}, runtime::stack::Stack};
26

37
pub enum ExecutionException {
48
Runtime(RuntimeException),
59
Compilation(CompilationException)
610
}
711

8-
pub fn execute(code: &str, stack: &mut Stack) -> Result<(), ExecutionException> {
12+
pub fn simplify(tokens: &Vec<Token>, map: &CommandMap) -> impl Iterator<Item = Token> {
13+
tokens.iter().map(|e| match e {
14+
Token::CommandToken(CommandToken::CommandOrAlias(e)) =>
15+
Token::CommandToken(CommandToken::CommandOrAlias(String::from(map.get_by_alias(&e).unwrap_or(e)))),
16+
any => any.clone()
17+
})
18+
}
19+
20+
pub fn join(tokens: impl Iterator<Item = Token>) -> String {
21+
tokens
22+
.map(|e| e.to_string())
23+
.join(", ")
24+
}
25+
26+
pub fn execute_commands(commands: Vec<Arc<DescribedCommand>>, stack: &mut Stack) -> Result<(), RuntimeException> {
27+
for command in commands {
28+
command.execution.execute(stack)?;
29+
}
30+
Ok(())
31+
}
32+
33+
pub fn execute_code(code: &str, stack: &mut Stack) -> Result<(), ExecutionException> {
934
let tokens = split_string_to_tokens(code);
10-
let mut compiletime = CompileTime::new();
35+
let mut compiletime = Scope::new();
1136

1237
bind_default_commands(&mut compiletime.command_map);
1338

1439
match compiletime.compile(tokens) {
15-
Ok(commands) => {
16-
commands
17-
.iter()
18-
.try_for_each(|command| {
19-
command.execution.execute(stack).or_else(|e| Err(ExecutionException::Runtime(e)))
20-
})?;
21-
Ok(())
22-
},
40+
Ok(commands) =>
41+
execute_commands(commands, stack).or_else(|e| Err(ExecutionException::Runtime(e))),
2342

2443
Err(err) => {
2544
Err(ExecutionException::Compilation(err))

0 commit comments

Comments
 (0)