From 46e9783257d132e935e49f192d4c476b9a1d8bc8 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 26 Jun 2026 02:04:33 +0200 Subject: [PATCH] Fixed bugs --- crates/codegen/src/fmt/mod.rs | 10 ++--- crates/codegen/src/fmt/statement.rs | 1 + crates/parser/src/grammar.pest | 2 +- mist-test/src/common/decl_test.mist | 33 ++++++++++----- mist-test/src/common/expr_test.mist | 54 ++++++++++++++++-------- mist-test/src/common/statement_test.mist | 53 ++++++++++++++--------- mist-test/src/common/types_test.mist | 2 +- mist-test/src/items/class_test.mist | 2 +- 8 files changed, 101 insertions(+), 56 deletions(-) diff --git a/crates/codegen/src/fmt/mod.rs b/crates/codegen/src/fmt/mod.rs index abce1a4..6da4bdd 100644 --- a/crates/codegen/src/fmt/mod.rs +++ b/crates/codegen/src/fmt/mod.rs @@ -188,18 +188,18 @@ impl GetMist for TypeExpr { match lifetime { Lifetime::Lifetime(v) => { format!( - "{} &'{} {}", + "{} {} '{}&", base, - v.get_mist(), - if *mutable { "mut" } else { "" } + if *mutable { "mut" } else { "" }, + v.get_mist() ) } Lifetime::Unsafe => { - format!("{} *{}", base, if *mutable { "mut" } else { "const" }) + format!("{} {} unsafe&", base, if *mutable { "mut" } else { "const" }) } } } else if *mutable { - format!("{} &mut", base) + format!("{} mut&", base) } else { format!("{}&", base) } diff --git a/crates/codegen/src/fmt/statement.rs b/crates/codegen/src/fmt/statement.rs index 15d1147..e38a87e 100644 --- a/crates/codegen/src/fmt/statement.rs +++ b/crates/codegen/src/fmt/statement.rs @@ -69,6 +69,7 @@ impl GenMist for Statement { body.gen_mist(ctx, cg); cg.addln(""); } else { + ctx.expr_ensure_semicolon = false; body.gen_mist(ctx, cg); cg.addln(","); } diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 719e55d..ae5c4bc 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -450,7 +450,7 @@ loop_stmt = { pattern_list = { pattern ~ ("|" ~ pattern)* } match_item = { - pattern_list ~ "=>" ~ (statement | (expr ~ ",")) + pattern_list ~ "=>" ~ (block | (expr ~ ",")) } match_stmt = { diff --git a/mist-test/src/common/decl_test.mist b/mist-test/src/common/decl_test.mist index 1374c12..4f00a07 100644 --- a/mist-test/src/common/decl_test.mist +++ b/mist-test/src/common/decl_test.mist @@ -1,11 +1,13 @@ pub module decl_test; -pub void test_let_basic() { +pub void test_let_basic() +{ let x = 42; assert_eq!(x, 42); } -pub void test_let_typed() { +pub void test_let_typed() +{ i32 x = 42; assert_eq!(x, 42); str& s = "hello"; @@ -16,26 +18,30 @@ pub void test_let_typed() { assert_eq!(f, 3.14); } -pub void test_let_mut() { +pub void test_let_mut() +{ let mut _x = 42; _x = 100; assert_eq!(_x, 100); } -pub void test_let_tuple_destructure() { +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() { +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() { +pub void test_let_pattern_irrefutable() +{ let x = 5; let (y, z) = (1, 2); assert_eq!(x, 5); @@ -43,30 +49,35 @@ pub void test_let_pattern_irrefutable() { assert_eq!(z, 2); } -pub void test_multiple_decls() { +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() { +pub void test_let_with_type_and_init() +{ f64 x = 10.0; assert_eq!(x, 10.0); } -pub void test_string_literal() { +pub void test_string_literal() +{ let s = "hello, world!"; assert_eq!(s.len(), 13); } -pub void test_let_bool() { +pub void test_let_bool() +{ let t = true; let f = false; assert_eq!(t, !f); } -pub void run_tests() { +pub void run_tests() +{ test_let_basic(); test_let_typed(); test_let_mut(); diff --git a/mist-test/src/common/expr_test.mist b/mist-test/src/common/expr_test.mist index 5877501..c2f9fd7 100644 --- a/mist-test/src/common/expr_test.mist +++ b/mist-test/src/common/expr_test.mist @@ -1,6 +1,7 @@ pub module expr_test; -pub void test_literals() { +pub void test_literals() +{ assert_eq!(42, 42); assert_eq!(3.14, 3.14); assert_eq!(true, true); @@ -8,28 +9,32 @@ pub void test_literals() { assert_eq!("hello", "hello"); } -pub void test_tuples() { +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() { +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() { +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() { +pub void test_binary_arith() +{ assert_eq!(10 + 5, 15); assert_eq!(10 - 5, 5); assert_eq!(10 * 5, 50); @@ -37,7 +42,8 @@ pub void test_binary_arith() { assert_eq!(10 % 3, 1); } -pub void test_binary_cmp() { +pub void test_binary_cmp() +{ assert_eq!(5 < 10, true); assert_eq!(10 <= 10, true); assert_eq!(10 > 5, true); @@ -46,14 +52,16 @@ pub void test_binary_cmp() { assert_eq!(5 != 10, true); } -pub void test_binary_logic() { +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() { +pub void test_binary_bit() +{ assert_eq!(5 & 3, 1); assert_eq!(5 | 3, 7); assert_eq!(5 ^ 3, 6); @@ -61,20 +69,23 @@ pub void test_binary_bit() { assert_eq!(4 >> 1, 2); } -pub void test_prefix() { +pub void test_prefix() +{ let x = 42; assert_eq!(-x, -42); assert_eq!(!true, false); assert_eq!(!false, true); } -pub void test_ref_deref() { +pub void test_ref_deref() +{ let x = 42; let r = &x; assert_eq!(*r, 42); } -pub void test_postfix_inc_dec() { +pub void test_postfix_inc_dec() +{ let mut x = 5; x++; assert_eq!(x, 6); @@ -82,17 +93,20 @@ pub void test_postfix_inc_dec() { assert_eq!(x, 5); } -pub void test_as_cast() { +pub void test_as_cast() +{ let x = 42; assert_eq!(x as f64, 42.0); } -pub void test_closure() { +pub void test_closure() +{ let add = (a, b) => a + b; assert_eq!(add(2, 3), 5); } -pub void test_block_expr() { +pub void test_block_expr() +{ let x = { let y = 10; y + 5 @@ -100,7 +114,8 @@ pub void test_block_expr() { assert_eq!(x, 15); } -pub void test_if_expr() { +pub void test_if_expr() +{ let x = if true { 1 } else { @@ -115,7 +130,8 @@ pub void test_if_expr() { assert_eq!(y, 2); } -pub void test_range_expr() { +pub void test_range_expr() +{ let r = 0 .. 5; let mut count = 0; for i in r { @@ -124,7 +140,8 @@ pub void test_range_expr() { assert_eq!(count, 5); } -pub void test_binary_assign() { +pub void test_binary_assign() +{ let mut x = 10; x += 5; assert_eq!(x, 15); @@ -138,7 +155,8 @@ pub void test_binary_assign() { assert_eq!(x, 2); } -pub void run_tests() { +pub void run_tests() +{ test_literals(); test_tuples(); test_arrays(); diff --git a/mist-test/src/common/statement_test.mist b/mist-test/src/common/statement_test.mist index 4f35d21..aeab488 100644 --- a/mist-test/src/common/statement_test.mist +++ b/mist-test/src/common/statement_test.mist @@ -1,6 +1,7 @@ pub module statement_test; -pub void test_var_decl() { +pub void test_var_decl() +{ let x = 42; assert_eq!(x, 42); i32 y = 10; @@ -9,13 +10,15 @@ pub void test_var_decl() { assert_eq!(name, "mist"); } -pub void test_var_decl_with_init() { +pub void test_var_decl_with_init() +{ let mut x = 0; x = 5; assert_eq!(x, 5); } -pub void test_if_statement() { +pub void test_if_statement() +{ let mut x = 0; if true { x = 1; @@ -29,7 +32,8 @@ pub void test_if_statement() { assert_eq!(x, 3); } -pub void test_if_else_if() { +pub void test_if_else_if() +{ let mut x = 0; if x == 1 { x = 10; @@ -41,7 +45,8 @@ pub void test_if_else_if() { assert_eq!(x, 20); } -pub void test_while_loop() { +pub void test_while_loop() +{ let mut count = 0; while count < 5 { count++; @@ -49,7 +54,8 @@ pub void test_while_loop() { assert_eq!(count, 5); } -pub void test_for_loop() { +pub void test_for_loop() +{ let mut sum = 0; for i in 0 .. 4 { sum += i; @@ -57,7 +63,8 @@ pub void test_for_loop() { assert_eq!(sum, 6); } -pub void test_loop_stmt() { +pub void test_loop_stmt() +{ let mut count = 0; loop { count++; @@ -68,7 +75,8 @@ pub void test_loop_stmt() { assert_eq!(count, 3); } -pub void test_break_continue() { +pub void test_break_continue() +{ let mut sum = 0; for i in 0 .. 10 { if i % 2 == 0 { @@ -82,11 +90,13 @@ pub void test_break_continue() { assert_eq!(sum, 1 + 3 + 5); } -pub i32 test_return() { +pub i32 test_return() +{ return 42; } -pub void test_block_scope() { +pub void test_block_scope() +{ let x = { let inner = 10; inner + 5 @@ -94,21 +104,23 @@ pub void test_block_scope() { assert_eq!(x, 15); } -pub void test_match_literal() { +pub void test_match_literal() +{ let x = 2; let result; match x { 1 => { result = 10; } - 2 => result = 20;, - 3 => result = 30;, - _ => panic!();, + 2 => result = 20, + 3 => result = 30, + _ => panic!(), } assert_eq!(result, 20); } -pub void test_match_or_pattern() { +pub void test_match_or_pattern() +{ let x = 1; i32 result; match x { @@ -118,12 +130,13 @@ pub void test_match_or_pattern() { 3 => { result = 200; } - _ => panic!();, + _ => panic!(), } assert_eq!(result, 100); } -pub void test_match_default() { +pub void test_match_default() +{ let x = 99; let result; match x { @@ -137,7 +150,8 @@ pub void test_match_default() { assert_eq!(result, 99); } -pub void test_for_with_pattern_destructure() { +pub void test_for_with_pattern_destructure() +{ let pairs = [(1, "a"), (2, "b")]; let mut keys = 0; for (k, _) in pairs { @@ -146,7 +160,8 @@ pub void test_for_with_pattern_destructure() { assert_eq!(keys, 3); } -pub void run_tests() { +pub void run_tests() +{ test_var_decl(); test_var_decl_with_init(); test_if_statement(); diff --git a/mist-test/src/common/types_test.mist b/mist-test/src/common/types_test.mist index 7c7e62d..76894b9 100644 --- a/mist-test/src/common/types_test.mist +++ b/mist-test/src/common/types_test.mist @@ -36,7 +36,7 @@ pub void test_ref_type() { pub void test_mut_ref_type() { let mut x = 42; - i32 &mut r = &mut x; + i32 mut& r = &mut x; *r = 100; assert_eq!(x, 100); } diff --git a/mist-test/src/items/class_test.mist b/mist-test/src/items/class_test.mist index 586502d..a0a0f5b 100644 --- a/mist-test/src/items/class_test.mist +++ b/mist-test/src/items/class_test.mist @@ -52,7 +52,7 @@ pub class Dog : Mammal impl std::fmt::Display { - Result<(), std::fmt::Error> fmt(&self, std::fmt::Formatter<'_>& mut f) + Result<(), std::fmt::Error> fmt(&self, std::fmt::Formatter<'_> mut& f) { write!(f, "🐾 {}", self.name) }