Skip to content

Commit

Permalink
chore: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
arexon committed Dec 13, 2024
0 parents commit b17c2be
Show file tree
Hide file tree
Showing 86 changed files with 7,306 additions and 0 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

env:
CARGO_TERM_COLOR: always

jobs:
ci:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Rust Toolchain
run: |
rustup toolchain install stable --profile minimal
rustup component add clippy
- name: Setup Rust cache
uses: Swatinem/rust-cache@v2

- name: Format
run: cargo fmt --check

- name: Lint
run: cargo clippy -- -D warnings

- name: Test
run: cargo test

- name: Doc
run: RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --document-private-items
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.direnv
target
Cargo.lock
27 changes: 27 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "nolana"
version = "1.0.0"
edition = "2021"
authors = ["arexon <[email protected]>"]
categories = ["parser-implementations", "compilers"]
description = "An extremely fast parser Molang parser."
repository = "https://github.com/arexon/nolana"
license = "MIT"

[dependencies]
logos = "0.15.0"
miette = "7.2.0"
oxc_allocator = "0.40.1"

[dev-dependencies]
criterion = "0.5.1"
insta = "1.41.1"
miette = { version = "7.2.0", features = ["fancy"] }

[[bench]]
name = "parser"
harness = false

[[bench]]
name = "codegen"
harness = false
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 arexon <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
## 🌺 Nolana

> Nolana is an extremely fast parser for [Molang](https://bedrock.dev/docs/stable/Molang).
Project goals, in roughly descending priority:

- Be fully compliant with Molang
- Optimize for speed while maintaining a user-friendly AST
- Ensure the parser can recover from most syntax errors
- Provide extra tools for code generation (printing), simple semantic analysis, and AST traversal

## ⚡ Performance

Run `just bench` to try the benchmarks.

```norust
test parser ... bench: 593 ns/iter (+/- 5)
test codegen ... bench: 182 ns/iter (+/- 1)
```

> CPU: Intel Core i5-12400F
Nolana achieves this performance by leveraging [logos](https://github.com/maciejhirsz/logos) as its lexer, avoiding unnecessary allocations by using an arena allocator, and ensuring the memory size of each AST node is small.

## 📝 Example

```rust
use nolana::{
allocator::Allocator,
codegen::{Codegen, CodegenOptions},
parser::{Parser, ParserReturn},
semantic::SemanticChecker,
};

let source_text = "math.cos(q.anim_time * 38) * v.rotation_scale + v.x * v.x * q.life_time";

// Create an arena allocator to store the AST nodes.
let allocator = Allocator::default();

// Parse the provided Molang source code.
let ParserReturn {
program,
errors,
panicked,
} = Parser::new(&allocator, source_text).parse();

// Check for syntax errors.
if !errors.is_empty() {
for error in errors {
let error = error.with_source_code(source_text);
print!("{error:?}");
}
if panicked {
println!("Encountered an unrecoverable error");
}
return;
}

// Pretty print the AST.
println!("AST: {:#?}", program);
```

For more examples, check the [examples](./examples) directory.

## 📖 License

Nolana is free and open-source software distributed under the [MIT License](./LICENSE).
22 changes: 22 additions & 0 deletions benches/codegen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::fs;

use criterion::{criterion_group, criterion_main, Criterion};
use nolana::{allocator::Allocator, ast::Program, codegen::Codegen, parser::Parser};

fn codegen(program: &Program) {
let _ = Codegen::default().build(program);
}

fn bench_codegen(c: &mut Criterion) {
let allocator = Allocator::default();
let source_code = fs::read_to_string("benches/sample.molang").unwrap();
let ret = Parser::new(&allocator, &source_code).parse();
c.bench_function("codegen", |b| {
b.iter(|| {
codegen(&ret.program);
});
});
}

criterion_group!(parser, bench_codegen);
criterion_main!(parser);
22 changes: 22 additions & 0 deletions benches/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::fs;

use criterion::{criterion_group, criterion_main, Criterion};
use nolana::{allocator::Allocator, parser::Parser};

fn parse(allocator: &Allocator, source: &str) {
let _ = Parser::new(allocator, source).parse();
}

fn bench_parser(c: &mut Criterion) {
let mut allocator = Allocator::default();
let source_code = fs::read_to_string("benches/sample.molang").unwrap();
c.bench_function("parser", |b| {
b.iter(|| {
parse(&allocator, &source_code);
allocator.reset();
});
});
}

criterion_group!(parser, bench_parser);
criterion_main!(parser);
1 change: 1 addition & 0 deletions benches/sample.molang
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
variable.hand_bob = query.life_time < 0.01 ? 0.0 : variable.hand_bob + ((query.is_on_ground && query.is_alive ? math.clamp(math.sqrt(math.pow(query.position_delta(0), 2.0) + math.pow(query.position_delta(2), 2.0)), 0.0, 0.1) : 0.0) - variable.hand_bob) * 0.02;
48 changes: 48 additions & 0 deletions examples/full.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::fs;

use nolana::{
allocator::Allocator,
codegen::{Codegen, CodegenOptions},
parser::{Parser, ParserReturn},
semantic::SemanticChecker,
};

fn main() {
let source_text = fs::read_to_string("examples/sample.molang").unwrap();

let allocator = Allocator::default();

let ParserReturn {
program,
errors,
panicked,
} = Parser::new(&allocator, &source_text).parse();

if !errors.is_empty() {
for error in errors {
let error = error.with_source_code(source_text.clone());
print!("{error:?}");
}
if panicked {
println!("Encountered an unrecoverable error");
}
return;
}

let errors = SemanticChecker::default().check(&program);
if !errors.is_empty() {
for error in errors {
let error = error.with_source_code(source_text.clone());
print!("{error:?}");
}
return;
}

println!("AST: {:#?}", program);

let output = Codegen::default()
.with_options(CodegenOptions { minify: true })
.build(&program);

println!("Printed Molang: {output}");
}
31 changes: 31 additions & 0 deletions examples/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::fs;

use nolana::{
allocator::Allocator,
parser::{Parser, ParserReturn},
};

fn main() {
let source_text = fs::read_to_string("examples/sample.molang").unwrap();

let allocator = Allocator::default();

let ParserReturn {
program,
errors,
panicked,
} = Parser::new(&allocator, &source_text).parse();

if !errors.is_empty() {
for error in errors {
let error = error.with_source_code(source_text.clone());
print!("{error:?}");
}
if panicked {
println!("Encountered an unrecoverable error");
}
return;
}

println!("AST: {:#?}", program);
}
1 change: 1 addition & 0 deletions examples/sample.molang
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
math.cos(q.anim_time * 38) * v.rotation_scale + v.x * v.x * q.life_time
61 changes: 61 additions & 0 deletions examples/stats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::fs;

use nolana::{
allocator::Allocator,
ast::{CallExpression, CallKind, Program},
parser::{Parser, ParserReturn},
visit::{walk::walk_call_expression, Visit},
};

#[derive(Debug)]
struct MolangStats {
pub math_functions: u32,
pub queries: u32,
}

impl MolangStats {
pub fn new(program: &Program) -> Self {
let mut stats = Self {
math_functions: 0,
queries: 0,
};
stats.visit_program(program);
stats
}
}

impl<'a> Visit<'a> for MolangStats {
fn visit_call_expression(&mut self, it: &CallExpression<'a>) {
match it.kind {
CallKind::Math => self.math_functions += 1,
CallKind::Query => self.queries += 1,
}
walk_call_expression(self, it);
}
}

fn main() {
let source_text = fs::read_to_string("examples/sample.molang").unwrap();

let allocator = Allocator::default();

let ParserReturn {
program,
errors,
panicked,
} = Parser::new(&allocator, &source_text).parse();

if !errors.is_empty() {
for error in errors {
let error = error.with_source_code(source_text.clone());
print!("{error:?}");
}
if panicked {
println!("The encountered errors were unrecoverable");
}
return;
}

let molang_stats = MolangStats::new(&program);
println!("{molang_stats:?}");
}
Loading

0 comments on commit b17c2be

Please sign in to comment.