pull interpreter value out into its own file

This commit is contained in:
Sebastian Hugentobler 2025-02-11 14:09:52 +01:00
parent 6440819ff2
commit 15b331f447
3 changed files with 48 additions and 30 deletions

View file

@ -3,6 +3,7 @@ use thiserror::Error;
use crate::{
expression::Expression,
token::{Literal, Token, TokenType},
value::Value,
};
#[derive(Error, Debug)]
@ -15,35 +16,6 @@ pub enum InterpreterError {
BinaryOperatorUnknown(usize, String),
}
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
String(String),
Number(f64),
Boolean(bool),
Nil,
}
impl Value {
fn is_truthy(&self) -> bool {
match self {
Value::Nil => false,
Value::Boolean(b) => *b,
_ => true,
}
}
}
impl From<Literal> for Value {
fn from(value: Literal) -> Self {
match value {
Literal::String(x) => Value::String(x),
Literal::Number(x) => Value::Number(x),
Literal::Boolean(x) => Value::Boolean(x),
Literal::Nil => Value::Nil,
}
}
}
/// Try to evaluate an expression and return its result.
pub fn evaluate(expression: Expression) -> Result<Value, InterpreterError> {
match expression {