1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use wast::parser::{self, Parse, Parser};
use crate::{Atom, Expr, Index, Param, Result, SExpr};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeUse {
type_def: Option<Type>,
params: Vec<Param>,
results: Vec<Result>,
}
impl TypeUse {
pub fn new(
type_def: Option<Type>,
params: Vec<Param>,
results: Vec<Result>,
) -> Self {
Self {
type_def,
params,
results,
}
}
pub(crate) fn exprs(&self) -> Vec<Expr> {
let mut v = Vec::new();
if let Some(ref type_def) = self.type_def {
v.push(Expr::SExpr(Box::new(type_def.clone())));
}
v.append(
&mut self
.params
.iter()
.map(|p| Expr::SExpr(Box::new(p.clone())))
.collect(),
);
v.append(
&mut self
.results
.iter()
.map(|r| Expr::SExpr(Box::new(r.clone())))
.collect(),
);
v
}
}
impl Parse<'_> for TypeUse {
fn parse(parser: Parser<'_>) -> parser::Result<Self> {
let mut type_def = None;
if parser.peek2::<wast::kw::r#type>() {
type_def = Some(parser.parens(Type::parse)?);
}
let mut params = Vec::new();
let mut results = Vec::new();
while !parser.is_empty() {
if parser.peek2::<wast::kw::param>() {
params.push(parser.parens(Param::parse)?)
} else {
break;
}
}
while !parser.is_empty() {
if parser.peek2::<wast::kw::result>() {
results.push(parser.parens(Result::parse)?)
} else {
break;
}
}
Ok(Self {
type_def,
params,
results,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Type {
pub idx: Index,
}
impl SExpr for Type {
fn car(&self) -> String {
"type".to_owned()
}
fn cdr(&self) -> Vec<Expr> {
vec![Expr::Atom(Atom::new(self.idx.to_string()))]
}
}
impl Parse<'_> for Type {
fn parse(parser: Parser<'_>) -> parser::Result<Self> {
parser.parse::<wast::kw::r#type>()?;
let idx = parser.parse::<Index>()?;
Ok(Self { idx })
}
}