Skip to content

Commit

Permalink
refactor: move Value under types
Browse files Browse the repository at this point in the history
Signed-off-by: Prajwal S N <[email protected]>
  • Loading branch information
snprajwal committed Sep 18, 2024
1 parent ed60c3f commit cdff580
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 68 deletions.
31 changes: 2 additions & 29 deletions lost_compile/src/environment.rs
Original file line number Diff line number Diff line change
@@ -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<String, Value>,
Expand Down
2 changes: 1 addition & 1 deletion lost_compile/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Display;

use crate::environment::Value;
use crate::types::Value;

#[derive(Debug)]
pub enum Exception {
Expand Down
24 changes: 4 additions & 20 deletions lost_compile/src/interpret.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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};
Expand Down
11 changes: 4 additions & 7 deletions lost_compile/src/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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| {
Expand All @@ -28,7 +25,7 @@ pub fn init(env: &mut Env) {
),
(
"clock",
NativeFunc(types::NativeFunc {
Value::NativeFunc(NativeFunc {
name: "clock".to_string(),
args: vec![],
body: |_, _| {
Expand Down
47 changes: 46 additions & 1 deletion lost_compile/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value, Exception>;
Expand Down
5 changes: 1 addition & 4 deletions lost_playground/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
9 changes: 3 additions & 6 deletions lost_playground/src/stdlib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use lost_compile::{
environment::{
Env,
Value::{self, NativeFunc},
},
types,
environment::Env,
types::{NativeFunc, Value},
};

use crate::REPL_OUTPUT_VAR;
Expand All @@ -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| {
Expand Down

0 comments on commit cdff580

Please sign in to comment.