holey-bytes/lang/src/lib.rs

550 lines
15 KiB
Rust
Raw Normal View History

2024-07-08 03:13:50 -05:00
#![feature(
2024-11-14 13:25:52 -06:00
iter_array_chunks,
assert_matches,
2024-07-08 03:13:50 -05:00
let_chains,
if_let_guard,
macro_metavar_expr,
anonymous_lifetime_in_impl_trait,
core_intrinsics,
never_type,
unwrap_infallible,
slice_partition_dedup,
portable_simd,
iter_collect_into,
ptr_metadata,
2024-09-03 15:34:17 -05:00
slice_ptr_get,
slice_take,
2024-09-06 09:11:57 -05:00
map_try_insert,
2024-09-12 11:42:21 -05:00
extract_if,
2024-09-30 12:09:17 -05:00
ptr_internals,
2024-10-01 08:28:18 -05:00
iter_intersperse,
2024-10-10 01:35:17 -05:00
str_from_raw_parts,
ptr_sub_ptr,
2024-10-12 06:07:49 -05:00
slice_from_ptr_range,
2024-10-27 07:57:00 -05:00
iter_next_chunk,
2024-10-30 12:42:25 -05:00
pointer_is_aligned_to,
maybe_uninit_fill,
array_chunks
2024-07-08 03:13:50 -05:00
)]
2024-09-30 12:09:17 -05:00
#![warn(clippy::dbg_macro)]
2024-11-16 10:41:40 -06:00
#![expect(internal_features)]
2024-09-30 12:09:17 -05:00
#![no_std]
2024-05-17 12:53:59 -05:00
2024-09-30 12:09:17 -05:00
#[cfg(feature = "std")]
pub use fs::*;
2024-11-08 03:25:34 -06:00
pub use utils::Ent;
use {self::ty::Builtin, alloc::vec::Vec};
2024-05-20 07:11:58 -05:00
2024-09-30 12:09:17 -05:00
#[macro_use]
extern crate alloc;
#[cfg(any(feature = "std", test))]
extern crate std;
2024-09-28 09:34:08 -05:00
#[cfg(test)]
const README: &str = include_str!("../README.md");
#[cfg(test)]
2024-05-09 16:41:59 -05:00
#[macro_export]
macro_rules! run_tests {
2024-09-28 09:34:08 -05:00
($runner:path: $($name:ident;)*) => {$(
2024-05-09 16:41:59 -05:00
#[test]
fn $name() {
2024-09-30 12:09:17 -05:00
$crate::run_test(core::any::type_name_of_val(&$name), stringify!($name), $crate::README, $runner);
2024-05-09 16:41:59 -05:00
}
)*};
}
2024-10-04 14:44:29 -05:00
pub mod fmt;
2024-09-30 12:09:17 -05:00
#[cfg(any(feature = "std", test))]
pub mod fs;
2024-10-27 07:57:00 -05:00
pub mod fuzz;
pub mod lexer;
2024-06-23 06:55:48 -05:00
pub mod parser;
2024-09-02 17:07:20 -05:00
pub mod son;
pub mod ty;
pub mod backend {
use {
crate::{
parser,
son::Nodes,
ty::{self, Module, Types},
utils::EntSlice,
},
alloc::{string::String, vec::Vec},
};
pub mod hbvm;
pub struct AssemblySpec {
pub entry: u32,
pub code_length: u64,
pub data_length: u64,
}
pub trait Backend {
fn assemble_reachable(
&mut self,
from: ty::Func,
types: &Types,
to: &mut Vec<u8>,
) -> AssemblySpec;
fn disasm<'a>(
&'a self,
sluce: &[u8],
eca_handler: &mut dyn FnMut(&mut &[u8]),
types: &'a Types,
files: &'a EntSlice<Module, parser::Ast>,
output: &mut String,
) -> Result<(), hbbytecode::DisasmError<'a>>;
fn emit_body(
&mut self,
id: ty::Func,
ci: &Nodes,
tys: &Types,
files: &EntSlice<Module, parser::Ast>,
);
fn emit_ct_body(
&mut self,
id: ty::Func,
ci: &Nodes,
tys: &Types,
files: &EntSlice<Module, parser::Ast>,
) {
self.emit_body(id, ci, tys, files);
}
fn assemble_bin(&mut self, from: ty::Func, types: &Types, to: &mut Vec<u8>) {
self.assemble_reachable(from, types, to);
}
}
}
2024-06-23 06:55:48 -05:00
2024-10-30 12:42:25 -05:00
mod utils;
2024-06-23 06:55:48 -05:00
2024-10-24 06:25:30 -05:00
mod debug {
pub fn panicking() -> bool {
#[cfg(feature = "std")]
{
std::thread::panicking()
}
#[cfg(not(feature = "std"))]
{
false
}
}
#[cfg(all(debug_assertions, feature = "std"))]
pub type Trace = std::rc::Rc<std::backtrace::Backtrace>;
#[cfg(not(all(debug_assertions, feature = "std")))]
pub type Trace = ();
pub fn trace() -> Trace {
#[cfg(all(debug_assertions, feature = "std"))]
{
std::rc::Rc::new(std::backtrace::Backtrace::capture())
}
#[cfg(not(all(debug_assertions, feature = "std")))]
{}
}
}
2024-10-01 14:33:30 -05:00
mod ctx_map {
use core::hash::BuildHasher;
pub type Hash = u64;
pub type HashBuilder = core::hash::BuildHasherDefault<IdentityHasher>;
#[derive(Default)]
pub struct IdentityHasher(u64);
impl core::hash::Hasher for IdentityHasher {
fn finish(&self) -> u64 {
self.0
}
fn write(&mut self, _: &[u8]) {
unimplemented!()
}
fn write_u64(&mut self, i: u64) {
self.0 = i;
}
}
2024-10-21 11:57:23 -05:00
#[derive(Clone)]
2024-10-01 14:33:30 -05:00
pub struct Key<T> {
pub value: T,
pub hash: Hash,
}
impl<T> core::hash::Hash for Key<T> {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
state.write_u64(self.hash);
}
}
pub trait CtxEntry {
type Ctx: ?Sized;
type Key<'a>: Eq + core::hash::Hash;
fn key<'a>(&self, ctx: &'a Self::Ctx) -> Self::Key<'a>;
}
2024-10-21 11:57:23 -05:00
#[derive(Clone)]
2024-10-01 14:33:30 -05:00
pub struct CtxMap<T> {
inner: hashbrown::HashMap<Key<T>, (), HashBuilder>,
}
impl<T> Default for CtxMap<T> {
fn default() -> Self {
Self { inner: Default::default() }
}
}
impl<T: CtxEntry> CtxMap<T> {
pub fn entry<'a, 'b>(
&'a mut self,
2024-10-01 15:53:03 -05:00
key: T::Key<'b>,
2024-10-01 14:33:30 -05:00
ctx: &'b T::Ctx,
) -> (hashbrown::hash_map::RawEntryMut<'a, Key<T>, (), HashBuilder>, Hash) {
2024-10-01 15:53:03 -05:00
let hash = crate::FnvBuildHasher::default().hash_one(&key);
(self.inner.raw_entry_mut().from_hash(hash, |k| k.value.key(ctx) == key), hash)
2024-10-01 14:33:30 -05:00
}
2024-10-01 15:53:03 -05:00
pub fn get<'a>(&self, key: T::Key<'a>, ctx: &'a T::Ctx) -> Option<&T> {
let hash = crate::FnvBuildHasher::default().hash_one(&key);
2024-10-01 14:33:30 -05:00
self.inner
.raw_entry()
2024-10-01 15:53:03 -05:00
.from_hash(hash, |k| k.value.key(ctx) == key)
2024-10-01 14:33:30 -05:00
.map(|(k, _)| &k.value)
}
pub fn clear(&mut self) {
self.inner.clear();
}
2024-10-01 15:53:03 -05:00
pub fn remove(&mut self, value: &T, ctx: &T::Ctx) -> Option<T> {
2024-10-01 14:33:30 -05:00
let (entry, _) = self.entry(value.key(ctx), ctx);
match entry {
hashbrown::hash_map::RawEntryMut::Occupied(o) => Some(o.remove_entry().0.value),
hashbrown::hash_map::RawEntryMut::Vacant(_) => None,
}
}
2024-10-01 15:53:03 -05:00
pub fn insert<'a>(&mut self, key: T::Key<'a>, value: T, ctx: &'a T::Ctx) {
let (entry, hash) = self.entry(key, ctx);
match entry {
hashbrown::hash_map::RawEntryMut::Occupied(_) => unreachable!(),
hashbrown::hash_map::RawEntryMut::Vacant(v) => {
_ = v.insert(Key { hash, value }, ())
}
}
}
pub fn get_or_insert<'a>(
&mut self,
key: T::Key<'a>,
ctx: &'a mut T::Ctx,
with: impl FnOnce(&'a mut T::Ctx) -> T,
) -> &mut T {
let (entry, hash) = self.entry(key, unsafe { &mut *(&mut *ctx as *mut _) });
match entry {
hashbrown::hash_map::RawEntryMut::Occupied(o) => &mut o.into_key_value().0.value,
hashbrown::hash_map::RawEntryMut::Vacant(v) => {
&mut v.insert(Key { hash, value: with(ctx) }, ()).0.value
}
}
}
2024-10-01 14:33:30 -05:00
}
}
2024-10-27 07:57:00 -05:00
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct Ident(u32);
2024-10-27 07:57:00 -05:00
impl Ident {
pub const INVALID: Self = Self(u32::MAX);
const LEN_BITS: u32 = 6;
2024-10-27 07:57:00 -05:00
pub fn len(self) -> u32 {
self.0 & ((1 << Self::LEN_BITS) - 1)
}
pub fn is_type(self) -> bool {
ty::Builtin::try_from(self) == Ok(ty::Builtin::TYPE)
}
2024-10-27 07:57:00 -05:00
pub fn is_empty(self) -> bool {
self.len() == 0
}
2024-10-27 07:57:00 -05:00
pub fn is_null(self) -> bool {
(self.0 >> Self::LEN_BITS) == 0
}
2024-10-27 07:57:00 -05:00
pub fn pos(self) -> u32 {
(self.0 >> Self::LEN_BITS).saturating_sub(1)
}
2024-10-27 07:57:00 -05:00
pub fn new(pos: u32, len: u32) -> Option<Self> {
(len < (1 << Self::LEN_BITS)).then_some(((pos + 1) << Self::LEN_BITS) | len).map(Self)
}
2024-10-27 07:57:00 -05:00
pub fn range(self) -> core::ops::Range<usize> {
let (len, pos) = (self.len() as usize, self.pos() as usize);
pos..pos + len
}
2024-10-27 07:57:00 -05:00
2024-11-08 03:25:34 -06:00
fn builtin(builtin: Builtin) -> Ident {
Self(builtin.index() as _)
2024-10-27 07:57:00 -05:00
}
}
fn endoce_string(
literal: &str,
str: &mut Vec<u8>,
report: impl Fn(&core::str::Bytes, &str),
) -> Option<()> {
let report = |bytes: &core::str::Bytes, msg: &_| {
report(bytes, msg);
None::<u8>
2024-09-13 08:12:20 -05:00
};
let decode_braces = |str: &mut Vec<u8>, bytes: &mut core::str::Bytes| {
while let Some(b) = bytes.next()
&& b != b'}'
{
let c = bytes.next().or_else(|| report(bytes, "incomplete escape sequence"))?;
let decode = |b: u8| {
Some(match b {
b'0'..=b'9' => b - b'0',
b'a'..=b'f' => b - b'a' + 10,
b'A'..=b'F' => b - b'A' + 10,
_ => report(bytes, "expected hex digit or '}'")?,
})
};
str.push(decode(b)? << 4 | decode(c)?);
2024-10-24 05:28:18 -05:00
}
2024-09-13 08:12:20 -05:00
Some(())
};
2024-09-13 08:12:20 -05:00
let mut bytes = literal.bytes();
while let Some(b) = bytes.next() {
if b != b'\\' {
str.push(b);
continue;
2024-09-13 08:12:20 -05:00
}
let b = match bytes.next().or_else(|| report(&bytes, "incomplete escape sequence"))? {
b'n' => b'\n',
b'r' => b'\r',
b't' => b'\t',
b'\\' => b'\\',
b'\'' => b'\'',
b'"' => b'"',
b'0' => b'\0',
b'{' => {
decode_braces(str, &mut bytes);
continue;
2024-11-08 03:25:34 -06:00
}
_ => report(&bytes, "unknown escape sequence, expected [nrt\\\"'{0]")?,
2024-09-13 08:12:20 -05:00
};
str.push(b);
2024-09-13 08:12:20 -05:00
}
if str.last() != Some(&0) {
report(&bytes, "string literal must end with null byte (for now)");
2024-09-30 12:09:17 -05:00
}
Some(())
2024-09-30 12:09:17 -05:00
}
pub fn quad_sort<T>(mut slice: &mut [T], mut cmp: impl FnMut(&T, &T) -> core::cmp::Ordering) {
while let Some(it) = slice.take_first_mut() {
for ot in &mut *slice {
if cmp(it, ot) == core::cmp::Ordering::Greater {
core::mem::swap(it, ot);
2024-09-30 12:09:17 -05:00
}
}
}
debug_assert!(slice.is_sorted_by(|a, b| cmp(a, b) != core::cmp::Ordering::Greater));
2024-09-13 08:12:20 -05:00
}
type FnvBuildHasher = core::hash::BuildHasherDefault<FnvHasher>;
2024-11-24 09:43:45 -06:00
struct FnvHasher(u64);
2024-11-24 09:43:45 -06:00
impl core::hash::Hasher for FnvHasher {
fn finish(&self) -> u64 {
self.0
2024-11-24 09:43:45 -06:00
}
fn write(&mut self, bytes: &[u8]) {
self.0 = bytes.iter().fold(self.0, |hash, &byte| {
let mut hash = hash;
hash ^= byte as u64;
hash = hash.wrapping_mul(0x100000001B3);
hash
});
}
2024-09-13 08:12:20 -05:00
}
impl Default for FnvHasher {
fn default() -> Self {
Self(0xCBF29CE484222325)
2024-05-17 12:53:59 -05:00
}
}
2024-06-23 06:55:48 -05:00
#[cfg(test)]
pub fn run_test(
name: &'static str,
ident: &'static str,
input: &'static str,
test: fn(&'static str, &'static str, &mut alloc::string::String),
2024-06-23 06:55:48 -05:00
) {
use std::{
io::Write,
path::PathBuf,
string::{String, ToString},
};
2024-06-23 06:55:48 -05:00
let filter = std::env::var("PT_FILTER").unwrap_or_default();
if !filter.is_empty() && !name.contains(&filter) {
return;
}
let mut output = String::new();
2024-09-04 16:46:32 -05:00
{
struct DumpOut<'a>(&'a mut String);
impl Drop for DumpOut<'_> {
fn drop(&mut self) {
if std::thread::panicking() {
2024-09-30 12:09:17 -05:00
std::println!("{}", self.0);
2024-09-04 16:46:32 -05:00
}
}
}
let dump = DumpOut(&mut output);
test(ident, input, dump.0);
}
2024-06-23 06:55:48 -05:00
let mut root = PathBuf::from(
std::env::var("PT_TEST_ROOT")
.unwrap_or(concat!(env!("CARGO_MANIFEST_DIR"), "/tests").to_string()),
);
2024-07-08 00:22:53 -05:00
root.push(name.replace("::", "_").replace(concat!(env!("CARGO_PKG_NAME"), "_"), ""));
2024-06-23 06:55:48 -05:00
root.set_extension("txt");
let expected = std::fs::read_to_string(&root).unwrap_or_default();
if output == expected {
return;
}
if std::env::var("PT_UPDATE").is_ok() {
std::fs::write(&root, output).unwrap();
return;
}
if !root.exists() {
std::fs::create_dir_all(root.parent().unwrap()).unwrap();
std::fs::write(&root, vec![]).unwrap();
}
let mut proc = std::process::Command::new("diff")
.arg("-u")
.arg("--color")
.arg(&root)
.arg("-")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::inherit())
.spawn()
.unwrap();
2024-07-08 00:22:53 -05:00
proc.stdin.as_mut().unwrap().write_all(output.as_bytes()).unwrap();
2024-06-23 06:55:48 -05:00
proc.wait().unwrap();
panic!("test failed");
}
2024-09-13 07:30:23 -05:00
#[cfg(test)]
fn test_parse_files(
ident: &str,
input: &str,
ctx: &mut parser::Ctx,
) -> (Vec<parser::Ast>, Vec<Vec<u8>>) {
2024-10-13 08:22:16 -05:00
use {
self::parser::FileKind,
std::{borrow::ToOwned, string::ToString},
};
2024-09-30 12:09:17 -05:00
fn find_block<'a>(mut input: &'a str, test_name: &str) -> &'a str {
2024-09-13 07:30:23 -05:00
const CASE_PREFIX: &str = "#### ";
const CASE_SUFFIX: &str = "\n```hb";
loop {
let Some(pos) = input.find(CASE_PREFIX) else {
unreachable!("test {test_name} not found");
};
input = unsafe { input.get_unchecked(pos + CASE_PREFIX.len()..) };
if !input.starts_with(test_name) {
continue;
}
input = unsafe { input.get_unchecked(test_name.len()..) };
if !input.starts_with(CASE_SUFFIX) {
continue;
}
input = unsafe { input.get_unchecked(CASE_SUFFIX.len()..) };
let end = input.find("```").unwrap_or(input.len());
break unsafe { input.get_unchecked(..end) };
}
}
let input = find_block(input, ident);
let mut module_map = Vec::new();
2024-10-13 13:01:18 -05:00
let mut embed_map = Vec::new();
2024-09-13 07:30:23 -05:00
let mut last_start = 0;
2024-10-13 13:01:18 -05:00
let mut last_module_name = "test.hb";
2024-09-13 07:30:23 -05:00
for (i, m) in input.match_indices("// in module: ") {
2024-10-13 13:01:18 -05:00
if last_module_name.ends_with(".hb") {
fmt::test::format(ident, input[last_start..i].trim());
module_map.push((last_module_name, &input[last_start..i]));
} else {
embed_map.push((last_module_name, &input[last_start..i]));
}
2024-09-13 07:30:23 -05:00
let (module_name, _) = input[i + m.len()..].split_once('\n').unwrap();
last_module_name = module_name;
last_start = i + m.len() + module_name.len() + 1;
}
2024-10-13 13:01:18 -05:00
if last_module_name.ends_with(".hb") {
fmt::test::format(ident, input[last_start..].trim());
module_map.push((last_module_name, &input[last_start..]));
} else {
embed_map.push((last_module_name, &input[last_start..]));
}
let mut loader = |path: &str, _: &str, kind| match kind {
FileKind::Module => module_map
2024-09-13 07:30:23 -05:00
.iter()
.position(|&(name, _)| name == path)
2024-10-13 13:01:18 -05:00
.ok_or("Module Not Found".to_string()),
FileKind::Embed => embed_map
.iter()
.position(|&(name, _)| name == path)
.ok_or("Embed Not Found".to_string()),
2024-09-13 07:30:23 -05:00
};
2024-10-13 13:01:18 -05:00
(
module_map
.iter()
.map(|&(path, content)| parser::Ast::new(path, content.to_owned(), ctx, &mut loader))
2024-10-13 13:01:18 -05:00
.collect(),
embed_map.iter().map(|&(_, content)| content.to_owned().into_bytes()).collect(),
)
2024-09-13 07:30:23 -05:00
}