From adeaac24d9519454028c3a7bd3787cbb59a4ed14 Mon Sep 17 00:00:00 2001 From: JJ Date: Tue, 11 Apr 2023 21:39:32 -0700 Subject: more util helpers, cut down test code verbosity --- src/util.rs | 16 ++++++++++++++-- tests/test_execution.rs | 16 ++++++++-------- tests/test_parser.rs | 22 +++++++++++----------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/util.rs b/src/util.rs index 9ed38d1..b2b3035 100644 --- a/src/util.rs +++ b/src/util.rs @@ -24,9 +24,9 @@ pub fn Ann(expr: Expression, kind: Type) -> Expression { }; } -pub fn Const(val: Value, kind: Type) -> Expression { +pub fn Const(val: Value) -> Expression { return Expression::Constant { - term: Term {val, kind} + term: Term {val, kind: Type::Empty} }; } @@ -58,3 +58,15 @@ pub fn Cond(if_cond: Expression, if_then: Expression, if_else: Expression) -> Ex }; } +pub fn Func(from: Type, to: Type) -> Type { + return Type::Function { + from: Box::new(from), + to: Box::new(to) + } +} + +pub const Empty: Type = Type::Empty; +pub const Unit: Type = Type::Unit; +pub const Bool: Type = Type::Boolean; +pub const Nat: Type = Type::Natural; +pub const Int: Type = Type::Integer; diff --git a/tests/test_execution.rs b/tests/test_execution.rs index dd1b038..5aeac19 100644 --- a/tests/test_execution.rs +++ b/tests/test_execution.rs @@ -4,18 +4,18 @@ use chrysanthemum::util::*; #[test] fn test_simple() { - assert_eq!(execute(Context::new(), Const(0, Type::Boolean)), Ok(Term(0, Type::Boolean))); - assert_eq!(execute(Context::new(), Const(123, Type::Natural)), Ok(Term(123, Type::Natural))); - assert_eq!(execute(Context::new(), Const(123, Type::Integer)), Ok(Term(123, Type::Integer))); + assert_eq!(execute(Context::new(), Const(0)), Ok(Term(0, Empty))); + assert_eq!(execute(Context::new(), Const(123)), Ok(Term(123, Empty))); + assert_eq!(execute(Context::new(), Const(123)), Ok(Term(123, Empty))); assert!(execute(Context::new(), Var("x")).is_err()); } #[test] fn test_complex() { let mut context = Context::new(); - context.insert(String::from("x"), Term(413, Type::Natural)); - context.insert(String::from("y"), Term(1, Type::Boolean)); - assert_eq!(execute(context.clone(), Var("x")), Ok(Term(413, Type::Natural))); - assert_eq!(execute(context.clone(), Cond(Var("y"), Const(612, Type::Integer), Var("x"))), Ok(Term(612, Type::Integer))); - assert_eq!(execute(context.clone(), App(Abs("z", Cond(Const(0, Type::Boolean), Var("x"), Var("z"))), Const(1025, Type::Integer))), Ok(Term(1025, Type::Integer))); + context.insert(String::from("x"), Term(413, Empty)); + context.insert(String::from("y"), Term(1, Empty)); + assert_eq!(execute(context.clone(), Var("x")), Ok(Term(413, Empty))); + assert_eq!(execute(context.clone(), Cond(Var("y"), Const(612), Var("x"))), Ok(Term(612, Empty))); + assert_eq!(execute(context.clone(), App(Abs("z", Cond(Const(0), Var("x"), Var("z"))), Const(1025))), Ok(Term(1025, Empty))); } diff --git a/tests/test_parser.rs b/tests/test_parser.rs index e33cdfa..a7d15f8 100644 --- a/tests/test_parser.rs +++ b/tests/test_parser.rs @@ -6,7 +6,7 @@ use chrysanthemum::util::*; #[test] fn test_simple_phrases() { - assert_eq!(parse_lambda("123"), Ok(Const(123, Type::Empty))); + assert_eq!(parse_lambda("123"), Ok(Const(123))); assert_eq!(parse_lambda("x12"), Ok(Var("x12"))); assert_eq!(parse_lambda("x12x2"), Ok(Var("x12x2"))); // so i _don't_ want these to be valid identifiers: @@ -17,8 +17,8 @@ fn test_simple_phrases() { #[test] fn test_simple_annotations() { - assert_eq!(parse_lambda("t: int"), Ok(Ann(Var("t"), Type::Integer))); - assert_eq!(parse_lambda("12: nat"), Ok(Ann(Const(12, Type::Empty), Type::Natural))); + assert_eq!(parse_lambda("t: int"), Ok(Ann(Var("t"), Int))); + assert_eq!(parse_lambda("12: nat"), Ok(Ann(Const(12), Nat))); assert!(parse_lambda("t: fake").is_err()); } @@ -33,22 +33,22 @@ fn test_simple_expressions() { assert_eq!(parse_lambda("(λx.y) x"), Ok(App(Abs("x", Var("y")), Var("x")))); assert_eq!(parse_lambda("if x then y else z"), Ok(Cond(Var("x"), Var("y"), Var("z")))); assert_eq!(parse_lambda("if xeme then yak else zebra"), Ok(Cond(Var("xeme"), Var("yak"), Var("zebra")))); - assert_eq!(parse_lambda("if 413 then 612 else 1025"), Ok(Cond(Const(413, Type::Empty), Const(612, Type::Empty), Const(1025, Type::Empty)))); // invalid, but should parse + assert_eq!(parse_lambda("if 413 then 612 else 1025"), Ok(Cond(Const(413), Const(612), Const(1025)))); // invalid, but should parse } #[test] fn test_complex_expressions() { - assert_eq!(parse_lambda("(λy.if y then 0 else 1) z"), Ok(App(Abs("y", Cond(Var("y"), Const(0, Type::Empty), Const(1, Type::Empty))), Var("z")))); + assert_eq!(parse_lambda("(λy.if y then 0 else 1) z"), Ok(App(Abs("y", Cond(Var("y"), Const(0), Const(1))), Var("z")))); } #[test] fn test_complex_annotations() { - assert_eq!(parse_lambda("(lambda x . y) : int"), Ok(Ann(Abs("x", Var("y")), Type::Integer))); - assert_eq!(parse_lambda("((lambda x. y): (int -> int)) 413: int"), Ok(App(Ann(Abs("x", Var("y")), Type::Function { from: Box::new(Type::Integer), to: Box::new(Type::Integer) }), Ann(Const(413, Type::Empty), Type::Integer)))); - assert_eq!(parse_lambda("if 0: bool then 1: bool else 2: int"), Ok(Cond(Ann(Const(0, Type::Empty), Type::Boolean), Ann(Const(1, Type::Empty), Type::Boolean), Ann(Const(2, Type::Empty), Type::Integer)))); - assert_eq!(parse_lambda("(lambda x. if x then 1: bool else 0: bool): (int -> bool)"), Ok(Ann(Abs("x", Cond(Var("x"), Ann(Const(1, Type::Empty), Type::Boolean), Ann(Const(0, Type::Empty), Type::Boolean))), Type::Function { from: Box::new(Type::Integer), to: Box::new(Type::Boolean) }))); - assert_eq!(parse_lambda("(lambda x. if x then 1: int else 0: int): (bool -> int)"), Ok(Ann(Abs("x", Cond(Var("x"), Ann(Const(1, Type::Empty), Type::Integer), Ann(Const(0, Type::Empty), Type::Integer))), Type::Function { from: Box::new(Type::Boolean), to: Box::new(Type::Integer) }))); - assert_eq!(parse_lambda("(lambda x. if x then 0 else 1): (bool -> bool)"), Ok(Ann(Abs("x", Cond(Var("x"), Const(0, Type::Empty), Const(1, Type::Empty))), Type::Function { from: Box::new(Type::Boolean), to: Box::new(Type::Boolean) }))); + assert_eq!(parse_lambda("(lambda x . y) : int"), Ok(Ann(Abs("x", Var("y")), Int))); + assert_eq!(parse_lambda("((lambda x. y): (int -> int)) 413: int"), Ok(App(Ann(Abs("x", Var("y")), Func(Int, Int) ), Ann(Const(413), Int)))); + assert_eq!(parse_lambda("if 0: bool then 1: bool else 2: int"), Ok(Cond(Ann(Const(0), Bool), Ann(Const(1), Bool), Ann(Const(2), Int)))); + assert_eq!(parse_lambda("(lambda x. if x then 1: bool else 0: bool): (int -> bool)"), Ok(Ann(Abs("x", Cond(Var("x"), Ann(Const(1), Bool), Ann(Const(0), Bool))), Func(Int, Bool)))); + assert_eq!(parse_lambda("(lambda x. if x then 1: int else 0: int): (bool -> int)"), Ok(Ann(Abs("x", Cond(Var("x"), Ann(Const(1), Int), Ann(Const(0), Int))), Func(Bool, Int)))); + assert_eq!(parse_lambda("(lambda x. if x then 0 else 1): (bool -> bool)"), Ok(Ann(Abs("x", Cond(Var("x"), Const(0), Const(1))), Func(Bool, Bool)))); } const program: &'static str = -- cgit v1.2.3-70-g09d2