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

wasm-wave: Accept nullary func calls #1817

Merged
merged 1 commit into from
Sep 23, 2024
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
8 changes: 7 additions & 1 deletion crates/wasm-wave/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ impl<'source> Parser<'source> {
let name = self.parse_label()?;
self.advance()?;
self.expect_token(Token::ParenOpen)?;
let params = self.parse_tuple()?;

let params = if self.next_is(Token::ParenClose) {
self.advance()?;
None
} else {
Some(self.parse_tuple()?)
};
Ok(UntypedFuncCall::new(self.lex.source(), name, params))
}

Expand Down
24 changes: 18 additions & 6 deletions crates/wasm-wave/src/untyped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ impl std::fmt::Display for UntypedValue<'_> {
pub struct UntypedFuncCall<'source> {
source: Cow<'source, str>,
name: Node,
params: Node,
params: Option<Node>,
}

impl<'source> UntypedFuncCall<'source> {
pub(crate) fn new(source: impl Into<Cow<'source, str>>, name: Node, params: Node) -> Self {
pub(crate) fn new(
source: impl Into<Cow<'source, str>>,
name: Node,
params: Option<Node>,
) -> Self {
Self {
source: source.into(),
name,
Expand Down Expand Up @@ -100,8 +104,10 @@ impl<'source> UntypedFuncCall<'source> {
}

/// Returns the function parameters node.
pub fn params_node(&self) -> &Node {
&self.params
///
/// Returns `None` if the function call has no parameters.
pub fn params_node(&self) -> Option<&Node> {
self.params.as_ref()
}

/// Returns the function name.
Expand All @@ -117,14 +123,20 @@ impl<'source> UntypedFuncCall<'source> {
&self,
types: impl IntoIterator<Item = &'types V::Type>,
) -> Result<Vec<V>, ParserError> {
self.params.to_wasm_params(types, self.source())
match &self.params {
Some(params) => params.to_wasm_params(types, self.source()),
None => Ok(vec![]),
}
}
}

impl std::fmt::Display for UntypedFuncCall<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.name.slice(&self.source))?;
fmt_node(f, &self.params, &self.source)
match &self.params {
Some(params) => fmt_node(f, params, &self.source),
None => f.write_str("()"),
}
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/wasm-wave/tests/ui/accept-nullary.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nullary()
1 change: 1 addition & 0 deletions crates/wasm-wave/tests/ui/accept-nullary.waves
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nullary();
1 change: 1 addition & 0 deletions crates/wasm-wave/tests/ui/ui.wit
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ui:tests;

interface ui {
nullary: func();
%float32: func(val: f32);
%list-float32: func(vals: list<f32>);
%float64: func(val: f64);
Expand Down