Skip to content

Commit

Permalink
#748 Fix signed numbers not parsing correctly after parens (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
developedby authored Feb 22, 2025
1 parent 0ac443c commit 36c7f37
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project does not currently adhere to a particular versioning scheme.

### Fixed
- Fix type checker not properly unifying all the arms of a match expression. ([#734][gh-734])
- Fix signed numbers (i24 and f24) not being parsed after parenthesis `(`. ([#748][gh-748])

## [0.2.37] - 2024-10-18

Expand Down Expand Up @@ -443,3 +444,4 @@ and this project does not currently adhere to a particular versioning scheme.
[gh-706]: https://github.com/HigherOrderCO/Bend/issues/706
[gh-734]: https://github.com/HigherOrderCO/Bend/issues/734
[gh-736]: https://github.com/HigherOrderCO/Bend/issues/736
[gh-748]: https://github.com/HigherOrderCO/Bend/issues/748
44 changes: 39 additions & 5 deletions src/fun/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,21 +590,55 @@ impl<'a> FunParser<'a> {
// App, Tup, Num Op
if self.starts_with("(") {
self.advance_one();

// Opr but maybe a tup
self.skip_trivia();

// Opr but maybe something else
// ( +/-n , -> Tup with Int/Float
// ( +/-n ) -> Int/Float
// ( +/-n term -> App with Int/Float
// ( * , -> Tup with Era
// ( * ) -> Era
// ( opr -> Num Op
if let Some(opr) = self.try_parse_oper() {
if (opr == Op::ADD || opr == Op::SUB) && self.peek_one().is_some_and(|c| "0123456789".contains(c)) {
unexpected_tag(self)?;
*self.index() -= 1;
let num = self.parse_number()?;
let head = Term::Num { val: num };
self.skip_trivia();

if self.starts_with(",") {
self.consume_exactly(",")?;
let tail = self.list_like(|p| p.parse_term(), "", ")", ",", true, 1)?;
let els = [head].into_iter().chain(tail).collect();
return Ok(Term::Fan { fan: FanKind::Tup, tag: tag.unwrap_or(Tag::Static), els });
}

if self.starts_with(")") {
self.consume_exactly(")")?;
return Ok(head);
}

let els = self.list_like(|p| p.parse_term(), "", ")", "", false, 0)?;
let term = els.into_iter().fold(head, |fun, arg| Term::App {
tag: tag.clone().unwrap_or(Tag::Static),
fun: Box::new(fun),
arg: Box::new(arg),
});
return Ok(term);
}

self.skip_trivia();

// jk, actually a tuple
if self.starts_with(",") && opr == Op::MUL {
if opr == Op::MUL && self.starts_with(",") {
self.consume_exactly(",")?;
let tail = self.list_like(|p| p.parse_term(), "", ")", ",", true, 1)?;
let els = [Term::Era].into_iter().chain(tail).collect();
return Ok(Term::Fan { fan: FanKind::Tup, tag: tag.unwrap_or(Tag::Static), els });
}

if opr == Op::MUL && self.try_consume(")") {
if opr == Op::MUL && self.starts_with(")") {
self.consume_exactly(")")?;
return Ok(Term::Era);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/golden_tests/parse_file/tup_with_signed.bend
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Test that we can parse tuples and apps beginning with signed numbers
main =
let a = (+1, +1)
let b = (+1.1324)
let c = (-6.234, -1)
let d = (-1)
let e = (+6 * λx x)
let f = ((*) λx x)
(+ a (+ b (+ c (+ d (- e f)))))
6 changes: 6 additions & 0 deletions tests/snapshots/parse_file__tup_with_signed.bend.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/parse_file/tup_with_signed.bend
---
unchecked main: Any
(main) = let a = (+1, +1); let b = 1.132; let c = (-6.234, -1); let d = -1; let e = (+6 * λx x); let f = (* λx x); (+ a (+ b (+ c (+ d (- e f)))))

0 comments on commit 36c7f37

Please sign in to comment.