refactor: Move expression to eval
This commit is contained in:
parent
b9b833bf5c
commit
dfeb5d1b45
2 changed files with 55 additions and 60 deletions
56
src/env.rs
56
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<Rc<Expression>>),
|
||||
HashMap(HashMap<String, Rc<Expression>>),
|
||||
|
||||
NativeFunc {
|
||||
func: fn(args: Vec<Rc<Expression>>) -> Result<Rc<Expression>>,
|
||||
},
|
||||
}
|
||||
|
||||
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<String, Rc<Expression>>;
|
||||
|
||||
|
|
59
src/eval.rs
59
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<Expression> = Rc::new(Expression::Boolean(true));
|
||||
|
@ -14,6 +11,58 @@ thread_local! {
|
|||
static NIL: Rc<Expression> = Rc::new(Expression::Nil);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Expression {
|
||||
// Values
|
||||
Int(i64),
|
||||
Boolean(bool),
|
||||
Keyword(String),
|
||||
String(String),
|
||||
Nil,
|
||||
|
||||
// Collections
|
||||
Vector(Vec<Rc<Expression>>),
|
||||
HashMap(HashMap<String, Rc<Expression>>),
|
||||
|
||||
NativeFunc {
|
||||
func: fn(args: Vec<Rc<Expression>>) -> Result<Rc<Expression>>,
|
||||
},
|
||||
}
|
||||
|
||||
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")]
|
||||
|
|
Loading…
Reference in a new issue