added unit type as ()

This commit is contained in:
russ morris 2018-05-22 16:44:41 -07:00
parent aae4345911
commit 2f46af64dd
4 changed files with 46 additions and 2 deletions

View File

@ -499,6 +499,7 @@ impl Engine {
), ),
Expr::True => Ok(Box::new(true)), Expr::True => Ok(Box::new(true)),
Expr::False => Ok(Box::new(false)), 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_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_f64(x: f64, y: f64) -> f64 { x.powf(y) }
fn pow_f64_i64(x: f64, y: i64) -> f64 { x.powi(y as i32) } 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, "+", add, i32, i64, u32, u64, f32, f64);
reg_op!(engine, "-", sub, 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); reg_un!(engine, "!", not, bool);
engine.register_fn("+", concat); engine.register_fn("+", concat);
engine.register_fn("==", unit_eq);
// engine.register_fn("[]", idx); // engine.register_fn("[]", idx);
// FIXME? Registering array lookups are a special case because we want to return boxes // FIXME? Registering array lookups are a special case because we want to return boxes

View File

@ -109,6 +109,7 @@ pub enum Expr {
Array(Vec<Expr>), Array(Vec<Expr>),
True, True,
False, False,
Unit,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -1065,9 +1066,14 @@ fn parse_binop<'a>(input: &mut Peekable<TokenIterator<'a>>,
} }
fn parse_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> { fn parse_expr<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, ParseError> {
match input.peek() {
Some(Token::RParen) => Ok(Expr::Unit),
_ => {
let lhs = try!(parse_unary(input)); let lhs = try!(parse_unary(input));
parse_binop(input, 0, lhs) parse_binop(input, 0, lhs)
}
}
} }
fn parse_if<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, ParseError> { fn parse_if<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Stmt, ParseError> {

View File

@ -21,5 +21,6 @@ mod power_of;
mod string; mod string;
mod unary_after_binary; mod unary_after_binary;
mod unary_minus; mod unary_minus;
mod unit;
mod var_scope; mod var_scope;
mod while_loop; mod while_loop;

34
src/tests/unit.rs Normal file
View File

@ -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::<bool>("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);
}
}