-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more thorough testing around associativity
- Loading branch information
1 parent
0b1120a
commit 13fe33c
Showing
6 changed files
with
479 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
use formality_core::{term, test}; | ||
use std::sync::Arc; | ||
|
||
#[term] | ||
pub enum Expr { | ||
#[cast] | ||
Id(Id), | ||
|
||
#[grammar($v0 + $v1)] | ||
#[precedence(1)] | ||
Add(Arc<Expr>, Arc<Expr>), | ||
|
||
#[grammar($v0 * $v1)] | ||
#[precedence(2)] | ||
Mul(Arc<Expr>, Arc<Expr>), | ||
} | ||
|
||
formality_core::id!(Id); | ||
|
||
#[test] | ||
fn add_mul() { | ||
let term: Expr = crate::ptt::term("a + b * c"); | ||
expect_test::expect![[r#" | ||
Add( | ||
Id( | ||
a, | ||
), | ||
Mul( | ||
Id( | ||
b, | ||
), | ||
Id( | ||
c, | ||
), | ||
), | ||
) | ||
"#]] | ||
.assert_debug_eq(&term); | ||
} | ||
|
||
#[test] | ||
fn mul_add() { | ||
let term: Expr = crate::ptt::term("a * b + c"); | ||
expect_test::expect![[r#" | ||
Add( | ||
Mul( | ||
Id( | ||
a, | ||
), | ||
Id( | ||
b, | ||
), | ||
), | ||
Id( | ||
c, | ||
), | ||
) | ||
"#]] | ||
.assert_debug_eq(&term); | ||
} | ||
|
||
#[test] | ||
fn add_add() { | ||
let term: Expr = crate::ptt::term("a + b + c"); | ||
expect_test::expect![[r#" | ||
Add( | ||
Add( | ||
Id( | ||
a, | ||
), | ||
Id( | ||
b, | ||
), | ||
), | ||
Id( | ||
c, | ||
), | ||
) | ||
"#]] | ||
.assert_debug_eq(&term); | ||
} | ||
|
||
#[test] | ||
fn mul_mul() { | ||
let term: Expr = crate::ptt::term("a * b * c"); | ||
expect_test::expect![[r#" | ||
Mul( | ||
Mul( | ||
Id( | ||
a, | ||
), | ||
Id( | ||
b, | ||
), | ||
), | ||
Id( | ||
c, | ||
), | ||
) | ||
"#]] | ||
.assert_debug_eq(&term); | ||
} | ||
|
||
#[test] | ||
fn mul_mul_mul() { | ||
let term: Expr = crate::ptt::term("a * b * c * d"); | ||
expect_test::expect![[r#" | ||
Mul( | ||
Mul( | ||
Mul( | ||
Id( | ||
a, | ||
), | ||
Id( | ||
b, | ||
), | ||
), | ||
Id( | ||
c, | ||
), | ||
), | ||
Id( | ||
d, | ||
), | ||
) | ||
"#]] | ||
.assert_debug_eq(&term); | ||
} | ||
|
||
#[test] | ||
fn add_add_mul_add() { | ||
let term: Expr = crate::ptt::term("a + b + c * d + e"); | ||
expect_test::expect![[r#" | ||
Add( | ||
Add( | ||
Add( | ||
Id( | ||
a, | ||
), | ||
Id( | ||
b, | ||
), | ||
), | ||
Mul( | ||
Id( | ||
c, | ||
), | ||
Id( | ||
d, | ||
), | ||
), | ||
), | ||
Id( | ||
e, | ||
), | ||
) | ||
"#]] | ||
.assert_debug_eq(&term); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
use formality_core::{term, test}; | ||
use std::sync::Arc; | ||
|
||
use crate::expect_remainder; | ||
|
||
#[term] | ||
pub enum Expr { | ||
#[cast] | ||
Id(Id), | ||
|
||
#[grammar($v0 + $v1)] | ||
#[precedence(1, none)] | ||
Add(Arc<Expr>, Arc<Expr>), | ||
|
||
#[grammar($v0 * $v1)] | ||
#[precedence(2, none)] | ||
Mul(Arc<Expr>, Arc<Expr>), | ||
} | ||
|
||
formality_core::id!(Id); | ||
|
||
#[test] | ||
fn add_mul() { | ||
let term: Expr = crate::ptt::term("a + b * c"); | ||
expect_test::expect![[r#" | ||
Add( | ||
Id( | ||
a, | ||
), | ||
Mul( | ||
Id( | ||
b, | ||
), | ||
Id( | ||
c, | ||
), | ||
), | ||
) | ||
"#]] | ||
.assert_debug_eq(&term); | ||
} | ||
|
||
#[test] | ||
fn mul_add() { | ||
let term: Expr = crate::ptt::term("a * b + c"); | ||
expect_test::expect![[r#" | ||
Add( | ||
Mul( | ||
Id( | ||
a, | ||
), | ||
Id( | ||
b, | ||
), | ||
), | ||
Id( | ||
c, | ||
), | ||
) | ||
"#]] | ||
.assert_debug_eq(&term); | ||
} | ||
|
||
#[test] | ||
fn add_add() { | ||
let term = expect_remainder::<Expr>("a + b + c"); | ||
expect_test::expect![[r#" | ||
( | ||
Add( | ||
Id( | ||
a, | ||
), | ||
Id( | ||
b, | ||
), | ||
), | ||
" + c", | ||
) | ||
"#]] | ||
.assert_debug_eq(&term); | ||
} | ||
|
||
#[test] | ||
fn mul_mul() { | ||
let term = expect_remainder::<Expr>("a * b * c"); | ||
expect_test::expect![[r#" | ||
( | ||
Mul( | ||
Id( | ||
a, | ||
), | ||
Id( | ||
b, | ||
), | ||
), | ||
" * c", | ||
) | ||
"#]] | ||
.assert_debug_eq(&term); | ||
} | ||
|
||
#[test] | ||
fn mul_mul_mul() { | ||
let term = expect_remainder::<Expr>("a * b * c * d"); | ||
expect_test::expect![[r#" | ||
( | ||
Mul( | ||
Id( | ||
a, | ||
), | ||
Id( | ||
b, | ||
), | ||
), | ||
" * c * d", | ||
) | ||
"#]] | ||
.assert_debug_eq(&term); | ||
} | ||
|
||
#[test] | ||
fn add_add_mul_add() { | ||
let term = expect_remainder::<Expr>("a + b + c * d + e"); | ||
expect_test::expect![[r#" | ||
( | ||
Add( | ||
Id( | ||
a, | ||
), | ||
Id( | ||
b, | ||
), | ||
), | ||
" + c * d + e", | ||
) | ||
"#]] | ||
.assert_debug_eq(&term); | ||
} |
Oops, something went wrong.