mirror of
https://github.com/teo3300/rust-mal.git
synced 2026-01-12 17:25:33 +01:00
Eval cycle implemented
Yet to be implement: - Environment - Function calls Signed-off-by: teo3300 <matteo.rogora@live.it>
This commit is contained in:
41
src/step2_eval.rs
Normal file
41
src/step2_eval.rs
Normal file
@ -0,0 +1,41 @@
|
||||
// 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::envs::Env;
|
||||
use crate::mal_core::eval;
|
||||
use crate::printer::pr_str;
|
||||
use crate::reader::read_str;
|
||||
use crate::types::{MalRet, MalType};
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
/// Read input and generate an ast
|
||||
fn READ(input: &str) -> MalRet {
|
||||
match read_str(input) {
|
||||
Ok(ast) => Ok(ast),
|
||||
Err(err) => Err(format!("@ READ: {}", err)),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
/// Evaluate the generated ast
|
||||
fn EVAL(ast: MalType, env: &Env) -> MalRet {
|
||||
match eval(ast, env) {
|
||||
Ok(ast) => Ok(ast),
|
||||
Err(err) => Err(format!("@ EVAL: {}", err)),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
/// Print out the result of the evaluation
|
||||
fn PRINT(output: MalType) -> String {
|
||||
pr_str(&output, true)
|
||||
}
|
||||
|
||||
pub fn rep(input: &str, env: &Env) -> Result<String, String> {
|
||||
let ast = READ(input)?;
|
||||
let out = EVAL(ast, env)?;
|
||||
Ok(PRINT(out))
|
||||
}
|
||||
Reference in New Issue
Block a user