From 6c1e53ade43ba3500331f0fa585269f9fac6dae9 Mon Sep 17 00:00:00 2001 From: Caznix Date: Thu, 5 Dec 2024 17:05:20 -0500 Subject: [PATCH] make repl support strings via both double and single quotes --- engine/src/core/repl/repl.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/engine/src/core/repl/repl.rs b/engine/src/core/repl/repl.rs index 21c19a3..0798286 100644 --- a/engine/src/core/repl/repl.rs +++ b/engine/src/core/repl/repl.rs @@ -111,6 +111,32 @@ fn register_commands() { COMMAND_LIST.add_alias("clear".to_string(), "cls".to_string()); } +fn tokenize(command: &str) -> Vec { + let mut tokens = Vec::new(); + let mut current_token = String::new(); + let mut inside_string = false; + + for char in command.chars() { + if char == '"' || char == '\'' { + inside_string = !inside_string; + } else if char.is_whitespace() && !inside_string { + if !current_token.is_empty() { + tokens.push(current_token); + current_token = String::new(); + } + } else { + current_token.push(char); + } + } + + // ignore the last token if it's empty. Who are we. Mojang? - Caz + if !current_token.is_empty() { + tokens.push(current_token); + } + + tokens +} + fn evaluate_command(input: &str) { if input.trim().is_empty() { return; @@ -126,13 +152,13 @@ fn evaluate_command(input: &str) { continue; } - let tokens: Vec<&str> = command.split_whitespace().collect(); + let tokens = tokenize(command); if tokens.is_empty() { - return; + println!("Empty command, skipping."); + continue; } - - let cmd_name = tokens[0]; - let args: Vec = tokens[1..].iter().map(|&s| s.to_string()).collect(); + let cmd_name = &tokens[0]; + let args: Vec = tokens[1..].iter().map(|s| s.to_string()).collect(); COMMAND_LIST.execute_command( cmd_name.to_string(),