Skip to content

Commit 9dfc6b4

Browse files
authored
initial boolean support (#16)
1 parent 7ecb690 commit 9dfc6b4

8 files changed

Lines changed: 86 additions & 9 deletions

File tree

src/api.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ fn library() -> lu::Library<Config> {
110110

111111
lu::Library::default()
112112
.with_function_norm("event", event)
113+
.with_function_norm("boolean", boolean)
113114
.with_function_norm("u8", u8)
114115
.with_function_norm("u16", u16)
115116
.with_function_norm("u32", u32)
@@ -169,6 +170,7 @@ extern "C-unwind" fn event(ctx: Context) -> lu::FnReturn {
169170

170171
#[derive(lu::Userdata, Clone)]
171172
pub enum Type {
173+
Boolean(BooleanType),
172174
Number(NumberType),
173175
Vector(VectorType),
174176
BinaryString(BinaryStringType),
@@ -180,6 +182,14 @@ pub enum Type {
180182
Struct(StructType),
181183
}
182184

185+
#[derive(Clone)]
186+
pub struct BooleanType;
187+
188+
extern "C-unwind" fn boolean(ctx: Context) -> lu::FnReturn {
189+
ctx.push_userdata(Type::Boolean(BooleanType));
190+
ctx.ret_with(1)
191+
}
192+
183193
#[derive(Clone)]
184194
pub struct NumberType {
185195
pub kind: NumberKind,

src/header.luau

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
--!nolint
33
-- This file was generated by zap
44
local buf, pos, len = buffer.create(1024), 0, 1024
5+
local bit = { [true] = 1, [false] = 0 }
56
local function resize(bytes)
67
local newlen = pos * 2
78
if newlen < pos + bytes then

src/hir/build.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
22
api,
33
hir::{
4-
ArrayType, BinaryStringType, EnumType, Event, Item, Length, MapType, NumberType, SetType,
5-
StructType, Type, Utf8StringType, VectorType,
4+
ArrayType, BinaryStringType, BooleanType, EnumType, Event, Item, Length, MapType,
5+
NumberType, SetType, StructType, Type, Utf8StringType, VectorType,
66
},
77
shared::Range,
88
};
@@ -35,6 +35,7 @@ impl From<api::Event> for Event {
3535
impl From<api::Type> for Type {
3636
fn from(value: api::Type) -> Self {
3737
match value {
38+
api::Type::Boolean(ty) => Type::Boolean(ty.into()),
3839
api::Type::Number(ty) => Type::Number(ty.into()),
3940
api::Type::Vector(ty) => Type::Vector(ty.into()),
4041
api::Type::BinaryString(ty) => Type::BinaryString(ty.into()),
@@ -48,6 +49,12 @@ impl From<api::Type> for Type {
4849
}
4950
}
5051

52+
impl From<api::BooleanType> for BooleanType {
53+
fn from(_: api::BooleanType) -> Self {
54+
BooleanType
55+
}
56+
}
57+
5158
impl From<api::NumberType> for NumberType {
5259
fn from(value: api::NumberType) -> Self {
5360
let kind = value.kind;
@@ -141,7 +148,7 @@ impl From<api::StructType> for StructType {
141148

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

147154
Length { min, max }

src/hir/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct Event {
2020

2121
#[derive(Clone)]
2222
pub enum Type {
23+
Boolean(BooleanType),
2324
Number(NumberType),
2425
Vector(VectorType),
2526
BinaryString(BinaryStringType),
@@ -31,6 +32,9 @@ pub enum Type {
3132
Struct(StructType),
3233
}
3334

35+
#[derive(Clone)]
36+
pub struct BooleanType;
37+
3438
#[derive(Clone)]
3539
pub struct NumberType {
3640
pub kind: NumberKind,
@@ -86,13 +90,19 @@ pub struct StructType {
8690

8791
#[derive(Clone, Copy)]
8892
pub struct Length {
89-
pub min: Option<u32>,
93+
pub min: u32,
9094
pub max: Option<u32>,
9195
}
9296

9397
impl Length {
9498
pub fn exact(&self) -> Option<u32> {
95-
if self.min == self.max { self.min } else { None }
99+
if let Some(max) = self.max
100+
&& self.min == max
101+
{
102+
Some(max)
103+
} else {
104+
None
105+
}
96106
}
97107

98108
pub fn kind(&self) -> NumberKind {
@@ -111,7 +121,7 @@ impl Length {
111121
NumberType {
112122
kind: self.kind(),
113123
range: Range {
114-
min: self.min.map(|min| min as f64),
124+
min: Some(self.min as f64),
115125
max: self.max.map(|max| max as f64),
116126
},
117127
}
@@ -121,7 +131,7 @@ impl Length {
121131
impl From<Length> for Range {
122132
fn from(value: Length) -> Self {
123133
Range {
124-
min: value.min.map(|min| min as f64),
134+
min: Some(value.min as f64),
125135
max: value.max.map(|max| max as f64),
126136
}
127137
}

src/hir/size.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Mul<hir::Length> for Size {
4848
type Output = Self;
4949

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

5454
Self {
@@ -83,6 +83,7 @@ impl Size {
8383
impl hir::Type {
8484
pub fn size(&self) -> Size {
8585
match self {
86+
hir::Type::Boolean(ty) => ty.size(),
8687
hir::Type::Number(ty) => ty.size(),
8788
hir::Type::Vector(ty) => ty.size(),
8889
hir::Type::BinaryString(ty) => ty.size(),
@@ -96,6 +97,12 @@ impl hir::Type {
9697
}
9798
}
9899

100+
impl hir::BooleanType {
101+
pub fn size(&self) -> Size {
102+
Size::from(1)
103+
}
104+
}
105+
99106
impl hir::NumberType {
100107
pub fn size(&self) -> Size {
101108
Size {

src/mir/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ pub enum Expr {
313313
Vector(Box<Expr>, Box<Expr>, Box<Expr>),
314314
Type(Box<Expr>),
315315
Utf8(Box<Expr>),
316+
Bit(Box<Expr>),
316317
}
317318

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

372+
pub fn mud(self, rhs: impl Into<Expr>) -> Self {
373+
Expr::Binary(Box::new(self), BinaryOp::Mod, Box::new(rhs.into()))
374+
}
375+
371376
pub fn eq(self, rhs: impl Into<Expr>) -> Self {
372377
Expr::Binary(Box::new(self), BinaryOp::Eq, Box::new(rhs.into()))
373378
}
@@ -391,6 +396,10 @@ impl Expr {
391396
pub fn len(self) -> Self {
392397
Expr::Unary(UnaryOp::Len, Box::new(self))
393398
}
399+
400+
pub fn bit(self) -> Self {
401+
Expr::Bit(Box::new(self))
402+
}
394403
}
395404

396405
impl Display for Expr {
@@ -423,6 +432,7 @@ impl Display for Expr {
423432
Expr::Vector(x, y, z) => write!(f, "vector.create({x}, {y}, {z})"),
424433
Expr::Type(expr) => write!(f, "type({expr})"),
425434
Expr::Utf8(expr) => write!(f, "utf8.len({expr})"),
435+
Expr::Bit(expr) => write!(f, "bit[{expr}]"),
426436
}
427437
}
428438
}
@@ -433,6 +443,7 @@ pub enum BinaryOp {
433443

434444
Add,
435445
Mul,
446+
Mod,
436447

437448
Eq,
438449
Lt,
@@ -447,6 +458,7 @@ impl Display for BinaryOp {
447458
BinaryOp::And => write!(f, "and"),
448459
BinaryOp::Add => write!(f, "+"),
449460
BinaryOp::Mul => write!(f, "*"),
461+
BinaryOp::Mod => write!(f, "%"),
450462
BinaryOp::Eq => write!(f, "=="),
451463
BinaryOp::Lt => write!(f, "<"),
452464
BinaryOp::Gt => write!(f, ">"),

src/mir/serdes.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
hir,
33
mir::{
4-
Expr, FuncD,
4+
Expr, FuncD, FuncK,
55
builder::{Builder, InitVar},
66
},
77
shared::{ApiCheck, NumberKind, Range},
@@ -118,6 +118,7 @@ impl Serdes for hir::Type {
118118
) -> impl Fn(&mut Builder, Expr) + use<'ty, 'ser> + 'ty {
119119
#[allow(clippy::type_complexity)]
120120
let cb: Box<dyn Fn(&mut Builder, Expr)> = match self {
121+
hir::Type::Boolean(ty) => Box::new(ty.ser(b, ser)),
121122
hir::Type::Number(ty) => Box::new(ty.ser(b, ser)),
122123
hir::Type::Vector(ty) => Box::new(ty.ser(b, ser)),
123124
hir::Type::BinaryString(ty) => Box::new(ty.ser(b, ser)),
@@ -140,6 +141,7 @@ impl Serdes for hir::Type {
140141
des: &'des Des,
141142
) -> impl Fn(&mut Builder) -> InitVar + use<'ty, 'des> + 'ty {
142143
let cb: Box<dyn Fn(&mut Builder) -> InitVar> = match self {
144+
hir::Type::Boolean(ty) => Box::new(ty.des(b, des)),
143145
hir::Type::Number(ty) => Box::new(ty.des(b, des)),
144146
hir::Type::Vector(ty) => Box::new(ty.des(b, des)),
145147
hir::Type::BinaryString(ty) => Box::new(ty.des(b, des)),
@@ -155,6 +157,32 @@ impl Serdes for hir::Type {
155157
}
156158
}
157159

160+
impl Serdes for hir::BooleanType {
161+
fn ser<'ty, 'b, 'ser: 'ty>(
162+
&'ty self,
163+
_: &'b mut Builder,
164+
ser: &'ser Ser,
165+
) -> impl Fn(&mut Builder, Expr) + use<'ty, 'ser> + 'ty {
166+
move |b: &mut Builder, from: Expr| {
167+
apicheck_full!(ser, check_type(b, from.clone(), "boolean"));
168+
169+
b.alloc_k(1);
170+
b.write_k(FuncK::U8, from.bit());
171+
}
172+
}
173+
174+
fn des<'ty, 'b, 'des: 'ty>(
175+
&'ty self,
176+
_: &'b mut Builder,
177+
_: &'des Des,
178+
) -> impl Fn(&mut Builder) -> InitVar + use<'ty, 'des> + 'ty {
179+
move |b: &mut Builder| {
180+
let value = b.read_k(FuncK::U8);
181+
b.expr(value.expr().eq(1))
182+
}
183+
}
184+
}
185+
158186
impl Serdes for hir::NumberType {
159187
fn ser<'ty, 'b, 'ser: 'ty>(
160188
&'ty self,

src/zap.d.luau

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ declare class ZapEvent
88
end
99

1010
declare zap: {
11+
boolean: () -> ZapType,
12+
1113
u8: (min: number?, max: number?) -> ZapNumberType,
1214
u16: (min: number?, max: number?) -> ZapNumberType,
1315
u32: (min: number?, max: number?) -> ZapNumberType,

0 commit comments

Comments
 (0)