diff --git a/crates/codegen/src/expr.rs b/crates/codegen/src/expr.rs index 8cdbaca..fbfdfe8 100644 --- a/crates/codegen/src/expr.rs +++ b/crates/codegen/src/expr.rs @@ -48,24 +48,6 @@ impl GenRust for Literal { cg.add(")"); } - - Self::Array(values) => { - cg.add("["); - - for val in values { - val.gen_rust(ctx, cg); - } - - cg.add("]"); - } - - Self::ArrayRepeat(value, repeat) => { - cg.add("["); - value.gen_rust(ctx, cg); - cg.add("; "); - repeat.gen_rust(ctx, cg); - cg.add("]"); - } } } } @@ -84,6 +66,26 @@ impl GenRust for Expression { Expression::Path(path) => cg.add(&path.get_rust()), Expression::Literal(literal) => literal.gen_rust(ctx, cg), Expression::Statement(stmt) => stmt.gen_rust(ctx, cg), + Expression::Array(values) => { + cg.add("["); + + for (i, val) in values.iter().enumerate() { + if i > 0 { + cg.add(", "); + } + + val.gen_rust(ctx, cg); + } + + cg.add("]"); + } + Expression::ArrayRepeat(value, repeat) => { + cg.add("["); + value.gen_rust(ctx, cg); + cg.add("; "); + repeat.gen_rust(ctx, cg); + cg.add("]"); + } Expression::Fix { initial, prefixes, @@ -101,6 +103,32 @@ impl GenRust for Expression { cg.add(op); rhs.gen_rust(ctx, cg); } + + Expression::Closure { + return_type, + params, + body, + } => { + cg.add("|"); + for (i, arg) in params.iter().enumerate() { + if i > 0 { + cg.add(", "); + } + + arg.gen_rust(ctx, cg); + } + cg.add("| "); + + if let Some(ty) = return_type { + cg.add("-> "); + cg.add(&ty.get_rust()); + cg.add(" "); + + cg.ensure_brackets_expr(ctx, body); + } else { + body.gen_rust(ctx, cg); + } + } } if ensure_semicolon { @@ -114,30 +142,13 @@ impl GenRust for Expression { } impl GenRust for Prefix { - fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { + fn gen_rust(&self, _ctx: &mut Context, cg: &mut RustCodegen) { match self { Self::Deref => cg.add("*"), Self::Ref => cg.add("&"), Self::RefMut => cg.add("&mut "), Self::Not => cg.add("!"), Self::Neg => cg.add("-"), - Self::Closure(ty, args) => { - cg.add("|"); - for (i, arg) in args.iter().enumerate() { - if i > 0 { - cg.add(", "); - } - - arg.gen_rust(ctx, cg); - } - cg.add("| "); - - if let Some(ty) = ty { - cg.add("-> "); - cg.add(&ty.get_rust()); - cg.add(" "); - } - } } } } diff --git a/crates/parser/src/ast/expr.rs b/crates/parser/src/ast/expr.rs index 969cf21..04715c6 100644 --- a/crates/parser/src/ast/expr.rs +++ b/crates/parser/src/ast/expr.rs @@ -7,6 +7,13 @@ pub enum Expression { Literal(Literal), Path(ExprPath), Statement(Box), + Array(Vec), + ArrayRepeat(Box, Box), + Closure { + return_type: Option, + params: Vec, + body: Box, + }, Fix { initial: Box, prefixes: Vec, @@ -26,8 +33,6 @@ pub enum Literal { Float(f64), Bool(bool), Tuple(Vec), - Array(Vec), - ArrayRepeat(Box, Box), } #[derive(Debug, Clone, Serialize)] @@ -72,7 +77,6 @@ pub enum Prefix { Deref, Not, Neg, - Closure(Option, Vec), } #[derive(Debug, Clone, Serialize)] diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index e4f748d..7eba693 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -30,6 +30,7 @@ keyword = { | "false" | "dyn" | "loop" + | "fn" } identifier = @{ @@ -448,7 +449,7 @@ match_stmt = { // CLOSURES // ------------------------------------------------------ -closure = { type_expr? ~ "|" ~ param_list ~ "|" } +closure = { type_expr? ~ "fn" ~ "(" ~ param_list ~ ")" ~ expr } // ------------------------------------------------------ // TUPLES & ARRAYS @@ -458,10 +459,12 @@ tuple = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ","? ~ ")" } -array = { - "[" ~ expr ~ ";" ~ expr ~ "]" // Repeat notation: [0; 10] +array_repeat = { + "[" ~ expr ~ ";" ~ expr ~ "]" +} - | "[" ~ (expr ~ ("," ~ expr)* ~ ","?)? ~ "]" // Standard elements initialization +array = { + "[" ~ (expr ~ ("," ~ expr)* ~ ","?)? ~ "]" } // ------------------------------------------------------ @@ -490,10 +493,12 @@ expr_path = { primary = { control_flow + | closure | basic_stmt | literal | expr_path | tuple + | array_repeat | array } @@ -571,8 +576,7 @@ not_px = { "!" } neg_px = { "-" } prefix = { - closure - | deref_px + deref_px | mut_ref_px | ref_px | not_px @@ -586,6 +590,7 @@ prefix = { bin_op = { "+=" | "-=" + | "*=" | "/=" | "%=" | "&=" @@ -623,3 +628,4 @@ bin_op = { expr = { term ~ (bin_op ~ term)* } term = { prefix* ~ primary ~ postfix* } + diff --git a/crates/parser/src/parser/common/expr.rs b/crates/parser/src/parser/common/expr.rs index b3236d8..e105511 100644 --- a/crates/parser/src/parser/common/expr.rs +++ b/crates/parser/src/parser/common/expr.rs @@ -75,6 +75,27 @@ impl<'a> TryFrom> for Expression { )) } + Rule::closure => { + ast_expr!(Expression::Closure { + return_type: consume_rule(&mut inner, Rule::type_expr) + .map(TypeExpr::try_from) + .transpose(), + params: collect_recovered(inner.next().unwrap().into_inner()), + body: Expression::try_from(inner.next().unwrap()).map(Box::new), + }) + } + + Rule::array => { + ast_expr!(Expression::Array(collect_recovered(inner))) + } + + Rule::array_repeat => { + ast_expr!(Expression::ArrayRepeat( + Expression::try_from(inner.next().unwrap()).map(Box::new), + Expression::try_from(inner.next().unwrap()).map(Box::new) + )) + } + Rule::primary => pair.into_inner().next().unwrap().try_into(), Rule::static_path => ast_expr!(Expression::Path(pair.try_into())), Rule::literal => ast_expr!(Expression::Literal(pair.try_into())), @@ -108,16 +129,6 @@ impl<'a> TryFrom> for Prefix { Rule::ref_px => Self::Ref, Rule::not_px => Self::Not, Rule::neg_px => Self::Neg, - Rule::closure => { - let mut inner = pair.into_inner(); - - return ast_expr!(Self::Closure( - consume_rule(&mut inner, Rule::type_expr) - .map(TypeExpr::try_from) - .transpose(), - collect_recovered(inner.next().unwrap().into_inner()) - )); - } _ => return AstError::bug_unimplemented(pair), }) diff --git a/crates/parser/src/parser/common/mod.rs b/crates/parser/src/parser/common/mod.rs index 6994f64..c1d8f58 100644 --- a/crates/parser/src/parser/common/mod.rs +++ b/crates/parser/src/parser/common/mod.rs @@ -76,7 +76,7 @@ impl<'a> TryFrom> for Pattern { return Ok(None); } - let mut inner = dbg!(v).into_inner(); + let mut inner = v.into_inner(); Some(ast_expr!(( Identifier::try_from(inner.next().unwrap()), inner diff --git a/mist-test/src/common/decl_test.mist b/mist-test/src/common/decl_test.mist new file mode 100644 index 0000000..8c67ab1 --- /dev/null +++ b/mist-test/src/common/decl_test.mist @@ -0,0 +1,81 @@ +pub void test_let_basic() { + let x = 42; + assert_eq!(x, 42); +} + +pub void test_let_typed() { + i32 x = 42; + assert_eq!(x, 42); + + str* s = "hello"; + assert_eq!(s, "hello"); + + bool b = true; + assert_eq!(b, true); + + f64 f = 3.14; + assert_eq!(f, 3.14); +} + +pub void test_let_mut() { + let mut x = 42; + x = 100; + assert_eq!(x, 100); +} + +pub void test_let_tuple_destructure() { + let [a, b] = (10, "hello"); + assert_eq!(a, 10); + assert_eq!(b, "hello"); +} + +pub void test_let_nested_tuple() { + let [a, [b, c]] = (1, (2, 3)); + assert_eq!(a, 1); + assert_eq!(b, 2); + assert_eq!(c, 3); +} + +pub void test_let_pattern_irrefutable() { + let x = 5; + let [y, z] = (1, 2); + assert_eq!(x, 5); + assert_eq!(y, 1); + assert_eq!(z, 2); +} + +pub void test_multiple_decls() { + let a = 1; + let b = 2; + let c = 3; + assert_eq!(a + b + c, 6); +} + +pub void test_let_with_type_and_init() { + f64 x = 10.0; + assert_eq!(x, 10.0); +} + +pub void test_string_literal() { + let s = "hello, world!"; + assert_eq!(s.len(), 13); +} + +pub void test_let_bool() { + let t = true; + let f = false; + assert_eq!(t, !f); +} + +pub void run_tests() { + test_let_basic(); + test_let_typed(); + test_let_mut(); + test_let_tuple_destructure(); + test_let_nested_tuple(); + test_let_pattern_irrefutable(); + test_multiple_decls(); + test_let_with_type_and_init(); + test_string_literal(); + test_let_bool(); +} diff --git a/mist-test/src/common/expr_test.mist b/mist-test/src/common/expr_test.mist new file mode 100644 index 0000000..1132fba --- /dev/null +++ b/mist-test/src/common/expr_test.mist @@ -0,0 +1,152 @@ +use std::cmp; + +pub void test_literals() { + assert_eq!(42, 42); + assert_eq!(3.14, 3.14); + assert_eq!(true, true); + assert_eq!(false, false); + assert_eq!("hello", "hello"); +} + +pub void test_tuples() { + let t = (1, true, "three"); + assert_eq!(t.0, 1); + assert_eq!(t.1, true); + assert_eq!(t.2, "three"); +} + +pub void test_arrays() { + let arr = [1, 2, 3]; + assert_eq!(arr[0], 1); + assert_eq!(arr[1], 2); + assert_eq!(arr[2], 3); +} + +pub void test_array_repeat() { + let arr = [0; 5]; + assert_eq!(arr[0], 0); + assert_eq!(arr[4], 0); + assert_eq!(arr.len(), 5); +} + +pub void test_binary_arith() { + assert_eq!(10 + 5, 15); + assert_eq!(10 - 5, 5); + assert_eq!(10 * 5, 50); + assert_eq!(10 / 5, 2); + assert_eq!(10 % 3, 1); +} + +pub void test_binary_cmp() { + assert_eq!(5 < 10, true); + assert_eq!(10 <= 10, true); + assert_eq!(10 > 5, true); + assert_eq!(10 >= 10, true); + assert_eq!(5 == 5, true); + assert_eq!(5 != 10, true); +} + +pub void test_binary_logic() { + assert_eq!(true && true, true); + assert_eq!(true && false, false); + assert_eq!(true || false, true); + assert_eq!(false || false, false); +} + +pub void test_binary_bit() { + assert_eq!(5 & 3, 1); + assert_eq!(5 | 3, 7); + assert_eq!(5 ^ 3, 6); + assert_eq!(1 << 2, 4); + assert_eq!(4 >> 1, 2); +} + +pub void test_prefix() { + let x = 42; + assert_eq!(-x, -42); + assert_eq!(!true, false); + assert_eq!(!false, true); +} + +pub void test_ref_deref() { + let x = 42; + let r = &x; + assert_eq!(*r, 42); +} + +pub void test_postfix_inc_dec() { + let mut x = 5; + x++; + assert_eq!(x, 6); + x--; + assert_eq!(x, 5); +} + +pub void test_as_cast() { + let x = 42; + assert_eq!(x as f64, 42.0); +} + +pub void test_closure() { + let add = fn(let a, let b) a + b; + assert_eq!(add(2, 3), 5); +} + +pub void test_block_expr() { + let x = { + let y = 10; + y + 5 + }; + assert_eq!(x, 15); +} + +pub void test_if_expr() { + let x = if (true) { 1 } else { 2 }; + assert_eq!(x, 1); + + let y = if (false) { 1 } else { 2 }; + assert_eq!(y, 2); +} + +pub void test_range_expr() { + let r = 0..5; + let mut count = 0; + for (i : r) { + count++; + } + assert_eq!(count, 5); +} + +pub void test_binary_assign() { + let mut x = 10; + x += 5; + assert_eq!(x, 15); + x -= 3; + assert_eq!(x, 12); + x *= 2; + assert_eq!(x, 24); + x /= 4; + assert_eq!(x, 6); + x %= 4; + assert_eq!(x, 2); +} + +pub void run_tests() { + test_literals(); + test_tuples(); + test_arrays(); + test_array_repeat(); + test_binary_arith(); + test_binary_cmp(); + test_binary_logic(); + test_binary_bit(); + test_prefix(); + test_ref_deref(); + test_postfix_inc_dec(); + test_as_cast(); + test_closure(); + test_block_expr(); + test_if_expr(); + test_range_expr(); + test_binary_assign(); +} diff --git a/mist-test/src/common/statement_test.mist b/mist-test/src/common/statement_test.mist new file mode 100644 index 0000000..1aab6fa --- /dev/null +++ b/mist-test/src/common/statement_test.mist @@ -0,0 +1,165 @@ +pub void test_var_decl() { + let x = 42; + assert_eq!(x, 42); + + i32 y = 10; + assert_eq!(y, 10); + + str* name = "mist"; + assert_eq!(name, "mist"); +} + +pub void test_var_decl_with_init() { + let mut x = 0; + x = 5; + assert_eq!(x, 5); +} + +pub void test_if_statement() { + let mut x = 0; + + if (true) { + x = 1; + } + assert_eq!(x, 1); + + if (false) { + x = 2; + } else { + x = 3; + } + assert_eq!(x, 3); +} + +pub void test_if_else_if() { + let mut x = 0; + + if (x == 1) { + x = 10; + } else if (x == 0) { + x = 20; + } else { + x = 30; + } + assert_eq!(x, 20); +} + +pub void test_while_loop() { + let mut count = 0; + + while (count < 5) { + count++; + } + assert_eq!(count, 5); +} + +pub void test_for_loop() { + let mut sum = 0; + + for (i : 0..4) { + sum += i; + } + assert_eq!(sum, 6); +} + +pub void test_loop_stmt() { + let mut count = 0; + + loop { + count++; + if (count >= 3) { + break; + } + } + assert_eq!(count, 3); +} + +pub void test_break_continue() { + let mut sum = 0; + + for (i : 0..10) { + if (i % 2 == 0) { + continue; + } + if (i >= 7) { + break; + } + sum += i; + } + assert_eq!(sum, 1 + 3 + 5); +} + +pub i32 test_return_from_fn() { + return 42; +} + +pub void test_block_scope() { + let x = { + let inner = 10; + inner + 5 + }; + assert_eq!(x, 15); +} + +pub void test_match_literal() { + let x = 2; + let mut result = 0; + + match (x) { + 1 => { result = 10; } + 2 => { result = 20; } + 3 => { result = 30; } + _ => panic!(); + } + assert_eq!(result, 20); +} + +pub void test_match_or_pattern() { + let x = 1; + let mut result = 0; + + match (x) { + 1 | 2 => { result = 100; } + 3 => { result = 200; } + _ => panic!(); + } + assert_eq!(result, 100); +} + +pub void test_match_default() { + let x = 99; + let mut result = 0; + + match (x) { + 1 => { result = 10; } + y => { result = y; } + } + assert_eq!(result, 99); +} + +pub void test_for_with_pattern_destructure() { + let pairs = [(1, "a"), (2, "b")]; + let mut keys = 0; + + for ([k, _] : pairs) { + keys += k; + } + assert_eq!(keys, 3); +} + +pub void run_tests() { + test_var_decl(); + test_var_decl_with_init(); + test_if_statement(); + test_if_else_if(); + test_while_loop(); + test_for_loop(); + test_loop_stmt(); + test_break_continue(); + assert_eq!(test_return_from_fn(), 42); + test_block_scope(); + test_match_literal(); + test_match_or_pattern(); + test_match_default(); + test_for_with_pattern_destructure(); +} diff --git a/mist-test/src/common/types_test.mist b/mist-test/src/common/types_test.mist new file mode 100644 index 0000000..2fac144 --- /dev/null +++ b/mist-test/src/common/types_test.mist @@ -0,0 +1,63 @@ +pub void test_integer_type() { + i32 x = 42; + assert_eq!(x, 42); +} + +pub void test_float_type() { + f64 x = 3.14; + assert_eq!(x, 3.14); +} + +pub void test_bool_type() { + bool x = true; + assert_eq!(x, true); + + bool y = false; + assert_eq!(y, false); +} + +pub void test_string_type() { + str* s = "hello"; + assert_eq!(s, "hello"); +} + +pub void test_tuple_type() { + [i32, str*] t = (42, "answer"); + assert_eq!(t.0, 42); + assert_eq!(t.1, "answer"); +} + +pub void test_ref_type() { + i32 x = 42; + i32* r = &x; + assert_eq!(*r, 42); +} + +pub void test_mut_ref_type() { + let mut x = 42; + i32 mut* r = &mut x; + *r = 100; + assert_eq!(x, 100); +} + +pub void test_void_type() {} + +i32 add(i32 a, i32 b) { + a + b +} + +pub void test_fn_return_type() { + assert_eq!(add(3, 4), 7); +} + +pub void run_tests() { + test_integer_type(); + test_float_type(); + test_bool_type(); + test_string_type(); + test_tuple_type(); + test_ref_type(); + test_mut_ref_type(); + test_void_type(); + test_fn_return_type(); +} diff --git a/mist-test/src/items/attribute_test.mist b/mist-test/src/items/attribute_test.mist new file mode 100644 index 0000000..3dd06b2 --- /dev/null +++ b/mist-test/src/items/attribute_test.mist @@ -0,0 +1,21 @@ +#[derive(Debug)] +pub struct Debuggable { + i32 value, +} + +pub void test_derive_attribute() { + let d = Debuggable { value: 42 }; + assert_eq!(d.value, 42); +} + +#[allow(unused_variables)] +pub void test_allow_attribute() { + let x = 42; + let y = "unused"; + assert_eq!(x, 42); +} + +pub void run_tests() { + test_derive_attribute(); + test_allow_attribute(); +} diff --git a/mist-test/src/items/class_test.mist b/mist-test/src/items/class_test.mist new file mode 100644 index 0000000..90d6720 --- /dev/null +++ b/mist-test/src/items/class_test.mist @@ -0,0 +1,58 @@ +pub class Counter { + pub i32 count; + + pub constructor(i32 start) { + self.count = start; + } + + pub void increment(self mut*) { + self.count++; + } + + pub i32 get(self*) { + self.count + } +} + +pub void test_class_create() { + let c = Counter::new(0); + assert_eq!(c.get(), 0); +} + +pub void test_class_method() { + let mut c = Counter::new(5); + c.increment(); + assert_eq!(c.get(), 6); +} + +pub void test_class_field_access() { + let c = Counter::new(42); + assert_eq!(c.count, 42); +} + +class NamedCounter : Counter { + pub String name; + + pub constructor(i32 start, String name) { + self._super = Counter::new(start); + + self.name = name; + } + + pub String get_name(self*) { + self.name.clone() + } +} + +pub void test_class_inherit() { + let nc = NamedCounter::new(10, "test".to_string()); + assert_eq!(nc.get_name(), "test"); + assert_eq!(nc.count, 10); +} + +pub void run_tests() { + test_class_create(); + test_class_method(); + test_class_field_access(); + test_class_inherit(); +} diff --git a/mist-test/src/items/enum_test.mist b/mist-test/src/items/enum_test.mist new file mode 100644 index 0000000..d111175 --- /dev/null +++ b/mist-test/src/items/enum_test.mist @@ -0,0 +1,60 @@ +enum OptionInt { + None, + Some[i32], +} + +pub void test_enum_named() { + let x = OptionInt::None; + match (x) { + OptionInt::None => { assert_eq!(1, 1); } + OptionInt::Some[_] => { assert_eq!(0, 1); } + } +} + +pub void test_enum_tuple() { + let x = OptionInt::Some(42); + match (x) { + OptionInt::None => { assert_eq!(0, 1); } + OptionInt::Some[v] => { assert_eq!(v, 42); } + } +} + +enum Shape { + Circle { i32 radius }, + Rect { i32 w, i32 h }, +} + +pub void test_enum_struct() { + let c = Shape::Circle { radius: 5 }; + match (c) { + Shape::Circle { radius } => { assert_eq!(radius, 5); } + Shape::Rect { .. } => { assert_eq!(0, 1); } + } + + let r = Shape::Rect { w: 3, h: 4 }; + match (r) { + Shape::Circle { .. } => { assert_eq!(0, 1); } + Shape::Rect { w, h } => { assert_eq!(w * h, 12); } + } +} + +enum Color { + Red, + Green, + Blue, +} + +pub void test_enum_eq() { + let c = Color::Red; + match (c) { + Color::Red => { assert_eq!(1, 1); } + _ => { assert_eq!(0, 1); } + } +} + +pub void run_tests() { + test_enum_named(); + test_enum_tuple(); + test_enum_struct(); + test_enum_eq(); +} diff --git a/mist-test/src/items/function_test.mist b/mist-test/src/items/function_test.mist new file mode 100644 index 0000000..0278874 --- /dev/null +++ b/mist-test/src/items/function_test.mist @@ -0,0 +1,64 @@ +void greet() { + let mut x = 1; + x++; +} + +pub void test_basic_function() { + greet(); +} + +i32 add(i32 a, i32 b) { + a + b +} + +pub void test_fn_params() { + assert_eq!(add(2, 3), 5); +} + +pub i32 test_fn_return() { + 42 +} + +pub i32 test_fn_early_return() { + return 100; + 0 +} + +pub i32 test_fn_mut_param(i32 mut x) { + x = x + 10; + x +} + +pub void test_method_call() { + str* s = "hello"; + assert_eq!(s.len(), 5); + assert_eq!(s.to_uppercase(), "HELLO"); +} + +i32 triple(i32 x) { + add(x, add(x, x)) +} + +pub void test_nested_fn_calls() { + assert_eq!(triple(5), 15); +} + +i32 factorial(i32 n) { + if (n <= 1) 1 + else (n) * (factorial(n - 1)) +} + +pub void test_recursive_fn() { + assert_eq!(factorial(5), 120); +} + +pub void run_tests() { + test_basic_function(); + test_fn_params(); + assert_eq!(test_fn_return(), 42); + assert_eq!(test_fn_early_return(), 100); + assert_eq!(test_fn_mut_param(5), 15); + test_method_call(); + test_nested_fn_calls(); + test_recursive_fn(); +} diff --git a/mist-test/src/items/impl_test.mist b/mist-test/src/items/impl_test.mist new file mode 100644 index 0000000..4032913 --- /dev/null +++ b/mist-test/src/items/impl_test.mist @@ -0,0 +1,54 @@ +struct Vector2 { + i32 x, + i32 y, +} + +impl Vector2 { + pub i32 magnitude(self*) { + self.x * self.x + self.y * self.y + } +} + +pub void test_inherent_impl() { + let v = Vector2 { x: 3, y: 4 }; + assert_eq!(v.magnitude(), 25); +} + +trait Display { + String display(self*); +} + +impl Display for Vector2 { + String display(self*) { + format!("({}, {})", self.x, self.y) + } +} + +pub void test_trait_impl_block() { + let v = Vector2 { x: 1, y: 2 }; + let s = v.display(); + assert_eq!(s.len() > 0, true); +} + +impl Vector2 { + pub Vector2 add(self*, Vector2 other) { + Vector2 { + x: self.x + other.x, + y: self.y + other.y, + } + } +} + +pub void test_multiple_impls() { + let a = Vector2 { x: 1, y: 2 }; + let b = Vector2 { x: 3, y: 4 }; + let c = a.add(b); + assert_eq!(c.x, 4); + assert_eq!(c.y, 6); +} + +pub void run_tests() { + test_inherent_impl(); + test_trait_impl_block(); + test_multiple_impls(); +} diff --git a/mist-test/src/items/struct_test.mist b/mist-test/src/items/struct_test.mist new file mode 100644 index 0000000..53698da --- /dev/null +++ b/mist-test/src/items/struct_test.mist @@ -0,0 +1,44 @@ +struct Point { + i32 x, + i32 y, +} + +pub void test_struct_create_access() { + let p = Point { x: 10, y: 20 }; + assert_eq!(p.x, 10); + assert_eq!(p.y, 20); +} + +pub void test_struct_mut_field() { + let mut p = Point { x: 1, y: 2 }; + p.x = 100; + assert_eq!(p.x, 100); +} + +pub void test_struct_destructure() { + let p = Point { x: 3, y: 4 }; + let Point { x, y } = p; + assert_eq!(x, 3); + assert_eq!(y, 4); +} + +struct Line { + Point start, + Point end, +} + +pub void test_struct_nested() { + let l = Line { + start: Point { x: 0, y: 0 }, + end: Point { x: 5, y: 10 }, + }; + assert_eq!(l.start.x, 0); + assert_eq!(l.end.y, 10); +} + +pub void run_tests() { + test_struct_create_access(); + test_struct_mut_field(); + test_struct_destructure(); + test_struct_nested(); +} diff --git a/mist-test/src/items/trait_test.mist b/mist-test/src/items/trait_test.mist new file mode 100644 index 0000000..ba93611 --- /dev/null +++ b/mist-test/src/items/trait_test.mist @@ -0,0 +1,39 @@ +trait Speak { + String speak(self*); +} + +struct Dog { + String name, +} + +impl Speak for Dog { + String speak(self*) { + "woof".to_string() + } +} + +pub void test_trait_impl() { + let d = Dog { name: "Rex".to_string() }; + assert_eq!(d.speak(), "woof"); +} + +trait Greet : Speak { + String greet(self*); +} + +impl Greet for Dog { + String greet(self*) { + "hello".to_string() + } +} + +pub void test_trait_with_requirement() { + let d = Dog { name: "Buddy".to_string() }; + assert_eq!(d.speak(), "woof"); + assert_eq!(d.greet(), "hello"); +} + +pub void run_tests() { + test_trait_impl(); + test_trait_with_requirement(); +} diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index 1a14ec9..6e8730e 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1,3 +1,18 @@ void main() { - test_package::hello(); + println!("running common tests..."); + common::expr_test::run_tests(); + common::statement_test::run_tests(); + common::decl_test::run_tests(); + common::types_test::run_tests(); + + println!("running items tests..."); + items::function_test::run_tests(); + items::struct_test::run_tests(); + items::enum_test::run_tests(); + items::class_test::run_tests(); + items::trait_test::run_tests(); + items::impl_test::run_tests(); + items::attribute_test::run_tests(); + + println!("\x1b[32m\nAll tests passed!\x1b[0m"); } diff --git a/mist-test/src/test_package.mist b/mist-test/src/test_package.mist deleted file mode 100644 index c35a53e..0000000 --- a/mist-test/src/test_package.mist +++ /dev/null @@ -1,3 +0,0 @@ -pub void hello() { - println!("Hello") -} \ No newline at end of file