Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sets #117

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion config.ebnf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ evdecl = "event", ident, '=', '{',

tydecl = "type", ident, '=', ty, [';'];

ty = ty-num | ty-str | ty-buf | ty-arr | ty-map | ty-opt | ty-ref | ty-enum | ty-struct | ty-instance;
ty = ty-num | ty-str | ty-buf | ty-arr | ty-map | ty-set | ty-opt | ty-ref | ty-enum | ty-struct | ty-instance;

ty-num = ("f32" | "f64"), ['(', range-num,')']
| ("u8" | "u16" | "u32" | "i8" | "i16" | "i32"), ['(', range-int,')'];
Expand All @@ -32,6 +32,7 @@ ty-str = "string", ['(', range-int,')'];
ty-buf = "buffer", ['(', range-int,')'];
ty-arr = ty, '[', range-num, ']';
ty-map = "map", '[', ty, ']', ':', ty;
ty-set = "set", '{', ty, '}'
ty-opt = ty, '?';
ty-ref = ident;

Expand Down
10 changes: 9 additions & 1 deletion docs/.vitepress/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const beforeMount = (monaco: Monaco) => {

const Keywords = ["event", "opt", "type"] as const;

const TypeKeywords = ["enum", "struct", "map"] as const;
const TypeKeywords = ["enum", "struct", "map", "set"] as const;

const Operators = ["true", "false"] as const;

Expand Down Expand Up @@ -360,6 +360,14 @@ const beforeMount = (monaco: Monaco) => {
documentation: "Map",
range: range,
},
{
label: "set",
kind: monaco.languages.CompletionItemKind.Snippet,
insertText: "set { $1 }\n",
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
documentation: "Set",
range: range,
}
{
label: "struct",
kind: monaco.languages.CompletionItemKind.Snippet,
Expand Down
6 changes: 6 additions & 0 deletions docs/config/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ Maps are defined using the `map` keyword, followed by a Luau-like map syntax. Fo

<CodeBlock code="map { [string]: u8 }" />

## Sets

Sets are equivalent to a map where all values are `true`, and are defined using the `set` keyword. For example, a map of `string` keys to `true` would look like:

<CodeBlock code="set { string }" />

## Enums

Zap has two types of enums, unit enums and tagged enums.
Expand Down
3 changes: 3 additions & 0 deletions zap/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub enum Ty<'src> {
Buf(Range),
Arr(Box<Ty<'src>>, Range),
Map(Box<Ty<'src>>, Box<Ty<'src>>),
Set(Box<Ty<'src>>),
Opt(Box<Ty<'src>>),
Ref(&'src str),

Expand Down Expand Up @@ -196,6 +197,8 @@ impl<'src> Ty<'src> {

Self::Map(..) => (2, None),

Self::Set(..) => (2, None),

Self::Opt(ty) => {
let (_, ty_max) = ty.size(tydecls, recursed);

Expand Down
18 changes: 18 additions & 0 deletions zap/src/irgen/des.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,24 @@ impl Des {
self.push_stmt(Stmt::End);
}

Ty::Set(key) => {
self.push_assign(into.clone(), Expr::EmptyTable);

self.push_stmt(Stmt::NumFor {
var: "_".into(),
from: 1.0.into(),
to: self.readu16(),
});

let (key_name, key_expr) = self.add_occurrence("key");

self.push_ty(key, Var::Name(key_name.clone()));
Ketasaja marked this conversation as resolved.
Show resolved Hide resolved

self.push_assign(into.clone().eindex(key_expr.clone()), Expr::True);

self.push_stmt(Stmt::End);
}

Ty::Opt(ty) => {
self.push_stmt(Stmt::If(self.readu8().eq(1.0.into())));

Expand Down
28 changes: 28 additions & 0 deletions zap/src/irgen/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,34 @@ impl Ser {
));
}

Ty::Set(key) => {
let (len_name, len_expr) = self.add_occurrence("len");
let (len_pos_name, len_pos_expr) = self.add_occurrence("len_pos");

self.push_local(len_pos_name.clone(), Some(Var::from("alloc").call(vec![2.0.into()])));
self.push_local(len_name.clone(), Some(0.0.into()));

let (key_name, _) = self.add_occurrence("k");
let (val_name, _) = self.add_occurrence("_");
Ketasaja marked this conversation as resolved.
Show resolved Hide resolved

self.push_stmt(Stmt::GenFor {
key: key_name.clone(),
val: val_name.clone(),
obj: from_expr,
});

self.push_assign(Var::Name(len_name.clone()), len_expr.clone().add(1.0.into()));
self.push_ty(key, key_name.as_str().into());

self.push_stmt(Stmt::End);

self.push_stmt(Stmt::Call(
Var::from("buffer").nindex("writeu16"),
None,
vec!["outgoing_buff".into(), len_pos_expr.clone(), len_expr.clone()],
));
}

Ty::Opt(ty) => {
self.push_stmt(Stmt::If(from_expr.clone().eq(Expr::Nil)));

Expand Down
7 changes: 7 additions & 0 deletions zap/src/output/luau/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ pub trait Output {
self.push(" }");
}

Ty::Set(key) => {
self.push("{ [");
self.push_ty(key);
self.push("]: true");
self.push(" }");
}

Ty::Opt(ty) => {
self.push_ty(ty);

Expand Down
6 changes: 6 additions & 0 deletions zap/src/output/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ pub trait Output: ConfigProvider {
self.push(">");
}

Ty::Set(key) => {
self.push("Map<");
Ketasaja marked this conversation as resolved.
Show resolved Hide resolved
self.push_ty(key);
self.push(", true>");
Ketasaja marked this conversation as resolved.
Show resolved Hide resolved
}

Ty::Opt(ty) => {
self.push_ty(ty);

Expand Down
6 changes: 6 additions & 0 deletions zap/src/parser/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,12 @@ impl<'src> Converter<'src> {
Ty::Map(Box::new(key_ty), Box::new(val_ty))
}

SyntaxTyKind::Set(key) => {
let key_ty = self.ty(key);

Ty::Set(Box::new(key_ty))
}

SyntaxTyKind::Opt(ty) => {
let parsed_ty = self.ty(ty);

Expand Down
2 changes: 2 additions & 0 deletions zap/src/parser/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ TyKind: SyntaxTyKind<'input> = {

<ty:Ty> "[" <r:IntRange?> "]" => SyntaxTyKind::Arr(Box::new(ty), r),
"map" "{" "[" <k:Ty> "]" ":" <v:Ty> "}" => SyntaxTyKind::Map(Box::new(k), Box::new(v)),
"set" "{" <k:Ty> "}" => SyntaxTyKind::Set(Box::new(k)),

<ty:Ty> "?" => SyntaxTyKind::Opt(Box::new(ty)),
<name:Identifier> => SyntaxTyKind::Ref(name),
Expand Down Expand Up @@ -200,6 +201,7 @@ Identifier: SyntaxIdentifier<'input> = {
<start:@L> "enum" <end:@R> => SyntaxIdentifier { start, name: "enum", end },
<start:@L> "struct" <end:@R> => SyntaxIdentifier { start, name: "struct", end },
<start:@L> "map" <end:@R> => SyntaxIdentifier { start, name: "map", end },
<start:@L> "set" <end:@R> => SyntaxIdentifier { start, name: "set", end },
<start:@L> "true" <end:@R> => SyntaxIdentifier { start, name: "true", end },
<start:@L> "false" <end:@R> => SyntaxIdentifier { start, name: "false", end },
}
Expand Down
1 change: 1 addition & 0 deletions zap/src/parser/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub enum SyntaxTyKind<'src> {
Buf(Option<SyntaxRange<'src>>),
Arr(Box<SyntaxTy<'src>>, Option<SyntaxRange<'src>>),
Map(Box<SyntaxTy<'src>>, Box<SyntaxTy<'src>>),
Set(Box<SyntaxTy<'src>>),
Opt(Box<SyntaxTy<'src>>),
Ref(SyntaxIdentifier<'src>),

Expand Down