pub struct Expression<'a, T> {
pub on: T,
pub arguments: Vec<Expression<'a, T>, &'a Allocator>,
}Fields§
§on: T§arguments: Vec<Expression<'a, T>, &'a Allocator>Implementations§
Source§impl<'a, T> Expression<'a, T>where
T: Literal<'a>,
impl<'a, T> Expression<'a, T>where
T: Literal<'a>,
Sourcepub fn from_string(
source: &'a str,
configuration: &'a Configuration<'_>,
allocator: &'a Allocator,
) -> Self
pub fn from_string( source: &'a str, configuration: &'a Configuration<'_>, allocator: &'a Allocator, ) -> Self
Examples found in repository?
examples/wat.rs (line 16)
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}More examples
examples/operations.rs (line 21)
3fn main() {
4 let multiply_operator = BinaryOperator { representation: "*", precedence: 4 };
5 let sources = &["3xy+rx", "3*x*y+r*x", "(x + t)(x + z)", "sin(-x)"];
6 let configuration = Configuration {
7 prefix_unary_operators: vec![UnaryOperator { representation: "-", precedence: 2 }],
8 binary_operators: vec![
9 BinaryOperator { representation: "^", precedence: 5 },
10 BinaryOperator { representation: "*", precedence: 4 },
11 multiply_operator,
12 BinaryOperator { representation: "+", precedence: 3 },
13 ],
14 adjacency: Some(Adjacency { operator: multiply_operator, functions: vec!["sin"] }),
15 ..Default::default()
16 };
17
18 for source in sources {
19 let allocator = bumpalo::Bump::new();
20 let expression: Expression<&str> =
21 Expression::from_string(source, &configuration, &allocator);
22 eprintln!("{expression:#?}");
23 }
24}examples/parse.rs (line 22)
6fn main() {
7 let arg = std::env::args().nth(1);
8
9 if let Some("--interactive") = arg.as_deref() {
10 run_interactive();
11 return;
12 }
13
14 if let Some(path) = std::env::args().nth(1) {
15 let source = std::fs::read_to_string(&path).unwrap();
16 let (configuration, source) = extract_configuration_and_source(&source);
17
18 eprintln!("{configuration:?}");
19
20 let allocator = bumpalo::Bump::new();
21 let expression: Expression<&str> =
22 Expression::from_string(&source, &configuration, &allocator);
23
24 let expression = ExpressionRepresentation(&expression);
25 println!("{source}\n -> {expression}");
26 } else {
27 let configuration = Configuration {
28 binary_operators: vec![
29 BinaryOperator { representation: "*", precedence: 4 },
30 BinaryOperator { representation: "+", precedence: 3 },
31 ],
32 ..Default::default()
33 };
34
35 let sources: &[&str] = &["(x (a * b) (d * 2 + e))", "(x (a b) (c d e))"];
36
37 for source in sources {
38 let allocator = bumpalo::Bump::new();
39 let expression: Expression<&str> =
40 Expression::from_string(source, &configuration, &allocator);
41 let expression = ExpressionRepresentation(&expression);
42 eprintln!("{source}\n -> {expression}");
43 }
44 }
45}
46
47fn run_interactive() {
48 use std::io::{BufRead, stdin};
49 let stdin = stdin();
50 let mut buf = Vec::new();
51
52 println!("start");
53
54 for line in stdin.lock().lines().map_while(Result::ok) {
55 if line == "close" {
56 if !buf.is_empty() {
57 eprintln!("no end to message {buf:?}");
58 }
59 break;
60 }
61
62 if line == "end" {
63 let output = String::from_utf8_lossy(&buf);
64
65 let (configuration, source) = extract_configuration_and_source(&output);
66
67 // eprintln!("{configuration:?} {source}");
68
69 {
70 let allocator = bumpalo::Bump::new();
71 let expression: Expression<&str> =
72 Expression::from_string(&source, &configuration, &allocator);
73 let expression = ExpressionRepresentation(&expression);
74 println!("{expression}");
75 }
76
77 // if let Err(error) = out {
78 // println!("Error: {error:?}");
79 // }
80
81 println!("end");
82 buf.clear();
83 continue;
84 }
85
86 buf.extend_from_slice(line.as_bytes());
87 buf.push(b'\n');
88 }
89}pub fn from_reader( reader: &mut Lexer<'a>, configuration: &'a Configuration<'_>, allocator: &'a Allocator, ) -> Self
Trait Implementations§
Auto Trait Implementations§
impl<'a, T> Freeze for Expression<'a, T>where
T: Freeze,
impl<'a, T> !RefUnwindSafe for Expression<'a, T>
impl<'a, T> !Send for Expression<'a, T>
impl<'a, T> !Sync for Expression<'a, T>
impl<'a, T> Unpin for Expression<'a, T>where
T: Unpin,
impl<'a, T> !UnwindSafe for Expression<'a, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more