diff --git a/src/engine.rs b/src/engine.rs index 4b6438d3..7fe9a561 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -499,6 +499,7 @@ impl Engine { ), Expr::True => Ok(Box::new(true)), Expr::False => Ok(Box::new(false)), + Expr::Unit => Ok(Box::new(())), } } @@ -796,6 +797,7 @@ impl Engine { fn pow_i64_i64(x: i64, y: i64) -> i64 { x.pow(y as u32) } fn pow_f64_f64(x: f64, y: f64) -> f64 { x.powf(y) } fn pow_f64_i64(x: f64, y: i64) -> f64 { x.powi(y as i32) } + fn unit_eq(a: (), b: ()) -> bool { true } reg_op!(engine, "+", add, i32, i64, u32, u64, f32, f64); reg_op!(engine, "-", sub, i32, i64, u32, u64, f32, f64); @@ -827,6 +829,7 @@ impl Engine { reg_un!(engine, "!", not, bool); engine.register_fn("+", concat); + engine.register_fn("==", unit_eq); // engine.register_fn("[]", idx); // FIXME? Registering array lookups are a special case because we want to return boxes diff --git a/src/parser.rs b/src/parser.rs index e61bee25..34ed60f4 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -109,6 +109,7 @@ pub enum Expr { Array(Vec), True, False, + Unit, } #[derive(Debug, Clone)] @@ -1065,9 +1066,14 @@ fn parse_binop<'a>(input: &mut Peekable>, } fn parse_expr<'a>(input: &mut Peekable>) -> Result { - let lhs = try!(parse_unary(input)); + match input.peek() { + Some(Token::RParen) => Ok(Expr::Unit), + _ => { + let lhs = try!(parse_unary(input)); - parse_binop(input, 0, lhs) + parse_binop(input, 0, lhs) + } + } } fn parse_if<'a>(input: &mut Peekable>) -> Result { diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 1cb35a66..080cea67 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -21,5 +21,6 @@ mod power_of; mod string; mod unary_after_binary; mod unary_minus; +mod unit; mod var_scope; mod while_loop; \ No newline at end of file diff --git a/src/tests/unit.rs b/src/tests/unit.rs new file mode 100644 index 00000000..1b9c0c36 --- /dev/null +++ b/src/tests/unit.rs @@ -0,0 +1,34 @@ +use engine::Engine; + +#[test] +fn test_unit() { + let mut engine = Engine::new(); + + if let Ok(result) = engine.eval::<()>("let x = (); x") { + assert_eq!(result, ()); + } else { + assert!(false); + } +} + +#[test] +fn test_unit_eq() { + let mut engine = Engine::new(); + + if let Ok(result) = engine.eval::("let x = (); let y = (); x == y") { + assert!(result); + } else { + assert!(false); + } +} + +#[test] +fn test_unit_with_spaces() { + let mut engine = Engine::new(); + + if let Ok(result) = engine.eval::<()>("let x = ( ); x") { + assert_eq!(result, ()); + } else { + assert!(false); + } +} \ No newline at end of file