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