Skip to content

Commit

Permalink
Merge pull request #21 from lykia-rs/milestone/phase-1
Browse files Browse the repository at this point in the history
Phase 1 / More test coverage
  • Loading branch information
can-keklik authored Dec 25, 2023
2 parents 9fabbd8 + 1ed41de commit 8502485
Show file tree
Hide file tree
Showing 35 changed files with 3,087 additions and 443 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ Lykia is a toy document database basically written for educational purposes. It

- [x] Core scripting language
- [x] A minimal standard library
- [x] SQL "SELECT" statements parsing
- [ ] Rest of the SQL syntax
- [ ] SQL parsing
- [x] "SELECT" expressions (the most complex part of the SQL syntax)
- [ ] "INSERT" statements
- [ ] "UPDATE" statements
- [ ] "DELETE" statements
- [ ] Query planning
- [ ] Plan optimization
- [ ] Async runtime/event loop
- [ ] In-memory storage engine
- [ ] Type checker
- [ ] Persistent storage engine (Bitcask)
- [ ] Transaction management with MVCC
- [ ] B-Tree implementation for indexes
Expand Down
33 changes: 26 additions & 7 deletions server/src/lang/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
use std::rc::Rc;

use serde::{Deserialize, Serialize};

use super::{sql::SqlSelect, stmt::StmtId, Literal};
use crate::lang::token::{Span, Spanned, Token, TokenType};
use crate::lang::token::{Span, Spanned, Token};

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum Operation {
Add,
Subtract,
Multiply,
Divide,
IsEqual,
IsNotEqual,
Greater,
GreaterEqual,
Less,
LessEqual,
And,
Or,
Not,
}

#[derive(Debug, Eq, PartialEq)]
pub enum Expr {
Expand Down Expand Up @@ -30,12 +49,12 @@ pub enum Expr {
},
Binary {
left: ExprId,
symbol: TokenType,
operation: Operation,
right: ExprId,
span: Span,
},
Unary {
symbol: TokenType,
operation: Operation,
expr: ExprId,
span: Span,
},
Expand All @@ -46,7 +65,7 @@ pub enum Expr {
},
Logical {
left: ExprId,
symbol: TokenType,
operation: Operation,
right: ExprId,
span: Span,
},
Expand Down Expand Up @@ -87,12 +106,12 @@ impl Spanned for Expr {
}
| Expr::Binary {
left: _,
symbol: _,
operation: _,
right: _,
span,
}
| Expr::Unary {
symbol: _,
operation: _,
expr: _,
span,
}
Expand All @@ -103,7 +122,7 @@ impl Spanned for Expr {
}
| Expr::Logical {
left: _,
symbol: _,
operation: _,
right: _,
span,
}
Expand Down
20 changes: 20 additions & 0 deletions server/src/lang/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustc_hash::FxHashMap;

use self::{
expr::{Expr, ExprId},
sql::{SqlCollectionSubquery, SqlExpr, SqlSelect, SqlSelectCore},
stmt::{Stmt, StmtId},
};
pub mod expr;
Expand All @@ -25,10 +26,29 @@ pub enum Literal {
impl Eq for Literal {}

pub trait Visitor<T, Q> {
fn visit_expr(&self, e: ExprId) -> Result<T, Q>;
fn visit_stmt(&self, e: StmtId) -> Result<T, Q>;
}

pub trait SqlVisitor<T, Q> {
fn visit_sql_select(&self, e: &SqlSelect) -> Result<T, Q>;
fn visit_sql_select_core(&self, core: &SqlSelectCore) -> Result<T, Q>;
fn visit_sql_subquery(&self, subquery: &SqlCollectionSubquery) -> Result<T, Q>;
fn visit_sql_expr(&self, sql_expr: &SqlExpr) -> Result<T, Q>;
}

pub trait VisitorMut<T, Q> {
fn visit_expr(&mut self, e: ExprId) -> Result<T, Q>;
fn visit_stmt(&mut self, e: StmtId) -> Result<T, Q>;
}

pub trait SqlVisitorMut<T, Q> {
fn visit_sql_select(&mut self, e: &SqlSelect) -> Result<T, Q>;
fn visit_sql_select_core(&mut self, core: &SqlSelectCore) -> Result<T, Q>;
fn visit_sql_subquery(&mut self, subquery: &SqlCollectionSubquery) -> Result<T, Q>;
fn visit_sql_expr(&mut self, sql_expr: &SqlExpr) -> Result<T, Q>;
}

pub struct ParserArena {
expressions: Vec<Expr>,
statements: Vec<Stmt>,
Expand Down
28 changes: 23 additions & 5 deletions server/src/lang/ast/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ pub enum SqlProjection {
Expr { expr: SqlExpr, alias: Option<Token> },
}

#[derive(Debug, Eq, PartialEq)]
pub struct SqlLimitClause {
pub count: SqlExpr,
pub offset: Option<SqlExpr>,
}

#[derive(Debug, Eq, PartialEq)]
pub struct SqlOrderByClause {
pub expr: SqlExpr,
pub ordering: SqlOrdering,
}

#[derive(Debug, Eq, PartialEq)]
pub struct SqlSelectCompound {
pub operator: SqlCompoundOperator,
pub core: SqlSelectCore,
}

#[derive(Debug, Eq, PartialEq)]
pub struct SqlJoin {
pub join_type: SqlJoinType,
Expand All @@ -68,7 +86,7 @@ pub enum SqlCollectionSubquery {
}

#[derive(Debug, Eq, PartialEq)]
pub struct SelectCore {
pub struct SqlSelectCore {
pub distinct: SqlDistinct,
pub projection: Vec<SqlProjection>,
pub from: Option<SqlCollectionSubquery>,
Expand All @@ -79,8 +97,8 @@ pub struct SelectCore {

#[derive(Debug, Eq, PartialEq)]
pub struct SqlSelect {
pub core: SelectCore,
pub compound: Vec<(SqlCompoundOperator, SelectCore)>,
pub order_by: Option<Vec<(SqlExpr, SqlOrdering)>>,
pub limit: Option<(SqlExpr, Option<SqlExpr>)>,
pub core: SqlSelectCore,
pub compound: Vec<SqlSelectCompound>,
pub order_by: Option<Vec<SqlOrderByClause>>,
pub limit: Option<SqlLimitClause>,
}
6 changes: 3 additions & 3 deletions server/src/lang/ast/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::expr::ExprId;
#[derive(Debug, Eq, PartialEq)]
pub enum Stmt {
Program {
stmts: Vec<StmtId>,
body: Vec<StmtId>,
span: Span,
},
Expression {
Expand All @@ -19,7 +19,7 @@ pub enum Stmt {
span: Span,
},
Block {
stmts: Vec<StmtId>,
body: Vec<StmtId>,
span: Span,
},
Declaration {
Expand All @@ -30,7 +30,7 @@ pub enum Stmt {
If {
condition: ExprId,
body: StmtId,
r#else: Option<StmtId>,
r#else_body: Option<StmtId>,
span: Span,
},
Loop {
Expand Down
Loading

0 comments on commit 8502485

Please sign in to comment.