Skip to content

Commit eaafd2b

Browse files
Extract REPL metacommand handling to reduce nesting
Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>
1 parent 514e6c0 commit eaafd2b

1 file changed

Lines changed: 92 additions & 80 deletions

File tree

‎cli/src/commands/repl.rs‎

Lines changed: 92 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -68,86 +68,8 @@ pub fn execute() -> ExitCode {
6868
// Process REPL Metacommand
6969
let parts: Vec<&str> = trimmed.split_whitespace().collect();
7070
let cmd = parts[0];
71-
match cmd {
72-
":quit" | ":q" => {
73-
println!("Goodbye!");
74-
break;
75-
}
76-
":help" | ":h" => {
77-
print_help();
78-
}
79-
":clear" | ":c" => {
80-
interpreter = techscript_interpreter::Interpreter::new();
81-
println!("REPL environment state cleared.");
82-
}
83-
":history" => {
84-
for (i, h) in session_history.iter().enumerate() {
85-
println!("{:>4}: {}", i + 1, h);
86-
}
87-
}
88-
":type" => {
89-
if parts.len() < 2 {
90-
println!("Usage: :type <expr>");
91-
} else {
92-
let expr = parts[1..].join(" ");
93-
evaluate_type(&expr, &mut interpreter);
94-
}
95-
}
96-
":ast" => {
97-
if parts.len() < 2 {
98-
println!("Usage: :ast <expr>");
99-
} else {
100-
let expr = parts[1..].join(" ");
101-
dump_repl_ast(&expr);
102-
}
103-
}
104-
":ir" => {
105-
if parts.len() < 2 {
106-
println!("Usage: :ir <expr>");
107-
} else {
108-
let expr = parts[1..].join(" ");
109-
dump_repl_ir(&expr);
110-
}
111-
}
112-
":bytecode" => {
113-
if parts.len() < 2 {
114-
println!("Usage: :bytecode <expr>");
115-
} else {
116-
let expr = parts[1..].join(" ");
117-
dump_repl_bytecode(&expr);
118-
}
119-
}
120-
":load" => {
121-
if parts.len() < 2 {
122-
println!("Usage: :load <file>");
123-
} else {
124-
let file = parts[1];
125-
if let Ok(content) = std::fs::read_to_string(file) {
126-
eval_code(&content, &mut interpreter);
127-
} else {
128-
println!("Error: Could not read file '{}'.", file);
129-
}
130-
}
131-
}
132-
":save" => {
133-
if parts.len() < 2 {
134-
println!("Usage: :save <file>");
135-
} else {
136-
let file = parts[1];
137-
let session_data = session_history.join("\n");
138-
if std::fs::write(file, session_data).is_ok() {
139-
println!("Session history saved to '{}'.", file);
140-
} else {
141-
println!("Error: Could not write to file '{}'.", file);
142-
}
143-
}
144-
}
145-
other => {
146-
println!(
147-
"Unknown command '{}'. Type ':help' for instructions.",
148-
other
149-
);
150-
}
71+
if handle_metacommand(cmd, &parts, &mut interpreter, &session_history) {
72+
break;
15173
}
15274
} else {
15375
// Regular statement/expression evaluation
@@ -172,6 +94,96 @@ pub fn execute() -> ExitCode {
17294
ExitCode::Success
17395
}
17496

97+
fn handle_metacommand(
98+
cmd: &str,
99+
parts: &[&str],
100+
interpreter: &mut techscript_interpreter::Interpreter,
101+
session_history: &[String],
102+
) -> bool {
103+
match cmd {
104+
":quit" | ":q" => {
105+
println!("Goodbye!");
106+
return true;
107+
}
108+
":help" | ":h" => {
109+
print_help();
110+
}
111+
":clear" | ":c" => {
112+
*interpreter = techscript_interpreter::Interpreter::new();
113+
println!("REPL environment state cleared.");
114+
}
115+
":history" => {
116+
for (i, h) in session_history.iter().enumerate() {
117+
println!("{:>4}: {}", i + 1, h);
118+
}
119+
}
120+
":type" => {
121+
if parts.len() < 2 {
122+
println!("Usage: :type <expr>");
123+
} else {
124+
let expr = parts[1..].join(" ");
125+
evaluate_type(&expr, interpreter);
126+
}
127+
}
128+
":ast" => {
129+
if parts.len() < 2 {
130+
println!("Usage: :ast <expr>");
131+
} else {
132+
let expr = parts[1..].join(" ");
133+
dump_repl_ast(&expr);
134+
}
135+
}
136+
":ir" => {
137+
if parts.len() < 2 {
138+
println!("Usage: :ir <expr>");
139+
} else {
140+
let expr = parts[1..].join(" ");
141+
dump_repl_ir(&expr);
142+
}
143+
}
144+
":bytecode" => {
145+
if parts.len() < 2 {
146+
println!("Usage: :bytecode <expr>");
147+
} else {
148+
let expr = parts[1..].join(" ");
149+
dump_repl_bytecode(&expr);
150+
}
151+
}
152+
":load" => {
153+
if parts.len() < 2 {
154+
println!("Usage: :load <file>");
155+
} else {
156+
let file = parts[1];
157+
if let Ok(content) = std::fs::read_to_string(file) {
158+
eval_code(&content, interpreter);
159+
} else {
160+
println!("Error: Could not read file '{}'.", file);
161+
}
162+
}
163+
}
164+
":save" => {
165+
if parts.len() < 2 {
166+
println!("Usage: :save <file>");
167+
} else {
168+
let file = parts[1];
169+
let session_data = session_history.join("\n");
170+
if std::fs::write(file, session_data).is_ok() {
171+
println!("Session history saved to '{}'.", file);
172+
} else {
173+
println!("Error: Could not write to file '{}'.", file);
174+
}
175+
}
176+
}
177+
other => {
178+
println!(
179+
"Unknown command '{}'. Type ':help' for instructions.",
180+
other
181+
);
182+
}
183+
}
184+
false
185+
}
186+
175187
fn print_help() {
176188
println!("TechScript REPL Shell Metacommands:");
177189
println!(" :help, :h Show this help text");

0 commit comments

Comments
 (0)