Add tests for unary ops and assignment
This commit is contained in:
parent
0691c22487
commit
bb3c13372f
2 changed files with 32 additions and 0 deletions
|
@ -3,6 +3,7 @@ pub enum Expression<'source> {
|
||||||
IntLiteral(u32),
|
IntLiteral(u32),
|
||||||
BoolLiteral(bool),
|
BoolLiteral(bool),
|
||||||
Identifier(&'source str),
|
Identifier(&'source str),
|
||||||
|
UnaryOp(&'source str, Box<Expression<'source>>),
|
||||||
BinaryOp(
|
BinaryOp(
|
||||||
Box<Expression<'source>>,
|
Box<Expression<'source>>,
|
||||||
&'source str,
|
&'source str,
|
||||||
|
|
|
@ -13,6 +13,12 @@ macro_rules! id_ast {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! un_ast {
|
||||||
|
($x:expr, $y:expr) => {
|
||||||
|
Box::new(UnaryOp($x, $y))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! bin_ast {
|
macro_rules! bin_ast {
|
||||||
($x:expr, $y:expr, $z:expr) => {
|
($x:expr, $y:expr, $z:expr) => {
|
||||||
Box::new(BinaryOp($x, $y, $z))
|
Box::new(BinaryOp($x, $y, $z))
|
||||||
|
@ -122,6 +128,31 @@ fn test_binary_op_precedence() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_assignment() {
|
||||||
|
let result = parse(&tokenize("a = b = 1 + 2"));
|
||||||
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
BinaryOp(
|
||||||
|
id_ast!("a"),
|
||||||
|
"=",
|
||||||
|
bin_ast!(id_ast!("b"), "=", bin_ast!(int_ast!(1), "+", int_ast!(2)))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_unary() {
|
||||||
|
let result = parse(&tokenize("not not x"));
|
||||||
|
assert_eq!(result, UnaryOp("not", un_ast!("not", id_ast!("x"))));
|
||||||
|
|
||||||
|
let result = parse(&tokenize("--x"));
|
||||||
|
assert_eq!(result, UnaryOp("-", un_ast!("-", id_ast!("x"))));
|
||||||
|
|
||||||
|
let result = parse(&tokenize("--1"));
|
||||||
|
assert_eq!(result, UnaryOp("-", un_ast!("-", int_ast!(1))));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parenthesized() {
|
fn test_parenthesized() {
|
||||||
let result = parse(&tokenize("(1+2)*3"));
|
let result = parse(&tokenize("(1+2)*3"));
|
||||||
|
|
Reference in a new issue