Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the reverse function call more useful #518

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion book/src/list-functions-lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn cons<A>(x: A, xs: List<A>) -> List<A>
Append an element to the end of a list.

```nbt
fn cons_end<A>(xs: List<A>, x: A) -> List<A>
fn cons_end<A>(x: A, xs: List<A>) -> List<A>
```

### `is_empty`
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/lists.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ assert_eq(tail(xs), [2, 3])

assert_eq(cons(0, xs), [0, 1, 2, 3])

assert_eq(cons_end(xs, 4), [1, 2, 3, 4])
assert_eq(cons_end([], 0), [0])
assert_eq(cons_end(4, xs), [1, 2, 3, 4])
assert_eq(cons_end(0, []), [0])

assert(is_empty([]))
assert(!is_empty(xs))
Expand Down
4 changes: 2 additions & 2 deletions numbat/modules/core/lists.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn tail<A>(xs: List<A>) -> List<A>
fn cons<A>(x: A, xs: List<A>) -> List<A>

@description("Append an element to the end of a list")
fn cons_end<A>(xs: List<A>, x: A) -> List<A>
fn cons_end<A>(x: A, xs: List<A>) -> List<A>

@description("Check if a list is empty")
fn is_empty<A>(xs: List<A>) -> Bool = xs == []
Expand Down Expand Up @@ -55,7 +55,7 @@ fn range(start: Scalar, end: Scalar) -> List<Scalar> =
fn reverse<A>(xs: List<A>) -> List<A> =
if is_empty(xs)
then []
else cons_end(reverse(tail(xs)), head(xs))
else cons_end(head(xs), reverse(tail(xs)))

@description("Generate a new list by applying a function to each element of the input list")
fn map<A, B>(f: Fn[(A) -> B], xs: List<A>) -> List<B> =
Expand Down
2 changes: 1 addition & 1 deletion numbat/src/ffi/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub fn cons(mut args: Args) -> Result<Value> {
}

pub fn cons_end(mut args: Args) -> Result<Value> {
let mut list = list_arg!(args);
let element = arg!(args);
let mut list = list_arg!(args);
list.push_back(element);

return_list!(list)
Expand Down
57 changes: 46 additions & 11 deletions numbat/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ pub enum ParseErrorKind {
#[error("Expected identifier")]
ExpectedIdentifier,

#[error("Expected identifier or function call after postfix apply (`//`)")]
ExpectedIdentifierOrCallAfterPostfixApply,

#[error("Expected dimension identifier, '1', or opening parenthesis")]
ExpectedDimensionPrimary,

Expand Down Expand Up @@ -880,16 +883,30 @@ impl<'a> Parser<'a> {
let mut expr = self.condition()?;
let mut full_span = expr.full_span();
while self.match_exact(TokenKind::PostfixApply).is_some() {
let identifier = self.identifier()?;
let identifier_span = self.last().unwrap().span;
full_span = full_span.extend(&identifier_span);

expr = Expression::FunctionCall(
identifier_span,
full_span,
Box::new(Expression::Identifier(identifier_span, identifier)),
vec![expr],
);
match self.call()? {
Expression::Identifier(span, ident) => {
full_span = full_span.extend(&span);

expr = Expression::FunctionCall(
span,
full_span,
Box::new(Expression::Identifier(span, ident)),
vec![expr],
);
}
Expression::FunctionCall(call_span, fn_full_span, call, mut params) => {
full_span = full_span.extend(&fn_full_span);

params.push(expr);
expr = Expression::FunctionCall(call_span, full_span, call, params);
}
_other => {
return Err(ParseError::new(
ParseErrorKind::ExpectedIdentifierOrCallAfterPostfixApply,
full_span,
))
}
}
}
Ok(expr)
}
Expand Down Expand Up @@ -1857,7 +1874,7 @@ mod tests {
for input in inputs {
match parse(input, 0) {
Err((_, errors)) => {
assert_eq!(errors[0].kind, error_kind);
assert_eq!(errors[0].kind, error_kind, "Failed on {}", input);
irevoire marked this conversation as resolved.
Show resolved Hide resolved
}
_ => {
panic!();
Expand Down Expand Up @@ -2734,6 +2751,24 @@ mod tests {
vec![binop!(scalar!(1.0), Add, scalar!(1.0))],
),
);
parse_as_expression(
&["1 + 1 // kefir(2)"],
Expression::FunctionCall(
Span::dummy(),
Span::dummy(),
Box::new(identifier!("kefir")),
vec![scalar!(2.0), binop!(scalar!(1.0), Add, scalar!(1.0))],
),
);

should_fail_with(&["1 // print()"], ParseErrorKind::InlineProcedureUsage);

should_fail_with(&["1 // +"], ParseErrorKind::ExpectedPrimary);

should_fail_with(
&["1 // 2", "1 // 1 +"],
ParseErrorKind::ExpectedIdentifierOrCallAfterPostfixApply,
);
}

#[test]
Expand Down
Loading