wat/
wat.rs

1use general_parser::{Configuration, Expression}; // BinaryOperator,
2
3fn main() {
4	let allocator = bumpalo::Bump::new();
5
6	let expression = r#"(module
7  (func (result i32)
8    (i32.const 42)
9  )
10  (export "forty_two" (func 0))
11)"#;
12
13	let configuration = Configuration::default();
14
15	let expression: Expression<WASMValue> =
16		Expression::from_string(expression, &configuration, &allocator);
17
18	dbg!(expression);
19}
20
21#[derive(Debug)]
22pub enum WASMValue<'a> {
23	String(&'a str),
24	Index(usize),
25	Float(f64),
26	Keyword(&'a str),
27	Instruction(Instruction<'a>),
28}
29
30#[derive(Debug)]
31pub struct Instruction<'a>(pub &'a str);
32
33impl<'a> general_parser::Literal<'a> for WASMValue<'a> {
34	fn from_str(on: &'a str) -> Self {
35		if let Ok(idx) = on.parse::<usize>() {
36			Self::Index(idx)
37		} else if let Ok(idx) = on.parse::<f64>() {
38			Self::Float(idx)
39		} else if let Some(on) = on.strip_prefix('"').and_then(|rest| rest.strip_suffix('"')) {
40			Self::String(on)
41		} else if on.contains('.') {
42			// TODO check
43			Self::Instruction(Instruction(on))
44		} else {
45			Self::Keyword(on)
46		}
47	}
48}