summaryrefslogtreecommitdiff
path: root/rust/src/parser.rs
diff options
context:
space:
mode:
authorArnaud Bailly <arnaud.bailly@iohk.io>2024-09-25 12:12:53 +0200
committerArnaud Bailly <arnaud.bailly@iohk.io>2024-09-25 12:12:53 +0200
commitb849745636c17a9015198c2a1865b8dbb43ff4a3 (patch)
treecf3e8f34672a183f42dc8bf0c35a92afd1b3c381 /rust/src/parser.rs
parentec3637ab27df1fc9faeaac9977ee9e184cf4942e (diff)
downloadlambda-nantes-b849745636c17a9015198c2a1865b8dbb43ff4a3.tar.gz
Prepare parser to return multiple values
Diffstat (limited to 'rust/src/parser.rs')
-rw-r--r--rust/src/parser.rs29
1 files changed, 16 insertions, 13 deletions
diff --git a/rust/src/parser.rs b/rust/src/parser.rs
index 9dc0bb8..c54386c 100644
--- a/rust/src/parser.rs
+++ b/rust/src/parser.rs
@@ -50,12 +50,15 @@ impl Parser {
}
}
-pub fn parse(arg: &str) -> Value {
+pub fn parse(arg: &str) -> Vec<Value> {
let tokens = tokenize(arg);
let mut parser = Parser { tokens, index: 0 };
- parse_expression(&mut parser)
+ let mut result = Vec::new();
+ let expr = parse_expression(&mut parser)
.map_err(|e| panic!("Syntax error: {}", e))
- .unwrap()
+ .unwrap();
+ result.push(expr);
+ result
}
fn parse_expression(parser: &mut Parser) -> Result<Value, String> {
@@ -168,26 +171,26 @@ mod tests {
#[test]
fn parse_integer_as_number(i in -1000i32..1000) {
let result = parse(&i.to_string());
- assert_eq!(Num(i), result);
+ assert_eq!(vec![Num(i)], result);
}
}
#[test]
fn parse_truth_values_as_booleans() {
- assert_eq!(Bool(true), parse("true"));
- assert_eq!(Bool(false), parse("false"));
+ assert_eq!(vec![Bool(true)], parse("true"));
+ assert_eq!(vec![Bool(false)], parse("false"));
}
#[test]
fn parse_identifiers_values_as_symbols() {
- assert_eq!(Sym("foo".to_string()), parse("foo"));
+ assert_eq!(vec![Sym("foo".to_string())], parse("foo"));
}
#[test]
fn ignores_whitespace() {
- assert_eq!(Sym("foo".to_string()), parse(" foo \n\r"));
- assert_eq!(Num(-42), parse("\n-42"));
+ assert_eq!(vec![Sym("foo".to_string())], parse(" foo \n\r"));
+ assert_eq!(vec![Num(-42)], parse("\n-42"));
}
#[test]
@@ -226,7 +229,7 @@ mod tests {
#[test]
fn parse_application_of_two_values() {
assert_eq!(
- App(Box::new(Sym("foo".to_string())), Box::new(Num(42))),
+ vec![App(Box::new(Sym("foo".to_string())), Box::new(Num(42)))],
parse("(foo 42)")
);
}
@@ -234,13 +237,13 @@ mod tests {
#[test]
fn parse_abstraction() {
assert_eq!(
- Lam(
+ vec![Lam(
"x".to_string(),
Box::new(App(
Box::new(Sym("x".to_string())),
Box::new(Sym("x".to_string()))
))
- ),
+ )],
parse("(lam x (x x))")
);
}
@@ -271,7 +274,7 @@ mod tests {
#[test]
fn parse_is_inverse_to_display(values in any::<Vec<Value>>()) {
let result : Vec<String> = values.iter().map(|v:&Value| v.to_string()).collect();
- assert_eq!(values, result.iter().map(|s| parse(s)).collect::<Vec<Value>>());
+ assert_eq!(values, result.iter().flat_map(|s| parse(s)).collect::<Vec<Value>>());
}
}
}