mirror of
https://github.com/teo3300/rust-mal.git
synced 2026-01-12 17:25:33 +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:
33
src/step1_read_print.rs
Normal file
33
src/step1_read_print.rs
Normal file
@ -0,0 +1,33 @@
|
||||
// Structure the main functions of the interpreter
|
||||
//
|
||||
// For now just act as an echo, note that each function should not modify the
|
||||
// input, thus this can be referenced by the previous step without the need
|
||||
// to allocate more memory
|
||||
|
||||
use crate::printer::pr_str;
|
||||
use crate::reader::read_str;
|
||||
use crate::types::MalType;
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
/// Read input and generate an ast
|
||||
fn READ(input: &str) -> MalType {
|
||||
read_str(input)
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
/// Evaluate the generated ast
|
||||
fn EVAL(ast: MalType) -> MalType {
|
||||
ast
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
/// Print out the result of the evaluation
|
||||
fn PRINT(input: MalType) -> String {
|
||||
pr_str(&input)
|
||||
}
|
||||
|
||||
pub fn rep(input: &str) -> String {
|
||||
let ast = READ(input);
|
||||
let out = EVAL(ast);
|
||||
PRINT(out /*&result*/)
|
||||
}
|
||||
Reference in New Issue
Block a user