From aadccb7f3843476bd20377e825534b1ed7210a1e Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 16 Jun 2026 03:29:01 +0200 Subject: [PATCH 1/7] Syntax: fn type name --- crates/parser/src/grammar.pest | 2 +- crates/parser/src/parser/items/function.rs | 9 +- mist-test/src/common/decl_test.mist | 83 ---------- mist-test/src/common/expr_test.mist | 152 ------------------- mist-test/src/common/package.mist | 3 - mist-test/src/common/statement_test.mist | 167 --------------------- mist-test/src/common/types_test.mist | 65 -------- mist-test/src/items/attribute_test.mist | 23 --- mist-test/src/items/class_test.mist | 56 ------- mist-test/src/items/enum_test.mist | 62 -------- mist-test/src/items/function_test.mist | 66 -------- mist-test/src/items/impl_test.mist | 56 ------- mist-test/src/items/struct_test.mist | 46 ------ mist-test/src/items/trait_test.mist | 41 ----- mist-test/src/main.mist | 20 +-- 15 files changed, 9 insertions(+), 842 deletions(-) delete mode 100644 mist-test/src/common/decl_test.mist delete mode 100644 mist-test/src/common/expr_test.mist delete mode 100644 mist-test/src/common/package.mist delete mode 100644 mist-test/src/common/statement_test.mist delete mode 100644 mist-test/src/common/types_test.mist delete mode 100644 mist-test/src/items/attribute_test.mist delete mode 100644 mist-test/src/items/class_test.mist delete mode 100644 mist-test/src/items/enum_test.mist delete mode 100644 mist-test/src/items/function_test.mist delete mode 100644 mist-test/src/items/impl_test.mist delete mode 100644 mist-test/src/items/struct_test.mist delete mode 100644 mist-test/src/items/trait_test.mist diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 7a88a2f..9a46e40 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -330,7 +330,7 @@ trait_decl = { override_kw = { "override" ~ ("(" ~ expr_path ~ ")")? } function_decl = { - visibility? ~ override_kw? ~ "fn" ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ type_expr? ~ (block | semicolon) + visibility? ~ override_kw? ~ "fn" ~ ("void" | type_expr) ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ (block | semicolon) } // ====================================================== diff --git a/crates/parser/src/parser/items/function.rs b/crates/parser/src/parser/items/function.rs index 18b720b..27da885 100644 --- a/crates/parser/src/parser/items/function.rs +++ b/crates/parser/src/parser/items/function.rs @@ -14,6 +14,11 @@ impl<'a> TryFrom> for FunctionDecl { let mut inner = pair.into_inner(); let visibility = Visibility::try_from(&mut inner); let is_override = consume_rule(&mut inner, Rule::override_kw).map(Override::try_from).transpose(); + + let return_type = consume_rule(&mut inner, Rule::type_expr) + .map(TypeExpr::try_from) + .transpose(); + let name = Identifier::try_from(inner.next().unwrap()); let generics = consume_rule(&mut inner, Rule::generics_decl) @@ -59,10 +64,6 @@ impl<'a> TryFrom> for FunctionDecl { }) .unwrap_or_else(|| Ok(ParamList(self_param.into_iter().collect()))); - let return_type = consume_rule(&mut inner, Rule::type_expr) - .map(TypeExpr::try_from) - .transpose(); - let body = inner.next().map(Block::try_from).transpose(); ast_expr!(Self { diff --git a/mist-test/src/common/decl_test.mist b/mist-test/src/common/decl_test.mist deleted file mode 100644 index e9a2e09..0000000 --- a/mist-test/src/common/decl_test.mist +++ /dev/null @@ -1,83 +0,0 @@ -pub module decl_test; - -pub fn test_let_basic() { - let x = 42; - assert_eq!(x, 42); -} - -pub fn test_let_typed() { - let x i32 = 42; - assert_eq!(x, 42); - - let s *str = "hello"; - assert_eq!(s, "hello"); - - let b bool = true; - assert_eq!(b, true); - - let f f64 = 3.14; - assert_eq!(f, 3.14); -} - -pub fn test_let_mut() { - let mut _x = 42; - _x = 100; - assert_eq!(_x, 100); -} - -pub fn test_let_tuple_destructure() { - let [a, b] = (10, "hello"); - assert_eq!(a, 10); - assert_eq!(b, "hello"); -} - -pub fn test_let_nested_tuple() { - let [a, [b, c]] = (1, (2, 3)); - assert_eq!(a, 1); - assert_eq!(b, 2); - assert_eq!(c, 3); -} - -pub fn 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 fn test_multiple_decls() { - let a = 1; - let b = 2; - let c = 3; - assert_eq!(a + b + c, 6); -} - -pub fn test_let_with_type_and_init() { - let x f64 = 10.0; - assert_eq!(x, 10.0); -} - -pub fn test_string_literal() { - let s = "hello, world!"; - assert_eq!(s.len(), 13); -} - -pub fn test_let_bool() { - let t = true; - let f = false; - assert_eq!(t, !f); -} - -pub fn 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 deleted file mode 100644 index c7a7cee..0000000 --- a/mist-test/src/common/expr_test.mist +++ /dev/null @@ -1,152 +0,0 @@ -pub module expr_test; - -pub fn 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 fn test_tuples() { - let t = (1, true, "three"); - assert_eq!(t.0, 1); - assert_eq!(t.1, true); - assert_eq!(t.2, "three"); -} - -pub fn test_arrays() { - let arr = [1, 2, 3]; - assert_eq!(arr[0], 1); - assert_eq!(arr[1], 2); - assert_eq!(arr[2], 3); -} - -pub fn test_array_repeat() { - let arr = [0; 5]; - assert_eq!(arr[0], 0); - assert_eq!(arr[4], 0); - assert_eq!(arr.len(), 5); -} - -pub fn 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 fn 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 fn test_binary_logic() { - assert_eq!(true && true, true); - assert_eq!(true && false, false); - assert_eq!(true || false, true); - assert_eq!(false || false, false); -} - -pub fn 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 fn test_prefix() { - let x = 42; - assert_eq!(-x, -42); - assert_eq!(!true, false); - assert_eq!(!false, true); -} - -pub fn test_ref_deref() { - let x = 42; - let r = &x; - assert_eq!(*r, 42); -} - -pub fn test_postfix_inc_dec() { - let mut x = 5; - x++; - assert_eq!(x, 6); - x--; - assert_eq!(x, 5); -} - -pub fn test_as_cast() { - let x = 42; - assert_eq!(x as f64, 42.0); -} - -pub fn test_closure() { - let add = fn(a, b) -> a + b; - assert_eq!(add(2, 3), 5); -} - -pub fn test_block_expr() { - let x = { - let y = 10; - y + 5 - }; - assert_eq!(x, 15); -} - -pub fn 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 fn test_range_expr() { - let r = 0..5; - let mut count = 0; - for i in r { - count++; - } - assert_eq!(count, 5); -} - -pub fn 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 fn 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/package.mist b/mist-test/src/common/package.mist deleted file mode 100644 index b2b6254..0000000 --- a/mist-test/src/common/package.mist +++ /dev/null @@ -1,3 +0,0 @@ -pub module common; - -pub fn test_package() {} diff --git a/mist-test/src/common/statement_test.mist b/mist-test/src/common/statement_test.mist deleted file mode 100644 index 8829946..0000000 --- a/mist-test/src/common/statement_test.mist +++ /dev/null @@ -1,167 +0,0 @@ -pub module statement_test; - -pub fn test_var_decl() { - let x = 42; - assert_eq!(x, 42); - - let y i32 = 10; - assert_eq!(y, 10); - - let name *str = "mist"; - assert_eq!(name, "mist"); -} - -pub fn test_var_decl_with_init() { - let mut x = 0; - x = 5; - assert_eq!(x, 5); -} - -pub fn 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 fn 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 fn test_while_loop() { - let mut count = 0; - - while count < 5 { - count++; - } - assert_eq!(count, 5); -} - -pub fn test_for_loop() { - let mut sum = 0; - - for i in 0..4 { - sum += i; - } - assert_eq!(sum, 6); -} - -pub fn test_loop_stmt() { - let mut count = 0; - - loop { - count++; - if count >= 3 { - break; - } - } - assert_eq!(count, 3); -} - -pub fn test_break_continue() { - let mut sum = 0; - - for i in 0..10 { - if i % 2 == 0 { - continue; - } - if i >= 7 { - break; - } - sum += i; - } - assert_eq!(sum, 1 + 3 + 5); -} - -pub fn test_return_from_fn() i32 { - return 42; -} - -pub fn test_block_scope() { - let x = { - let inner = 10; - inner + 5 - }; - assert_eq!(x, 15); -} - -pub fn test_match_literal() { - let x = 2; - let result; - - match x { - 1 => { result = 10; } - 2 => { result = 20; } - 3 => { result = 30; } - _ => panic!(); - } - assert_eq!(result, 20); -} - -pub fn test_match_or_pattern() { - let x = 1; - let result i32; - - match x { - 1 | 2 => { result = 100; } - 3 => { result = 200; } - _ => panic!(); - } - assert_eq!(result, 100); -} - -pub fn test_match_default() { - let x = 99; - let result; - - match x { - 1 => { result = 10; } - y => { result = y; } - } - assert_eq!(result, 99); -} - -pub fn test_for_with_pattern_destructure() { - let pairs = [(1, "a"), (2, "b")]; - let mut keys = 0; - - for [k, _] in pairs { - keys += k; - } - assert_eq!(keys, 3); -} - -pub fn 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 deleted file mode 100644 index 8edf714..0000000 --- a/mist-test/src/common/types_test.mist +++ /dev/null @@ -1,65 +0,0 @@ -pub module types_test; - -pub fn test_integer_type() { - let x i32 = 42; - assert_eq!(x, 42); -} - -pub fn test_float_type() { - let x f64 = 3.14; - assert_eq!(x, 3.14); -} - -pub fn test_bool_type() { - let x bool = true; - assert_eq!(x, true); - - let y bool = false; - assert_eq!(y, false); -} - -pub fn test_string_type() { - let s *str = "hello"; - assert_eq!(s, "hello"); -} - -pub fn test_tuple_type() { - let t [i32, *str] = (42, "answer"); - assert_eq!(t.0, 42); - assert_eq!(t.1, "answer"); -} - -pub fn test_ref_type() { - let x i32 = 42; - let r *i32 = &x; - assert_eq!(*r, 42); -} - -pub fn test_mut_ref_type() { - let mut x = 42; - let r *mut i32 = &mut x; - *r = 100; - assert_eq!(x, 100); -} - -pub fn test_void_type() {} - -fn add(a i32, b i32) i32 { - a + b -} - -pub fn test_fn_return_type() { - assert_eq!(add(3, 4), 7); -} - -pub fn 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 deleted file mode 100644 index 09f76f2..0000000 --- a/mist-test/src/items/attribute_test.mist +++ /dev/null @@ -1,23 +0,0 @@ -pub module attribute_test; - -#[derive(Debug)] -pub struct Debuggable { - value i32, -} - -pub fn test_derive_attribute() { - let d = Debuggable { value: 42 }; - assert_eq!(d.value, 42); -} - -#[allow(unused_variables)] -pub fn test_allow_attribute() { - let x = 42; - let y = "unused"; - assert_eq!(x, 42); -} - -pub fn 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 deleted file mode 100644 index 8c441e4..0000000 --- a/mist-test/src/items/class_test.mist +++ /dev/null @@ -1,56 +0,0 @@ -pub module class_test; - -pub class Animal { - pub name String; - pub val T; - - constructor() { - self.name = "Rex".to_string(); - } - - pub fn speak(*self) { - println!("Unknown"); - } - - pub fn test_speak(*self) { - self.speak(); - } - - pub fn static_method() {} -} - -pub class Mammal : Animal { - constructor() { - super = Super::new(); - } - - pub fn args_test(*self, x i32, y *str) {} - - pub fn returning<'a>(*self) *'a str { - "Mammal" - } -} - -pub class Dog : Mammal { - constructor() { - super = Super::new(); - } - - pub override(Animal) fn speak(*self) { - println!("Woof!"); - } - - pub override fn returning(*self) *str { - "Dog" - } -} - -pub fn run_tests() { - let dawg = Dog::new(); - - dawg.args_test(10, "hello"); - - dawg.test_speak(); - - println!("{}", dawg.returning()); -} diff --git a/mist-test/src/items/enum_test.mist b/mist-test/src/items/enum_test.mist deleted file mode 100644 index a005f46..0000000 --- a/mist-test/src/items/enum_test.mist +++ /dev/null @@ -1,62 +0,0 @@ -pub module enum_test; - -enum OptionInt { - None, - Some[i32], -} - -pub fn test_enum_named() { - let x = OptionInt::None; - match x { - OptionInt::None => { assert_eq!(1, 1); } - OptionInt::Some[_] => { assert_eq!(0, 1); } - } -} - -pub fn 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 { radius i32 }, - Rect { w i32, h i32 }, -} - -pub fn 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 fn test_enum_eq() { - let c = Color::Red; - match c { - Color::Red => { assert_eq!(1, 1); } - _ => { assert_eq!(0, 1); } - } -} - -pub fn 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 deleted file mode 100644 index 8cbcb21..0000000 --- a/mist-test/src/items/function_test.mist +++ /dev/null @@ -1,66 +0,0 @@ -pub module function_test; - -fn greet() { - let mut x = 1; - x++; -} - -pub fn test_basic_function() { - greet(); -} - -fn add(a i32, b i32) i32 { - a + b -} - -pub fn test_fn_params() { - assert_eq!(add(2, 3), 5); -} - -pub fn test_fn_return() i32 { - 42 -} - -pub fn test_fn_early_return() i32 { - return 100; - 0 -} - -pub fn test_fn_mut_param(mut x i32) i32 { - x = x + 10; - x -} - -pub fn test_method_call() { - let s = "hello"; - assert_eq!(s.len(), 5); - assert_eq!(s.to_uppercase(), "HELLO"); -} - -fn triple(x i32) i32 { - add(x, add(x, x)) -} - -pub fn test_nested_fn_calls() { - assert_eq!(triple(5), 15); -} - -fn factorial(n i32) i32 { - if n <= 1 { 1 } - else { n * factorial(n - 1) } -} - -pub fn test_recursive_fn() { - assert_eq!(factorial(5), 120); -} - -pub fn 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 deleted file mode 100644 index 2c9b50a..0000000 --- a/mist-test/src/items/impl_test.mist +++ /dev/null @@ -1,56 +0,0 @@ -pub module impl_test; - -struct Vector2 { - x i32, - y i32, -} - -impl Vector2 { - pub fn magnitude(*self) i32 { - self.x * self.x + self.y * self.y - } -} - -pub fn test_inherent_impl() { - let v = Vector2 { x: 3, y: 4 }; - assert_eq!(v.magnitude(), 25); -} - -trait Display { - fn display(*self) String; -} - -impl Display for Vector2 { - fn display(*self) String { - format!("({}, {})", self.x, self.y) - } -} - -pub fn test_trait_impl_block() { - let v = Vector2 { x: 1, y: 2 }; - let s = v.display(); - assert_eq!(s.len() > 0, true); -} - -impl Vector2 { - pub fn add(*self, other Vector2) Vector2 { - Vector2 { - x: self.x + other.x, - y: self.y + other.y, - } - } -} - -pub fn 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 fn 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 deleted file mode 100644 index bd83f6b..0000000 --- a/mist-test/src/items/struct_test.mist +++ /dev/null @@ -1,46 +0,0 @@ -pub module struct_test; - -struct Point { - x i32, - y i32, -} - -pub fn test_struct_create_access() { - let p = Point { x: 10, y: 20 }; - assert_eq!(p.x, 10); - assert_eq!(p.y, 20); -} - -pub fn test_struct_mut_field() { - let mut p = Point { x: 1, y: 2 }; - p.x = 100; - assert_eq!(p.x, 100); -} - -pub fn 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 { - start Point, - end Point, -} - -pub fn 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 fn 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 deleted file mode 100644 index 41fe093..0000000 --- a/mist-test/src/items/trait_test.mist +++ /dev/null @@ -1,41 +0,0 @@ -pub module trait_test; - -trait Speak { - fn speak(*self) String; -} - -trait Greet : Speak { - fn greet(*self) String; -} - -struct Dog { - name String, -} - -impl Speak for Dog { - fn speak(*self) String { - "woof".to_string() - } -} - -impl Greet for Dog { - fn greet(*self) String { - "hello".to_string() - } -} - -pub fn test_trait_impl() { - let d = Dog { name: "Rex".to_string() }; - assert_eq!(d.speak(), "woof"); -} - -pub fn test_trait_with_requirement() { - let d = Dog { name: "Buddy".to_string() }; - assert_eq!(d.speak(), "woof"); - assert_eq!(d.greet(), "hello"); -} - -pub fn run_tests() { - test_trait_impl(); - test_trait_with_requirement(); -} diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index 6bf0069..c6bc7b1 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1,19 +1,5 @@ -fn main() { +fn Result<[], String> main() { println!("running common tests..."); - common::test_package(); - 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"); -} \ No newline at end of file + Ok(()) +} From d7e5e72bbb2aa60046a14a444a8a0713ee223bef Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 16 Jun 2026 03:32:45 +0200 Subject: [PATCH 2/7] Syntax: Tuples as () --- crates/parser/src/grammar.pest | 6 +++--- mist-test/src/main.mist | 4 +--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 9a46e40..9451996 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -97,7 +97,7 @@ generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" } // ====================================================== tuple_type = { - "[" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ","? ~ "]" + "(" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ","? ~ ")" } path_type = { @@ -142,11 +142,11 @@ multi_pat_template = _{ } tuple_pattern = { - "[" ~ (pattern | etc_pattern) ~ "," ~ multi_pat_template ~ "]" + "(" ~ (pattern | etc_pattern) ~ "," ~ multi_pat_template ~ ")" } named_tuple_pattern = { - static_path ~ "[" ~ multi_pat_template ~ "]" + static_path ~ "(" ~ multi_pat_template ~ ")" } struct_destruct = { diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index c6bc7b1..259dd4a 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1,5 +1,3 @@ -fn Result<[], String> main() { +fn void main() { println!("running common tests..."); - - Ok(()) } From f07bde8d3fa8fabbba758b472efacf12967444bd Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 16 Jun 2026 03:59:28 +0200 Subject: [PATCH 3/7] Unsafe lifetime --- crates/codegen/src/lib.rs | 22 ++++++++++++++++++---- crates/parser/src/ast/mod.rs | 8 +++++++- crates/parser/src/grammar.pest | 5 ++++- crates/parser/src/parser/common/types.rs | 4 ++-- crates/parser/src/parser/items/function.rs | 19 +++++++++++++++++-- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index 6b17a85..9fac9df 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -236,12 +236,26 @@ impl GetRust for TypeExpr { mutable, ty, } => { - let mutable = if *mutable { "mut " } else { "" }; - if let Some(lifetime) = lifetime { - format!("&'{} {mutable}{}", lifetime.get_rust(), ty.get_rust()) + match lifetime { + Lifetime::Lifetime(v) => { + format!( + "&{} {}{}", + v.get_rust(), + if *mutable { "mut " } else { "" }, + ty.get_rust() + ) + } + Lifetime::Unsafe => { + format!( + "*{} {}", + if *mutable { "mut" } else { "const" }, + ty.get_rust() + ) + } + } } else { - format!("&{mutable}{}", ty.get_rust()) + format!("&{}{}", if *mutable { "mut " } else { "" }, ty.get_rust()) } } diff --git a/crates/parser/src/ast/mod.rs b/crates/parser/src/ast/mod.rs index 28e8cf8..04e6d4b 100644 --- a/crates/parser/src/ast/mod.rs +++ b/crates/parser/src/ast/mod.rs @@ -17,10 +17,16 @@ pub struct Identifier(pub String); #[derive(Debug, Clone, Serialize, Default)] pub struct ParamList(pub Vec); +#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)] +pub enum Lifetime { + Lifetime(Identifier), + Unsafe, +} + #[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)] pub enum TypeExpr { Ref { - lifetime: Option, + lifetime: Option, mutable: bool, ty: Box, }, diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 9451996..5af7c30 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -82,6 +82,7 @@ module_program = { visibility = { "pub" ~ ("(" ~ static_path ~ ")")? } mutable = { "mut" } +unsafe_kw = { "unsafe" } // ====================================================== // GENERICS @@ -104,8 +105,10 @@ path_type = { static_path ~ generics? } +ref_lifetime = { lifetime | unsafe_kw } + ref_type = { - "*" ~ lifetime? ~ mutable? ~ type_expr + "*" ~ ref_lifetime? ~ mutable? ~ type_expr } dyn_type = { diff --git a/crates/parser/src/parser/common/types.rs b/crates/parser/src/parser/common/types.rs index fc610f0..3d2fa2b 100644 --- a/crates/parser/src/parser/common/types.rs +++ b/crates/parser/src/parser/common/types.rs @@ -28,8 +28,8 @@ impl<'a> TryFrom> for TypeExpr { Rule::ref_type => { ast_expr!(TypeExpr::Ref { - lifetime: consume_rule(&mut inner, Rule::lifetime) - .map(|v| v.into_inner().next().map(Identifier::try_from)) + lifetime: consume_rule(&mut inner, Rule::ref_lifetime) + .map(|v| v.into_inner().next().map(Lifetime::try_from)) .unwrap_or_default() .transpose(), mutable: Ok(listen_rule(&mut inner, Rule::mutable)) as AstResult, diff --git a/crates/parser/src/parser/items/function.rs b/crates/parser/src/parser/items/function.rs index 27da885..ef37078 100644 --- a/crates/parser/src/parser/items/function.rs +++ b/crates/parser/src/parser/items/function.rs @@ -2,7 +2,7 @@ use crate::{ Rule, ast::*, ast_ensure, ast_expr, - error::{AstError, AstResult, IntoErr}, + error::{self, AstError, AstResult, IntoErr}, parser::{consume_rule, listen_rule}, }; @@ -39,7 +39,7 @@ impl<'a> TryFrom> for FunctionDecl { type_: Some(if is_ref { TypeExpr::Ref { lifetime: - lifetime.map(|v| Identifier::try_from(v.into_inner().next().unwrap())) + lifetime.map(|v| Lifetime::try_from(v.into_inner().next().unwrap())) .transpose() .expect("Failed to get lifetime identifier"), mutable, @@ -88,3 +88,18 @@ impl<'a> TryFrom> for Override { }) } } + +impl<'a> TryFrom> for Lifetime { + type Error = AstError<'a, Self>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { + let mut inner = pair.clone().into_inner(); + + match pair.as_rule() { + Rule::ref_lifetime => inner.next().unwrap().try_into(), + Rule::lifetime => ast_expr!(Lifetime::Lifetime(inner.next().unwrap().try_into())), + Rule::unsafe_kw => Ok(Lifetime::Unsafe), + _ => error::AstError::bug_unimplemented(pair), + } + } +} From fa6765cdd93a8f2a99b3fa595f7d6b5384ed3232 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 16 Jun 2026 12:46:07 +0200 Subject: [PATCH 4/7] Updated common tests --- mist-test/src/common/decl_test.mist | 83 +++++++++++ mist-test/src/common/expr_test.mist | 152 +++++++++++++++++++++ mist-test/src/common/package.mist | 3 + mist-test/src/common/statement_test.mist | 167 +++++++++++++++++++++++ mist-test/src/common/types_test.mist | 65 +++++++++ 5 files changed, 470 insertions(+) create mode 100644 mist-test/src/common/decl_test.mist create mode 100644 mist-test/src/common/expr_test.mist create mode 100644 mist-test/src/common/package.mist create mode 100644 mist-test/src/common/statement_test.mist create mode 100644 mist-test/src/common/types_test.mist diff --git a/mist-test/src/common/decl_test.mist b/mist-test/src/common/decl_test.mist new file mode 100644 index 0000000..d1a1688 --- /dev/null +++ b/mist-test/src/common/decl_test.mist @@ -0,0 +1,83 @@ +pub module decl_test; + +pub fn void test_let_basic() { + let x = 42; + assert_eq!(x, 42); +} + +pub fn void test_let_typed() { + let x i32 = 42; + assert_eq!(x, 42); + + let s *str = "hello"; + assert_eq!(s, "hello"); + + let b bool = true; + assert_eq!(b, true); + + let f f64 = 3.14; + assert_eq!(f, 3.14); +} + +pub fn void test_let_mut() { + let mut _x = 42; + _x = 100; + assert_eq!(_x, 100); +} + +pub fn void test_let_tuple_destructure() { + let (a, b) = (10, "hello"); + assert_eq!(a, 10); + assert_eq!(b, "hello"); +} + +pub fn 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 fn 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 fn void test_multiple_decls() { + let a = 1; + let b = 2; + let c = 3; + assert_eq!(a + b + c, 6); +} + +pub fn void test_let_with_type_and_init() { + let x f64 = 10.0; + assert_eq!(x, 10.0); +} + +pub fn void test_string_literal() { + let s = "hello, world!"; + assert_eq!(s.len(), 13); +} + +pub fn void test_let_bool() { + let t = true; + let f = false; + assert_eq!(t, !f); +} + +pub fn 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..88ab9b3 --- /dev/null +++ b/mist-test/src/common/expr_test.mist @@ -0,0 +1,152 @@ +pub module expr_test; + +pub fn 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 fn void test_tuples() { + let t = (1, true, "three"); + assert_eq!(t.0, 1); + assert_eq!(t.1, true); + assert_eq!(t.2, "three"); +} + +pub fn void test_arrays() { + let arr = [1, 2, 3]; + assert_eq!(arr[0], 1); + assert_eq!(arr[1], 2); + assert_eq!(arr[2], 3); +} + +pub fn void test_array_repeat() { + let arr = [0; 5]; + assert_eq!(arr[0], 0); + assert_eq!(arr[4], 0); + assert_eq!(arr.len(), 5); +} + +pub fn 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 fn 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 fn 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 fn 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 fn void test_prefix() { + let x = 42; + assert_eq!(-x, -42); + assert_eq!(!true, false); + assert_eq!(!false, true); +} + +pub fn void test_ref_deref() { + let x = 42; + let r = &x; + assert_eq!(*r, 42); +} + +pub fn void test_postfix_inc_dec() { + let mut x = 5; + x++; + assert_eq!(x, 6); + x--; + assert_eq!(x, 5); +} + +pub fn void test_as_cast() { + let x = 42; + assert_eq!(x as f64, 42.0); +} + +pub fn void test_closure() { + let add = fn(a, b) -> a + b; + assert_eq!(add(2, 3), 5); +} + +pub fn void test_block_expr() { + let x = { + let y = 10; + y + 5 + }; + assert_eq!(x, 15); +} + +pub fn 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 fn void test_range_expr() { + let r = 0..5; + let mut count = 0; + for i in r { + count++; + } + assert_eq!(count, 5); +} + +pub fn 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 fn 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/package.mist b/mist-test/src/common/package.mist new file mode 100644 index 0000000..324108d --- /dev/null +++ b/mist-test/src/common/package.mist @@ -0,0 +1,3 @@ +pub module common; + +pub fn void test_package() {} diff --git a/mist-test/src/common/statement_test.mist b/mist-test/src/common/statement_test.mist new file mode 100644 index 0000000..ce9a7e9 --- /dev/null +++ b/mist-test/src/common/statement_test.mist @@ -0,0 +1,167 @@ +pub module statement_test; + +pub fn void test_var_decl() { + let x = 42; + assert_eq!(x, 42); + + let y i32 = 10; + assert_eq!(y, 10); + + let name *str = "mist"; + assert_eq!(name, "mist"); +} + +pub fn void test_var_decl_with_init() { + let mut x = 0; + x = 5; + assert_eq!(x, 5); +} + +pub fn 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 fn 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 fn void test_while_loop() { + let mut count = 0; + + while count < 5 { + count++; + } + assert_eq!(count, 5); +} + +pub fn void test_for_loop() { + let mut sum = 0; + + for i in 0..4 { + sum += i; + } + assert_eq!(sum, 6); +} + +pub fn void test_loop_stmt() { + let mut count = 0; + + loop { + count++; + if count >= 3 { + break; + } + } + assert_eq!(count, 3); +} + +pub fn void test_break_continue() { + let mut sum = 0; + + for i in 0..10 { + if i % 2 == 0 { + continue; + } + if i >= 7 { + break; + } + sum += i; + } + assert_eq!(sum, 1 + 3 + 5); +} + +pub fn i32 test_return() { + return 42; +} + +pub fn void test_block_scope() { + let x = { + let inner = 10; + inner + 5 + }; + assert_eq!(x, 15); +} + +pub fn void test_match_literal() { + let x = 2; + let result; + + match x { + 1 => { result = 10; } + 2 => { result = 20; } + 3 => { result = 30; } + _ => panic!(); + } + assert_eq!(result, 20); +} + +pub fn void test_match_or_pattern() { + let x = 1; + let result i32; + + match x { + 1 | 2 => { result = 100; } + 3 => { result = 200; } + _ => panic!(); + } + assert_eq!(result, 100); +} + +pub fn void test_match_default() { + let x = 99; + let result; + + match x { + 1 => { result = 10; } + y => { result = y; } + } + assert_eq!(result, 99); +} + +pub fn void test_for_with_pattern_destructure() { + let pairs = [(1, "a"), (2, "b")]; + let mut keys = 0; + + for (k, _) in pairs { + keys += k; + } + assert_eq!(keys, 3); +} + +pub fn 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(), 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..eaf2cf4 --- /dev/null +++ b/mist-test/src/common/types_test.mist @@ -0,0 +1,65 @@ +pub module types_test; + +pub fn void test_integer_type() { + let x i32 = 42; + assert_eq!(x, 42); +} + +pub fn void test_float_type() { + let x f64 = 3.14; + assert_eq!(x, 3.14); +} + +pub fn void test_bool_type() { + let x bool = true; + assert_eq!(x, true); + + let y bool = false; + assert_eq!(y, false); +} + +pub fn void test_string_type() { + let s *str = "hello"; + assert_eq!(s, "hello"); +} + +pub fn void test_tuple_type() { + let t (i32, *str) = (42, "answer"); + assert_eq!(t.0, 42); + assert_eq!(t.1, "answer"); +} + +pub fn void test_ref_type() { + let x i32 = 42; + let r *i32 = &x; + assert_eq!(*r, 42); +} + +pub fn void test_mut_ref_type() { + let mut x = 42; + let r *mut i32 = &mut x; + *r = 100; + assert_eq!(x, 100); +} + +pub fn void test_void_type() {} + +fn i32 add(a i32, b i32) { + a + b +} + +pub fn void test_fn_return_type() { + assert_eq!(add(3, 4), 7); +} + +pub fn 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(); +} From e4fbab190c0013485e02f249fa4b0e4dd1629769 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 16 Jun 2026 13:15:01 +0200 Subject: [PATCH 5/7] Added items --- mist-test/src/items/attribute_test.mist | 23 +++++++++ mist-test/src/items/class_test.mist | 56 +++++++++++++++++++++ mist-test/src/items/enum_test.mist | 62 +++++++++++++++++++++++ mist-test/src/items/function_test.mist | 66 +++++++++++++++++++++++++ mist-test/src/items/impl_test.mist | 56 +++++++++++++++++++++ mist-test/src/items/struct_test.mist | 46 +++++++++++++++++ mist-test/src/items/trait_test.mist | 41 +++++++++++++++ mist-test/src/main.mist | 16 ++++++ 8 files changed, 366 insertions(+) create mode 100644 mist-test/src/items/attribute_test.mist create mode 100644 mist-test/src/items/class_test.mist create mode 100644 mist-test/src/items/enum_test.mist create mode 100644 mist-test/src/items/function_test.mist create mode 100644 mist-test/src/items/impl_test.mist create mode 100644 mist-test/src/items/struct_test.mist create mode 100644 mist-test/src/items/trait_test.mist diff --git a/mist-test/src/items/attribute_test.mist b/mist-test/src/items/attribute_test.mist new file mode 100644 index 0000000..5363dd1 --- /dev/null +++ b/mist-test/src/items/attribute_test.mist @@ -0,0 +1,23 @@ +pub module attribute_test; + +#[derive(Debug)] +pub struct Debuggable { + value i32, +} + +pub fn void test_derive_attribute() { + let d = Debuggable { value: 42 }; + assert_eq!(d.value, 42); +} + +#[allow(unused_variables)] +pub fn void test_allow_attribute() { + let x = 42; + let y = "unused"; + assert_eq!(x, 42); +} + +pub fn 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..ae2d9a3 --- /dev/null +++ b/mist-test/src/items/class_test.mist @@ -0,0 +1,56 @@ +pub module class_test; + +pub class Animal { + pub name String; + pub val T; + + constructor() { + self.name = "Rex".to_string(); + } + + pub fn void speak(*self) { + println!("Unknown"); + } + + pub fn void test_speak(*self) { + self.speak(); + } + + pub fn void static_method() {} +} + +pub class Mammal : Animal { + constructor() { + super = Super::new(); + } + + pub fn void args_test(*self, x i32, y *str) {} + + pub fn *'a str returning<'a>(*self) { + "Mammal" + } +} + +pub class Dog : Mammal { + constructor() { + super = Super::new(); + } + + pub override(Animal) fn void speak(*self) { + println!("Woof!"); + } + + pub override fn *str returning(*self) { + "Dog" + } +} + +pub fn void run_tests() { + let dawg = Dog::new(); + + dawg.args_test(10, "hello"); + + dawg.test_speak(); + + println!("{}", dawg.returning()); +} diff --git a/mist-test/src/items/enum_test.mist b/mist-test/src/items/enum_test.mist new file mode 100644 index 0000000..db6c763 --- /dev/null +++ b/mist-test/src/items/enum_test.mist @@ -0,0 +1,62 @@ +pub module enum_test; + +enum OptionInt { + None, + Some(i32), +} + +pub fn void test_enum_named() { + let x = OptionInt::None; + match x { + OptionInt::None => { assert_eq!(1, 1); } + OptionInt::Some(_) => { assert_eq!(0, 1); } + } +} + +pub fn 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 { radius i32 }, + Rect { w i32, h i32 }, +} + +pub fn 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 fn void test_enum_eq() { + let c = Color::Red; + match c { + Color::Red => { assert_eq!(1, 1); } + _ => { assert_eq!(0, 1); } + } +} + +pub fn 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..3cec722 --- /dev/null +++ b/mist-test/src/items/function_test.mist @@ -0,0 +1,66 @@ +pub module function_test; + +fn void greet() { + let mut x = 1; + x++; +} + +pub fn void test_basic_function() { + greet(); +} + +fn i32 add(a i32, b i32) { + a + b +} + +pub fn void test_fn_params() { + assert_eq!(add(2, 3), 5); +} + +pub fn i32 test_fn_return() { + 42 +} + +pub fn i32 test_fn_early_return() { + return 100; + 0 +} + +pub fn i32 test_fn_mut_param(mut x i32) { + x = x + 10; + x +} + +pub fn void test_method_call() { + let s = "hello"; + assert_eq!(s.len(), 5); + assert_eq!(s.to_uppercase(), "HELLO"); +} + +fn i32 triple(x i32) { + add(x, add(x, x)) +} + +pub fn void test_nested_fn_calls() { + assert_eq!(triple(5), 15); +} + +fn i32 factorial(n i32) { + if n <= 1 { 1 } + else { n * factorial(n - 1) } +} + +pub fn void test_recursive_fn() { + assert_eq!(factorial(5), 120); +} + +pub fn 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..68d0ed6 --- /dev/null +++ b/mist-test/src/items/impl_test.mist @@ -0,0 +1,56 @@ +pub module impl_test; + +struct Vector2 { + x i32, + y i32, +} + +impl Vector2 { + pub fn i32 magnitude(*self) { + self.x * self.x + self.y * self.y + } +} + +pub fn void test_inherent_impl() { + let v = Vector2 { x: 3, y: 4 }; + assert_eq!(v.magnitude(), 25); +} + +trait Display { + fn String display(*self); +} + +impl Display for Vector2 { + fn String display(*self) { + format!("({}, {})", self.x, self.y) + } +} + +pub fn 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 fn Vector2 add(*self, other Vector2) { + Vector2 { + x: self.x + other.x, + y: self.y + other.y, + } + } +} + +pub fn 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 fn 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..d5e27b1 --- /dev/null +++ b/mist-test/src/items/struct_test.mist @@ -0,0 +1,46 @@ +pub module struct_test; + +struct Point { + x i32, + y i32, +} + +pub fn void test_struct_create_access() { + let p = Point { x: 10, y: 20 }; + assert_eq!(p.x, 10); + assert_eq!(p.y, 20); +} + +pub fn void test_struct_mut_field() { + let mut p = Point { x: 1, y: 2 }; + p.x = 100; + assert_eq!(p.x, 100); +} + +pub fn 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 { + start Point, + end Point, +} + +pub fn 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 fn 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..4e7a2bf --- /dev/null +++ b/mist-test/src/items/trait_test.mist @@ -0,0 +1,41 @@ +pub module trait_test; + +trait Speak { + fn String speak(*self); +} + +trait Greet : Speak { + fn String greet(*self); +} + +struct Dog { + name String, +} + +impl Speak for Dog { + fn String speak(*self) { + "woof".to_string() + } +} + +impl Greet for Dog { + fn String greet(*self) { + "hello".to_string() + } +} + +pub fn void test_trait_impl() { + let d = Dog { name: "Rex".to_string() }; + assert_eq!(d.speak(), "woof"); +} + +pub fn void test_trait_with_requirement() { + let d = Dog { name: "Buddy".to_string() }; + assert_eq!(d.speak(), "woof"); + assert_eq!(d.greet(), "hello"); +} + +pub fn 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 259dd4a..0025a0c 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1,3 +1,19 @@ fn void main() { println!("running common tests..."); + common::test_package(); + 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"); } From 001165ee58d8349a395d851445aa14f32ddeb886 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 16 Jun 2026 13:17:06 +0200 Subject: [PATCH 6/7] Fixed lifetime on ref --- crates/codegen/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index 9fac9df..1d926e1 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -240,7 +240,7 @@ impl GetRust for TypeExpr { match lifetime { Lifetime::Lifetime(v) => { format!( - "&{} {}{}", + "&'{} {}{}", v.get_rust(), if *mutable { "mut " } else { "" }, ty.get_rust() From 21226980000a5514e506a0f6bcfbb028d1d6b0ae Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 16 Jun 2026 13:49:38 +0200 Subject: [PATCH 7/7] Better class test --- mist-test/src/items/class_test.mist | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/mist-test/src/items/class_test.mist b/mist-test/src/items/class_test.mist index ae2d9a3..a95c15a 100644 --- a/mist-test/src/items/class_test.mist +++ b/mist-test/src/items/class_test.mist @@ -8,12 +8,12 @@ pub class Animal { self.name = "Rex".to_string(); } - pub fn void speak(*self) { - println!("Unknown"); + pub fn String speak(*self) { + "Unknown".to_string() } pub fn void test_speak(*self) { - self.speak(); + assert_eq!(self.speak(), "Woof!"); } pub fn void static_method() {} @@ -24,10 +24,9 @@ pub class Mammal : Animal { super = Super::new(); } - pub fn void args_test(*self, x i32, y *str) {} - - pub fn *'a str returning<'a>(*self) { - "Mammal" + pub fn void args_test(*self, x i32, y *str) { + assert_eq!(x, 10); + assert_eq!(y, "hello"); } } @@ -36,12 +35,14 @@ pub class Dog : Mammal { super = Super::new(); } - pub override(Animal) fn void speak(*self) { - println!("Woof!"); + pub override(Animal) fn String speak(*self) { + "Woof!".to_string() } - pub override fn *str returning(*self) { - "Dog" + impl std::fmt::Display { + fn Result<(), std::fmt::Error> fmt(*self, f *mut std::fmt::Formatter<'_>) { + write!(f, "🐾 {}", self.name) + } } } @@ -52,5 +53,5 @@ pub fn void run_tests() { dawg.test_speak(); - println!("{}", dawg.returning()); + assert_eq!(format!("{dawg}"), "🐾 Rex"); }