mirror of
https://github.com/teo3300/rust-mal.git
synced 2026-01-13 01:35:31 +01:00
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:
15
src/printer.rs
Normal file
15
src/printer.rs
Normal 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(" ")
|
||||
),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user