mirror of
https://github.com/teo3300/rust-mal.git
synced 2026-01-13 01:35:31 +01:00
Error handling and small editor
- Error handling to signal what is preventing evaluation instead of
program crash
- Small editor feature (allow edit new
line of code to complete the previous ones instead of destroying the
input, if an empty line inserted return the error preventing evaluation
- Auto indentation on multiline input based on depth
Still does not parse multi-statement code
- This is a problem when dealing with macros: does not allow
expressions like `'()` since the atomic `'` hides the list ().
Need to chose between:
- Replace `'...` with `(quote ... )` during tokenization (may be
hard to implement macros later)
- Allows multi-statement code (this also allows to execute multiple
statements when reading a file)
Will probably delete auto-indentation since it breaks code's uniformity
too much
This commit is contained in:
29
src/main.rs
29
src/main.rs
@ -15,9 +15,34 @@ fn main() -> io::Result<()> {
|
||||
let _ = io::stdout().flush();
|
||||
|
||||
let mut input = String::new();
|
||||
loop {
|
||||
// Read line to compose program inpug
|
||||
let mut line = String::new();
|
||||
io::stdin().read_line(&mut line)?;
|
||||
|
||||
io::stdin().read_line(&mut input)?;
|
||||
// Append line to input
|
||||
input.push_str(&line);
|
||||
|
||||
println!("{}", rep(&input.replace("\n", " ")));
|
||||
// If there is nothing to evaluate skip rep
|
||||
if input == "\n" {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Perform rep on whole available input
|
||||
match rep(&input) {
|
||||
Ok(output) => println!("{}", output),
|
||||
Err((err, depth)) => {
|
||||
if line == "\n" {
|
||||
println!("ERROR: {}", err);
|
||||
} else {
|
||||
print!("user> {}", " ".repeat(depth));
|
||||
// Flush the prompt to appear before command
|
||||
let _ = io::stdout().flush();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user