Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Planner improvements #40

Merged
merged 8 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lykiadb-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ tokio = { version = "~1.35.1", features = [
tokio-stream = { version = "~0.1.6", features = ["net"] }
tracing = "0.1"
tracing-subscriber = "0.3"
pretty_assertions = "1.4.1"

[dev-dependencies]
criterion = { version = "0.4", features = ["html_reports"] }
test_each_file = "0.3.4"

[[bench]]
name = "interpreter"
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-server/src/comm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::engine::{Runtime, RuntimeMode};
use crate::value::types::RV;
use crate::value::RV;
use ::std::time::Instant;
use tcp::TcpConnection;
use tokio::net::TcpStream;
Expand Down
6 changes: 3 additions & 3 deletions lykiadb-server/src/engine/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::plan::planner::Planner;
use crate::util::{alloc_shared, Shared};
use crate::value::callable::{Callable, CallableKind, Function, Stateful};
use crate::value::environment::{EnvId, Environment};
use crate::value::types::{eval_binary, RV};
use crate::value::{RV, eval::eval_binary};

use std::sync::Arc;
use std::vec;
Expand Down Expand Up @@ -706,7 +706,7 @@ impl Stateful for Output {
pub mod test_helpers {
use crate::engine::{Runtime, RuntimeMode};
use crate::util::{alloc_shared, Shared};
use crate::value::types::RV;
use crate::value::RV;

use super::Output;

Expand All @@ -725,7 +725,7 @@ pub mod test_helpers {

#[cfg(test)]
mod test {
use crate::value::types::RV;
use crate::value::RV;

use super::test_helpers::get_runtime;

Expand Down
2 changes: 1 addition & 1 deletion lykiadb-server/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use self::stdlib::stdlib;
use crate::engine::interpreter::Interpreter;
use crate::util::{alloc_shared, Shared};
use crate::value::environment::Environment;
use crate::value::types::RV;
use crate::value::RV;
use lykiadb_lang::parser::Parser;
use lykiadb_lang::tokenizer::scanner::Scanner;
use serde_json::Value;
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-server/src/engine/stdlib/fib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
engine::interpreter::{HaltReason, InterpretError, Interpreter},
value::types::RV,
value::RV,
};

fn _calculate(n: f64) -> f64 {
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-server/src/engine/stdlib/json.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
engine::interpreter::{HaltReason, InterpretError, Interpreter},
value::types::RV,
value::RV,
};
use serde_json::json;
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-server/src/engine/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc_hash::FxHashMap;

use crate::{
util::{alloc_shared, Shared},
value::{callable::{Callable, CallableKind, Function}, types::RV},
value::{callable::{Callable, CallableKind, Function}, RV},
};

use self::{
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-server/src/engine/stdlib/out.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
engine::interpreter::{HaltReason, Interpreter},
value::types::RV,
value::RV,
};

pub fn nt_print(_interpreter: &mut Interpreter, args: &[RV]) -> Result<RV, HaltReason> {
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-server/src/engine/stdlib/time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::engine::interpreter::{HaltReason, Interpreter};
use crate::value::types::RV;
use crate::value::RV;
use std::time;

pub fn nt_clock(_interpreter: &mut Interpreter, _args: &[RV]) -> Result<RV, HaltReason> {
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-server/src/value/callable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::{Debug, Display, Formatter};
use lykiadb_lang::ast::stmt::Stmt;
use crate::{engine::interpreter::{HaltReason, Interpreter}, util::Shared};
use super::environment::EnvId;
use super::types::RV;
use super::RV;

#[derive(Debug, Clone)]
pub enum CallableKind {
Expand Down
4 changes: 2 additions & 2 deletions lykiadb-server/src/value/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::borrow::{Borrow, BorrowMut};

use super::types::RV;
use super::RV;

#[repr(transparent)]
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Environment {

#[cfg(test)]
mod test {
use crate::value::types::RV;
use crate::value::RV;

#[test]
fn test_read_basic() {
Expand Down
157 changes: 3 additions & 154 deletions lykiadb-server/src/value/types.rs → lykiadb-server/src/value/eval.rs
Original file line number Diff line number Diff line change
@@ -1,158 +1,7 @@
use lykiadb_lang::ast::expr::Operation;
use rustc_hash::FxHashMap;
use serde::ser::{SerializeMap, SerializeSeq};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::ops;
use std::sync::{Arc, RwLock};

use crate::util::{alloc_shared, Shared};

use super::callable::Callable;

#[derive(Debug, Clone)]
pub enum RV {
Str(Arc<String>),
Num(f64),
Bool(bool),
Object(Shared<FxHashMap<String, RV>>),
Array(Shared<Vec<RV>>),
Callable(Callable),
Undefined,
NaN,
Null,
}

impl Serialize for RV {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
RV::Str(s) => serializer.serialize_str(s),
RV::Num(n) => serializer.serialize_f64(*n),
RV::Bool(b) => serializer.serialize_bool(*b),
RV::Undefined => serializer.serialize_none(),
RV::NaN => serializer.serialize_none(),
RV::Null => serializer.serialize_none(),
RV::Array(arr) => {
let mut seq = serializer.serialize_seq(None).unwrap();
let arr = (arr as &RwLock<Vec<RV>>).read().unwrap();
for item in arr.iter() {
seq.serialize_element(&item)?;
}
seq.end()
}
RV::Object(obj) => {
let mut map = serializer.serialize_map(None).unwrap();
let arr = (obj as &RwLock<FxHashMap<String, RV>>)
.read()
.unwrap();
for (key, value) in arr.iter() {
map.serialize_entry(key, value)?;
}
map.end()
},
_ => serializer.serialize_none(),
}
}
}

impl<'de> Deserialize<'de> for RV {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = serde_json::Value::deserialize(deserializer)?;
match value {
serde_json::Value::String(s) => Ok(RV::Str(Arc::new(s))),
serde_json::Value::Number(n) => Ok(RV::Num(n.as_f64().unwrap())),
serde_json::Value::Bool(b) => Ok(RV::Bool(b)),
serde_json::Value::Array(arr) => {
let mut vec = Vec::new();
for item in arr {
vec.push(serde_json::from_value(item).unwrap());
}
Ok(RV::Array(alloc_shared(vec)))
}
serde_json::Value::Object(obj) => {
let mut map = FxHashMap::default();
for (key, value) in obj {
map.insert(key, serde_json::from_value(value).unwrap());
}
Ok(RV::Object(alloc_shared(map)))
}
serde_json::Value::Null => Ok(RV::Null),
}
}
}

impl RV {
pub fn as_bool(&self) -> bool {
match &self {
RV::Num(value) => !value.is_nan() && value.abs() > 0.0,
RV::Str(value) => !value.is_empty(),
RV::Bool(value) => *value,
RV::Null | RV::Undefined | RV::NaN => false,
_ => true,
}
}

pub fn as_number(&self) -> Option<f64> {
match self {
RV::Num(value) => Some(*value),
RV::Bool(true) => Some(1.0),
RV::Bool(false) => Some(0.0),
RV::Str(s) => {
if let Ok(num) = s.parse::<f64>() {
Some(num)
} else {
None
}
}
_ => None,
}
}

pub fn eq_any_bool(&self, b: bool) -> bool {
self.as_bool() == b
}

pub fn eq_str_num(&self, n: f64) -> bool {
if let RV::Str(s) = self {
if let Ok(num) = s.parse::<f64>() {
return num == n;
}
}
false
}

pub fn partial_cmp_str_bool(&self, other: bool) -> Option<std::cmp::Ordering> {
if let Some(num) = self.as_number() {
return num.partial_cmp(&if other { 1.0 } else { 0.0 });
}
self.as_bool().partial_cmp(&other)
}

pub fn is_in(&self, other: &RV) -> RV {
match (self, other) {
(RV::Str(lhs), RV::Str(rhs)) => {
RV::Bool(rhs.contains(lhs.as_str()))
}
(lhs, RV::Array(rhs)) => {
RV::Bool(rhs.read().unwrap().contains(&lhs))
}
(RV::Str(key), RV::Object(map)) => {
RV::Bool(map.read().unwrap().contains_key(key.as_str()))
},
_ => RV::Bool(false),
}
}

pub fn not(&self) -> RV {
RV::Bool(!self.as_bool())
}
}
use std::sync::Arc;
use super::RV;

impl PartialEq for RV {
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -350,7 +199,7 @@ mod test {

use crate::{
util::alloc_shared,
value::types::{eval_binary, RV},
value::eval::{eval_binary, RV},
};

#[test]
Expand Down
Loading
Loading