Skip to content

Commit

Permalink
Allow expressions like sin2x
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddiM8 committed Jul 19, 2023
1 parent 7e41531 commit 9fff7ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions kalk/src/integration_testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ mod tests {
}

#[test_case("ambiguities/comparison_in_function")]
#[test_case("ambiguities/fn_call_no_parenthesis")]
#[test_case("basics")]
#[test_case("comparisons")]
#[test_case("comprehensions")]
Expand Down
16 changes: 12 additions & 4 deletions kalk/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,18 @@ fn parse_identifier(context: &mut Context) -> Result<Expr, KalkError> {
let identifier_pos = context.pos;

// Function call
let mut arguments = match parse_primary(context)? {
Expr::Vector(arguments) => arguments,
Expr::Group(argument) => vec![*argument],
argument => vec![argument],
// If there is a parenthesis/brace, parse that as a
// vector/group, otherwise it's an expression like sqrt4,
// which should be parsed as a factor, to allow eg. sqrt2x.
let mut arguments = if match_token(context, TokenKind::OpenBrace)
|| match_token(context, TokenKind::OpenParenthesis) {
match parse_primary(context)? {
Expr::Vector(arguments) => arguments,
Expr::Group(argument) => vec![*argument],
argument => vec![argument],
}
} else {
vec![parse_factor(context)?]
};

// If it's a re-definition, revert and parse as a declaration
Expand Down
5 changes: 5 additions & 0 deletions tests/ambiguities/fn_call_no_parenthesis.kalker
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x = 3

sin2deg = sin(2deg) and
sin2x = sin(2x) and
sin2 = sin(2)

0 comments on commit 9fff7ef

Please sign in to comment.