Skip to content

Commit

Permalink
Merge pull request #14 from lykia-rs/feature/parsing-tests
Browse files Browse the repository at this point in the history
feat: Major changes to AST, parsing, SQL parsing, module structure
  • Loading branch information
can-keklik authored Dec 16, 2023
2 parents a659c93 + 972b612 commit f9c15c4
Show file tree
Hide file tree
Showing 50 changed files with 2,434 additions and 1,575 deletions.
28 changes: 5 additions & 23 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
[package]
name = "lykia"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bumpalo = "3.12.2"
clap = { version = "4.4.6", features = ["derive"] }
phf = { version = "0.11", default-features = false, features = ["macros"] }
rustc-hash = "1.1.0"
serde = { version = "1.0.188", features=["derive"] }
serde_json = "1.0.105"
ariadne = { features = ["auto-color"] }
[workspace]
resolver = "1"
members = ["server"]

[profile.release]
debug = true

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

[[bench]]
name = "interpreter"
harness = false
opt-level = 3
strip = "debuginfo"
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Lykia is a toy document database basically written for educational purposes. It

## Overview
- Written in Rust
- A weird scripting and query language, combination of PHP and SQL. Built based on the language "Lox" which is explained in the famous book, Crafting Interpreters.
- A weird scripting and query language, combination of JavaScript and SQL. Built based on the language "Lox" which is explained in the famous book, Crafting Interpreters.
- A subset of JSON data types in both scripting language itself and storage
- In-disk and in-memory storage
- ACID compliance
Expand All @@ -19,9 +19,11 @@ Lykia is a toy document database basically written for educational purposes. It
## Roadmap

- [x] Core scripting language
- [ ] A minimal standard library (in progress)
- [ ] SQL parsing (in progress)
- [x] A minimal standard library
- [x] SQL "SELECT" statements parsing
- [ ] Rest of the SQL syntax
- [ ] Query planning
- [ ] Plan optimization
- [ ] Async runtime/event loop
- [ ] In-memory storage engine
- [ ] Persistent storage engine (Bitcask)
Expand All @@ -40,7 +42,7 @@ $ cargo run
Alternatively, you can run a Lykia script by passing its name as the first argument.

```shell
$ cargo run examples/fib.ly
$ cargo run server/examples/fib.ly
```

## License
Expand Down
13 changes: 0 additions & 13 deletions examples/fib.ly

This file was deleted.

24 changes: 24 additions & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
authors = ["Vedat Can Keklik <[email protected]>"]
name = "lykiadb-server"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bumpalo = "3.12.2"
clap = { version = "4.4.6", features = ["derive"] }
phf = { version = "0.11", default-features = false, features = ["macros"] }
rustc-hash = "1.1.0"
serde = { version = "1.0.188", features=["derive"] }
serde_json = "1.0.105"
ariadne = { features = ["auto-color"] }
assert-json-diff = "2.0.2"

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

[[bench]]
name = "interpreter"
harness = false
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions server/examples/fib.ly
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Define recursive Fibonacci function
fun fib($n) {
if ($n < 2) return $n;
return fib($n - 2) + fib($n - 1);
};

var $start_ly = Time.clock();
print(fib(35) == 9227465);
print("elapsed (user defined):", Time.clock() - $start_ly);

var $start_rs = Time.clock();
print(Benchmark.fib(35) == 9227465);
print("elapsed (native):", Time.clock() - $start_rs);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
132 changes: 132 additions & 0 deletions server/src/lang/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use std::rc::Rc;

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

#[derive(Debug, Eq, PartialEq)]
pub enum Expr {
Select {
query: SqlSelect,
span: Span,
},
Variable {
name: Token,
span: Span,
},
Grouping {
expr: ExprId,
span: Span,
},
Literal {
value: Literal,
raw: String,
span: Span,
},
Function {
name: Option<Token>,
parameters: Vec<Token>,
body: Rc<Vec<StmtId>>,
span: Span,
},
Binary {
left: ExprId,
symbol: TokenType,
right: ExprId,
span: Span,
},
Unary {
symbol: TokenType,
expr: ExprId,
span: Span,
},
Assignment {
dst: Token,
expr: ExprId,
span: Span,
},
Logical {
left: ExprId,
symbol: TokenType,
right: ExprId,
span: Span,
},
Call {
callee: ExprId,
args: Vec<ExprId>,
span: Span,
},
Get {
object: ExprId,
name: Token,
span: Span,
},
Set {
object: ExprId,
name: Token,
value: ExprId,
span: Span,
},
}

impl Spanned for Expr {
fn get_span(&self) -> Span {
match self {
Expr::Select { query: _, span }
| Expr::Variable { name: _, span }
| Expr::Grouping { expr: _, span }
| Expr::Literal {
value: _,
raw: _,
span,
}
| Expr::Function {
name: _,
parameters: _,
body: _,
span,
}
| Expr::Binary {
left: _,
symbol: _,
right: _,
span,
}
| Expr::Unary {
symbol: _,
expr: _,
span,
}
| Expr::Assignment {
dst: _,
expr: _,
span,
}
| Expr::Logical {
left: _,
symbol: _,
right: _,
span,
}
| Expr::Call {
callee: _,
args: _,
span,
}
| Expr::Get {
object: _,
name: _,
span,
}
| Expr::Set {
object: _,
name: _,
value: _,
span,
} => *span,
}
}
}

#[repr(transparent)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct ExprId(pub usize);
19 changes: 18 additions & 1 deletion src/lang/ast/mod.rs → server/src/lang/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
use std::rc::Rc;

use rustc_hash::FxHashMap;

use self::{
expr::{Expr, ExprId},
stmt::{Stmt, StmtId},
};

pub mod expr;
pub mod sql;
pub mod stmt;

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

impl Eq for Literal {}

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

0 comments on commit f9c15c4

Please sign in to comment.