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

feature/optimizations #49

Merged
merged 4 commits into from
Dec 16, 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
8 changes: 2 additions & 6 deletions lykiadb-lang/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ use serde::{Deserialize, Serialize};

use std::{fmt::Display, sync::Arc};

use crate::{Identifier, Span, Spanned};

use super::{
sql::{SqlDelete, SqlInsert, SqlSelect, SqlUpdate},
stmt::Stmt,
AstNode,
AstNode, Identifier, Literal, Span, Spanned,
};

use crate::Literal;

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)]
#[serde(tag = "@type")]
pub enum Operation {
Expand Down Expand Up @@ -497,7 +493,7 @@ impl Expr {
pub mod test {
use std::collections::HashSet;

use crate::{ast::expr::Expr, Literal, Span};
use crate::ast::expr::Expr;

use super::*;

Expand Down
107 changes: 106 additions & 1 deletion lykiadb-lang/src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
use crate::Spanned;
use std::{
fmt::{Display, Formatter},
hash::Hash,
sync::Arc,
};

use derivative::Derivative;
use expr::Expr;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};

pub mod expr;
pub mod sql;
Expand All @@ -8,3 +17,99 @@
pub trait AstNode: Spanned {
fn get_id(&self) -> usize;
}

#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize, Hash)]
pub struct Span {
pub start: usize,
pub end: usize,
pub line: u32,
pub line_end: u32,
}

pub trait Spanned {
fn get_span(&self) -> Span;
}

impl Spanned for Span {
fn get_span(&self) -> Span {
*self
}
}

impl Span {
pub fn merge(&self, other: &Span) -> Span {
Span {
start: self.start.min(other.start),
end: self.end.max(other.end),
line: self.line.min(other.line),
line_end: self.line_end.min(other.line_end),
}
}
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Literal {
Str(Arc<String>),
Num(f64),
Bool(bool),
Undefined,
Object(FxHashMap<String, Box<Expr>>),
Array(Vec<Expr>),
NaN,
Null,
}

impl Literal {
pub fn as_str(&self) -> Option<&str> {
match self {
Literal::Str(s) => Some(s),
_ => None,

Check warning on line 66 in lykiadb-lang/src/ast/mod.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/ast/mod.rs#L66

Added line #L66 was not covered by tests
}
}
}

impl Hash for Literal {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
match self {
Literal::Str(s) => s.hash(state),

Check warning on line 74 in lykiadb-lang/src/ast/mod.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/ast/mod.rs#L74

Added line #L74 was not covered by tests
Literal::Num(n) => n.to_bits().hash(state),
Literal::Bool(b) => b.hash(state),
Literal::Object(o) => (o as *const _ as usize).hash(state),
Literal::Array(a) => a.hash(state),

Check warning on line 78 in lykiadb-lang/src/ast/mod.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/ast/mod.rs#L76-L78

Added lines #L76 - L78 were not covered by tests
//
Literal::Undefined => "undefined".hash(state),
Literal::NaN => "NaN".hash(state),
Literal::Null => "null".hash(state),

Check warning on line 82 in lykiadb-lang/src/ast/mod.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/ast/mod.rs#L80-L82

Added lines #L80 - L82 were not covered by tests
}
}
}

impl Eq for Literal {}

#[derive(Debug, Clone, Serialize, Deserialize, Derivative)]

Check warning on line 89 in lykiadb-lang/src/ast/mod.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/ast/mod.rs#L89

Added line #L89 was not covered by tests
#[serde(tag = "@type")]
#[derivative(Eq, PartialEq, Hash)]
pub struct Identifier {
pub name: String,
pub dollar: bool,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
pub span: Span,
}

impl Identifier {
pub fn new(name: &str, dollar: bool) -> Self {
Identifier {
name: name.to_string(),
dollar,
span: Span::default(),
}
}
}

impl Display for Identifier {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
3 changes: 1 addition & 2 deletions lykiadb-lang/src/ast/sql.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::Identifier;
use serde::{Deserialize, Serialize};

use super::expr::Expr;
use super::{expr::Expr, Identifier};

// Enums

Expand Down
12 changes: 4 additions & 8 deletions lykiadb-lang/src/ast/stmt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use derivative::Derivative;
use serde::{Deserialize, Serialize};

use crate::{Identifier, Span, Spanned};

use super::expr::Expr;
use super::{expr::Expr, Identifier, Span, Spanned};

#[derive(Debug, Serialize, Deserialize, Clone, Derivative)]
#[serde(tag = "@type")]
Expand Down Expand Up @@ -106,11 +104,9 @@ impl Spanned for Stmt {
mod test {
use std::collections::HashSet;

use crate::{
ast::{
expr::{test::create_simple_add_expr, Expr},
stmt::Stmt,
},
use crate::ast::{
expr::{test::create_simple_add_expr, Expr},
stmt::Stmt,
Span,
};

Expand Down
120 changes: 40 additions & 80 deletions lykiadb-lang/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::{
fmt::{Display, Formatter, Result},
hash::Hash,
sync::Arc,
use parser::{
program::Program,
resolver::{ResolveError, Resolver},
ParseError, Parser,
};

use ast::expr::Expr;
use derivative::Derivative;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use tokenizer::scanner::{ScanError, Scanner};

pub mod ast;
pub mod parser;
Expand All @@ -16,98 +14,60 @@
pub type Scopes = Vec<FxHashMap<String, bool>>;
pub type Locals = FxHashMap<usize, usize>;

#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize, Hash)]
pub struct Span {
pub start: usize,
pub end: usize,
pub line: u32,
pub line_end: u32,
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
pub enum LangError {
Parse(ParseError),
Scan(ScanError),
Resolve(ResolveError),
}

pub trait Spanned {
fn get_span(&self) -> Span;
impl From<ParseError> for LangError {
fn from(err: ParseError) -> Self {
LangError::Parse(err)
}

Check warning on line 27 in lykiadb-lang/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/lib.rs#L25-L27

Added lines #L25 - L27 were not covered by tests
}

impl Spanned for Span {
fn get_span(&self) -> Span {
*self
impl From<ScanError> for LangError {
fn from(err: ScanError) -> Self {
LangError::Scan(err)

Check warning on line 32 in lykiadb-lang/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/lib.rs#L31-L32

Added lines #L31 - L32 were not covered by tests
}
}

impl Span {
pub fn merge(&self, other: &Span) -> Span {
Span {
start: self.start.min(other.start),
end: self.end.max(other.end),
line: self.line.min(other.line),
line_end: self.line_end.min(other.line_end),
}
impl From<ResolveError> for LangError {
fn from(err: ResolveError) -> Self {
LangError::Resolve(err)

Check warning on line 38 in lykiadb-lang/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/lib.rs#L37-L38

Added lines #L37 - L38 were not covered by tests
}
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Literal {
Str(Arc<String>),
Num(f64),
Bool(bool),
Undefined,
Object(FxHashMap<String, Box<Expr>>),
Array(Vec<Expr>),
NaN,
Null,
pub struct SourceProcessor {
scopes: Scopes,
locals: Locals,
}

impl Literal {
pub fn as_str(&self) -> Option<&str> {
match self {
Literal::Str(s) => Some(s),
_ => None,
}
impl Default for SourceProcessor {
fn default() -> Self {
Self::new()

Check warning on line 49 in lykiadb-lang/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/lib.rs#L48-L49

Added lines #L48 - L49 were not covered by tests
}
}

impl Hash for Literal {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
match self {
Literal::Str(s) => s.hash(state),
Literal::Num(n) => n.to_bits().hash(state),
Literal::Bool(b) => b.hash(state),
Literal::Object(o) => (o as *const _ as usize).hash(state),
Literal::Array(a) => a.hash(state),
//
Literal::Undefined => "undefined".hash(state),
Literal::NaN => "NaN".hash(state),
Literal::Null => "null".hash(state),
impl SourceProcessor {
pub fn new() -> SourceProcessor {
SourceProcessor {
scopes: vec![],
locals: FxHashMap::default(),
}
}
}

impl Eq for Literal {}
pub fn process(&mut self, source: &str) -> Result<Program, LangError> {
let tokens = Scanner::scan(source)?;
let mut program = Parser::parse(&tokens)?;
let mut resolver = Resolver::new(self.scopes.clone(), &program, Some(self.locals.clone()));
let (scopes, locals) = resolver.resolve()?;

#[derive(Debug, Clone, Serialize, Deserialize, Derivative)]
#[serde(tag = "@type")]
#[derivative(Eq, PartialEq, Hash)]
pub struct Identifier {
pub name: String,
pub dollar: bool,
#[serde(skip)]
#[derivative(PartialEq = "ignore")]
#[derivative(Hash = "ignore")]
pub span: Span,
}

impl Identifier {
pub fn new(name: &str, dollar: bool) -> Self {
Identifier {
name: name.to_string(),
dollar,
span: Span::default(),
}
}
}
self.scopes = scopes;
self.locals.clone_from(&locals);
program.set_locals(self.locals.clone());

impl Display for Identifier {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}", self.name)
Ok(program)
}
}
4 changes: 2 additions & 2 deletions lykiadb-lang/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use self::program::Program;
use super::ast::expr::{Expr, Operation};
use super::ast::stmt::Stmt;
use crate::ast::expr::RangeKind;
use crate::ast::{Literal, Span, Spanned};
use crate::tokenizer::token::{
Keyword::*, SqlKeyword, SqlKeyword::*, Symbol::*, Token, TokenType, TokenType::*,
};
use crate::{kw, skw, sym};
use crate::{Literal, Span, Spanned};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
Expand Down Expand Up @@ -616,7 +616,7 @@ impl<'a> Parser<'a> {
&& self.in_select_depth > 0
{
let head = name.clone();
let mut tail: Vec<crate::Identifier> = vec![];
let mut tail: Vec<super::ast::Identifier> = vec![];
while self.match_next(&sym!(Dot)) {
let identifier = self.expected(&Identifier { dollar: false })?.clone();
tail.push(identifier.extract_identifier().unwrap());
Expand Down
5 changes: 3 additions & 2 deletions lykiadb-lang/src/parser/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::ast::expr::Expr;
use crate::ast::stmt::Stmt;
use crate::ast::visitor::VisitorMut;
use crate::{Identifier, Literal};
use crate::{Locals, Scopes, Span};
use crate::ast::Span;
use crate::ast::{Identifier, Literal};
use crate::{Locals, Scopes};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};

Expand Down
2 changes: 1 addition & 1 deletion lykiadb-lang/src/tokenizer/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use serde::{Deserialize, Serialize};

use crate::ast::{Literal::*, Span};
use crate::sym;
use crate::tokenizer::token::Symbol::*;
use crate::tokenizer::token::TokenType::{Eof, Identifier};
use crate::tokenizer::token::*;
use crate::{Literal::*, Span};
use std::iter::{Enumerate, Peekable};
use std::str::Chars;
use std::sync::Arc;
Expand Down
3 changes: 2 additions & 1 deletion lykiadb-lang/src/tokenizer/token.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{Identifier, Literal, Span};
use phf::phf_map;
use serde::{Deserialize, Serialize};

use crate::ast::{Identifier, Literal, Span};

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum Symbol {
Comma,
Expand Down
Loading
Loading