Skip to content
Open
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
23 changes: 8 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
[workspace]
resolver = "2"
members = [
"interning/",
"expr/",
"frontend/",
"kernel/"
]
members = ["interning/", "expr/", "frontend/", "kernel/"]

[workspace.package]
edition = "2024"

[workspace.dependencies]
log = { version = "0.4.27" }
env_logger = "0.11.8"
# https://github.com/Adam-Vandervorst/PathMap
pathmap = { path = "../PathMap", features = ["jemalloc", "arena_compact"] }

# Internal to MORK
mork-interning = {features = ["debug_api"], path = "./interning"}
mork-expr = {path = "./expr"}
mork-frontend = {path = "./frontend"}
mork = {path = "./kernel"}
mork-interning = { features = ["debug_api"], path = "./interning" }
mork-expr = { path = "./expr" }
mork-frontend = { path = "./frontend" }
mork = { path = "./kernel" }

# External to MORK
#freeze = { version="0.1.1", git="https://github.com/luketpeterson/Freeze.git" } #Fixes build on MacOS
gxhash = {version="3.4.1", git="https://github.com/luketpeterson/gxhash/"} # for dag_serialization

[workspace.dependencies.pathmap]
path = "../PathMap"
features = ["jemalloc", "arena_compact"]
gxhash = { version = "3.4.1", git = "https://github.com/luketpeterson/gxhash/" } # for dag_serialization
79 changes: 40 additions & 39 deletions expr/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::unused_io_amount)]
#[allow(unused_imports)]
use std::{
fmt::{format, Debug, Formatter, Write},
Expand Down Expand Up @@ -94,7 +95,7 @@ pub enum Tag {
// - stay shared as long as possible
// - bring shared information to the front (bulk)

#[inline(always)]
#[inline]
pub const fn item_byte(b: Tag) -> u8 {
match b {
Tag::NewVar => { 0b1100_0000 | 0 }
Expand All @@ -104,7 +105,7 @@ pub const fn item_byte(b: Tag) -> u8 {
}
}

#[inline(always)]
#[inline]
pub fn byte_item(b: u8) -> Tag {
if b == 0b1100_0000 { return Tag::NewVar; }
else if (b & 0b1100_0000) == 0b1100_0000 { return Tag::SymbolSize(b & 0b0011_1111) }
Expand Down Expand Up @@ -197,12 +198,12 @@ macro_rules! traverse {
($t1:ty, $t2:ty, $x:expr, $new_var:expr, $var_ref:expr, $symbol:expr, $zero:expr, $add:expr, $finalize:expr) => {{
struct AnonTraversal {}
impl Traversal<$t1, $t2> for AnonTraversal {
#[inline(always)] fn new_var(&mut self, offset: usize) -> $t2 { ($new_var)(offset) }
#[inline(always)] fn var_ref(&mut self, offset: usize, i: u8) -> $t2 { ($var_ref)(offset, i) }
#[inline(always)] fn symbol(&mut self, offset: usize, s: &[u8]) -> $t2 { ($symbol)(offset, s) }
#[inline(always)] fn zero(&mut self, offset: usize, a: u8) -> $t1 { ($zero)(offset, a) }
#[inline(always)] fn add(&mut self, offset: usize, acc: $t1, sub: $t2) -> $t1 { ($add)(offset, acc, sub) }
#[inline(always)] fn finalize(&mut self, offset: usize, acc: $t1) -> $t2 { ($finalize)(offset, acc) }
#[inline] fn new_var(&mut self, offset: usize) -> $t2 { ($new_var)(offset) }
#[inline] fn var_ref(&mut self, offset: usize, i: u8) -> $t2 { ($var_ref)(offset, i) }
#[inline] fn symbol(&mut self, offset: usize, s: &[u8]) -> $t2 { ($symbol)(offset, s) }
#[inline] fn zero(&mut self, offset: usize, a: u8) -> $t1 { ($zero)(offset, a) }
#[inline] fn add(&mut self, offset: usize, acc: $t1, sub: $t2) -> $t1 { ($add)(offset, acc, sub) }
#[inline] fn finalize(&mut self, offset: usize, acc: $t1) -> $t2 { ($finalize)(offset, acc) }
}

execute_loop(&mut AnonTraversal{}, $x, 0).1
Expand Down Expand Up @@ -994,15 +995,15 @@ let mut stack: Vec<(u8, A)> = Vec::with_capacity(8);
struct DebugTraversal { string: String, transient: bool }
#[allow(unused_variables)]
impl Traversal<(), ()> for DebugTraversal {
#[inline(always)] fn new_var(&mut self, offset: usize) -> () { if self.transient { self.string.push(' '); }; self.string.push('$'); }
#[inline(always)] fn var_ref(&mut self, offset: usize, i: u8) -> () { if self.transient { self.string.push(' '); }; self.string.push('_'); self.string.push_str((i as u16 + 1).to_string().as_str()); }
#[inline(always)] fn symbol(&mut self, offset: usize, s: &[u8]) -> () { match std::str::from_utf8(s) {
#[inline] fn new_var(&mut self, offset: usize) -> () { if self.transient { self.string.push(' '); }; self.string.push('$'); }
#[inline] fn var_ref(&mut self, offset: usize, i: u8) -> () { if self.transient { self.string.push(' '); }; self.string.push('_'); self.string.push_str((i as u16 + 1).to_string().as_str()); }
#[inline] fn symbol(&mut self, offset: usize, s: &[u8]) -> () { match std::str::from_utf8(s) {
Ok(string) => { if self.transient { self.string.push(' '); }; self.string.push_str(string); }
Err(_) => { if self.transient { self.string.push(' '); }; for b in s { self.string.push_str(format!("\\x{:x}", b).as_str()); }; }
} }
#[inline(always)] fn zero(&mut self, offset: usize, a: u8) -> () { if self.transient { self.string.push(' '); }; self.string.push('('); self.transient = false; }
#[inline(always)] fn add(&mut self, offset: usize, acc: (), sub: ()) -> () { self.transient = true; }
#[inline(always)] fn finalize(&mut self, offset: usize, acc: ()) -> () { self.string.push(')'); }
#[inline] fn zero(&mut self, offset: usize, a: u8) -> () { if self.transient { self.string.push(' '); }; self.string.push('('); self.transient = false; }
#[inline] fn add(&mut self, offset: usize, acc: (), sub: ()) -> () { self.transient = true; }
#[inline] fn finalize(&mut self, offset: usize, acc: ()) -> () { self.string.push(')'); }
}

impl Debug for Expr {
Expand All @@ -1016,59 +1017,59 @@ impl Debug for Expr {
struct SerializerTraversal<'a, Target : std::io::Write, F : for <'b> Fn(&'b [u8]) -> &'b str> { out: &'a mut Target, map_symbol: F, transient: bool }
#[allow(unused_variables, unused_must_use)]
impl <Target : std::io::Write, F : for <'b> Fn(&'b [u8]) -> &'b str> Traversal<(), ()> for SerializerTraversal<'_, Target, F> {
#[inline(always)] fn new_var(&mut self, offset: usize) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write("$".as_bytes()); }
#[inline(always)] fn var_ref(&mut self, offset: usize, i: u8) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write("_".as_bytes()); self.out.write((i as u16 + 1).to_string().as_bytes()); }
#[inline(always)] fn symbol(&mut self, offset: usize, s: &[u8]) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write((self.map_symbol)(s).as_bytes()); }
#[inline(always)] fn zero(&mut self, offset: usize, a: u8) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write("(".as_bytes()); self.transient = false; }
#[inline(always)] fn add(&mut self, offset: usize, acc: (), sub: ()) -> () { self.transient = true; }
#[inline(always)] fn finalize(&mut self, offset: usize, acc: ()) -> () { self.out.write(")".as_bytes()); }
#[inline] fn new_var(&mut self, offset: usize) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write("$".as_bytes()); }
#[inline] fn var_ref(&mut self, offset: usize, i: u8) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write("_".as_bytes()); self.out.write((i as u16 + 1).to_string().as_bytes()); }
#[inline] fn symbol(&mut self, offset: usize, s: &[u8]) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write((self.map_symbol)(s).as_bytes()); }
#[inline] fn zero(&mut self, offset: usize, a: u8) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write("(".as_bytes()); self.transient = false; }
#[inline] fn add(&mut self, offset: usize, acc: (), sub: ()) -> () { self.transient = true; }
#[inline] fn finalize(&mut self, offset: usize, acc: ()) -> () { self.out.write(")".as_bytes()); }
}

struct SerializerTraversal2<'a, Target : std::io::Write, F : for <'b> Fn(&'b [u8]) -> &'b str, G : Fn(u8, bool) -> &'static str> { out: &'a mut Target, map_symbol: F, map_variable: G, transient: bool, n: u8 }
#[allow(unused_variables, unused_must_use)]
impl <Target : std::io::Write, F : for <'b> Fn(&'b [u8]) -> &'b str, G : Fn(u8, bool) -> &'static str> Traversal<(), ()> for SerializerTraversal2<'_, Target, F, G> {
#[inline(always)] fn new_var(&mut self, offset: usize) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write((self.map_variable)(self.n, true).as_bytes()); self.n += 1; }
#[inline(always)] fn var_ref(&mut self, offset: usize, i: u8) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write((self.map_variable)(i, false).as_bytes()); }
#[inline(always)] fn symbol(&mut self, offset: usize, s: &[u8]) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write((self.map_symbol)(s).as_bytes()); }
#[inline(always)] fn zero(&mut self, offset: usize, a: u8) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write("(".as_bytes()); self.transient = false; }
#[inline(always)] fn add(&mut self, offset: usize, acc: (), sub: ()) -> () { self.transient = true; }
#[inline(always)] fn finalize(&mut self, offset: usize, acc: ()) -> () { self.out.write(")".as_bytes()); }
#[inline] fn new_var(&mut self, offset: usize) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write((self.map_variable)(self.n, true).as_bytes()); self.n += 1; }
#[inline] fn var_ref(&mut self, offset: usize, i: u8) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write((self.map_variable)(i, false).as_bytes()); }
#[inline] fn symbol(&mut self, offset: usize, s: &[u8]) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write((self.map_symbol)(s).as_bytes()); }
#[inline] fn zero(&mut self, offset: usize, a: u8) -> () { if self.transient { self.out.write(" ".as_bytes()); }; self.out.write("(".as_bytes()); self.transient = false; }
#[inline] fn add(&mut self, offset: usize, acc: (), sub: ()) -> () { self.transient = true; }
#[inline] fn finalize(&mut self, offset: usize, acc: ()) -> () { self.out.write(")".as_bytes()); }
}

struct SerializerTraversalHighlights<'a, 't, Target : std::io::Write, F : for <'b> Fn(&'b [u8]) -> &'b str, G : Fn(u8, bool) -> &'static str> { out: &'a mut Target, map_symbol: F, map_variable: G, transient: bool, n: u8, targets: &'t [(usize, &'static str, &'static str)] }
#[allow(unused_variables, unused_must_use)]
impl <Target : std::io::Write, F : for <'b> Fn(&'b [u8]) -> &'b str, G : Fn(u8, bool) -> &'static str> Traversal<Option<&'static str>, ()> for SerializerTraversalHighlights<'_, '_, Target, F, G> {
#[inline(always)] fn new_var(&mut self, offset: usize) -> () {
#[inline] fn new_var(&mut self, offset: usize) -> () {
if self.transient { self.out.write(" ".as_bytes()); };
if offset == self.targets[0].0 { self.out.write(self.targets[0].1.as_bytes()); }
self.out.write((self.map_variable)(self.n, true).as_bytes());
if offset == self.targets[0].0 { self.out.write(self.targets[0].2.as_bytes()); self.targets = &self.targets[1..]; }
self.n += 1;
}
#[inline(always)] fn var_ref(&mut self, offset: usize, i: u8) -> () {
#[inline] fn var_ref(&mut self, offset: usize, i: u8) -> () {
if self.transient { self.out.write(" ".as_bytes()); };
if offset == self.targets[0].0 { self.out.write(self.targets[0].1.as_bytes()); }
self.out.write((self.map_variable)(i, false).as_bytes());
if offset == self.targets[0].0 { self.out.write(self.targets[0].2.as_bytes()); self.targets = &self.targets[1..]; }
}
#[inline(always)] fn symbol(&mut self, offset: usize, s: &[u8]) -> () {
#[inline] fn symbol(&mut self, offset: usize, s: &[u8]) -> () {
if self.transient { self.out.write(" ".as_bytes()); };
if offset == self.targets[0].0 { self.out.write(self.targets[0].1.as_bytes()); }
self.out.write((self.map_symbol)(s).as_bytes());
if offset == self.targets[0].0 { self.out.write(self.targets[0].2.as_bytes()); self.targets = &self.targets[1..]; }
}
#[inline(always)] fn zero(&mut self, offset: usize, a: u8) -> Option<&'static str> {
#[inline] fn zero(&mut self, offset: usize, a: u8) -> Option<&'static str> {
if self.transient { self.out.write(" ".as_bytes()); };
if offset == self.targets[0].0 { self.out.write(self.targets[0].1.as_bytes()); }
self.out.write("(".as_bytes()); self.transient = false;
if offset == self.targets[0].0 { let r = Some(self.targets[0].2); self.targets = &self.targets[1..]; r }
else { None }
}
#[inline(always)] fn add(&mut self, offset: usize, acc: Option<&'static str>, sub: ()) -> Option<&'static str> {
#[inline] fn add(&mut self, offset: usize, acc: Option<&'static str>, sub: ()) -> Option<&'static str> {
self.transient = true;
acc
}
#[inline(always)] fn finalize(&mut self, offset: usize, acc: Option<&'static str>) -> () {
#[inline] fn finalize(&mut self, offset: usize, acc: Option<&'static str>) -> () {
self.out.write(")".as_bytes());
if let Some(end) = acc { self.out.write(end.as_bytes()); }
}
Expand Down Expand Up @@ -1122,7 +1123,7 @@ impl ExprZipper {
}
}

#[inline(always)]
#[inline]
pub fn write_symbol(&mut self, value: &[u8]) -> bool {
unsafe {
let l = value.len();
Expand Down Expand Up @@ -1599,12 +1600,12 @@ impl std::hash::Hash for ExprEnv {

pub struct TraverseSide { ee: ExprEnv }
impl Traversal<(), ()> for TraverseSide {
#[inline(always)] fn new_var(&mut self, offset: usize) -> () { self.ee.v += 1; }
#[inline(always)] fn var_ref(&mut self, offset: usize, i: u8) -> () {}
#[inline(always)] fn symbol(&mut self, offset: usize, s: &[u8]) -> () {}
#[inline(always)] fn zero(&mut self, offset: usize, a: u8) -> () {}
#[inline(always)] fn add(&mut self, offset: usize, acc: (), sub: ()) -> () {}
#[inline(always)] fn finalize(&mut self, offset: usize, acc: ()) -> () {}
#[inline] fn new_var(&mut self, offset: usize) -> () { self.ee.v += 1; }
#[inline] fn var_ref(&mut self, offset: usize, i: u8) -> () {}
#[inline] fn symbol(&mut self, offset: usize, s: &[u8]) -> () {}
#[inline] fn zero(&mut self, offset: usize, a: u8) -> () {}
#[inline] fn add(&mut self, offset: usize, acc: (), sub: ()) -> () {}
#[inline] fn finalize(&mut self, offset: usize, acc: ()) -> () {}
}

impl ExprEnv {
Expand Down
14 changes: 7 additions & 7 deletions expr/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ macro_rules! destruct {

impl TryFrom<Expr> for i32 {
type Error = String;
#[inline(always)]
#[inline]
fn try_from(value: Expr) -> Result<Self, Self::Error> {
let tag = unsafe { byte_item(*value.ptr) };
if tag != Tag::SymbolSize(4) {
Expand All @@ -163,36 +163,36 @@ impl SerializableExpr for i32 {
}

impl DeserializableExpr for i32 {
#[inline(always)]
#[inline]
fn advanced(e: Expr) -> usize {
4
}
#[inline(always)]
#[inline]
fn check(e: Expr) -> bool {
unsafe { *e.ptr == item_byte(Tag::SymbolSize(4)) }
}
#[inline(always)]
#[inline]
fn deserialize_unchecked(e: Expr) -> Self {
unsafe { std::ptr::read_unaligned(e.ptr.add(1) as *const i32) }.swap_bytes()
}
}

impl DeserializableExpr for &str {
#[inline(always)]
#[inline]
fn advanced(e: Expr) -> usize {
unsafe {
let Tag::SymbolSize(arity) = byte_item(*e.ptr) else { panic!("wrong symbol for str") };
1usize + (arity as usize)
}
}
#[inline(always)]
#[inline]
fn check(e: Expr) -> bool {
unsafe {
let Tag::SymbolSize(arity) = byte_item(*e.ptr) else { unreachable!() };
str::from_utf8(slice_from_raw_parts(e.ptr.add(1), arity as _).as_ref().unwrap()).is_ok()
}
}
#[inline(always)]
#[inline]
fn deserialize_unchecked(e: Expr) -> Self {
unsafe {
let Tag::SymbolSize(arity) = byte_item(*e.ptr) else { unreachable!() };
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/bytestring_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl <'a> Context<'a> {
Context{ src: r, loc: 0, variables: vec![] }
}

#[inline(always)]
#[inline]
fn peek(&mut self) -> Result<u8, ParserError> {
if self.loc == self.src.len() {
Err(ParserError::UnexpectedEOF)
Expand All @@ -41,7 +41,7 @@ impl <'a> Context<'a> {
}
}

#[inline(always)]
#[inline]
fn next(&mut self) -> Result<u8, ParserError> {
if self.loc == self.src.len() {
Err(ParserError::UnexpectedEOF)
Expand All @@ -52,7 +52,7 @@ impl <'a> Context<'a> {
}
}

#[inline(always)]
#[inline]
fn has_next(&mut self) -> bool {
self.loc < self.src.len()
}
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/json_parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::unused_io_amount)]
use std::{str, slice};
use std::char::decode_utf16;
use std::convert::TryFrom;
Expand Down Expand Up @@ -468,7 +469,7 @@ impl<'a> Parser<'a> {
}

// Check if we are at the end of the source.
#[inline(always)]
#[inline]
fn is_eof(&mut self) -> bool {
self.index == self.length
}
Expand All @@ -479,7 +480,7 @@ impl<'a> Parser<'a> {
// very very rarely, lead to a situation where the same byte is read
// twice, but since this operation is using a raw pointer, the cost
// is virtually irrelevant.
#[inline(always)]
#[inline]
fn read_byte(&mut self) -> u8 {
debug_assert!(self.index < self.length, "Reading out of bounds");

Expand All @@ -488,7 +489,7 @@ impl<'a> Parser<'a> {

// Manually increment the index. Calling `read_byte` and then `bump`
// is equivalent to consuming a byte on an iterator.
#[inline(always)]
#[inline]
fn bump(&mut self) {
self.index = self.index.wrapping_add(1);
}
Expand Down
Loading