closure
This commit is contained in:
+18
-92
@@ -30,109 +30,35 @@ fn main() -> LoxResult<()> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
let (stage, file_path, debug) = parse_args(&args);
|
||||
println!("running {file_path:?}");
|
||||
let mut lox = LoxInterpreter::new(debug);
|
||||
|
||||
let _ = match file_path {
|
||||
let res = match file_path {
|
||||
Some(path) => lox.run_file(&path, stage),
|
||||
None => lox.run_prompt(stage),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
res
|
||||
}
|
||||
|
||||
fn parse_args(args: &[String]) -> (ExecutionStage, Option<String>, bool) {
|
||||
if args.len() == 1 {
|
||||
// Solo il nome del programma: modalità interattiva completa
|
||||
(ExecutionStage::Full, None, false)
|
||||
} else if args.len() == 2 {
|
||||
// Un argomento: potrebbe essere file o flag
|
||||
let arg = &args[1];
|
||||
if arg == "--tokens" || arg == "--ast" || arg == "--full" || arg == "--debug" {
|
||||
// Flag senza file: modalità interattiva
|
||||
let stage = match arg.as_str() {
|
||||
"--tokens" => ExecutionStage::Tokens,
|
||||
"--ast" => ExecutionStage::Ast,
|
||||
"--full" => ExecutionStage::Full,
|
||||
"--debug" => ExecutionStage::Full,
|
||||
_ => ExecutionStage::Full,
|
||||
};
|
||||
let debug = arg == "--debug";
|
||||
(stage, None, debug)
|
||||
} else {
|
||||
// File senza flag: esecuzione completa del file
|
||||
(ExecutionStage::Full, Some(arg.clone()), false)
|
||||
println!("{args:?}");
|
||||
|
||||
let mut stage = ExecutionStage::Full;
|
||||
let mut file_path = None;
|
||||
let mut debug = false;
|
||||
|
||||
for arg in args.iter().skip(1) {
|
||||
match arg.as_str() {
|
||||
"-t" | "--tokens" => stage = ExecutionStage::Tokens,
|
||||
"-a" | "--ast" => stage = ExecutionStage::Ast,
|
||||
"-f" | "--full" => stage = ExecutionStage::Full,
|
||||
"-d" | "--debug" => debug = true,
|
||||
_ => file_path = Some(arg.clone()),
|
||||
}
|
||||
} else if args.len() == 3 {
|
||||
// Due argomenti: potrebbero essere flag + file o due flag
|
||||
let mut debug = false;
|
||||
let mut stage = ExecutionStage::Full;
|
||||
let mut file_path = None;
|
||||
|
||||
for arg in &args[1..3] {
|
||||
if arg == "--debug" {
|
||||
debug = true;
|
||||
} else if arg == "--tokens" || arg == "--ast" || arg == "--full" {
|
||||
stage = match arg.as_str() {
|
||||
"--tokens" => ExecutionStage::Tokens,
|
||||
"--ast" => ExecutionStage::Ast,
|
||||
"--full" => ExecutionStage::Full,
|
||||
_ => ExecutionStage::Full,
|
||||
};
|
||||
} else if !arg.starts_with("--") {
|
||||
file_path = Some(arg.clone());
|
||||
} else {
|
||||
eprintln!(
|
||||
"Unknown flag: {}. Use --tokens, --ast, --full, or --debug",
|
||||
arg
|
||||
);
|
||||
eprintln!(
|
||||
"Usage: {} [--tokens|--ast|--full] [--debug] [script]",
|
||||
args[0]
|
||||
);
|
||||
std::process::exit(64);
|
||||
}
|
||||
}
|
||||
|
||||
(stage, file_path, debug)
|
||||
} else if args.len() == 4 {
|
||||
// Tre argomenti: flag + flag + file
|
||||
let mut debug = false;
|
||||
let mut stage = ExecutionStage::Full;
|
||||
let mut file_path = None;
|
||||
|
||||
for arg in &args[1..4] {
|
||||
if arg == "--debug" {
|
||||
debug = true;
|
||||
} else if arg == "--tokens" || arg == "--ast" || arg == "--full" {
|
||||
stage = match arg.as_str() {
|
||||
"--tokens" => ExecutionStage::Tokens,
|
||||
"--ast" => ExecutionStage::Ast,
|
||||
"--full" => ExecutionStage::Full,
|
||||
_ => ExecutionStage::Full,
|
||||
};
|
||||
} else if !arg.starts_with("--") {
|
||||
file_path = Some(arg.clone());
|
||||
} else {
|
||||
eprintln!(
|
||||
"Unknown flag: {}. Use --tokens, --ast, --full, or --debug",
|
||||
arg
|
||||
);
|
||||
eprintln!(
|
||||
"Usage: {} [--tokens|--ast|--full] [--debug] [script]",
|
||||
args[0]
|
||||
);
|
||||
std::process::exit(64);
|
||||
}
|
||||
}
|
||||
|
||||
(stage, file_path, debug)
|
||||
} else {
|
||||
eprintln!(
|
||||
"Usage: {} [--tokens|--ast|--full] [--debug] [script]",
|
||||
args[0]
|
||||
);
|
||||
std::process::exit(64);
|
||||
}
|
||||
|
||||
(stage, file_path, debug)
|
||||
}
|
||||
|
||||
struct LoxInterpreter {
|
||||
|
||||
Reference in New Issue
Block a user