Files
rust-mal/src/printer.rs
teo3300 9d35d24cd4 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
2023-06-07 00:50:06 +02:00

42 lines
1.2 KiB
Rust

use crate::types::escape_str;
use crate::types::MalType;
use crate::types::MalType::*;
pub fn pr_str(ast: &MalType, print_readably: bool) -> String {
match ast {
Nil => "nil".to_string(),
Symbol(sym) | Keyword(sym) => sym.to_string(),
Int(val) => val.to_string(),
Bool(val) => val.to_string(),
Str(str) => {
if print_readably {
escape_str(str)
} else {
str.to_string()
}
}
List(el) => format!(
"({})",
el.iter()
.map(|e| pr_str(e, print_readably))
.collect::<Vec<String>>()
.join(" ")
),
// This is truly horrible
Vector(el) => format!(
"[{}]",
el.iter()
.map(|e| pr_str(e, print_readably))
.collect::<Vec<String>>()
.join(" ")
),
Map(el) => format!(
"{{{}}}",
el.iter()
.map(|sub| vec![sub.0.to_string(), pr_str(sub.1, print_readably)].join(" "))
.collect::<Vec<String>>()
.join(" ")
),
}
}