diff options
| author | Arnaud Bailly <arnaud.bailly@iohk.io> | 2024-09-25 12:18:02 +0200 |
|---|---|---|
| committer | Arnaud Bailly <arnaud.bailly@iohk.io> | 2024-09-25 12:19:07 +0200 |
| commit | a99986ec4c903f116a5ed264f5cbbcba954c9338 (patch) | |
| tree | 4f3449a13877db4aeda1964d930fb8a8186ccfd5 /rust | |
| parent | b849745636c17a9015198c2a1865b8dbb43ff4a3 (diff) | |
| download | lambda-nantes-a99986ec4c903f116a5ed264f5cbbcba954c9338.tar.gz | |
Can parse and evaluate multiple expressions from a single input
Diffstat (limited to 'rust')
| -rw-r--r-- | rust/sample/test.txt | 6 | ||||
| -rw-r--r-- | rust/sample/test_normal.txt | 1 | ||||
| -rw-r--r-- | rust/src/parser.rs | 15 |
3 files changed, 17 insertions, 5 deletions
diff --git a/rust/sample/test.txt b/rust/sample/test.txt index 3cacc0b..76dc68a 100644 --- a/rust/sample/test.txt +++ b/rust/sample/test.txt @@ -1 +1,5 @@ -12
\ No newline at end of file +12 +foo + true + +(x x) diff --git a/rust/sample/test_normal.txt b/rust/sample/test_normal.txt new file mode 100644 index 0000000..00abde9 --- /dev/null +++ b/rust/sample/test_normal.txt @@ -0,0 +1 @@ +((lam x 1) ((lam x (x x)) (lam x (x x)))) diff --git a/rust/src/parser.rs b/rust/src/parser.rs index c54386c..48ef2e0 100644 --- a/rust/src/parser.rs +++ b/rust/src/parser.rs @@ -54,10 +54,12 @@ pub fn parse(arg: &str) -> Vec<Value> { let tokens = tokenize(arg); let mut parser = Parser { tokens, index: 0 }; let mut result = Vec::new(); - let expr = parse_expression(&mut parser) - .map_err(|e| panic!("Syntax error: {}", e)) - .unwrap(); - result.push(expr); + while parser.index < parser.tokens.len() { + let expr = parse_expression(&mut parser) + .map_err(|e| panic!("Syntax error: {}", e)) + .unwrap(); + result.push(expr); + } result } @@ -248,6 +250,11 @@ mod tests { ); } + #[test] + fn parse_multiple_values() { + assert_eq!(vec![Sym("foo".to_string()), Num(42)], parse("foo 42")); + } + impl Arbitrary for Value { type Parameters = (); type Strategy = BoxedStrategy<Self>; |
