From 40b29812528bbc469a523a6f5746a69157145198 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 5 Jun 2026 17:06:06 +0200 Subject: [PATCH 01/11] Updated type grammar #65 --- crates/parser/src/grammar.pest | 8 +- mist-test/src/common/decl_test.mist | 81 ----------- mist-test/src/common/expr_test.mist | 152 --------------------- mist-test/src/common/statement_test.mist | 165 ----------------------- mist-test/src/common/types_test.mist | 63 --------- mist-test/src/items/attribute_test.mist | 21 --- mist-test/src/items/class_test.mist | 58 -------- mist-test/src/items/enum_test.mist | 60 --------- mist-test/src/items/function_test.mist | 64 --------- mist-test/src/items/impl_test.mist | 54 -------- mist-test/src/items/struct_test.mist | 44 ------ mist-test/src/items/trait_test.mist | 39 ------ mist-test/src/main.mist | 19 +-- 13 files changed, 5 insertions(+), 823 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/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 7eba693..46b9a1f 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -31,6 +31,7 @@ keyword = { | "dyn" | "loop" | "fn" + | "unsafe" } identifier = @{ @@ -100,15 +101,15 @@ path_type = { } ref_type = { - mutable? ~ lifetime? ~ "*" + "*" ~ lifetime? ~ mutable? ~ type_expr } dyn_type = { - "dyn" + "dyn" ~ type_expr } type_expr = { - (path_type | tuple_type) ~ (ref_type | dyn_type)* + (path_type | tuple_type | ref_type | dyn_type) } lifetime = { @@ -628,4 +629,3 @@ bin_op = { expr = { term ~ (bin_op ~ term)* } term = { prefix* ~ primary ~ postfix* } - diff --git a/mist-test/src/common/decl_test.mist b/mist-test/src/common/decl_test.mist deleted file mode 100644 index 8c67ab1..0000000 --- a/mist-test/src/common/decl_test.mist +++ /dev/null @@ -1,81 +0,0 @@ -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 deleted file mode 100644 index 1132fba..0000000 --- a/mist-test/src/common/expr_test.mist +++ /dev/null @@ -1,152 +0,0 @@ -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 deleted file mode 100644 index 1aab6fa..0000000 --- a/mist-test/src/common/statement_test.mist +++ /dev/null @@ -1,165 +0,0 @@ -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 deleted file mode 100644 index 2fac144..0000000 --- a/mist-test/src/common/types_test.mist +++ /dev/null @@ -1,63 +0,0 @@ -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 deleted file mode 100644 index 3dd06b2..0000000 --- a/mist-test/src/items/attribute_test.mist +++ /dev/null @@ -1,21 +0,0 @@ -#[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 deleted file mode 100644 index 90d6720..0000000 --- a/mist-test/src/items/class_test.mist +++ /dev/null @@ -1,58 +0,0 @@ -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 deleted file mode 100644 index d111175..0000000 --- a/mist-test/src/items/enum_test.mist +++ /dev/null @@ -1,60 +0,0 @@ -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 deleted file mode 100644 index 0278874..0000000 --- a/mist-test/src/items/function_test.mist +++ /dev/null @@ -1,64 +0,0 @@ -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 deleted file mode 100644 index 4032913..0000000 --- a/mist-test/src/items/impl_test.mist +++ /dev/null @@ -1,54 +0,0 @@ -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 deleted file mode 100644 index 53698da..0000000 --- a/mist-test/src/items/struct_test.mist +++ /dev/null @@ -1,44 +0,0 @@ -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 deleted file mode 100644 index ba93611..0000000 --- a/mist-test/src/items/trait_test.mist +++ /dev/null @@ -1,39 +0,0 @@ -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 6e8730e..05a37e9 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1,18 +1 @@ -void main() { - 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"); -} +void main() {} \ No newline at end of file From a589d7173db3998daaf65f3f40c900c6e93796bf Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 5 Jun 2026 17:11:46 +0200 Subject: [PATCH 02/11] Updated type ast #65 --- crates/parser/src/ast/mod.rs | 31 ++++++++----------------------- mist-test/src/main.mist | 2 +- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/crates/parser/src/ast/mod.rs b/crates/parser/src/ast/mod.rs index 23e9ce6..6a975df 100644 --- a/crates/parser/src/ast/mod.rs +++ b/crates/parser/src/ast/mod.rs @@ -18,19 +18,13 @@ pub struct Identifier(pub String); pub struct ParamList(pub Vec); #[derive(Debug, Clone, Serialize)] -pub struct TypeExpr(pub TypeExprKind, pub Vec); - -#[derive(Debug, Clone, Serialize)] -pub enum TypePostfix { - Ref, - RefMut, - RefLifetime(Identifier), - RefMutLifetime(Identifier), - Dyn, -} - -#[derive(Debug, Clone, Serialize)] -pub enum TypeExprKind { +pub enum TypeExpr { + Ref { + lifetime: Option, + mutable: bool, + ty: Box, + }, + Dyn(Box), Path(Path, Option), Tuple(Vec), Lifetime(Identifier), @@ -43,20 +37,11 @@ pub struct Spanned { pub item: T, } -impl TypeExpr { - pub fn no_px(kind: TypeExprKind) -> Self { - Self(kind, Vec::new()) - } -} - impl From for Generic { fn from(value: GenericDecl) -> Self { match value { GenericDecl::Lifetime(life) => Generic::Lifetime(life), - GenericDecl::Type(ty, _) => Generic::Type(TypeExpr( - TypeExprKind::Path(Path(vec![ty]), None), - Vec::new(), - )), + GenericDecl::Type(ty, _) => Generic::Type(TypeExpr::Path(Path(vec![ty]), None)), } } } diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index 05a37e9..d8b71aa 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1 +1 @@ -void main() {} \ No newline at end of file +*void main() {} \ No newline at end of file From 143d496ee804cce0cc5f7d080fe8a0289d5803f2 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 5 Jun 2026 17:25:39 +0200 Subject: [PATCH 03/11] Updated type parser #65 --- crates/parser/src/parser/common/types.rs | 87 ++++++---------------- crates/parser/src/parser/items/function.rs | 18 ++--- 2 files changed, 28 insertions(+), 77 deletions(-) diff --git a/crates/parser/src/parser/common/types.rs b/crates/parser/src/parser/common/types.rs index 2bf19d6..aa3307f 100644 --- a/crates/parser/src/parser/common/types.rs +++ b/crates/parser/src/parser/common/types.rs @@ -2,67 +2,10 @@ use crate::{ Rule, ast::*, ast_ensure, ast_expr, - error::{AstError, IntoErr, collect_recovered}, + error::{AstError, AstResult, IntoErr, collect_recovered}, parser::{consume_rule, listen_rule}, }; -impl<'a> TryFrom> for TypePostfix { - type Error = AstError<'a, Self>; - - fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { - let rule = pair.as_rule(); - let mut inner = pair.clone().into_inner(); - - match rule { - Rule::ref_type => { - let mutable = listen_rule(&mut inner, Rule::mutable); - let lifetime = consume_rule(&mut inner, Rule::lifetime) - .map(|pair| Identifier::try_from(pair.into_inner().next().unwrap())) - .transpose() - .get()?; - - Ok(if mutable { - if let Some(lifetime) = lifetime { - TypePostfix::RefMutLifetime(lifetime) - } else { - TypePostfix::RefMut - } - } else { - if let Some(lifetime) = lifetime { - TypePostfix::RefLifetime(lifetime) - } else { - TypePostfix::Ref - } - }) - } - - Rule::dyn_type => Ok(TypePostfix::Dyn), - - _ => AstError::bug_unimplemented(pair), - } - } -} - -impl<'a> TryFrom> for TypeExprKind { - type Error = AstError<'a, Self>; - - fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { - let rule = pair.as_rule(); - let mut inner = pair.clone().into_inner(); - - match rule { - Rule::tuple_type => ast_expr!(TypeExprKind::Tuple(collect_recovered(inner))), - Rule::path_type => { - ast_expr!(TypeExprKind::Path( - Path::try_from(inner.next().unwrap()), - inner.next().map(Generics::try_from).transpose() - )) - } - _ => AstError::bug_unimplemented(pair), - } - } -} - impl<'a> TryFrom> for TypeExpr { type Error = AstError<'a, Self>; @@ -72,13 +15,27 @@ impl<'a> TryFrom> for TypeExpr { match rule { Rule::generic => Self::try_from(inner.next().unwrap()), - Rule::type_expr => ast_expr!(TypeExpr( - inner.next().unwrap().try_into(), - collect_recovered(inner), - )), - Rule::lifetime => ast_expr!(TypeExprKind::Lifetime(inner.next().unwrap().try_into())) - .get_map(TypeExpr::no_px) - .map(TypeExpr::no_px), + Rule::type_expr => inner.next().unwrap().try_into(), + Rule::lifetime => ast_expr!(TypeExpr::Lifetime(inner.next().unwrap().try_into())), + + Rule::tuple_type => ast_expr!(TypeExpr::Tuple(collect_recovered(inner))), + Rule::path_type => { + ast_expr!(TypeExpr::Path( + Path::try_from(inner.next().unwrap()), + inner.next().map(Generics::try_from).transpose() + )) + } + + Rule::ref_type => { + ast_expr!(TypeExpr::Ref { + lifetime: consume_rule(&mut inner, Rule::lifetime) + .map(|v| v.into_inner().next().map(Identifier::try_from)) + .unwrap_or_default() + .transpose(), + mutable: Ok(listen_rule(&mut inner, Rule::mutable)) as AstResult, + ty: TypeExpr::try_from(inner.next().unwrap()).map(Box::new), + }) + } _ => AstError::bug_unimplemented(pair), } diff --git a/crates/parser/src/parser/items/function.rs b/crates/parser/src/parser/items/function.rs index 8f75627..248ef3a 100644 --- a/crates/parser/src/parser/items/function.rs +++ b/crates/parser/src/parser/items/function.rs @@ -26,21 +26,15 @@ impl<'a> TryFrom> for FunctionDecl { let mutable = listen_rule(&mut param_inner, Rule::mutable); let is_ref = listen_rule(&mut param_inner, Rule::deref_px); let name = Pattern::Path(mutable && !is_ref, Path(vec![Identifier(String::from("self"))])); + let self_ty = TypeExpr::Path(Path(vec![Identifier(String::from("Self"))]), None); VarDecl { name: name.clone(), - type_: Some(TypeExpr( - TypeExprKind::Path(Path(vec![Identifier("Self".to_string())]), None), - if is_ref { - vec![if mutable { - TypePostfix::RefMut - } else { - TypePostfix::Ref - }] - } else { - Vec::new() - }, - )), + type_: Some(if is_ref { + TypeExpr::Ref { lifetime: None, mutable, ty: Box::new(self_ty) } + } else { + self_ty + }), } }); From b5592152126b567395ceca60facfd740c6258e5f Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 5 Jun 2026 17:34:06 +0200 Subject: [PATCH 04/11] Updated type codegen #65 --- crates/codegen/src/lib.rs | 82 ++++++++++++++++----------------- crates/codegen/src/top_level.rs | 19 ++++---- 2 files changed, 51 insertions(+), 50 deletions(-) diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index a8f4433..770acd2 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -156,18 +156,6 @@ impl GetRust for Path { } } -impl GetRust for TypePostfix { - fn get_rust(&self) -> String { - match self { - TypePostfix::Ref => format!("&"), - TypePostfix::RefMut => format!("&mut "), - TypePostfix::RefLifetime(lifetime) => format!("&'{} ", lifetime.get_rust()), - TypePostfix::RefMutLifetime(lifetime) => format!("&'{} mut ", lifetime.get_rust()), - TypePostfix::Dyn => format!("dyn "), - } - } -} - impl GetRust for Visibility { fn get_rust(&self) -> String { match self { @@ -186,12 +174,47 @@ impl GetRust for Identifier { impl GetRust for TypeExpr { fn get_rust(&self) -> String { - self.1 - .iter() - .map(TypePostfix::get_rust) - .rev() - .collect::() - + &self.0.get_rust() + match self { + // TypePostfix::Ref => format!("&"), + // TypePostfix::RefMut => format!("&mut "), + // TypePostfix::RefLifetime(lifetime) => format!("&'{} ", lifetime.get_rust()), + // TypePostfix::RefMutLifetime(lifetime) => format!("&'{} mut ", lifetime.get_rust()), + // TypePostfix::Dyn => format!("dyn "), + Self::Path(path, generics) => { + if let Some(generics) = generics { + format!("{}{}", get_static_type_path(path), generics.get_rust()) + } else { + get_static_type_path(path) + } + } + Self::Lifetime(name) => format!("'{}", name.get_rust()), + Self::Tuple(types) => format!( + "({})", + types + .into_iter() + .map(|t| t.get_rust()) + .collect::>() + .join(", ") + ), + + Self::Ref { + lifetime, + mutable, + ty, + } => { + let mutable = if *mutable { "mut " } else { "" }; + + if let Some(lifetime) = lifetime { + format!("&{}{mutable}{}", lifetime.get_rust(), ty.get_rust()) + } else { + format!("&{mutable}{}", ty.get_rust()) + } + } + + Self::Dyn(ty) => { + format!("dyn {}", ty.get_rust()) + } + } } } @@ -205,29 +228,6 @@ pub fn get_static_type_path(path: &Path) -> String { } } -impl GetRust for TypeExprKind { - fn get_rust(&self) -> String { - match self { - TypeExprKind::Path(path, generics) => { - if let Some(generics) = generics { - format!("{}{}", get_static_type_path(path), generics.get_rust()) - } else { - get_static_type_path(path) - } - } - TypeExprKind::Lifetime(name) => format!("'{}", name.get_rust()), - TypeExprKind::Tuple(types) => format!( - "({})", - types - .into_iter() - .map(|t| t.get_rust()) - .collect::>() - .join(", ") - ), - } - } -} - impl GenRust for Pattern { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { match self { diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index bc4ef66..a9ef282 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -234,10 +234,14 @@ impl GenRust for (&Vec>, &Spanned) { let mut constructor_params = vec![VarDecl { name: Pattern::Path(false, Path(vec![Identifier(String::from("self"))])), - type_: Some(TypeExpr( - TypeExprKind::Path(Path(vec![Identifier(String::from("Self"))]), None), - vec![TypePostfix::RefMut], - )), + type_: Some(TypeExpr::Ref { + lifetime: None, + mutable: true, + ty: Box::new(TypeExpr::Path( + Path(vec![Identifier(String::from("Self"))]), + None, + )), + }), }]; constructor_params.append(&mut self.1.item.params.0.clone()); @@ -250,7 +254,7 @@ impl GenRust for (&Vec>, &Spanned) { name: Identifier(String::from("constructor")), generics: self.1.item.generics.clone(), params: ParamList(constructor_params), - return_type: TypeExpr::no_px(TypeExprKind::Tuple(Vec::new())), + return_type: TypeExpr::Tuple(Vec::new()), body: Some(self.1.item.body.clone()), }, } @@ -423,10 +427,7 @@ impl GenRust for TopLevelKind { let mut impl_ = impl_.clone(); impl_.item.trait_ = Some(impl_.item.target); - impl_.item.target = TypeExpr( - TypeExprKind::Path(Path(vec![name.clone()]), None), - Vec::new(), - ); + impl_.item.target = TypeExpr::Path(Path(vec![name.clone()]), None); impl_.gen_rust(ctx, cg); } From cac497a560bcd33fd1e43c177b0f8303764f486a Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 5 Jun 2026 17:39:41 +0200 Subject: [PATCH 05/11] Fixed type bugs #65 --- crates/codegen/src/lib.rs | 2 +- crates/parser/src/parser/common/types.rs | 5 +++++ mist-test/src/main.mist | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index 770acd2..168b0af 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -205,7 +205,7 @@ impl GetRust for TypeExpr { let mutable = if *mutable { "mut " } else { "" }; if let Some(lifetime) = lifetime { - format!("&{}{mutable}{}", lifetime.get_rust(), ty.get_rust()) + format!("&'{} {mutable}{}", lifetime.get_rust(), ty.get_rust()) } else { format!("&{mutable}{}", ty.get_rust()) } diff --git a/crates/parser/src/parser/common/types.rs b/crates/parser/src/parser/common/types.rs index aa3307f..fc610f0 100644 --- a/crates/parser/src/parser/common/types.rs +++ b/crates/parser/src/parser/common/types.rs @@ -36,6 +36,11 @@ impl<'a> TryFrom> for TypeExpr { ty: TypeExpr::try_from(inner.next().unwrap()).map(Box::new), }) } + Rule::dyn_type => { + ast_expr!(TypeExpr::Dyn( + TypeExpr::try_from(inner.next().unwrap()).map(Box::new), + )) + } _ => AstError::bug_unimplemented(pair), } diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index d8b71aa..05a37e9 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1 +1 @@ -*void main() {} \ No newline at end of file +void main() {} \ No newline at end of file From 56c22eb1ed4fbe1612a55ae593b2425df7a9a04c Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 5 Jun 2026 17:43:59 +0200 Subject: [PATCH 06/11] Update: Decleration Helpers, grammar #65 --- crates/parser/src/grammar.pest | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 46b9a1f..e3d0658 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -77,7 +77,6 @@ program = { visibility = { "pub" ~ ("(" ~ static_path ~ ")")? } mutable = { "mut" } -let_kw = { "let" } // ====================================================== // GENERICS @@ -193,11 +192,15 @@ attributes = { // DECLARATION HELPERS // ====================================================== var_decl = { - (let_kw | type_expr) ~ pattern + "let" ~ WHITESPACE ~ pattern ~ type_expr } field = { - visibility? ~ type_expr ~ identifier + visibility? ~ identifier ~ type_expr +} + +param = { + pattern ~ type_expr } field_list = { @@ -205,7 +208,7 @@ field_list = { } param_list = { - (var_decl ~ ("," ~ var_decl)* ~ ","?)? + (param ~ ("," ~ param)* ~ ","?)? } // ====================================================== From ac9ee5a4b66a7630cf8e5cc9498c52fa0a9fb165 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 5 Jun 2026 17:52:04 +0200 Subject: [PATCH 07/11] Update: Decleration Helpers, parser #65 --- crates/parser/src/grammar.pest | 4 +-- crates/parser/src/parser/common/decl.rs | 31 +++++++++------------- crates/parser/src/parser/items/function.rs | 10 +------ mist-test/src/main.mist | 10 ++++++- 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index e3d0658..97e1613 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -192,7 +192,7 @@ attributes = { // DECLARATION HELPERS // ====================================================== var_decl = { - "let" ~ WHITESPACE ~ pattern ~ type_expr + "let" ~ WHITESPACE ~ pattern ~ type_expr? } field = { @@ -200,7 +200,7 @@ field = { } param = { - pattern ~ type_expr + pattern ~ type_expr? } field_list = { diff --git a/crates/parser/src/parser/common/decl.rs b/crates/parser/src/parser/common/decl.rs index b0af0e0..7a07fb9 100644 --- a/crates/parser/src/parser/common/decl.rs +++ b/crates/parser/src/parser/common/decl.rs @@ -2,7 +2,7 @@ use crate::{ Rule, ast::*, ast_expr, - error::{AstError, IntoErr}, + error::{AstError, IntoErr, collect_recovered}, }; impl<'a> TryFrom> for VarDeclStmt { @@ -48,25 +48,12 @@ impl<'a> TryFrom> for VarDecl { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { match pair.as_rule() { - Rule::var_decl => { + Rule::var_decl | Rule::param => { let mut inner = pair.into_inner(); - let type_ = inner - .next() - .and_then(|pair| { - if pair.as_rule() == Rule::let_kw { - None - } else { - Some(TypeExpr::try_from(pair)) - } - }) - .transpose(); - - let name = Pattern::try_from(inner.next().unwrap()); - ast_expr!(VarDecl { - type_: type_, - name: name, + name: Pattern::try_from(inner.next().unwrap()), + type_: inner.next().map(TypeExpr::try_from).transpose(), }) } @@ -85,8 +72,8 @@ impl<'a> TryFrom> for FieldDecl { ast_expr!(FieldDecl { visibility: Visibility::try_from(&mut inner), - type_: TypeExpr::try_from(inner.next().unwrap()), name: Identifier::try_from(inner.next().unwrap()), + type_: TypeExpr::try_from(inner.next().unwrap()), }) } @@ -94,3 +81,11 @@ impl<'a> TryFrom> for FieldDecl { } } } + +impl<'a> TryFrom> for ParamList { + type Error = AstError<'a, Self>; + + fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { + Ok(ParamList(collect_recovered(pair.into_inner()).get()?)) + } +} diff --git a/crates/parser/src/parser/items/function.rs b/crates/parser/src/parser/items/function.rs index 248ef3a..d44d00d 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, collect_recovered}, + error::{AstError, AstResult, IntoErr}, parser::{consume_rule, listen_rule}, }; @@ -64,11 +64,3 @@ impl<'a> TryFrom> for FunctionDecl { }) } } - -impl<'a> TryFrom> for ParamList { - type Error = AstError<'a, Self>; - - fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { - Ok(ParamList(collect_recovered(pair.into_inner()).get()?)) - } -} diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index 05a37e9..0e319b9 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1 +1,9 @@ -void main() {} \ No newline at end of file +void main() {} + +struct MyStruct<'a> { + name *'a String +} + +i32 add(a i32, b i32) { + a + b +} From d364c41c2686a110891617343a20537897674b92 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 5 Jun 2026 18:07:47 +0200 Subject: [PATCH 08/11] Update: Function #65 --- crates/codegen/src/top_level.rs | 9 ++++++--- crates/parser/src/ast/top_level.rs | 2 +- crates/parser/src/grammar.pest | 4 ++-- crates/parser/src/parser/items/function.rs | 5 ++++- mist-test/src/main.mist | 4 ++-- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index a9ef282..94073c5 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -88,8 +88,11 @@ impl GenRust for FunctionDecl { param.gen_rust(ctx, cg); } - cg.add(") -> "); - cg.add(&self.return_type.get_rust()); + cg.add(") "); + if let Some(return_type) = &self.return_type { + cg.add("-> "); + cg.add(&return_type.get_rust()); + } if let Some(body) = &self.body { cg.add(" "); @@ -254,7 +257,7 @@ impl GenRust for (&Vec>, &Spanned) { name: Identifier(String::from("constructor")), generics: self.1.item.generics.clone(), params: ParamList(constructor_params), - return_type: TypeExpr::Tuple(Vec::new()), + return_type: Some(TypeExpr::Tuple(Vec::new())), body: Some(self.1.item.body.clone()), }, } diff --git a/crates/parser/src/ast/top_level.rs b/crates/parser/src/ast/top_level.rs index f87d011..dd29e9b 100644 --- a/crates/parser/src/ast/top_level.rs +++ b/crates/parser/src/ast/top_level.rs @@ -97,7 +97,7 @@ pub struct FunctionDecl { pub name: Identifier, pub generics: GenericsDecl, pub params: ParamList, - pub return_type: TypeExpr, + pub return_type: Option, pub body: Option, } diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 97e1613..07f4b7b 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -192,7 +192,7 @@ attributes = { // DECLARATION HELPERS // ====================================================== var_decl = { - "let" ~ WHITESPACE ~ pattern ~ type_expr? + "let" ~ pattern ~ type_expr? } field = { @@ -323,7 +323,7 @@ trait_decl = { // ====================================================== function_decl = { - visibility? ~ type_expr ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ (block | semicolon) + visibility? ~ "fn" ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ type_expr? ~ (block | semicolon) } // ====================================================== diff --git a/crates/parser/src/parser/items/function.rs b/crates/parser/src/parser/items/function.rs index d44d00d..7dbd50c 100644 --- a/crates/parser/src/parser/items/function.rs +++ b/crates/parser/src/parser/items/function.rs @@ -13,7 +13,6 @@ impl<'a> TryFrom> for FunctionDecl { ast_ensure!(pair, Rule::function_decl => { let mut inner = pair.into_inner(); let visibility = Visibility::try_from(&mut inner); - let return_type = TypeExpr::try_from(inner.next().unwrap()); let name = Identifier::try_from(inner.next().unwrap()); let generics = consume_rule(&mut inner, Rule::generics_decl) @@ -51,6 +50,10 @@ 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/main.mist b/mist-test/src/main.mist index 0e319b9..6ea27b3 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1,9 +1,9 @@ -void main() {} +fn main() {} struct MyStruct<'a> { name *'a String } -i32 add(a i32, b i32) { +fn add(a i32, b i32) i32 { a + b } From 765e2ded936cde7f0657ff7bc10a9e95c212663c Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 5 Jun 2026 18:16:37 +0200 Subject: [PATCH 09/11] Update: Self param #64 --- crates/parser/src/grammar.pest | 2 +- crates/parser/src/parser/items/function.rs | 12 ++++++++++-- mist-test/src/main.mist | 8 ++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 07f4b7b..f6cf6b5 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -278,7 +278,7 @@ class_field = { } self_param = { - "self" ~ mutable? ~ deref_px? + (deref_px ~ lifetime?)? ~ mutable? ~ "self" } method_args = _{ diff --git a/crates/parser/src/parser/items/function.rs b/crates/parser/src/parser/items/function.rs index 7dbd50c..3919cce 100644 --- a/crates/parser/src/parser/items/function.rs +++ b/crates/parser/src/parser/items/function.rs @@ -22,15 +22,23 @@ impl<'a> TryFrom> for FunctionDecl { let self_param = consume_rule(&mut inner, Rule::self_param).map(|param| { let mut param_inner = param.into_inner(); - let mutable = listen_rule(&mut param_inner, Rule::mutable); let is_ref = listen_rule(&mut param_inner, Rule::deref_px); + let lifetime = consume_rule(&mut param_inner, Rule::lifetime); + let mutable = listen_rule(&mut param_inner, Rule::mutable); let name = Pattern::Path(mutable && !is_ref, Path(vec![Identifier(String::from("self"))])); let self_ty = TypeExpr::Path(Path(vec![Identifier(String::from("Self"))]), None); VarDecl { name: name.clone(), type_: Some(if is_ref { - TypeExpr::Ref { lifetime: None, mutable, ty: Box::new(self_ty) } + TypeExpr::Ref { + lifetime: + lifetime.map(|v| Identifier::try_from(v.into_inner().next().unwrap())) + .transpose() + .expect("Failed to get lifetime identifier"), + mutable, + ty: Box::new(self_ty) + } } else { self_ty }), diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index 6ea27b3..b6563fc 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -7,3 +7,11 @@ struct MyStruct<'a> { fn add(a i32, b i32) i32 { a + b } + +class MyClass { + name String; + + constructor() {} + + fn my_func(*mut self) {} +} From a9eb77f1583dbf22326ca4400166cf91afb3b504 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 5 Jun 2026 18:51:55 +0200 Subject: [PATCH 10/11] Update: Closures #64 --- crates/parser/src/grammar.pest | 5 ++++- crates/parser/src/parser/common/expr.rs | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index f6cf6b5..e4f33fb 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -453,7 +453,10 @@ match_stmt = { // CLOSURES // ------------------------------------------------------ -closure = { type_expr? ~ "fn" ~ "(" ~ param_list ~ ")" ~ expr } +closure = { + "fn" ~ "(" ~ param_list ~ ")" ~ type_expr? ~ block + | "fn" ~ "(" ~ param_list ~ ")" ~ type_expr? ~ "->" ~ expr +} // ------------------------------------------------------ // TUPLES & ARRAYS diff --git a/crates/parser/src/parser/common/expr.rs b/crates/parser/src/parser/common/expr.rs index e105511..be1790a 100644 --- a/crates/parser/src/parser/common/expr.rs +++ b/crates/parser/src/parser/common/expr.rs @@ -77,10 +77,10 @@ impl<'a> TryFrom> for Expression { Rule::closure => { ast_expr!(Expression::Closure { + params: collect_recovered(inner.next().unwrap().into_inner()), 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), }) } @@ -109,7 +109,7 @@ impl<'a> TryFrom> for Expression { )), } } - Rule::statement | Rule::basic_stmt | Rule::control_flow => ast_expr!( + Rule::statement | Rule::basic_stmt | Rule::control_flow | Rule::block => ast_expr!( Expression::Statement(pair.try_into().get_map(Box::new).map(Box::new)) ), From 8c28e61e429375b4e3c800550a4148d3beebe4dc Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 5 Jun 2026 20:06:38 +0200 Subject: [PATCH 11/11] Updated test #64 --- mist-test/src/common/attribute_test.mist | 21 +++ mist-test/src/common/decl_test.mist | 81 +++++++++++ mist-test/src/common/expr_test.mist | 150 +++++++++++++++++++++ mist-test/src/common/statement_test.mist | 165 +++++++++++++++++++++++ mist-test/src/common/types_test.mist | 63 +++++++++ mist-test/src/items/attribute_test.mist | 21 +++ mist-test/src/items/class_test.mist | 58 ++++++++ mist-test/src/items/enum_test.mist | 60 +++++++++ mist-test/src/items/function_test.mist | 64 +++++++++ mist-test/src/items/impl_test.mist | 54 ++++++++ mist-test/src/items/struct_test.mist | 44 ++++++ mist-test/src/items/trait_test.mist | 39 ++++++ mist-test/src/main.mist | 31 ++--- 13 files changed, 836 insertions(+), 15 deletions(-) create mode 100644 mist-test/src/common/attribute_test.mist 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/statement_test.mist create mode 100644 mist-test/src/common/types_test.mist 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/common/attribute_test.mist b/mist-test/src/common/attribute_test.mist new file mode 100644 index 0000000..313a20d --- /dev/null +++ b/mist-test/src/common/attribute_test.mist @@ -0,0 +1,21 @@ +#[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/common/decl_test.mist b/mist-test/src/common/decl_test.mist new file mode 100644 index 0000000..60eda7f --- /dev/null +++ b/mist-test/src/common/decl_test.mist @@ -0,0 +1,81 @@ +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 new file mode 100644 index 0000000..dc4f85f --- /dev/null +++ b/mist-test/src/common/expr_test.mist @@ -0,0 +1,150 @@ +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 : 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/statement_test.mist b/mist-test/src/common/statement_test.mist new file mode 100644 index 0000000..69c44d9 --- /dev/null +++ b/mist-test/src/common/statement_test.mist @@ -0,0 +1,165 @@ +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 : 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 : 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, _] : 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 new file mode 100644 index 0000000..62779b0 --- /dev/null +++ b/mist-test/src/common/types_test.mist @@ -0,0 +1,63 @@ +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 new file mode 100644 index 0000000..313a20d --- /dev/null +++ b/mist-test/src/items/attribute_test.mist @@ -0,0 +1,21 @@ +#[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 new file mode 100644 index 0000000..7c44635 --- /dev/null +++ b/mist-test/src/items/class_test.mist @@ -0,0 +1,58 @@ +pub class Counter { + pub count i32; + + pub constructor(start i32) { + self.count = start; + } + + pub fn increment(*mut self) { + self.count++; + } + + pub fn get(*self) i32 { + self.count + } +} + +pub fn test_class_create() { + let c = Counter::new(0); + assert_eq!(c.get(), 0); +} + +pub fn test_class_method() { + let mut c = Counter::new(5); + c.increment(); + assert_eq!(c.get(), 6); +} + +pub fn test_class_field_access() { + let c = Counter::new(42); + assert_eq!(c.count, 42); +} + +class NamedCounter : Counter { + pub name String; + + pub constructor(start i32, name String) { + self._super = Counter::new(start); + + self.name = name; + } + + pub fn get_name(*self) String { + self.name.clone() + } +} + +pub fn test_class_inherit() { + let nc = NamedCounter::new(10, "test".to_string()); + assert_eq!(nc.get_name(), "test"); + assert_eq!(nc.count, 10); +} + +pub fn 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..03a69c9 --- /dev/null +++ b/mist-test/src/items/enum_test.mist @@ -0,0 +1,60 @@ +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 new file mode 100644 index 0000000..02c98c5 --- /dev/null +++ b/mist-test/src/items/function_test.mist @@ -0,0 +1,64 @@ +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 new file mode 100644 index 0000000..2c9240c --- /dev/null +++ b/mist-test/src/items/impl_test.mist @@ -0,0 +1,54 @@ +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 new file mode 100644 index 0000000..7475688 --- /dev/null +++ b/mist-test/src/items/struct_test.mist @@ -0,0 +1,44 @@ +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 new file mode 100644 index 0000000..55ce2c3 --- /dev/null +++ b/mist-test/src/items/trait_test.mist @@ -0,0 +1,39 @@ +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 b6563fc..e2761cb 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1,17 +1,18 @@ -fn main() {} +fn main() { + println!("running common tests..."); + common::expr_test::run_tests(); + common::statement_test::run_tests(); + common::decl_test::run_tests(); + common::types_test::run_tests(); -struct MyStruct<'a> { - name *'a String -} + 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(); -fn add(a i32, b i32) i32 { - a + b -} - -class MyClass { - name String; - - constructor() {} - - fn my_func(*mut self) {} -} + println!("\x1b[32m\nAll tests passed!\x1b[0m"); +} \ No newline at end of file