Skip to content

Commit 9eaaf5f

Browse files
authored
wasm-wave: Accept nullary func calls (#1817)
1 parent 6fc5d68 commit 9eaaf5f

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

crates/wasm-wave/src/parser.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ impl<'source> Parser<'source> {
5151
let name = self.parse_label()?;
5252
self.advance()?;
5353
self.expect_token(Token::ParenOpen)?;
54-
let params = self.parse_tuple()?;
54+
55+
let params = if self.next_is(Token::ParenClose) {
56+
self.advance()?;
57+
None
58+
} else {
59+
Some(self.parse_tuple()?)
60+
};
5561
Ok(UntypedFuncCall::new(self.lex.source(), name, params))
5662
}
5763

crates/wasm-wave/src/untyped.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ impl std::fmt::Display for UntypedValue<'_> {
6060
pub struct UntypedFuncCall<'source> {
6161
source: Cow<'source, str>,
6262
name: Node,
63-
params: Node,
63+
params: Option<Node>,
6464
}
6565

6666
impl<'source> UntypedFuncCall<'source> {
67-
pub(crate) fn new(source: impl Into<Cow<'source, str>>, name: Node, params: Node) -> Self {
67+
pub(crate) fn new(
68+
source: impl Into<Cow<'source, str>>,
69+
name: Node,
70+
params: Option<Node>,
71+
) -> Self {
6872
Self {
6973
source: source.into(),
7074
name,
@@ -100,8 +104,10 @@ impl<'source> UntypedFuncCall<'source> {
100104
}
101105

102106
/// Returns the function parameters node.
103-
pub fn params_node(&self) -> &Node {
104-
&self.params
107+
///
108+
/// Returns `None` if the function call has no parameters.
109+
pub fn params_node(&self) -> Option<&Node> {
110+
self.params.as_ref()
105111
}
106112

107113
/// Returns the function name.
@@ -117,14 +123,20 @@ impl<'source> UntypedFuncCall<'source> {
117123
&self,
118124
types: impl IntoIterator<Item = &'types V::Type>,
119125
) -> Result<Vec<V>, ParserError> {
120-
self.params.to_wasm_params(types, self.source())
126+
match &self.params {
127+
Some(params) => params.to_wasm_params(types, self.source()),
128+
None => Ok(vec![]),
129+
}
121130
}
122131
}
123132

124133
impl std::fmt::Display for UntypedFuncCall<'_> {
125134
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126135
f.write_str(self.name.slice(&self.source))?;
127-
fmt_node(f, &self.params, &self.source)
136+
match &self.params {
137+
Some(params) => fmt_node(f, params, &self.source),
138+
None => f.write_str("()"),
139+
}
128140
}
129141
}
130142

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nullary()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nullary();

crates/wasm-wave/tests/ui/ui.wit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ui:tests;
22

33
interface ui {
4+
nullary: func();
45
%float32: func(val: f32);
56
%list-float32: func(vals: list<f32>);
67
%float64: func(val: f64);

0 commit comments

Comments
 (0)