Step 1: Create an AST

This step consists of creating the AST to represent the input
expression, the only evaluation done for now is the recognition of three
MalTypes for the nodes of the AST:
- Symbols: atomic isolated groups of characters
- Integers: Symbols that can be parsed as number and are treated as so
- Lists: recognizable by the presence of parentheses (only "()" for now,
  "[]" and "{}" later), these can contain any number of MalTypes

The second half of this step (much easier) is to reconstruct the
original syntax (with clean whitespaces) to check the correctness of the
process

Signed-off-by: teo3300 <matteo.rogora@live.it>
This commit is contained in:
teo3300
2023-06-05 23:04:51 +02:00
parent 9144fc04bc
commit 13790d0864
5 changed files with 194 additions and 3 deletions

15
src/printer.rs Normal file
View File

@ -0,0 +1,15 @@
use crate::types::MalType;
pub fn pr_str(ast: &MalType) -> String {
match ast {
MalType::Symbol(sym) => sym.to_string(),
MalType::Integer(val) => val.to_string(),
MalType::List(el) => format!(
"({})",
el.iter()
.map(|sub| pr_str(sub))
.collect::<Vec<String>>()
.join(" ")
),
}
}