Skip to main content

wat_ast/
result.rs

1use wast::parser::{self, Parse, Parser};
2
3use crate::{Atom, Expr, SExpr, ValueType};
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct Result {
7    value_types: Vec<ValueType>,
8}
9
10impl Result {
11    pub fn with_value_types(value_types: Vec<ValueType>) -> Self {
12        Self { value_types }
13    }
14}
15
16impl SExpr for Result {
17    fn car(&self) -> String {
18        "result".to_owned()
19    }
20
21    fn cdr(&self) -> Vec<Expr> {
22        self.value_types
23            .iter()
24            .map(|v| Expr::Atom(Atom::new(v.to_string())))
25            .collect()
26    }
27}
28
29impl Parse<'_> for Result {
30    fn parse(parser: Parser<'_>) -> parser::Result<Self> {
31        parser.parse::<wast::kw::result>()?;
32
33        let mut value_types = Vec::new();
34
35        while !parser.is_empty() {
36            value_types.push(parser.parse()?);
37        }
38
39        Ok(Self { value_types })
40    }
41}