diff --git a/lost_compile/src/environment.rs b/lost_compile/src/environment.rs index a3de997..c508f5a 100644 --- a/lost_compile/src/environment.rs +++ b/lost_compile/src/environment.rs @@ -1,40 +1,13 @@ -use std::{cell::RefCell, collections::HashMap, fmt::Display, rc::Rc}; +use std::{cell::RefCell, collections::HashMap, rc::Rc}; use log::debug; use crate::{ error::{runtime_error, ErrorMsg, Exception}, stdlib, - types::{Class, Func, Instance, NativeFunc}, + types::Value, }; -#[derive(Clone, Debug, PartialEq)] -pub enum Value { - Boolean(bool), - Number(f64), - Str(String), - Func(Func), - NativeFunc(NativeFunc), - Class(Class), - Instance(Instance), - Null, -} - -impl Display for Value { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(&match self { - Self::Boolean(b) => b.to_string(), - Self::Number(n) => n.to_string(), - Self::Str(s) => s.clone(), - Self::Func(f) => f.to_string(), - Self::NativeFunc(f) => f.to_string(), - Self::Class(c) => c.to_string(), - Self::Instance(i) => i.to_string(), - Self::Null => "null".to_string(), - }) - } -} - #[derive(Debug, Default, Clone, PartialEq)] pub struct Env { values: HashMap, diff --git a/lost_compile/src/error.rs b/lost_compile/src/error.rs index e216507..84be18b 100644 --- a/lost_compile/src/error.rs +++ b/lost_compile/src/error.rs @@ -1,6 +1,6 @@ use std::fmt::Display; -use crate::environment::Value; +use crate::types::Value; #[derive(Debug)] pub enum Exception { diff --git a/lost_compile/src/interpret.rs b/lost_compile/src/interpret.rs index a0f2421..458c386 100644 --- a/lost_compile/src/interpret.rs +++ b/lost_compile/src/interpret.rs @@ -1,9 +1,9 @@ use std::{cell::RefCell, cmp::Ordering, collections::HashMap, rc::Rc}; use crate::{ - environment::{Env, Value}, + environment::Env, error::{runtime_error, ErrorMsg, Exception}, - types::{Callable, Class, Func}, + types::{to_bool, Callable, Class, Func, Value}, }; use lost_syntax::ast::{BinOp, Expr, Ident, Item, Literal, LogicalOp, Source, UnaryOp}; @@ -300,10 +300,10 @@ impl Interpreter { // Handle == and != if op == &BinOp::EqualEqual { - return Ok(Value::Boolean(is_eq(&left, &right))); + return Ok(Value::Boolean(left == right)); } if op == &BinOp::BangEqual { - return Ok(Value::Boolean(!(is_eq(&left, &right)))); + return Ok(Value::Boolean(left != right)); } // All other comparisons can be performed only on numbers. @@ -436,22 +436,6 @@ impl Interpreter { } } -fn to_bool(v: &Value) -> bool { - match v { - Value::Null => false, - Value::Boolean(b) => *b, - _ => true, - } -} - -fn is_eq(left: &Value, right: &Value) -> bool { - match (left, right) { - (Value::Number(m), Value::Number(n)) => m == n, - (Value::Str(m), Value::Str(n)) => m == n, - _ => to_bool(left) == to_bool(right), - } -} - #[cfg(test)] mod tests { use lost_syntax::{ast::Ident, token::Location}; diff --git a/lost_compile/src/stdlib.rs b/lost_compile/src/stdlib.rs index 9334fb8..3b120bd 100644 --- a/lost_compile/src/stdlib.rs +++ b/lost_compile/src/stdlib.rs @@ -4,11 +4,8 @@ use std::{ }; use crate::{ - environment::{ - Env, - Value::{self, NativeFunc}, - }, - types, + environment::Env, + types::{NativeFunc, Value}, }; /// Initialises the environment with stdlib functions @@ -17,7 +14,7 @@ pub fn init(env: &mut Env) { let fns: [(&str, Value); 2] = [ ( "print", - NativeFunc(types::NativeFunc { + Value::NativeFunc(NativeFunc { name: "print".to_string(), args: vec!["arg".to_string()], body: |_, args| { @@ -28,7 +25,7 @@ pub fn init(env: &mut Env) { ), ( "clock", - NativeFunc(types::NativeFunc { + Value::NativeFunc(NativeFunc { name: "clock".to_string(), args: vec![], body: |_, _| { diff --git a/lost_compile/src/types.rs b/lost_compile/src/types.rs index aa9f200..9ff4da0 100644 --- a/lost_compile/src/types.rs +++ b/lost_compile/src/types.rs @@ -8,11 +8,56 @@ use std::{ use lost_syntax::ast::Item; use crate::{ - environment::{Env, Value}, + environment::Env, error::{runtime_error, ErrorMsg, Exception}, interpret::Interpreter, }; +#[derive(Clone, Debug)] +pub enum Value { + Boolean(bool), + Number(f64), + Str(String), + Func(Func), + NativeFunc(NativeFunc), + Class(Class), + Instance(Instance), + Null, +} + +impl Display for Value { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&match self { + Self::Boolean(b) => b.to_string(), + Self::Number(n) => n.to_string(), + Self::Str(s) => s.clone(), + Self::Func(f) => f.to_string(), + Self::NativeFunc(f) => f.to_string(), + Self::Class(c) => c.to_string(), + Self::Instance(i) => i.to_string(), + Self::Null => "null".to_string(), + }) + } +} + +impl PartialEq for Value { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Value::Number(m), Value::Number(n)) => m == n, + (Value::Str(m), Value::Str(n)) => m == n, + _ => to_bool(self) == to_bool(other), + } + } +} + +pub fn to_bool(v: &Value) -> bool { + match v { + Value::Null => false, + Value::Boolean(b) => *b, + _ => true, + } +} + pub trait Callable { fn arity(&self) -> usize; fn call(&self, interpreter: &mut Interpreter, args: &[Value]) -> Result; diff --git a/lost_playground/src/lib.rs b/lost_playground/src/lib.rs index 3d89bc3..4bba1f4 100644 --- a/lost_playground/src/lib.rs +++ b/lost_playground/src/lib.rs @@ -2,10 +2,7 @@ mod stdlib; use crate::stdlib::init; use lost_compile::{ - environment::{Env, Value}, - interpret::Interpreter, - resolve::Resolver, - run, + environment::Env, interpret::Interpreter, resolve::Resolver, run, types::Value, }; use std::env; use wasm_bindgen::prelude::*; diff --git a/lost_playground/src/stdlib.rs b/lost_playground/src/stdlib.rs index 2700da5..0d58908 100644 --- a/lost_playground/src/stdlib.rs +++ b/lost_playground/src/stdlib.rs @@ -1,9 +1,6 @@ use lost_compile::{ - environment::{ - Env, - Value::{self, NativeFunc}, - }, - types, + environment::Env, + types::{NativeFunc, Value}, }; use crate::REPL_OUTPUT_VAR; @@ -14,7 +11,7 @@ pub fn init(env: &mut Env) { // print(args) env.set( "print", - NativeFunc(types::NativeFunc { + Value::NativeFunc(NativeFunc { name: "print".to_string(), args: vec!["arg".to_string()], body: |interpreter, args| {