diff --git a/src/env.rs b/src/env.rs index 10ac386..53ae9d1 100644 --- a/src/env.rs +++ b/src/env.rs @@ -1,60 +1,6 @@ use std::{borrow::Borrow, collections::HashMap, rc::Rc}; -use anyhow::Result; - -use crate::eval::Error; - -#[derive(Debug, PartialEq)] -pub enum Expression { - // Values - Int(i64), - Boolean(bool), - Keyword(String), - String(String), - Nil, - - // Collections - Vector(Vec>), - HashMap(HashMap>), - - NativeFunc { - func: fn(args: Vec>) -> Result>, - }, -} - -impl std::fmt::Display for Expression { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Expression::Int(val) => write!(f, "{}", val), - Expression::Boolean(true) => write!(f, "true"), - Expression::Boolean(false) => write!(f, "false"), - Expression::Keyword(val) => write!(f, ":{}", val), - Expression::String(val) => write!(f, "{}", val), - Expression::Nil => write!(f, "nil"), - - Expression::Vector(vec) => { - let s = vec - .iter() - .map(|elem| elem.to_string()) - .reduce(|lhs, rhs| format!("{lhs} {rhs}")) - .unwrap_or_default(); - - write!(f, "[{s}]") - } - Expression::HashMap(map) => { - let res = map - .into_iter() - .map(|(k, v)| format!("{k}: {v}")) - .reduce(|lhs, rhs| format!("{lhs}, {rhs}")) - .unwrap_or_default(); - - write!(f, "{{{res}}}") - } - - Expression::NativeFunc { .. } => write!(f, "function"), - } - } -} +use crate::eval::{Error, Expression}; pub type Environment = HashMap>; diff --git a/src/eval.rs b/src/eval.rs index 2256572..04a53b7 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -1,12 +1,9 @@ -use std::{borrow::Borrow, rc::Rc}; +use std::{borrow::Borrow, collections::HashMap, rc::Rc}; use anyhow::Result; use thiserror::Error; -use crate::{ - env::{Environment, Expression}, - parser::Node, -}; +use crate::{env::Environment, parser::Node}; thread_local! { static TRUE: Rc = Rc::new(Expression::Boolean(true)); @@ -14,6 +11,58 @@ thread_local! { static NIL: Rc = Rc::new(Expression::Nil); } +#[derive(Debug, PartialEq)] +pub enum Expression { + // Values + Int(i64), + Boolean(bool), + Keyword(String), + String(String), + Nil, + + // Collections + Vector(Vec>), + HashMap(HashMap>), + + NativeFunc { + func: fn(args: Vec>) -> Result>, + }, +} + +impl std::fmt::Display for Expression { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Expression::Int(val) => write!(f, "{}", val), + Expression::Boolean(true) => write!(f, "true"), + Expression::Boolean(false) => write!(f, "false"), + Expression::Keyword(val) => write!(f, ":{}", val), + Expression::String(val) => write!(f, "{}", val), + Expression::Nil => write!(f, "nil"), + + Expression::Vector(vec) => { + let s = vec + .iter() + .map(|elem| elem.to_string()) + .reduce(|lhs, rhs| format!("{lhs} {rhs}")) + .unwrap_or_default(); + + write!(f, "[{s}]") + } + Expression::HashMap(map) => { + let res = map + .into_iter() + .map(|(k, v)| format!("{k}: {v}")) + .reduce(|lhs, rhs| format!("{lhs}, {rhs}")) + .unwrap_or_default(); + + write!(f, "{{{res}}}") + } + + Expression::NativeFunc { .. } => write!(f, "function"), + } + } +} + #[derive(Debug, Error)] pub enum Error { #[error("could not find symbol in environment")]