Skip to content
Merged
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
10 changes: 10 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ fn library() -> lu::Library<Config> {

lu::Library::default()
.with_function_norm("event", event)
.with_function_norm("boolean", boolean)
.with_function_norm("u8", u8)
.with_function_norm("u16", u16)
.with_function_norm("u32", u32)
Expand Down Expand Up @@ -169,6 +170,7 @@ extern "C-unwind" fn event(ctx: Context) -> lu::FnReturn {

#[derive(lu::Userdata, Clone)]
pub enum Type {
Boolean(BooleanType),
Number(NumberType),
Vector(VectorType),
BinaryString(BinaryStringType),
Expand All @@ -180,6 +182,14 @@ pub enum Type {
Struct(StructType),
}

#[derive(Clone)]
pub struct BooleanType;

extern "C-unwind" fn boolean(ctx: Context) -> lu::FnReturn {
ctx.push_userdata(Type::Boolean(BooleanType));
ctx.ret_with(1)
}

#[derive(Clone)]
pub struct NumberType {
pub kind: NumberKind,
Expand Down
1 change: 1 addition & 0 deletions src/header.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
--!nolint
-- This file was generated by zap
local buf, pos, len = buffer.create(1024), 0, 1024
local bit = { [true] = 1, [false] = 0 }
local function resize(bytes)
local newlen = pos * 2
if newlen < pos + bytes then
Expand Down
13 changes: 10 additions & 3 deletions src/hir/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
api,
hir::{
ArrayType, BinaryStringType, EnumType, Event, Item, Length, MapType, NumberType, SetType,
StructType, Type, Utf8StringType, VectorType,
ArrayType, BinaryStringType, BooleanType, EnumType, Event, Item, Length, MapType,
NumberType, SetType, StructType, Type, Utf8StringType, VectorType,
},
shared::Range,
};
Expand Down Expand Up @@ -35,6 +35,7 @@ impl From<api::Event> for Event {
impl From<api::Type> for Type {
fn from(value: api::Type) -> Self {
match value {
api::Type::Boolean(ty) => Type::Boolean(ty.into()),
api::Type::Number(ty) => Type::Number(ty.into()),
api::Type::Vector(ty) => Type::Vector(ty.into()),
api::Type::BinaryString(ty) => Type::BinaryString(ty.into()),
Expand All @@ -48,6 +49,12 @@ impl From<api::Type> for Type {
}
}

impl From<api::BooleanType> for BooleanType {
fn from(_: api::BooleanType) -> Self {
BooleanType
}
}

impl From<api::NumberType> for NumberType {
fn from(value: api::NumberType) -> Self {
let kind = value.kind;
Expand Down Expand Up @@ -141,7 +148,7 @@ impl From<api::StructType> for StructType {

impl From<Range> for Length {
fn from(value: Range) -> Self {
let min = value.min.map(|n| n as u32);
let min = value.min.map(|n| n as u32).unwrap_or(0);
let max = value.max.map(|n| n as u32);

Length { min, max }
Expand Down
18 changes: 14 additions & 4 deletions src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Event {

#[derive(Clone)]
pub enum Type {
Boolean(BooleanType),
Number(NumberType),
Vector(VectorType),
BinaryString(BinaryStringType),
Expand All @@ -31,6 +32,9 @@ pub enum Type {
Struct(StructType),
}

#[derive(Clone)]
pub struct BooleanType;

#[derive(Clone)]
pub struct NumberType {
pub kind: NumberKind,
Expand Down Expand Up @@ -86,13 +90,19 @@ pub struct StructType {

#[derive(Clone, Copy)]
pub struct Length {
pub min: Option<u32>,
pub min: u32,
pub max: Option<u32>,
}

impl Length {
pub fn exact(&self) -> Option<u32> {
if self.min == self.max { self.min } else { None }
if let Some(max) = self.max
&& self.min == max
{
Some(max)
} else {
None
}
}

pub fn kind(&self) -> NumberKind {
Expand All @@ -111,7 +121,7 @@ impl Length {
NumberType {
kind: self.kind(),
range: Range {
min: self.min.map(|min| min as f64),
min: Some(self.min as f64),
max: self.max.map(|max| max as f64),
},
}
Expand All @@ -121,7 +131,7 @@ impl Length {
impl From<Length> for Range {
fn from(value: Length) -> Self {
Range {
min: value.min.map(|min| min as f64),
min: Some(value.min as f64),
max: value.max.map(|max| max as f64),
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/hir/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Mul<hir::Length> for Size {
type Output = Self;

fn mul(self, rhs: hir::Length) -> Self::Output {
let min = rhs.min.unwrap_or(0);
let min = rhs.min;
let max = rhs.max;

Self {
Expand Down Expand Up @@ -83,6 +83,7 @@ impl Size {
impl hir::Type {
pub fn size(&self) -> Size {
match self {
hir::Type::Boolean(ty) => ty.size(),
hir::Type::Number(ty) => ty.size(),
hir::Type::Vector(ty) => ty.size(),
hir::Type::BinaryString(ty) => ty.size(),
Expand All @@ -96,6 +97,12 @@ impl hir::Type {
}
}

impl hir::BooleanType {
pub fn size(&self) -> Size {
Size::from(1)
}
}

impl hir::NumberType {
pub fn size(&self) -> Size {
Size {
Expand Down
12 changes: 12 additions & 0 deletions src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ pub enum Expr {
Vector(Box<Expr>, Box<Expr>, Box<Expr>),
Type(Box<Expr>),
Utf8(Box<Expr>),
Bit(Box<Expr>),
}

impl From<bool> for Expr {
Expand Down Expand Up @@ -368,6 +369,10 @@ impl Expr {
Expr::Binary(Box::new(self), BinaryOp::Mul, Box::new(rhs.into()))
}

pub fn mud(self, rhs: impl Into<Expr>) -> Self {
Expr::Binary(Box::new(self), BinaryOp::Mod, Box::new(rhs.into()))
}

pub fn eq(self, rhs: impl Into<Expr>) -> Self {
Expr::Binary(Box::new(self), BinaryOp::Eq, Box::new(rhs.into()))
}
Expand All @@ -391,6 +396,10 @@ impl Expr {
pub fn len(self) -> Self {
Expr::Unary(UnaryOp::Len, Box::new(self))
}

pub fn bit(self) -> Self {
Expr::Bit(Box::new(self))
}
}

impl Display for Expr {
Expand Down Expand Up @@ -423,6 +432,7 @@ impl Display for Expr {
Expr::Vector(x, y, z) => write!(f, "vector.create({x}, {y}, {z})"),
Expr::Type(expr) => write!(f, "type({expr})"),
Expr::Utf8(expr) => write!(f, "utf8.len({expr})"),
Expr::Bit(expr) => write!(f, "bit[{expr}]"),
}
}
}
Expand All @@ -433,6 +443,7 @@ pub enum BinaryOp {

Add,
Mul,
Mod,

Eq,
Lt,
Expand All @@ -447,6 +458,7 @@ impl Display for BinaryOp {
BinaryOp::And => write!(f, "and"),
BinaryOp::Add => write!(f, "+"),
BinaryOp::Mul => write!(f, "*"),
BinaryOp::Mod => write!(f, "%"),
BinaryOp::Eq => write!(f, "=="),
BinaryOp::Lt => write!(f, "<"),
BinaryOp::Gt => write!(f, ">"),
Expand Down
30 changes: 29 additions & 1 deletion src/mir/serdes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
hir,
mir::{
Expr, FuncD,
Expr, FuncD, FuncK,
builder::{Builder, InitVar},
},
shared::{ApiCheck, NumberKind, Range},
Expand Down Expand Up @@ -118,6 +118,7 @@ impl Serdes for hir::Type {
) -> impl Fn(&mut Builder, Expr) + use<'ty, 'ser> + 'ty {
#[allow(clippy::type_complexity)]
let cb: Box<dyn Fn(&mut Builder, Expr)> = match self {
hir::Type::Boolean(ty) => Box::new(ty.ser(b, ser)),
hir::Type::Number(ty) => Box::new(ty.ser(b, ser)),
hir::Type::Vector(ty) => Box::new(ty.ser(b, ser)),
hir::Type::BinaryString(ty) => Box::new(ty.ser(b, ser)),
Expand All @@ -140,6 +141,7 @@ impl Serdes for hir::Type {
des: &'des Des,
) -> impl Fn(&mut Builder) -> InitVar + use<'ty, 'des> + 'ty {
let cb: Box<dyn Fn(&mut Builder) -> InitVar> = match self {
hir::Type::Boolean(ty) => Box::new(ty.des(b, des)),
hir::Type::Number(ty) => Box::new(ty.des(b, des)),
hir::Type::Vector(ty) => Box::new(ty.des(b, des)),
hir::Type::BinaryString(ty) => Box::new(ty.des(b, des)),
Expand All @@ -155,6 +157,32 @@ impl Serdes for hir::Type {
}
}

impl Serdes for hir::BooleanType {
fn ser<'ty, 'b, 'ser: 'ty>(
&'ty self,
_: &'b mut Builder,
ser: &'ser Ser,
) -> impl Fn(&mut Builder, Expr) + use<'ty, 'ser> + 'ty {
move |b: &mut Builder, from: Expr| {
apicheck_full!(ser, check_type(b, from.clone(), "boolean"));

b.alloc_k(1);
b.write_k(FuncK::U8, from.bit());
}
}

fn des<'ty, 'b, 'des: 'ty>(
&'ty self,
_: &'b mut Builder,
_: &'des Des,
) -> impl Fn(&mut Builder) -> InitVar + use<'ty, 'des> + 'ty {
move |b: &mut Builder| {
let value = b.read_k(FuncK::U8);
b.expr(value.expr().eq(1))
}
}
}

impl Serdes for hir::NumberType {
fn ser<'ty, 'b, 'ser: 'ty>(
&'ty self,
Expand Down
2 changes: 2 additions & 0 deletions src/zap.d.luau
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ declare class ZapEvent
end

declare zap: {
boolean: () -> ZapType,

u8: (min: number?, max: number?) -> ZapNumberType,
u16: (min: number?, max: number?) -> ZapNumberType,
u32: (min: number?, max: number?) -> ZapNumberType,
Expand Down
Loading