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:
teo3300
2023-06-07 00:50:06 +02:00
parent 703b6888b5
commit 9d35d24cd4
5 changed files with 213 additions and 103 deletions

View File

@ -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;
}
}
}