Skip to content

Commit

Permalink
fix: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Nov 22, 2024
1 parent 216a2ea commit 601007e
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 86 deletions.
7 changes: 5 additions & 2 deletions lykiadb-lang/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{fmt::{Display, Formatter, Result}, sync::Arc};
use std::{
fmt::{Display, Formatter, Result},
sync::Arc,
};

use ast::expr::Expr;
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -76,4 +79,4 @@ impl Display for Identifier {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}", self.name)
}
}
}
16 changes: 7 additions & 9 deletions lykiadb-lang/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,18 +542,12 @@ impl<'a> Parser<'a> {

match (&expr_conj_fst, &expr_conj_sec) {
(Some(SqlKeyword(Between)), None) => self.cmp_between(left, false),
(Some(SqlKeyword(Not)), Some(SqlKeyword(Between))) => {
self.cmp_between(left, true)
}
(Some(SqlKeyword(Not)), Some(SqlKeyword(Between))) => self.cmp_between(left, true),
_ => Ok(expr),
}
}

fn cmp_between(
&mut self,
subject: Box<Expr>,
falsy: bool,
) -> ParseResult<Box<Expr>> {
fn cmp_between(&mut self, subject: Box<Expr>, falsy: bool) -> ParseResult<Box<Expr>> {
let lower = self.term()?;

self.expected(skw!(And))?;
Expand All @@ -563,7 +557,11 @@ impl<'a> Parser<'a> {
subject: subject.clone(),
lower,
upper: upper.clone(),
kind: if falsy { RangeKind::NotBetween } else { RangeKind::Between },
kind: if falsy {
RangeKind::NotBetween
} else {
RangeKind::Between
},
span: subject.get_span().merge(&upper.get_span()),
id: self.get_expr_id(),
}))
Expand Down
4 changes: 3 additions & 1 deletion lykiadb-lang/src/parser/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{
ast::{expr::Expr, stmt::Stmt}, tokenizer::scanner::Scanner, Locals, Scopes
ast::{expr::Expr, stmt::Stmt},
tokenizer::scanner::Scanner,
Locals, Scopes,
};

use super::{resolver::Resolver, ParseError, ParseResult, Parser};
Expand Down
23 changes: 15 additions & 8 deletions lykiadb-server/src/engine/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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::{RV, eval::eval_binary};
use crate::value::{eval::eval_binary, RV};

use std::sync::Arc;
use std::vec;
Expand Down Expand Up @@ -182,7 +182,7 @@ pub struct Interpreter {
impl Interpreter {
pub fn new(out: Option<Shared<Output>>, with_stdlib: bool) -> Interpreter {
let mut env_man = Environment::new();
if with_stdlib {
if with_stdlib {
let native_fns = stdlib(out.clone());
let env = env_man.top();

Expand Down Expand Up @@ -475,7 +475,11 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
closure: self.env,
};

let callable = RV::Callable(Callable::new(Some(parameters.len()), CallableKind::Generic, fun.into()));
let callable = RV::Callable(Callable::new(
Some(parameters.len()),
CallableKind::Generic,
fun.into(),
));

if name.is_some() {
// TODO(vck): Callable shouldn't be cloned here
Expand All @@ -493,7 +497,7 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
upper,
subject,
kind,
span:_ ,
span: _,
id: _,
} => {
let lower_eval = self.visit_expr(lower)?;
Expand All @@ -507,9 +511,9 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
let max_num = lower_num.max(upper_num);

match kind {
RangeKind::Between => Ok(RV::Bool(
min_num <= subject_num && subject_num <= max_num,
)),
RangeKind::Between => {
Ok(RV::Bool(min_num <= subject_num && subject_num <= max_num))
}
RangeKind::NotBetween => {
Ok(RV::Bool(min_num > subject_num || subject_num > max_num))
}
Expand Down Expand Up @@ -722,7 +726,10 @@ pub mod test_helpers {
pub fn get_runtime() -> (Shared<Output>, Runtime) {
let out = alloc_shared(Output::new());

(out.clone(), Runtime::new(RuntimeMode::File, Interpreter::new(Some(out), true)))
(
out.clone(),
Runtime::new(RuntimeMode::File, Interpreter::new(Some(out), true)),
)
}

pub fn exec_assert(code: &str, output: Vec<RV>) {
Expand Down
5 changes: 1 addition & 4 deletions lykiadb-server/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ pub enum RuntimeMode {

impl Runtime {
pub fn new(mode: RuntimeMode, interpreter: Interpreter) -> Runtime {
Runtime {
mode,
interpreter,
}
Runtime { mode, interpreter }
}

pub fn ast(&mut self, source: &str) -> Result<Value, ExecutionError> {
Expand Down
47 changes: 30 additions & 17 deletions lykiadb-server/src/engine/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use rustc_hash::FxHashMap;

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

use self::{
Expand All @@ -28,20 +31,22 @@ pub fn stdlib(out: Option<Shared<Output>>) -> FxHashMap<String, RV> {

benchmark_namespace.insert(
"fib".to_owned(),
RV::Callable(Callable::new(Some(1), CallableKind::Generic, Function::Lambda { function: nt_fib })),
RV::Callable(Callable::new(
Some(1),
CallableKind::Generic,
Function::Lambda { function: nt_fib },
)),
);

json_namespace.insert(
"stringify".to_owned(),
RV::Callable(
Callable::new(
Some(1),
CallableKind::Generic,
Function::Lambda {
function: nt_json_encode,
}
)
),
RV::Callable(Callable::new(
Some(1),
CallableKind::Generic,
Function::Lambda {
function: nt_json_encode,
},
)),
);

json_namespace.insert(
Expand All @@ -51,16 +56,16 @@ pub fn stdlib(out: Option<Shared<Output>>) -> FxHashMap<String, RV> {
CallableKind::Generic,
Function::Lambda {
function: nt_json_decode,
}),
),
},
)),
);

time_namespace.insert(
"clock".to_owned(),
RV::Callable(Callable::new(
Some(0),
Some(0),
CallableKind::Generic,
Function::Lambda { function: nt_clock }
Function::Lambda { function: nt_clock },
)),
);

Expand All @@ -69,7 +74,11 @@ pub fn stdlib(out: Option<Shared<Output>>) -> FxHashMap<String, RV> {

test_namespace.insert(
"out".to_owned(),
RV::Callable(Callable::new(None, CallableKind::Generic, Function::Stateful(out.unwrap().clone()))),
RV::Callable(Callable::new(
None,
CallableKind::Generic,
Function::Stateful(out.unwrap().clone()),
)),
);

std.insert(
Expand All @@ -86,7 +95,11 @@ pub fn stdlib(out: Option<Shared<Output>>) -> FxHashMap<String, RV> {
std.insert("Time".to_owned(), RV::Object(alloc_shared(time_namespace)));
std.insert(
"print".to_owned(),
RV::Callable(Callable::new(None, CallableKind::Generic, Function::Lambda { function: nt_print })),
RV::Callable(Callable::new(
None,
CallableKind::Generic,
Function::Lambda { function: nt_print },
)),
);

std
Expand Down
38 changes: 31 additions & 7 deletions lykiadb-server/src/plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,27 @@ impl Node {
source._fmt_recursive(f, indent + 1)
}
Node::Scan { source, filter } => {
write!(f, "{}- scan [{} as {}]{}", indent_str, source.name, source.alias.as_ref().unwrap_or(&source.name), Self::NEWLINE)
write!(
f,
"{}- scan [{} as {}]{}",
indent_str,
source.name,
source.alias.as_ref().unwrap_or(&source.name),
Self::NEWLINE
)
}
Node::Compound { source, operator, right } => {
write!(f, "{}- compound [{:?}]{}", indent_str, operator, Self::NEWLINE)?;
Node::Compound {
source,
operator,
right,
} => {
write!(
f,
"{}- compound [{:?}]{}",
indent_str,
operator,
Self::NEWLINE
)?;
source._fmt_recursive(f, indent + 1)?;
right._fmt_recursive(f, indent + 1)
}
Expand All @@ -134,7 +151,14 @@ impl Node {
right,
constraint,
} => {
write!(f, "{}- join [{:?}, {}]:{}", indent_str, join_type, constraint.as_ref().unwrap(), Self::NEWLINE)?;
write!(
f,
"{}- join [{:?}, {}]:{}",
indent_str,
join_type,
constraint.as_ref().unwrap(),
Self::NEWLINE
)?;
left._fmt_recursive(f, indent + 1)?;
right._fmt_recursive(f, indent + 1)
}
Expand All @@ -145,6 +169,6 @@ impl Node {

impl Display for Node {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self._fmt_recursive(f, 0)
}
}
self._fmt_recursive(f, 0)
}
}
40 changes: 28 additions & 12 deletions lykiadb-server/src/plan/planner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::{engine::interpreter::{Interpreter, HaltReason}, util::Shared};
use crate::{
engine::interpreter::{HaltReason, Interpreter},
util::Shared,
};
use lykiadb_lang::ast::{
expr::Expr,
sql::{SqlFrom, SqlJoinType, SqlSelect, SqlSelectCore}, visitor::VisitorMut
sql::{SqlFrom, SqlJoinType, SqlSelect, SqlSelectCore},
visitor::VisitorMut,
};

use super::{Node, Plan};
Expand All @@ -11,9 +15,7 @@ pub struct Planner<'a> {

impl<'a> Planner<'a> {
pub fn new(interpreter: &'a mut Interpreter) -> Planner {
Planner {
interpreter
}
Planner { interpreter }
}

pub fn build(&mut self, expr: &Expr) -> Result<Plan, HaltReason> {
Expand Down Expand Up @@ -45,10 +47,10 @@ impl<'a> Planner<'a> {
}
}
if let Some(compound) = &core.compound {
node = Node::Compound {
node = Node::Compound {
source: Box::new(node),
operator: compound.operator.clone(),
right: Box::new(self.build_select_core(&compound.core)?)
right: Box::new(self.build_select_core(&compound.core)?),
}
}
// GROUP BY
Expand All @@ -61,12 +63,28 @@ impl<'a> Planner<'a> {
let mut node: Node = self.build_select_core(&query.core)?;

// TODO(vck): ORDER BY

if let Some(limit) = &query.limit {
if let Some(offset) = &limit.offset {
node = Node::Offset { source: Box::new(node), offset: self.interpreter.visit_expr(&offset)?.as_number().expect("Offset is not correct").floor() as usize }
node = Node::Offset {
source: Box::new(node),
offset: self
.interpreter
.visit_expr(&offset)?
.as_number()
.expect("Offset is not correct")
.floor() as usize,
}
}
node = Node::Limit {
source: Box::new(node),
limit: self
.interpreter
.visit_expr(&limit.count)?
.as_number()
.expect("Limit is not correct")
.floor() as usize,
}
node = Node::Limit { source: Box::new(node), limit: self.interpreter.visit_expr(&limit.count)?.as_number().expect("Limit is not correct").floor() as usize }
}

Ok(node)
Expand Down Expand Up @@ -113,11 +131,9 @@ impl<'a> Planner<'a> {
}
}


pub mod test_helpers {
use lykiadb_lang::{ast::stmt::Stmt, parser::program::Program};


use crate::engine::interpreter::Interpreter;

use super::Planner;
Expand Down
11 changes: 7 additions & 4 deletions lykiadb-server/src/value/callable.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::sync::Arc;
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::RV;
use crate::{
engine::interpreter::{HaltReason, Interpreter},
util::Shared,
};
use lykiadb_lang::ast::stmt::Stmt;
use std::fmt::{Debug, Display, Formatter};
use std::sync::Arc;

#[derive(Debug, Clone)]
pub enum CallableKind {
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-server/src/value/eval.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::RV;
use lykiadb_lang::ast::expr::Operation;
use std::ops;
use std::sync::Arc;
use super::RV;

impl PartialEq for RV {
fn eq(&self, other: &Self) -> bool {
Expand Down
Loading

0 comments on commit 601007e

Please sign in to comment.