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

@ -6,12 +6,15 @@
use crate::printer::pr_str;
use crate::reader::read_str;
use crate::types::MalType;
use crate::types::{MalErr, MalType};
#[allow(non_snake_case)]
/// Read input and generate an ast
fn READ(input: &str) -> MalType {
read_str(input)
fn READ(input: &str) -> Result<MalType, (MalErr, usize)> {
match read_str(input) {
Ok(ast) => Ok(ast),
Err((err, depth)) => Err((format!("Unexpected error during READ: {}", err), depth)),
}
}
#[allow(non_snake_case)]
@ -24,11 +27,11 @@ fn EVAL(ast: MalType) -> MalType {
#[allow(non_snake_case)]
/// Print out the result of the evaluation
fn PRINT(input: MalType) -> String {
pr_str(&input)
pr_str(&input, true)
}
pub fn rep(input: &str) -> String {
let ast = READ(input);
pub fn rep(input: &str) -> Result<String, (MalErr, usize)> {
let ast = READ(input)?;
let out = EVAL(ast);
PRINT(out /*&result*/)
Ok(PRINT(out))
}