Fix formatter bugs

This commit is contained in:
2026-06-26 01:29:51 +02:00
parent e5509e89a4
commit 6c9c0e03d2
13 changed files with 173 additions and 84 deletions
+8 -6
View File
@@ -141,16 +141,18 @@ impl GenMist for Postfix {
cg.add(close); cg.add(close);
} }
Postfix::StructCall(fields) => { Postfix::StructCall(fields) => {
cg.add("{"); cg.addln(" {");
cg.indent += 1;
for (name, expr) in fields { for (name, expr) in fields {
cg.add(&name.get_mist()); cg.add_indented(&name.get_mist());
if let Some(expr) = expr { if let Some(expr) = expr {
cg.add(": "); cg.add(": ");
expr.gen_mist(ctx, cg); expr.gen_mist(ctx, cg);
} }
cg.add(", "); cg.addln(",");
} }
cg.add("}"); cg.indent -= 1;
cg.add_indented("}");
} }
Postfix::Index(idx) => { Postfix::Index(idx) => {
cg.add("["); cg.add("[");
@@ -168,8 +170,8 @@ impl GenMist for Postfix {
cg.add(" "); cg.add(" ");
expr.gen_mist(ctx, cg); expr.gen_mist(ctx, cg);
} }
Postfix::Increment => cg.add(" += 1"), Postfix::Increment => cg.add("++"),
Postfix::Decrement => cg.add(" -= 1"), Postfix::Decrement => cg.add("--"),
} }
} }
} }
+22 -7
View File
@@ -4,17 +4,19 @@ use crate::fmt::{Context, GenMist, GetMist, MistCodegen};
impl GenMist for ImplDecl { impl GenMist for ImplDecl {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) { fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
cg.add_indented("impl"); cg.add_indented("impl ");
let generics = self.generics.get_mist(); let generics = self.generics.get_mist();
if !generics.is_empty() { if !generics.is_empty() {
cg.add(&generics); cg.add(&generics);
cg.add(" "); cg.add(" ");
} }
cg.add(&self.target.get_mist());
if let Some(trait_) = &self.trait_ { if let Some(trait_) = &self.trait_ {
cg.add(" for ");
cg.add(&trait_.get_mist()); cg.add(&trait_.get_mist());
cg.add(" for ");
} }
cg.add(&self.target.get_mist());
cg.addln(" {"); cg.addln(" {");
cg.indent += 1; cg.indent += 1;
@@ -79,6 +81,7 @@ impl GenMist for FunctionDecl {
if let Some(body) = &self.body { if let Some(body) = &self.body {
cg.add(" "); cg.add(" ");
body.gen_mist(ctx, cg); body.gen_mist(ctx, cg);
cg.addln("");
} else { } else {
cg.addln(";"); cg.addln(";");
} }
@@ -125,7 +128,7 @@ impl GetMist for EnumItem {
"{} {{{}}}", "{} {{{}}}",
id.get_mist(), id.get_mist(),
s.iter() s.iter()
.map(|field| format!("{}: {}", field.name.get_mist(), field.type_.get_mist())) .map(FieldDecl::get_mist)
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", ") .join(", ")
), ),
@@ -237,7 +240,7 @@ impl GenMist for TopLevelKind {
generics.get_mist() generics.get_mist()
)); ));
if !requirements.is_empty() { if !requirements.is_empty() {
cg.add(": "); cg.add(" : ");
cg.add( cg.add(
&requirements &requirements
.iter() .iter()
@@ -273,7 +276,7 @@ impl GenMist for TopLevelKind {
cg.add(" : "); cg.add(" : ");
cg.add(&inherits.get_mist()); cg.add(&inherits.get_mist());
} }
cg.addln(" {"); cg.addln(" {\n");
cg.indent += 1; cg.indent += 1;
for field in fields { for field in fields {
@@ -286,6 +289,10 @@ impl GenMist for TopLevelKind {
} }
if let Some(constructor) = constructor { if let Some(constructor) = constructor {
if fields.len() > 0 {
cg.addln("");
}
cg.add_indented(&constructor.item.visibility.get_mist()); cg.add_indented(&constructor.item.visibility.get_mist());
cg.add("constructor"); cg.add("constructor");
cg.add(&constructor.item.generics.get_mist()); cg.add(&constructor.item.generics.get_mist());
@@ -299,9 +306,17 @@ impl GenMist for TopLevelKind {
cg.add(") "); cg.add(") ");
constructor.item.body.gen_mist(ctx, cg); constructor.item.body.gen_mist(ctx, cg);
cg.addln(""); cg.addln("");
if items.len() > 0 {
cg.addln("");
}
}
for (i, item) in items.iter().enumerate() {
if i > 0 {
cg.addln("");
} }
for item in items {
match item { match item {
ClassItem::Method(method) => method.gen_mist(ctx, cg), ClassItem::Method(method) => method.gen_mist(ctx, cg),
ClassItem::ImplDecl(impl_) => impl_.gen_mist(ctx, cg), ClassItem::ImplDecl(impl_) => impl_.gen_mist(ctx, cg),
-3
View File
@@ -8,13 +8,10 @@ pub void test_let_basic() {
pub void test_let_typed() { pub void test_let_typed() {
i32 x = 42; i32 x = 42;
assert_eq!(x, 42); assert_eq!(x, 42);
str& s = "hello"; str& s = "hello";
assert_eq!(s, "hello"); assert_eq!(s, "hello");
bool b = true; bool b = true;
assert_eq!(b, true); assert_eq!(b, true);
f64 f = 3.14; f64 f = 3.14;
assert_eq!(f, 3.14); assert_eq!(f, 3.14);
} }
+11 -4
View File
@@ -101,15 +101,22 @@ pub void test_block_expr() {
} }
pub void test_if_expr() { pub void test_if_expr() {
let x = if true { 1 } else { 2 }; let x = if true {
1
} else {
2
};
assert_eq!(x, 1); assert_eq!(x, 1);
let y = if false {
let y = if false { 1 } else { 2 }; 1
} else {
2
};
assert_eq!(y, 2); assert_eq!(y, 2);
} }
pub void test_range_expr() { pub void test_range_expr() {
let r = 0..5; let r = 0 .. 5;
let mut count = 0; let mut count = 0;
for i in r { for i in r {
count++; count++;
+21 -24
View File
@@ -3,10 +3,8 @@ pub module statement_test;
pub void test_var_decl() { pub void test_var_decl() {
let x = 42; let x = 42;
assert_eq!(x, 42); assert_eq!(x, 42);
i32 y = 10; i32 y = 10;
assert_eq!(y, 10); assert_eq!(y, 10);
str& name = "mist"; str& name = "mist";
assert_eq!(name, "mist"); assert_eq!(name, "mist");
} }
@@ -19,12 +17,10 @@ pub void test_var_decl_with_init() {
pub void test_if_statement() { pub void test_if_statement() {
let mut x = 0; let mut x = 0;
if true { if true {
x = 1; x = 1;
} }
assert_eq!(x, 1); assert_eq!(x, 1);
if false { if false {
x = 2; x = 2;
} else { } else {
@@ -35,7 +31,6 @@ pub void test_if_statement() {
pub void test_if_else_if() { pub void test_if_else_if() {
let mut x = 0; let mut x = 0;
if x == 1 { if x == 1 {
x = 10; x = 10;
} else if x == 0 { } else if x == 0 {
@@ -48,7 +43,6 @@ pub void test_if_else_if() {
pub void test_while_loop() { pub void test_while_loop() {
let mut count = 0; let mut count = 0;
while count < 5 { while count < 5 {
count++; count++;
} }
@@ -57,8 +51,7 @@ pub void test_while_loop() {
pub void test_for_loop() { pub void test_for_loop() {
let mut sum = 0; let mut sum = 0;
for i in 0 .. 4 {
for i in 0..4 {
sum += i; sum += i;
} }
assert_eq!(sum, 6); assert_eq!(sum, 6);
@@ -66,7 +59,6 @@ pub void test_for_loop() {
pub void test_loop_stmt() { pub void test_loop_stmt() {
let mut count = 0; let mut count = 0;
loop { loop {
count++; count++;
if count >= 3 { if count >= 3 {
@@ -78,8 +70,7 @@ pub void test_loop_stmt() {
pub void test_break_continue() { pub void test_break_continue() {
let mut sum = 0; let mut sum = 0;
for i in 0 .. 10 {
for i in 0..10 {
if i % 2 == 0 { if i % 2 == 0 {
continue; continue;
} }
@@ -106,12 +97,13 @@ pub void test_block_scope() {
pub void test_match_literal() { pub void test_match_literal() {
let x = 2; let x = 2;
let result; let result;
match x { match x {
1 => { result = 10; } 1 => {
2 => result = 20; result = 10;
3 => result = 30, }
_ => panic!(); 2 => result = 20;,
3 => result = 30;,
_ => panic!();,
} }
assert_eq!(result, 20); assert_eq!(result, 20);
} }
@@ -119,11 +111,14 @@ pub void test_match_literal() {
pub void test_match_or_pattern() { pub void test_match_or_pattern() {
let x = 1; let x = 1;
i32 result; i32 result;
match x { match x {
1 | 2 => { result = 100; } 1 | 2 => {
3 => { result = 200; } result = 100;
_ => panic!(); }
3 => {
result = 200;
}
_ => panic!();,
} }
assert_eq!(result, 100); assert_eq!(result, 100);
} }
@@ -131,10 +126,13 @@ pub void test_match_or_pattern() {
pub void test_match_default() { pub void test_match_default() {
let x = 99; let x = 99;
let result; let result;
match x { match x {
1 => { result = 10; } 1 => {
y => { result = y; } result = 10;
}
y => {
result = y;
}
} }
assert_eq!(result, 99); assert_eq!(result, 99);
} }
@@ -142,7 +140,6 @@ pub void test_match_default() {
pub void test_for_with_pattern_destructure() { pub void test_for_with_pattern_destructure() {
let pairs = [(1, "a"), (2, "b")]; let pairs = [(1, "a"), (2, "b")];
let mut keys = 0; let mut keys = 0;
for (k, _) in pairs { for (k, _) in pairs {
keys += k; keys += k;
} }
+3 -3
View File
@@ -13,7 +13,6 @@ pub void test_float_type() {
pub void test_bool_type() { pub void test_bool_type() {
bool x = true; bool x = true;
assert_eq!(x, true); assert_eq!(x, true);
bool y = false; bool y = false;
assert_eq!(y, false); assert_eq!(y, false);
} }
@@ -37,12 +36,13 @@ pub void test_ref_type() {
pub void test_mut_ref_type() { pub void test_mut_ref_type() {
let mut x = 42; let mut x = 42;
i32 mut& r = &mut x; i32 &mut r = &mut x;
*r = 100; *r = 100;
assert_eq!(x, 100); assert_eq!(x, 100);
} }
pub void test_void_type() {} pub void test_void_type() {
}
i32 add(i32 a, i32 b) { i32 add(i32 a, i32 b) {
a + b a + b
+3 -1
View File
@@ -6,7 +6,9 @@ pub struct Debuggable {
} }
pub void test_derive_attribute() { pub void test_derive_attribute() {
let d = Debuggable { value: 42 }; let d = Debuggable {
value: 42,
};
assert_eq!(d.value, 42); assert_eq!(d.value, 42);
} }
+22 -6
View File
@@ -1,36 +1,52 @@
pub module class_test; pub module class_test;
pub class Animal { pub class Animal {
pub String name; pub String name;
constructor() { constructor() {
self.name = "Rex".to_string(); self.name = "Rex".to_string();
} }
pub String speak(&self) { pub String speak(&self) {
"Unknown".to_string() "Unknown".to_string()
} pub void test_speak<'a>(&'a self) { }
pub void test_speak<'a>(&'a self) {
assert_eq!(self.speak(), "Woof!"); assert_eq!(self.speak(), "Woof!");
} pub void static_method() { }
}}
pub void static_method() {
}
}
pub class Mammal : Animal { pub class Mammal : Animal {
constructor() { constructor() {
super = Super::new(); super = Super::new();
} }
pub void args_test(&self, i32 x, str& y) { pub void args_test(&self, i32 x, str& y) {
assert_eq!(x, 10); assert_eq!(x, 10);
assert_eq!(y, "hello"); assert_eq!(y, "hello");
}} }
}
pub class Dog : Mammal { pub class Dog : Mammal {
constructor() { constructor() {
super = Super::new(); super = Super::new();
} }
pub String speak(&self) override(Animal) { pub String speak(&self) override(Animal) {
"Woof!".to_string() "Woof!".to_string()
} implstd::fmt::Display { }
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) write!(f, "🐾 {}", self.name)
} } }
}
} }
pub void run_tests() { pub void run_tests() {
+34 -15
View File
@@ -8,35 +8,50 @@ enum OptionInt {
pub void test_enum_named() { pub void test_enum_named() {
let x = OptionInt::None; let x = OptionInt::None;
match x { match x {
OptionInt::None => { assert_eq!(1, 1); } OptionInt::None => {
OptionInt::Some(_) => { assert_eq!(0, 1); } assert_eq!(1, 1);
}
OptionInt::Some(_) => {
assert_eq!(0, 1);
}
} }
} }
pub void test_enum_tuple() { pub void test_enum_tuple() {
let x = OptionInt::Some(42); let x = OptionInt::Some(42);
match x { match x {
OptionInt::None => { assert_eq!(0, 1); } OptionInt::None => {
OptionInt::Some(v) => { assert_eq!(v, 42); } assert_eq!(0, 1);
}
OptionInt::Some(v) => {
assert_eq!(v, 42);
}
} }
} }
enum Shape { enum Shape {
Circle { i32 radius }, Circle {i32 radius},
Rect { i32 w, i32 h }, Rect {i32 w, i32 h},
} }
pub void test_enum_struct() { pub void test_enum_struct() {
let c = Shape::Circle { radius: 5 }; let c = Shape::Circle{radius: 5, };
match c { match c {
Shape::Circle { radius } => { assert_eq!(radius, 5); } Shape::Circle {radius} => {
Shape::Rect { .. } => { assert_eq!(0, 1); } assert_eq!(radius, 5);
} }
Shape::Rect {..} => {
let r = Shape::Rect { w: 3, h: 4 }; assert_eq!(0, 1);
}
}
let r = Shape::Rect{w: 3, h: 4, };
match r { match r {
Shape::Circle { .. } => { assert_eq!(0, 1); } Shape::Circle {..} => {
Shape::Rect { w, h } => { assert_eq!(w * h, 12); } assert_eq!(0, 1);
}
Shape::Rect {w, h} => {
assert_eq!(w * h, 12);
}
} }
} }
@@ -49,8 +64,12 @@ enum Color {
pub void test_enum_eq() { pub void test_enum_eq() {
let c = Color::Red; let c = Color::Red;
match c { match c {
Color::Red => { assert_eq!(1, 1); } Color::Red => {
_ => { assert_eq!(0, 1); } assert_eq!(1, 1);
}
_ => {
assert_eq!(0, 1);
}
} }
} }
+5 -2
View File
@@ -46,8 +46,11 @@ pub void test_nested_fn_calls() {
} }
i32 factorial(i32 n) { i32 factorial(i32 n) {
if n <= 1 { 1 } if n <= 1 {
else { n * factorial(n - 1) } 1
} else {
n * factorial(n - 1)
}
} }
pub void test_recursive_fn() { pub void test_recursive_fn() {
+16 -4
View File
@@ -12,7 +12,10 @@ impl Vector2 {
} }
pub void test_inherent_impl() { pub void test_inherent_impl() {
let v = Vector2 { x: 3, y: 4 }; let v = Vector2 {
x: 3,
y: 4,
};
assert_eq!(v.magnitude(), 25); assert_eq!(v.magnitude(), 25);
} }
@@ -27,7 +30,10 @@ impl Display for Vector2 {
} }
pub void test_trait_impl_block() { pub void test_trait_impl_block() {
let v = Vector2 { x: 1, y: 2 }; let v = Vector2 {
x: 1,
y: 2,
};
let s = v.display(); let s = v.display();
assert_eq!(s.len() > 0, true); assert_eq!(s.len() > 0, true);
} }
@@ -42,8 +48,14 @@ impl Vector2 {
} }
pub void test_multiple_impls() { pub void test_multiple_impls() {
let a = Vector2 { x: 1, y: 2 }; let a = Vector2 {
let b = Vector2 { x: 3, y: 4 }; x: 1,
y: 2,
};
let b = Vector2 {
x: 3,
y: 4,
};
let c = a.add(b); let c = a.add(b);
assert_eq!(c.x, 4); assert_eq!(c.x, 4);
assert_eq!(c.y, 6); assert_eq!(c.y, 6);
+21 -6
View File
@@ -6,20 +6,29 @@ struct Point {
} }
pub void test_struct_create_access() { pub void test_struct_create_access() {
let p = Point { x: 10, y: 20 }; let p = Point {
x: 10,
y: 20,
};
assert_eq!(p.x, 10); assert_eq!(p.x, 10);
assert_eq!(p.y, 20); assert_eq!(p.y, 20);
} }
pub void test_struct_mut_field() { pub void test_struct_mut_field() {
let mut p = Point { x: 1, y: 2 }; let mut p = Point {
x: 1,
y: 2,
};
p.x = 100; p.x = 100;
assert_eq!(p.x, 100); assert_eq!(p.x, 100);
} }
pub void test_struct_destructure() { pub void test_struct_destructure() {
let p = Point { x: 3, y: 4 }; let p = Point {
let Point { x, y } = p; x: 3,
y: 4,
};
let Point {x, y} = p;
assert_eq!(x, 3); assert_eq!(x, 3);
assert_eq!(y, 4); assert_eq!(y, 4);
} }
@@ -31,8 +40,14 @@ struct Line {
pub void test_struct_nested() { pub void test_struct_nested() {
let l = Line { let l = Line {
start: Point { x: 0, y: 0 }, start: Point {
end: Point { x: 5, y: 10 }, x: 0,
y: 0,
},
end: Point {
x: 5,
y: 10,
},
}; };
assert_eq!(l.start.x, 0); assert_eq!(l.start.x, 0);
assert_eq!(l.end.y, 10); assert_eq!(l.end.y, 10);
+6 -2
View File
@@ -25,12 +25,16 @@ impl Greet for Dog {
} }
pub void test_trait_impl() { pub void test_trait_impl() {
let d = Dog { name: "Rex".to_string() }; let d = Dog {
name: "Rex".to_string(),
};
assert_eq!(d.speak(), "woof"); assert_eq!(d.speak(), "woof");
} }
pub void test_trait_with_requirement() { pub void test_trait_with_requirement() {
let d = Dog { name: "Buddy".to_string() }; let d = Dog {
name: "Buddy".to_string(),
};
assert_eq!(d.speak(), "woof"); assert_eq!(d.speak(), "woof");
assert_eq!(d.greet(), "hello"); assert_eq!(d.greet(), "hello");
} }