starting to add an env so symbols can be found and functions run

This commit is contained in:
Chris Cochrun 2024-11-06 10:45:33 -06:00
parent f7f90cd78f
commit 9b252d9a6a
3 changed files with 69 additions and 35 deletions

View file

@ -1,4 +1,5 @@
use std::num::ParseIntError;
use color_eyre::{eyre::eyre, Result};
use std::{num::ParseIntError, rc::Rc};
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Expression {
@ -15,6 +16,8 @@ impl From<&str> for Expression {
}
}
pub type LispArgs = Vec<Value>;
#[derive(Default, Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub enum Value {
List(Vec<Value>),
@ -22,7 +25,7 @@ pub enum Value {
Number(i64),
#[default]
Nil,
Function,
Function(fn(LispArgs) -> Result<Value>, Rc<Value>),
True,
Symbol(Symbol),
Keyword(Keyword),
@ -35,6 +38,17 @@ impl Value {
_ => Self::Nil,
}
}
pub fn func(f: fn(LispArgs) -> Result<Value>) -> Self {
Self::Function(f, Rc::new(Self::Nil))
}
pub fn apply(&self, args: LispArgs) -> Result<Value> {
match self {
Self::Function(f, _) => f(args),
_ => Err(eyre!("No function here, shouldn't call apply")),
}
}
}
impl From<Vec<Value>> for Value {
@ -54,10 +68,6 @@ impl From<&str> for Value {
Value::True
} else if s.starts_with(r#"""#) {
Value::String(s.replace('"', ""))
} else if s == "fn".to_owned() {
Value::Function
} else if s == "defun".to_owned() {
Value::Function
} else if s == "nil".to_owned() {
Value::Nil
} else {